-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.py
More file actions
131 lines (117 loc) · 4.71 KB
/
metrics.py
File metadata and controls
131 lines (117 loc) · 4.71 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
128
129
130
131
from prometheus_client import Counter, Gauge, Histogram, start_http_server
import psutil
import time
from typing import Dict
class MetricsCollector:
def __init__(self, port: int = 9090):
# Detection metrics
self.detection_count = Counter(
'flying_lora_detections_total',
'Total number of detections made'
)
self.detection_latency = Histogram(
'flying_lora_detection_latency_seconds',
'Detection processing latency in seconds',
buckets=[0.1, 0.2, 0.5, 1.0, 2.0, 5.0]
)
self.detection_confidence = Histogram(
'flying_lora_detection_confidence',
'Detection confidence scores',
buckets=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
)
# API metrics
self.api_requests = Counter(
'flying_lora_api_requests_total',
'Total number of API requests',
['endpoint', 'method', 'status']
)
self.api_latency = Histogram(
'flying_lora_api_latency_seconds',
'API request latency in seconds',
['endpoint'],
buckets=[0.01, 0.05, 0.1, 0.5, 1.0, 5.0]
)
# System metrics
self.cpu_usage = Gauge(
'flying_lora_cpu_usage_percent',
'Current CPU usage percentage'
)
self.memory_usage = Gauge(
'flying_lora_memory_usage_bytes',
'Current memory usage in bytes'
)
self.disk_usage = Gauge(
'flying_lora_disk_usage_percent',
'Current disk usage percentage'
)
# LoRa metrics
self.lora_signal_strength = Gauge(
'flying_lora_signal_strength_dbm',
'LoRa signal strength in dBm'
)
self.lora_packets_sent = Counter(
'flying_lora_packets_sent_total',
'Total number of LoRa packets sent'
)
self.lora_packets_received = Counter(
'flying_lora_packets_received_total',
'Total number of LoRa packets received'
)
self.lora_packet_loss = Gauge(
'flying_lora_packet_loss_percent',
'LoRa packet loss percentage'
)
# Business metrics
self.mission_duration = Histogram(
'flying_lora_mission_duration_seconds',
'Mission duration in seconds',
buckets=[60, 300, 600, 1800, 3600, 7200]
)
self.mission_success_rate = Gauge(
'flying_lora_mission_success_rate',
'Mission success rate as a percentage'
)
# Start Prometheus HTTP server
start_http_server(port)
def record_detection(self, latency: float, confidence: float):
"""Record detection metrics"""
self.detection_count.inc()
self.detection_latency.observe(latency)
self.detection_confidence.observe(confidence)
def record_api_request(self, endpoint: str, method: str, status: int, latency: float):
"""Record API request metrics"""
self.api_requests.labels(endpoint=endpoint, method=method, status=status).inc()
self.api_latency.labels(endpoint=endpoint).observe(latency)
def update_system_metrics(self):
"""Update system resource metrics"""
self.cpu_usage.set(psutil.cpu_percent())
mem = psutil.virtual_memory()
self.memory_usage.set(mem.used)
disk = psutil.disk_usage('/')
self.disk_usage.set(disk.percent)
def record_lora_metrics(self, signal_strength: float, packets: Dict[str, int]):
"""Record LoRa communication metrics"""
self.lora_signal_strength.set(signal_strength)
self.lora_packets_sent.inc(packets.get('sent', 0))
self.lora_packets_received.inc(packets.get('received', 0))
if packets.get('sent', 0) > 0:
loss_rate = (packets.get('sent', 0) - packets.get('received', 0)) / packets.get('sent', 0) * 100
self.lora_packet_loss.set(loss_rate)
def record_mission_metrics(self, duration: float, success: bool):
"""Record mission-related metrics"""
self.mission_duration.observe(duration)
# Update success rate using exponential moving average
current_rate = self.mission_success_rate._value.get()
if current_rate is None:
current_rate = 100 if success else 0
else:
# Use 0.1 as smoothing factor
current_rate = (0.9 * current_rate) + (0.1 * (100 if success else 0))
self.mission_success_rate.set(current_rate)
# Example usage
if __name__ == '__main__':
metrics = MetricsCollector()
# Simulate metrics collection
while True:
metrics.update_system_metrics()
time.sleep(15) # Update every 15 seconds