Merge remote-tracking branch 'github/pr/1967'
[collectd.git] / src / write_prometheus.c
index 6fb5a86..fe7a5b5 100644 (file)
@@ -37,7 +37,7 @@
 #include <microhttpd.h>
 
 #ifndef PROMETHEUS_DEFAULT_STALENESS_DELTA
-#define PROMETHEUS_DEFAULT_STALENESS_DELTA TIME_T_TO_CDTIME_T(300)
+#define PROMETHEUS_DEFAULT_STALENESS_DELTA TIME_T_TO_CDTIME_T_STATIC(300)
 #endif
 
 #define VARINT_UINT32_BYTES 5
@@ -210,12 +210,11 @@ static int http_handler(void *cls, struct MHD_Connection *connection,
   else
     format_text(buffer);
 
-  struct MHD_Response *res =
 #if defined(MHD_VERSION) && MHD_VERSION >= 0x00090500
-    MHD_create_response_from_buffer(
+  struct MHD_Response *res = MHD_create_response_from_buffer(
       simple.len, simple.data, MHD_RESPMEM_MUST_COPY);
 #else
-    MHD_create_response_from_data(
+  struct MHD_Response *res = MHD_create_response_from_data(
       simple.len, simple.data, /* must_free = */ 0, /* must_copy = */ 1);
 #endif
   MHD_add_response_header(res, MHD_HTTP_HEADER_CONTENT_TYPE,
@@ -637,8 +636,8 @@ static char *metric_family_name(data_set_t const *ds, value_list_t const *vl,
 /* metric_family_get looks up the matching metric family, allocating it if
  * necessary. */
 static Io__Prometheus__Client__MetricFamily *
-metric_family_get(data_set_t const *ds, value_list_t const *vl,
-                  size_t ds_index) {
+metric_family_get(data_set_t const *ds, value_list_t const *vl, size_t ds_index,
+                  _Bool allocate) {
   char *name = metric_family_name(ds, vl, ds_index);
   if (name == NULL) {
     ERROR("write_prometheus plugin: Allocating metric family name failed.");
@@ -652,6 +651,9 @@ metric_family_get(data_set_t const *ds, value_list_t const *vl,
     return fam;
   }
 
+  if (!allocate)
+    return NULL;
+
   fam = metric_family_create(name, ds, vl, ds_index);
   if (fam == NULL) {
     ERROR("write_prometheus plugin: Allocating metric family failed.");
@@ -733,7 +735,8 @@ static int prom_write(data_set_t const *ds, value_list_t const *vl,
   pthread_mutex_lock(&metrics_lock);
 
   for (size_t i = 0; i < ds->ds_num; i++) {
-    Io__Prometheus__Client__MetricFamily *fam = metric_family_get(ds, vl, i);
+    Io__Prometheus__Client__MetricFamily *fam =
+        metric_family_get(ds, vl, i, /* allocate = */ 1);
     if (fam == NULL)
       continue;
 
@@ -759,7 +762,8 @@ static int prom_missing(value_list_t const *vl,
   pthread_mutex_lock(&metrics_lock);
 
   for (size_t i = 0; i < ds->ds_num; i++) {
-    Io__Prometheus__Client__MetricFamily *fam = metric_family_get(ds, vl, i);
+    Io__Prometheus__Client__MetricFamily *fam =
+        metric_family_get(ds, vl, i, /* allocate = */ 0);
     if (fam == NULL)
       continue;
 
@@ -768,6 +772,7 @@ static int prom_missing(value_list_t const *vl,
       ERROR("write_prometheus plugin: Deleting a metric in family \"%s\" "
             "failed with status %d",
             fam->name, status);
+
       continue;
     }