From a6d9d5c1c85a7e6641399c9d37dc087234d99e16 Mon Sep 17 00:00:00 2001 From: Ethan Shold Date: Wed, 26 Jun 2024 14:11:18 -0500 Subject: [PATCH] add response time metrics --- adguard_exporter.py | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/adguard_exporter.py b/adguard_exporter.py index d983efc..02a0756 100755 --- a/adguard_exporter.py +++ b/adguard_exporter.py @@ -2,8 +2,8 @@ import json import os import sys -from prometheus_client import start_http_server, Counter -from collections import Counter as CollectionsCounter +from prometheus_client import start_http_server, Counter, Gauge +from collections import Counter as CollectionsCounter, defaultdict # Define separate Prometheus metrics dns_queries = Counter('dns_queries', 'Total number of DNS queries') @@ -12,10 +12,15 @@ top_hosts = Counter('dns_query_hosts', 'Top DNS query hosts', ['host']) top_blocked_hosts = Counter('blocked_dns_query_hosts', 'Top blocked DNS query hosts', ['host']) safe_search_enforced_hosts = Counter('safe_search_enforced_hosts', 'Safe search enforced hosts', ['host']) +average_response_time = Gauge('dns_average_response_time', 'Average response time for DNS queries in milliseconds') +average_upstream_response_time = Gauge('dns_average_upstream_response_time', 'Average response time by upstream server', ['server']) -# Define counters to track hosts +# Define counters to track hosts and upstream response times host_counter = CollectionsCounter() blocked_host_counter = CollectionsCounter() +total_response_time = 0 +total_queries = 0 +upstream_response_times = defaultdict(list) log_file_path = '/opt/adguardhome/work/data/querylog.json' position_file_path = '/opt/adguardhome/work/data/.position' @@ -55,8 +60,18 @@ def update_top_hosts(counter, metric, top_n): for item in top_items: metric.labels(item[0]).inc(item[1]) +def calculate_averages(): + if total_queries > 0: + avg_response_time = total_response_time / total_queries + average_response_time.set(avg_response_time) + + for upstream, times in upstream_response_times.items(): + if times: + avg_upstream_time = sum(times) / len(times) + average_upstream_response_time.labels(upstream).set(avg_upstream_time) + def parse_and_export(lines): - global host_counter, blocked_host_counter + global host_counter, blocked_host_counter, total_response_time, total_queries, upstream_response_times for line in lines: if line.strip(): @@ -67,10 +82,21 @@ def parse_and_export(lines): is_blocked = data.get('Result', {}).get('IsFiltered', False) result_reason = str(data.get('Result', {}).get('Reason', 'unknown')) status = 'blocked' if is_blocked else 'success' + elapsed_ns = data.get('Elapsed', 0) + upstream = data.get('Upstream', 'unknown') dns_queries.inc() query_types.labels(query_type).inc() host_counter[host] += 1 + + # Convert nanoseconds to milliseconds + elapsed_ms = elapsed_ns / 1_000_000 + total_response_time += elapsed_ms + total_queries += 1 + + if upstream != 'unknown': + upstream_response_times[upstream].append(elapsed_ms) + if is_blocked and result_reason == '3': blocked_queries.inc() blocked_host_counter[host] += 1 @@ -85,6 +111,8 @@ def parse_and_export(lines): sys.stdout.flush() pass + calculate_averages() + if __name__ == '__main__': start_http_server(8000) print("Prometheus metrics server started on port 8000")