]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
res_prometheus.c: Set Content-Type header on /metrics response.
authorSean Bright <sean@seanbright.com>
Tue, 21 Jan 2025 23:37:54 +0000 (18:37 -0500)
committerSean Bright <sean@seanbright.com>
Thu, 23 Jan 2025 14:22:01 +0000 (14:22 +0000)
This should resolve the Prometheus error:

> Error scraping target: non-compliant scrape target
  sending blank Content-Type and no
  fallback_scrape_protocol specified for target.

Resolves: #1075

res/res_prometheus.c

index 6633c1755e6a014d60007f79eaa1ca5842d8fd56..a04dbd8520fdfcb7475dbe0eea9b38ea85e0bfdf 100644 (file)
@@ -603,6 +603,7 @@ static int http_callback(struct ast_tcptls_session_instance *ser,
 {
        RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(global_config), ao2_cleanup);
        struct ast_str *response = NULL;
+       struct ast_str *content_type_header = NULL;
        struct timeval start;
        struct timeval end;
 
@@ -635,10 +636,13 @@ static int http_callback(struct ast_tcptls_session_instance *ser,
        }
 
        response = ast_str_create(512);
-       if (!response) {
+       content_type_header = ast_str_create(32);
+       if (!response || !content_type_header) {
                goto err500;
        }
 
+       ast_str_set(&content_type_header, 0, "Content-Type: text/plain\r\n");
+
        start = ast_tvnow();
 
        ast_mutex_lock(&scrape_lock);
@@ -659,7 +663,7 @@ static int http_callback(struct ast_tcptls_session_instance *ser,
        }
        ast_mutex_unlock(&scrape_lock);
 
-       ast_http_send(ser, method, 200, "OK", NULL, response, 0, 0);
+       ast_http_send(ser, method, 200, "OK", content_type_header, response, 0, 0);
 
        return 0;
 
@@ -678,14 +682,17 @@ err401:
                ast_http_send(ser, method, 401, "Unauthorized", auth_challenge_headers, NULL, 0, 1);
        }
        ast_free(response);
+       ast_free(content_type_header);
        return 0;
 err503:
        ast_http_send(ser, method, 503, "Service Unavailable", NULL, NULL, 0, 1);
        ast_free(response);
+       ast_free(content_type_header);
        return 0;
 err500:
        ast_http_send(ser, method, 500, "Server Error", NULL, NULL, 0, 1);
        ast_free(response);
+       ast_free(content_type_header);
        return 0;
 }