Skip to content

Commit

Permalink
add response time metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
sholdee committed Jun 26, 2024
1 parent 14d64de commit a6d9d5c
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions adguard_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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'
Expand Down Expand Up @@ -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():
Expand All @@ -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
Expand All @@ -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")
Expand Down

0 comments on commit a6d9d5c

Please sign in to comment.