diff --git a/app.py b/app.py index aed5a62..177c5b7 100644 --- a/app.py +++ b/app.py @@ -1,28 +1,45 @@ +import os +import logging +import time from flask import Flask, request, render_template import requests app = Flask(__name__) +# Specify the full path to the logs folder +logs_folder = os.path.join(os.getcwd(), 'logs') + +# Create the logs folder if it doesn't exist +if not os.path.exists(logs_folder): + os.makedirs(logs_folder) + +# Configure logging to write logs to the specified folder +log_file = os.path.join(logs_folder, 'app.log') +logging.basicConfig(filename=log_file, level=logging.DEBUG, format='%(asctime)s %(levelname)s: %(message)s') @app.route("/") def home(): - return render_template("home.html") - @app.route("/search", methods=["POST"]) def search(): - - # Get the search query query = request.form["q"] - # Pass the search query to the Nominatim API to get a location + logging.info(f'Search query: {query}') + + start_time = time.time() + location = requests.get( "https://nominatim.openstreetmap.org/search", {"q": query, "format": "json", "limit": "1"}, ).json() - # If a location is found, pass the coordinate to the Time API to get the current time + end_time = time.time() + + duration = end_time - start_time + + logging.info(f'Latency for search: {duration} seconds') + if location: coordinate = [location[0]["lat"], location[0]["lon"]] @@ -31,9 +48,13 @@ def search(): {"latitude": coordinate[0], "longitude": coordinate[1]}, ) + logging.info(f'Successful search for location: {location[0]["display_name"]}') + return render_template("success.html", location=location[0], time=time.json()) - # If a location is NOT found, return the error page else: - + logging.warning(f'No location found for query: {query}') return render_template("fail.html") + +if __name__ == "__main__": + app.run(debug=True) diff --git a/docker-compose.monitoring.yaml b/docker-compose.monitoring.yaml new file mode 100644 index 0000000..b09c323 --- /dev/null +++ b/docker-compose.monitoring.yaml @@ -0,0 +1,42 @@ +version: "3.8" +networks: + monitoring: + driver: bridge +volumes: + prometheus-data: + driver: local + grafana-data: + driver: local +services: + grafana: + image: grafana/grafana-oss:latest + container_name: grafana + ports: + - "3000:3000" + volumes: + - grafana-data:/var/lib/grafana + restart: unless-stopped + + #password: admin/admin + + + + + loki: + image: grafana/loki:2.5.0 + container_name: loki + ports: + - "3100:3100" + volumes: + - ./loki-config/:/etc/loki/ + command: -config.file=/etc/loki/local-config.yaml + + promtail: + image: grafana/promtail:2.5.0 + ports: + - "9080:9080" + volumes: + - ./logs:/var/apilogs + - /var/log:/var/log + - ./promtail-config/:/etc/promtail/ + command: -config.file=/etc/promtail/promtail.yaml diff --git a/grafana.png b/grafana.png new file mode 100644 index 0000000..891d70e Binary files /dev/null and b/grafana.png differ diff --git a/loki-config/local-config.yaml b/loki-config/local-config.yaml new file mode 100644 index 0000000..0b15763 --- /dev/null +++ b/loki-config/local-config.yaml @@ -0,0 +1,40 @@ +auth_enabled: false + +server: + http_listen_port: 3100 + +common: + instance_addr: 127.0.0.1 + path_prefix: /tmp/loki + storage: + filesystem: + chunks_directory: /tmp/loki/chunks + rules_directory: /tmp/loki/rules + replication_factor: 1 + ring: + kvstore: + store: inmemory +query_range: + +ingester: + lifecycler: + address: 127.0.0.1 + ring: + kvstore: + store: inmemory + replication_factor: 1 + final_sleep: 0s + chunk_idle_period: 1h + max_chunk_age: 1h + chunk_target_size: 1048576 + chunk_retain_period: 30s + max_transfer_retries: 0 +schema_config: + configs: + - from: 2020-10-24 + store: boltdb-shipper + object_store: filesystem + schema: v12 + index: + prefix: index_ + period: 24h diff --git a/promtail-config/promtail.yaml b/promtail-config/promtail.yaml new file mode 100644 index 0000000..ece8ba3 --- /dev/null +++ b/promtail-config/promtail.yaml @@ -0,0 +1,24 @@ +server: + http_listen_port: 9080 + +positions: + filename: /tmp/positions.yaml + +clients: + - url: http://loki:3100/loki/api/v1/push + +scrape_configs: + - job_name: system + static_configs: + - targets: + - localhost + labels: + job: varlogs + __path__: /var/log/*log + - job_name: API + static_configs: + - targets: + - localhost + labels: + job: apilog + __path__: /var/apilogs/*log diff --git a/q1.png b/q1.png new file mode 100644 index 0000000..6bf5dca Binary files /dev/null and b/q1.png differ diff --git a/q2-3:promtail.png b/q2-3:promtail.png new file mode 100644 index 0000000..07864d4 Binary files /dev/null and b/q2-3:promtail.png differ