From cdfc7160657b6e513378857a0de1c66342d0417c Mon Sep 17 00:00:00 2001 From: Paul Traylor Date: Wed, 3 Jul 2024 16:10:39 +0900 Subject: [PATCH] Test metrics validity when testing Exporter When using the 'test' button for an exporter, we previously *only* checked the status code and assumed things were good. It is very possible for an incorrect endpoint to be registered, so we should ensure that the registered endpoint produces correct Prometheus metrics. Right now, we will return a somewhat *developer* like output, but I expect to make the output more user friendly as we cleanup some of our internal test URLs. --- promgen/views.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/promgen/views.py b/promgen/views.py index 642ce11a4..88dbd61a6 100644 --- a/promgen/views.py +++ b/promgen/views.py @@ -11,6 +11,8 @@ from itertools import chain import prometheus_client +from prometheus_client.parser import text_string_to_metric_families + import requests from django.conf import settings from django.contrib import messages @@ -667,7 +669,16 @@ def query(): try: result = future.result() result.raise_for_status() - yield result.url, result.status_code + metrics = list(text_string_to_metric_families(result.text)) + yield ( + result.url, + { + "status_code": result.status_code, + "metric_count": len(list(metrics)), + }, + ) + except ValueError as e: + yield result.url, f"Unable to parse metrics: {e}" except requests.ConnectionError as e: logger.warning("Error connecting to server") yield e.request.url, "Error connecting to server"