-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.py
More file actions
127 lines (108 loc) · 3.9 KB
/
Copy pathmetrics.py
File metadata and controls
127 lines (108 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from prometheus_client import Counter, Histogram, Gauge
# -------------------------------------------------------
# 🌐 HTTP Request Metrics (general API observability)
# -------------------------------------------------------
# Total number of HTTP requests, categorized by method, endpoint, and status code.
# Helps track traffic volume and API usage patterns.
REQUEST_COUNT = Counter(
"http_requests_total",
"Total HTTP requests",
["method", "endpoint", "http_status"]
)
# Distribution of HTTP request latency, measured in seconds.
# Useful for detecting performance issues and tracking response time SLAs.
REQUEST_LATENCY = Histogram(
"http_request_latency_seconds",
"HTTP request latency in seconds",
["endpoint"]
)
# -------------------------------------------------------
# 🚨 Fraud / Suspicious Activity Metrics
# -------------------------------------------------------
# Total number of suspicious requests detected, grouped by detection type
# (e.g., "ip_rate", "user_agent", "referrer", "ip_url").
# Maps to suspicious_clicks table (reason column).
SUSPICIOUS_REQUESTS = Counter(
"suspicious_requests_total",
"Total number of suspicious requests detected",
["type"]
)
# Current number of unique IPs flagged as suspicious (active cases).
# Derived from suspicious_clicks.ip field in the database.
SUSPICIOUS_IPS = Gauge(
"suspicious_ips_current",
"Number of currently suspicious IPs"
)
# Current number of URL-specific suspicious activities,
# showing how many unique IP+URL combinations were flagged.
SUSPICIOUS_IP_URLS = Gauge(
"suspicious_ip_urls_current",
"Number of currently suspicious IP+URL combinations"
)
# -------------------------------------------------------
# 📊 URL & Click Analytics (business KPIs)
# -------------------------------------------------------
# Tracks unique visitors per URL.
# Each visitor should be counted once, based on IP or fingerprint.
# Maps to url_clicks table (ip, fingerprint).
UNIQUE_VISITORS = Counter(
"unique_visitors_total",
"Number of unique visitors",
["url", "date"]
)
# Tracks how many clicks each URL received from each referrer domain.
# Maps to url_referrers table.
TOP_REFERRERS = Counter(
"top_referrers",
"Referrer counts",
["url", "referrer"]
)
# Distribution of the time gap between consecutive clicks on the same URL.
# Helps detect bots (low latency) vs. engaged users.
# Derived from url_clicks.clicked_at timestamps.
CLICK_LATENCY = Histogram(
"click_latency_seconds",
"Time between clicks",
["url"]
)
# Counts clicks flagged as suspicious or fraudulent.
# Label 'type' describes why it was flagged (e.g., "bot", "velocity", "ip_threshold").
# Maps to suspicious_clicks.reason column.
SUSPICIOUS_CLICKS = Counter(
"suspicious_clicks_total",
"Suspicious clicks detected",
["url", "type"]
)
# -------------------------------------------------------
# 🌍 Enriched Analytics: Geo & Device Insights
# -------------------------------------------------------
# Number of clicks per country for each URL.
# IP → Geo lookup populates this.
# Maps to url_clicks.ip + enrichment pipeline.
CLICKS_BY_COUNTRY = Counter(
"clicks_by_country_total",
"Number of clicks grouped by country",
["url", "country"]
)
# Number of clicks by device type (mobile, tablet, desktop).
# Parsed from User-Agent string.
CLICKS_BY_DEVICE = Counter(
"clicks_by_device_total",
"Clicks by device type (mobile, tablet, pc)",
["url", "device"]
)
# Number of clicks by browser family (Chrome, Firefox, Safari, etc.).
# Parsed from User-Agent string.
CLICKS_BY_BROWSER = Counter(
"clicks_by_browser_total",
"Clicks by browser",
["url", "browser"]
)
# Distribution of clicks over the 24 hours of the day.
# Useful for engagement timing and activity trends.
# Derived from url_clicks.clicked_at field.
CLICKS_BY_HOUR = Histogram(
"clicks_by_hour",
"Clicks by hour of day",
["url"]
)