This commit is contained in:
Mitch Ross 2023-02-26 21:22:13 -05:00
parent edfdbc20f5
commit 92fbc36549
2 changed files with 130 additions and 79 deletions

View File

@ -85,11 +85,9 @@ def create_app(
app.register_blueprint(bp) app.register_blueprint(bp)
app.wsgi_app = DispatcherMiddleware(
app.wsgi_app, {"/metrics": make_wsgi_app(registry=setupRegistry())}
app.wsgi_app = DispatcherMiddleware(app.wsgi_app, { )
"/metrics": make_wsgi_app(registry=setupRegistry())
})
return app return app

View File

@ -5,7 +5,6 @@ from prometheus_client import CollectorRegistry
from prometheus_client.metrics_core import GaugeMetricFamily, InfoMetricFamily from prometheus_client.metrics_core import GaugeMetricFamily, InfoMetricFamily
def setupRegistry(): def setupRegistry():
myregistry = CollectorRegistry() myregistry = CollectorRegistry()
myregistry.register(CustomCollector()) myregistry.register(CustomCollector())
@ -15,49 +14,68 @@ def setupRegistry():
def add_metric(metric, label, data, key, multiplier=1.0): def add_metric(metric, label, data, key, multiplier=1.0):
try: try:
string = str(data[key]) string = str(data[key])
value = float(re.findall(r'\d+', string)[0]) value = float(re.findall(r"\d+", string)[0])
metric.add_metric([label], value * multiplier) metric.add_metric([label], value * multiplier)
except (KeyError, TypeError, IndexError): except (KeyError, TypeError, IndexError):
pass pass
class CustomCollector:
class CustomCollector():
def __init__(self): def __init__(self):
self.stats_url = "http://localhost:5000/api/stats" self.stats_url = "http://localhost:5000/api/stats"
def collect(self): def collect(self):
data = json.loads(urlopen(self.stats_url).read()) data = json.loads(urlopen(self.stats_url).read())
# camera stats # camera stats
ffmpeg_pid = GaugeMetricFamily('frigate_ffmpeg_pid', 'PID for ffmpeg process', labels=['camera']) ffmpeg_pid = GaugeMetricFamily(
capture_pid = GaugeMetricFamily('frigate_capture_pid', 'PID for the ffmpeg process that consumes this camera', "frigate_ffmpeg_pid", "PID for ffmpeg process", labels=["camera"]
labels=['camera']) )
detect_pid = GaugeMetricFamily('frigate_detect_pid', 'PID for the process that runs detection for this camera', capture_pid = GaugeMetricFamily(
labels=['camera']) "frigate_capture_pid",
camera_fps = GaugeMetricFamily('frigate_camera_fps', 'Frames per second being consumed from your camera.', "PID for the ffmpeg process that consumes this camera",
labels=['camera']) labels=["camera"],
detection_fps = GaugeMetricFamily('frigate_detection_fps', 'Number of times detection is run per second.', )
labels=['camera']) detect_pid = GaugeMetricFamily(
process_fps = GaugeMetricFamily('frigate_process_fps', 'Frames per second being processed by frigate.', "frigate_detect_pid",
labels=['camera']) "PID for the process that runs detection for this camera",
skipped_fps = GaugeMetricFamily('frigate_skipped_fps', 'Frames per second skip for processing by frigate.', labels=["camera"],
labels=['camera']) )
detection_enabled = GaugeMetricFamily('frigate_detection_enabled', 'Detection enabled for camera', camera_fps = GaugeMetricFamily(
labels=['camera']) "frigate_camera_fps",
"Frames per second being consumed from your camera.",
labels=["camera"],
)
detection_fps = GaugeMetricFamily(
"frigate_detection_fps",
"Number of times detection is run per second.",
labels=["camera"],
)
process_fps = GaugeMetricFamily(
"frigate_process_fps",
"Frames per second being processed by frigate.",
labels=["camera"],
)
skipped_fps = GaugeMetricFamily(
"frigate_skipped_fps",
"Frames per second skip for processing by frigate.",
labels=["camera"],
)
detection_enabled = GaugeMetricFamily(
"frigate_detection_enabled",
"Detection enabled for camera",
labels=["camera"],
)
for k, d in data.items(): for k, d in data.items():
add_metric(ffmpeg_pid, k, d, 'ffmpeg_pid') add_metric(ffmpeg_pid, k, d, "ffmpeg_pid")
add_metric(detect_pid, k, d, 'pid') add_metric(detect_pid, k, d, "pid")
add_metric(capture_pid, k, d, 'capture_pid') add_metric(capture_pid, k, d, "capture_pid")
add_metric(camera_fps, k, d, 'camera_fps') add_metric(camera_fps, k, d, "camera_fps")
add_metric(detection_fps, k, d, 'detection_fps') add_metric(detection_fps, k, d, "detection_fps")
add_metric(process_fps, k, d, 'process_fps') add_metric(process_fps, k, d, "process_fps")
add_metric(skipped_fps, k, d, 'skipped_fps') add_metric(skipped_fps, k, d, "skipped_fps")
add_metric(detection_enabled, k, d, 'detection_enabled') add_metric(detection_enabled, k, d, "detection_enabled")
yield ffmpeg_pid yield ffmpeg_pid
yield capture_pid yield capture_pid
@ -70,25 +88,36 @@ class CustomCollector():
# detector stats # detector stats
try: try:
yield GaugeMetricFamily('frigate_detection_total_fps', yield GaugeMetricFamily(
'Sum of detection_fps across all cameras and detectors.', "frigate_detection_total_fps",
value=data['detection_fps']) "Sum of detection_fps across all cameras and detectors.",
value=data["detection_fps"],
)
except KeyError: except KeyError:
pass pass
detector_inference_speed = GaugeMetricFamily('frigate_detector_inference_speed_seconds', detector_inference_speed = GaugeMetricFamily(
'Time spent running object detection in seconds.', labels=['name']) "frigate_detector_inference_speed_seconds",
detector_pid = GaugeMetricFamily('frigate_detector_pid', "Time spent running object detection in seconds.",
'PID for the shared process that runs object detection on the detector', labels=["name"],
labels=['name']) )
detector_detection_start = GaugeMetricFamily('frigate_detection_start', detector_pid = GaugeMetricFamily(
'Detector start time (unix timestamp)', "frigate_detector_pid",
labels=['name']) "PID for the shared process that runs object detection on the detector",
labels=["name"],
)
detector_detection_start = GaugeMetricFamily(
"frigate_detection_start",
"Detector start time (unix timestamp)",
labels=["name"],
)
try: try:
for k, d in data['detectors'].items(): for k, d in data["detectors"].items():
add_metric(detector_inference_speed, k, d, 'inference_speed', 0.001) # ms to seconds add_metric(
add_metric(detector_pid, k, d, 'pid') detector_inference_speed, k, d, "inference_speed", 0.001
add_metric(detector_detection_start, k, d, 'detection_start') ) # ms to seconds
add_metric(detector_pid, k, d, "pid")
add_metric(detector_detection_start, k, d, "detection_start")
except KeyError: except KeyError:
pass pass
@ -97,13 +126,17 @@ class CustomCollector():
yield detector_detection_start yield detector_detection_start
# process stats # process stats
cpu_usages = GaugeMetricFamily('frigate_cpu_usage_percent', 'Process CPU usage %', labels=['pid']) cpu_usages = GaugeMetricFamily(
mem_usages = GaugeMetricFamily('frigate_mem_usage_percent', 'Process memory usage %', labels=['pid']) "frigate_cpu_usage_percent", "Process CPU usage %", labels=["pid"]
)
mem_usages = GaugeMetricFamily(
"frigate_mem_usage_percent", "Process memory usage %", labels=["pid"]
)
try: try:
for k, d in data['cpu_usages'].items(): for k, d in data["cpu_usages"].items():
add_metric(cpu_usages, k, d, 'cpu') add_metric(cpu_usages, k, d, "cpu")
add_metric(mem_usages, k, d, 'mem') add_metric(mem_usages, k, d, "mem")
except KeyError: except KeyError:
pass pass
@ -111,13 +144,17 @@ class CustomCollector():
yield mem_usages yield mem_usages
# gpu stats # gpu stats
gpu_usages = GaugeMetricFamily('frigate_gpu_usage_percent', 'GPU utilisation %', labels=['gpu']) gpu_usages = GaugeMetricFamily(
gpu_mem_usages = GaugeMetricFamily('frigate_gpu_mem_usage_percent', 'GPU memory usage %', labels=['gpu']) "frigate_gpu_usage_percent", "GPU utilisation %", labels=["gpu"]
)
gpu_mem_usages = GaugeMetricFamily(
"frigate_gpu_mem_usage_percent", "GPU memory usage %", labels=["gpu"]
)
try: try:
for k, d in data['gpu_usages'].items(): for k, d in data["gpu_usages"].items():
add_metric(gpu_usages, k, d, 'gpu') add_metric(gpu_usages, k, d, "gpu")
add_metric(gpu_usages, k, d, 'mem') add_metric(gpu_usages, k, d, "mem")
except KeyError: except KeyError:
pass pass
@ -125,17 +162,26 @@ class CustomCollector():
yield gpu_mem_usages yield gpu_mem_usages
# service stats # service stats
uptime_seconds = GaugeMetricFamily('frigate_service_uptime_seconds', 'Uptime seconds') uptime_seconds = GaugeMetricFamily(
last_updated_timestamp = GaugeMetricFamily('frigate_service_last_updated_timestamp', "frigate_service_uptime_seconds", "Uptime seconds"
'Stats recorded time (unix timestamp)') )
last_updated_timestamp = GaugeMetricFamily(
"frigate_service_last_updated_timestamp",
"Stats recorded time (unix timestamp)",
)
try: try:
s = data['service'] s = data["service"]
add_metric(uptime_seconds, '', s, 'uptime') add_metric(uptime_seconds, "", s, "uptime")
add_metric(last_updated_timestamp, '', s, 'last_updated') add_metric(last_updated_timestamp, "", s, "last_updated")
info = {'latest_version': data['service']['latest_version'], 'version': data['service']['version']} info = {
yield InfoMetricFamily('frigate_service', 'Frigate version info', value=info) "latest_version": data["service"]["latest_version"],
"version": data["service"]["version"],
}
yield InfoMetricFamily(
"frigate_service", "Frigate version info", value=info
)
except KeyError: except KeyError:
pass pass
@ -145,17 +191,25 @@ class CustomCollector():
# service->temperatures: no data for me # service->temperatures: no data for me
storage_free = GaugeMetricFamily('frigate_storage_free_bytes', 'Storage free bytes', labels=['storage']) storage_free = GaugeMetricFamily(
storage_mount_type = InfoMetricFamily('frigate_storage_mount_type', 'Storage mount type', labels=['storage']) "frigate_storage_free_bytes", "Storage free bytes", labels=["storage"]
storage_total = GaugeMetricFamily('frigate_storage_total_bytes', 'Storage total bytes', labels=['storage']) )
storage_used = GaugeMetricFamily('frigate_storage_used_bytes', 'Storage used bytes', labels=['storage']) storage_mount_type = InfoMetricFamily(
"frigate_storage_mount_type", "Storage mount type", labels=["storage"]
)
storage_total = GaugeMetricFamily(
"frigate_storage_total_bytes", "Storage total bytes", labels=["storage"]
)
storage_used = GaugeMetricFamily(
"frigate_storage_used_bytes", "Storage used bytes", labels=["storage"]
)
try: try:
for k, d in data['service']['storage'].items(): for k, d in data["service"]["storage"].items():
add_metric(storage_free, k, d, 'free', 1e6) # MB to bytes add_metric(storage_free, k, d, "free", 1e6) # MB to bytes
add_metric(storage_total, k, d, 'total', 1e6) # MB to bytes add_metric(storage_total, k, d, "total", 1e6) # MB to bytes
add_metric(storage_used, k, d, 'used', 1e6) # MB to bytes add_metric(storage_used, k, d, "used", 1e6) # MB to bytes
storage_mount_type.add_metric(k, {'mount_type': d['mount_type']}) storage_mount_type.add_metric(k, {"mount_type": d["mount_type"]})
except KeyError: except KeyError:
pass pass
@ -163,4 +217,3 @@ class CustomCollector():
yield storage_mount_type yield storage_mount_type
yield storage_total yield storage_total
yield storage_used yield storage_used