Merge remote-tracking branch 'github/pr/1967'
[collectd.git] / src / write_prometheus.c
1 /**
2  * collectd - src/write_prometheus.c
3  * Copyright (C) 2016       Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  */
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31 #include "utils_avltree.h"
32 #include "utils_complain.h"
33 #include "utils_time.h"
34
35 #include "prometheus.pb-c.h"
36
37 #include <microhttpd.h>
38
39 #ifndef PROMETHEUS_DEFAULT_STALENESS_DELTA
40 #define PROMETHEUS_DEFAULT_STALENESS_DELTA TIME_T_TO_CDTIME_T_STATIC(300)
41 #endif
42
43 #define VARINT_UINT32_BYTES 5
44
45 #define CONTENT_TYPE_PROTO                                                     \
46   "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; " \
47   "encoding=delimited"
48 #define CONTENT_TYPE_TEXT "text/plain; version=0.0.4"
49
50 static c_avl_tree_t *metrics;
51 static pthread_mutex_t metrics_lock = PTHREAD_MUTEX_INITIALIZER;
52
53 static unsigned short httpd_port = 9103;
54 static struct MHD_Daemon *httpd;
55
56 static cdtime_t staleness_delta = PROMETHEUS_DEFAULT_STALENESS_DELTA;
57
58 /* Unfortunately, protoc-c doesn't export it's implementation of varint, so we
59  * need to implement our own. */
60 static size_t varint(uint8_t buffer[static VARINT_UINT32_BYTES],
61                      uint32_t value) {
62   for (size_t i = 0; i < VARINT_UINT32_BYTES; i++) {
63     buffer[i] = (uint8_t)(value & 0x7f);
64     value >>= 7;
65
66     if (value == 0)
67       return i + 1;
68
69     buffer[i] |= 0x80;
70   }
71
72   return 0;
73 }
74
75 /* format_protobuf iterates over all metric families in "metrics" and adds them
76  * to a buffer in ProtoBuf format. It prefixes each protobuf with its encoded
77  * size, the so called "delimited" format. */
78 static void format_protobuf(ProtobufCBuffer *buffer) {
79   pthread_mutex_lock(&metrics_lock);
80
81   char *unused_name;
82   Io__Prometheus__Client__MetricFamily *fam;
83   c_avl_iterator_t *iter = c_avl_get_iterator(metrics);
84   while (c_avl_iterator_next(iter, (void *)&unused_name, (void *)&fam) == 0) {
85     /* Prometheus uses a message length prefix to determine where one
86      * MetricFamily ends and the next begins. This delimiter is encoded as a
87      * "varint", which is common in Protobufs. */
88     uint8_t delim[VARINT_UINT32_BYTES] = {0};
89     size_t delim_len = varint(
90         delim,
91         (uint32_t)io__prometheus__client__metric_family__get_packed_size(fam));
92     buffer->append(buffer, delim_len, delim);
93
94     io__prometheus__client__metric_family__pack_to_buffer(fam, buffer);
95   }
96   c_avl_iterator_destroy(iter);
97
98   pthread_mutex_unlock(&metrics_lock);
99 }
100
101 /* format_labels formats a metric's labels in Prometheus-compatible format. This
102  * format looks like this:
103  *
104  *   key0="value0",key1="value1"
105  */
106 static char *format_labels(char *buffer, size_t buffer_size,
107                            Io__Prometheus__Client__Metric const *m) {
108   /* our metrics always have at least one and at most three labels. */
109   assert(m->n_label >= 1);
110   assert(m->n_label <= 3);
111
112 #define LABEL_BUFFER_SIZE (2 * DATA_MAX_NAME_LEN + 4)
113
114   char *labels[3] = {
115       (char[LABEL_BUFFER_SIZE]){0}, (char[LABEL_BUFFER_SIZE]){0},
116       (char[LABEL_BUFFER_SIZE]){0},
117   };
118
119   for (size_t i = 0; i < m->n_label; i++)
120     ssnprintf(labels[i], LABEL_BUFFER_SIZE, "%s=\"%s\"", m->label[i]->name,
121               m->label[i]->value);
122
123   strjoin(buffer, buffer_size, labels, m->n_label, ",");
124   return buffer;
125 }
126
127 /* format_protobuf iterates over all metric families in "metrics" and adds them
128  * to a buffer in plain text format. */
129 static void format_text(ProtobufCBuffer *buffer) {
130   pthread_mutex_lock(&metrics_lock);
131
132   char *unused_name;
133   Io__Prometheus__Client__MetricFamily *fam;
134   c_avl_iterator_t *iter = c_avl_get_iterator(metrics);
135   while (c_avl_iterator_next(iter, (void *)&unused_name, (void *)&fam) == 0) {
136     char line[1024]; /* 4x DATA_MAX_NAME_LEN? */
137
138     ssnprintf(line, sizeof(line), "# HELP %s %s\n", fam->name, fam->help);
139     buffer->append(buffer, strlen(line), (uint8_t *)line);
140
141     ssnprintf(line, sizeof(line), "# TYPE %s %s\n", fam->name,
142               (fam->type == IO__PROMETHEUS__CLIENT__METRIC_TYPE__GAUGE)
143                   ? "gauge"
144                   : "counter");
145     buffer->append(buffer, strlen(line), (uint8_t *)line);
146
147     for (size_t i = 0; i < fam->n_metric; i++) {
148       Io__Prometheus__Client__Metric *m = fam->metric[i];
149
150       char labels[1024];
151
152       char timestamp_ms[24] = "";
153       if (m->has_timestamp_ms)
154         ssnprintf(timestamp_ms, sizeof(timestamp_ms), " %" PRIi64,
155                   m->timestamp_ms);
156
157       if (fam->type == IO__PROMETHEUS__CLIENT__METRIC_TYPE__GAUGE)
158         ssnprintf(line, sizeof(line), "%s{%s} " GAUGE_FORMAT "%s\n", fam->name,
159                   format_labels(labels, sizeof(labels), m), m->gauge->value,
160                   timestamp_ms);
161       else /* if (fam->type == IO__PROMETHEUS__CLIENT__METRIC_TYPE__COUNTER) */
162         ssnprintf(line, sizeof(line), "%s{%s} %.0f%s\n", fam->name,
163                   format_labels(labels, sizeof(labels), m), m->counter->value,
164                   timestamp_ms);
165
166       buffer->append(buffer, strlen(line), (uint8_t *)line);
167     }
168   }
169   c_avl_iterator_destroy(iter);
170
171   char server[1024];
172   ssnprintf(server, sizeof(server), "\n# collectd/write_prometheus %s at %s\n",
173             PACKAGE_VERSION, hostname_g);
174   buffer->append(buffer, strlen(server), (uint8_t *)server);
175
176   pthread_mutex_unlock(&metrics_lock);
177 }
178
179 /* http_handler is the callback called by the microhttpd library. It essentially
180  * handles all HTTP request aspects and creates an HTTP response. */
181 static int http_handler(void *cls, struct MHD_Connection *connection,
182                         const char *url, const char *method,
183                         const char *version, const char *upload_data,
184                         size_t *upload_data_size, void **connection_state) {
185   if (strcmp(method, MHD_HTTP_METHOD_GET) != 0) {
186     return MHD_NO;
187   }
188
189   /* On the first call for each connection, return without anything further.
190    * Apparently not everything has been initialized yet or so; the docs are not
191    * very specific on the issue. */
192   if (*connection_state == NULL) {
193     /* set to a random non-NULL pointer. */
194     *connection_state = &(int){42};
195     return MHD_YES;
196   }
197
198   char const *accept = MHD_lookup_connection_value(connection, MHD_HEADER_KIND,
199                                                    MHD_HTTP_HEADER_ACCEPT);
200   _Bool want_proto =
201       (accept != NULL) &&
202       (strstr(accept, "application/vnd.google.protobuf") != NULL);
203
204   uint8_t scratch[4096] = {0};
205   ProtobufCBufferSimple simple = PROTOBUF_C_BUFFER_SIMPLE_INIT(scratch);
206   ProtobufCBuffer *buffer = (ProtobufCBuffer *)&simple;
207
208   if (want_proto)
209     format_protobuf(buffer);
210   else
211     format_text(buffer);
212
213 #if defined(MHD_VERSION) && MHD_VERSION >= 0x00090500
214   struct MHD_Response *res = MHD_create_response_from_buffer(
215       simple.len, simple.data, MHD_RESPMEM_MUST_COPY);
216 #else
217   struct MHD_Response *res = MHD_create_response_from_data(
218       simple.len, simple.data, /* must_free = */ 0, /* must_copy = */ 1);
219 #endif
220   MHD_add_response_header(res, MHD_HTTP_HEADER_CONTENT_TYPE,
221                           want_proto ? CONTENT_TYPE_PROTO : CONTENT_TYPE_TEXT);
222
223   int status = MHD_queue_response(connection, MHD_HTTP_OK, res);
224
225   MHD_destroy_response(res);
226   PROTOBUF_C_BUFFER_SIMPLE_CLEAR(&simple);
227   return status;
228 }
229
230 /*
231  * Functions for manipulating the global state in "metrics". This is organized
232  * in two tiers: the global "metrics" tree holds "metric families", which are
233  * identified by a name (a string). Each metric family has one or more
234  * "metrics", which are identified by a unique set of key-value-pairs. For
235  * example:
236  *
237  * collectd_cpu_total
238  *   {cpu="0",type="idle"}
239  *   {cpu="0",type="user"}
240  *   ...
241  * collectd_memory
242  *   {memory="used"}
243  *   {memory="free"}
244  *   ...
245  * {{{ */
246 /* label_pair_destroy frees the memory used by a label pair. */
247 static void label_pair_destroy(Io__Prometheus__Client__LabelPair *msg) {
248   if (msg == NULL)
249     return;
250
251   sfree(msg->name);
252   sfree(msg->value);
253
254   sfree(msg);
255 }
256
257 /* label_pair_clone allocates and initializes a new label pair. */
258 static Io__Prometheus__Client__LabelPair *
259 label_pair_clone(Io__Prometheus__Client__LabelPair const *orig) {
260   Io__Prometheus__Client__LabelPair *copy = calloc(1, sizeof(*copy));
261   if (copy == NULL)
262     return NULL;
263   io__prometheus__client__label_pair__init(copy);
264
265   copy->name = strdup(orig->name);
266   copy->value = strdup(orig->value);
267   if ((copy->name == NULL) || (copy->value == NULL)) {
268     label_pair_destroy(copy);
269     return NULL;
270   }
271
272   return copy;
273 }
274
275 /* metric_destroy frees the memory used by a metric. */
276 static void metric_destroy(Io__Prometheus__Client__Metric *msg) {
277   if (msg == NULL)
278     return;
279
280   for (size_t i = 0; i < msg->n_label; i++) {
281     label_pair_destroy(msg->label[i]);
282   }
283   sfree(msg->label);
284
285   sfree(msg->gauge);
286   sfree(msg->counter);
287
288   sfree(msg);
289 }
290
291 /* metric_cmp compares two metrics. It's prototype makes it easy to use with
292  * qsort(3) and bsearch(3). */
293 static int metric_cmp(void const *a, void const *b) {
294   Io__Prometheus__Client__Metric const *m_a =
295       *((Io__Prometheus__Client__Metric **)a);
296   Io__Prometheus__Client__Metric const *m_b =
297       *((Io__Prometheus__Client__Metric **)b);
298
299   if (m_a->n_label < m_b->n_label)
300     return -1;
301   else if (m_a->n_label > m_b->n_label)
302     return 1;
303
304   /* Prometheus does not care about the order of labels. All labels in this
305    * plugin are created by METRIC_ADD_LABELS(), though, and therefore always
306    * appear in the same order. We take advantage of this and simplify the check
307    * by making sure all labels are the same in each position.
308    *
309    * We also only need to check the label values, because the label names are
310    * the same for all metrics in a metric family.
311    *
312    * 3 labels:
313    * [0] $plugin="$plugin_instance" => $plugin is the same within a family
314    * [1] type="$type_instance"      => "type" is a static string
315    * [2] instance="$host"           => "instance" is a static string
316    *
317    * 2 labels, variant 1:
318    * [0] $plugin="$plugin_instance" => $plugin is the same within a family
319    * [1] instance="$host"           => "instance" is a static string
320    *
321    * 2 labels, variant 2:
322    * [0] $plugin="$type_instance"   => $plugin is the same within a family
323    * [1] instance="$host"           => "instance" is a static string
324    *
325    * 1 label:
326    * [1] instance="$host"           => "instance" is a static string
327    */
328   for (size_t i = 0; i < m_a->n_label; i++) {
329     int status = strcmp(m_a->label[i]->value, m_b->label[i]->value);
330     if (status != 0)
331       return status;
332
333 #if COLLECT_DEBUG
334     assert(strcmp(m_a->label[i]->name, m_b->label[i]->name) == 0);
335 #endif
336   }
337
338   return 0;
339 }
340
341 #define METRIC_INIT                                                            \
342   &(Io__Prometheus__Client__Metric) {                                          \
343     .label =                                                                   \
344         (Io__Prometheus__Client__LabelPair *[]){                               \
345             &(Io__Prometheus__Client__LabelPair){                              \
346                 .name = NULL,                                                  \
347             },                                                                 \
348             &(Io__Prometheus__Client__LabelPair){                              \
349                 .name = NULL,                                                  \
350             },                                                                 \
351             &(Io__Prometheus__Client__LabelPair){                              \
352                 .name = NULL,                                                  \
353             },                                                                 \
354         },                                                                     \
355     .n_label = 0,                                                              \
356   }
357
358 #define METRIC_ADD_LABELS(m, vl)                                               \
359   do {                                                                         \
360     if (strlen((vl)->plugin_instance) != 0) {                                  \
361       (m)->label[(m)->n_label]->name = (char *)(vl)->plugin;                   \
362       (m)->label[(m)->n_label]->value = (char *)(vl)->plugin_instance;         \
363       (m)->n_label++;                                                          \
364     }                                                                          \
365                                                                                \
366     if (strlen((vl)->type_instance) != 0) {                                    \
367       (m)->label[(m)->n_label]->name = "type";                                 \
368       if (strlen((vl)->plugin_instance) == 0)                                  \
369         (m)->label[(m)->n_label]->name = (char *)(vl)->plugin;                 \
370       (m)->label[(m)->n_label]->value = (char *)(vl)->type_instance;           \
371       (m)->n_label++;                                                          \
372     }                                                                          \
373                                                                                \
374     (m)->label[(m)->n_label]->name = "instance";                               \
375     (m)->label[(m)->n_label]->value = (char *)(vl)->host;                      \
376     (m)->n_label++;                                                            \
377   } while (0)
378
379 /* metric_clone allocates and initializes a new metric based on orig. */
380 static Io__Prometheus__Client__Metric *
381 metric_clone(Io__Prometheus__Client__Metric const *orig) {
382   Io__Prometheus__Client__Metric *copy = calloc(1, sizeof(*copy));
383   if (copy == NULL)
384     return NULL;
385   io__prometheus__client__metric__init(copy);
386
387   copy->n_label = orig->n_label;
388   copy->label = calloc(copy->n_label, sizeof(*copy->label));
389   if (copy->label == NULL) {
390     sfree(copy);
391     return NULL;
392   }
393
394   for (size_t i = 0; i < copy->n_label; i++) {
395     copy->label[i] = label_pair_clone(orig->label[i]);
396     if (copy->label[i] == NULL) {
397       metric_destroy(copy);
398       return NULL;
399     }
400   }
401
402   return copy;
403 }
404
405 /* metric_update stores the new value and timestamp in m. */
406 static int metric_update(Io__Prometheus__Client__Metric *m, value_t value,
407                          int ds_type, cdtime_t t, cdtime_t interval) {
408   if (ds_type == DS_TYPE_GAUGE) {
409     sfree(m->counter);
410     if (m->gauge == NULL) {
411       m->gauge = calloc(1, sizeof(*m->gauge));
412       if (m->gauge == NULL)
413         return ENOMEM;
414       io__prometheus__client__gauge__init(m->gauge);
415     }
416
417     m->gauge->value = (double)value.gauge;
418     m->gauge->has_value = 1;
419   } else { /* not gauge */
420     sfree(m->gauge);
421     if (m->counter == NULL) {
422       m->counter = calloc(1, sizeof(*m->counter));
423       if (m->counter == NULL)
424         return ENOMEM;
425       io__prometheus__client__counter__init(m->counter);
426     }
427
428     switch (ds_type) {
429     case DS_TYPE_ABSOLUTE:
430       m->counter->value = (double)value.absolute;
431       break;
432     case DS_TYPE_COUNTER:
433       m->counter->value = (double)value.counter;
434       break;
435     default:
436       m->counter->value = (double)value.derive;
437       break;
438     }
439     m->counter->has_value = 1;
440   }
441
442   /* Prometheus has a globally configured timeout after which metrics are
443    * considered stale. This causes problems when metrics have an interval
444    * exceeding that limit. We emulate the behavior of "pushgateway" and *not*
445    * send a timestamp value â€“ Prometheus will fill in the current time. */
446   if (interval <= staleness_delta) {
447     m->timestamp_ms = CDTIME_T_TO_MS(t);
448     m->has_timestamp_ms = 1;
449   } else {
450     static c_complain_t long_metric = C_COMPLAIN_INIT_STATIC;
451     c_complain(
452         LOG_NOTICE, &long_metric,
453         "write_prometheus plugin: You have metrics with an interval exceeding "
454         "\"StalenessDelta\" setting (%.3fs). This is suboptimal, please check "
455         "the collectd.conf(5) manual page to understand what's going on.",
456         CDTIME_T_TO_DOUBLE(staleness_delta));
457
458     m->timestamp_ms = 0;
459     m->has_timestamp_ms = 0;
460   }
461
462   return 0;
463 }
464
465 /* metric_family_add_metric adds m to the metric list of fam. */
466 static int metric_family_add_metric(Io__Prometheus__Client__MetricFamily *fam,
467                                     Io__Prometheus__Client__Metric *m) {
468   Io__Prometheus__Client__Metric **tmp =
469       realloc(fam->metric, (fam->n_metric + 1) * sizeof(*fam->metric));
470   if (tmp == NULL)
471     return ENOMEM;
472   fam->metric = tmp;
473
474   fam->metric[fam->n_metric] = m;
475   fam->n_metric++;
476
477   /* Sort the metrics so that lookup is fast. */
478   qsort(fam->metric, fam->n_metric, sizeof(*fam->metric), metric_cmp);
479
480   return 0;
481 }
482
483 /* metric_family_delete_metric looks up and deletes the metric corresponding to
484  * vl. */
485 static int
486 metric_family_delete_metric(Io__Prometheus__Client__MetricFamily *fam,
487                             value_list_t const *vl) {
488   Io__Prometheus__Client__Metric *key = METRIC_INIT;
489   METRIC_ADD_LABELS(key, vl);
490
491   size_t i;
492   for (i = 0; i < fam->n_metric; i++) {
493     if (metric_cmp(&key, &fam->metric[i]) == 0)
494       break;
495   }
496
497   if (i >= fam->n_metric)
498     return ENOENT;
499
500   metric_destroy(fam->metric[i]);
501   if ((fam->n_metric - 1) > i)
502     memmove(&fam->metric[i], &fam->metric[i + 1],
503             ((fam->n_metric - 1) - i) * sizeof(fam->metric[i]));
504   fam->n_metric--;
505
506   Io__Prometheus__Client__Metric **tmp =
507       realloc(fam->metric, fam->n_metric * sizeof(*fam->metric));
508   if ((tmp != NULL) || (fam->n_metric == 0))
509     fam->metric = tmp;
510
511   return 0;
512 }
513
514 /* metric_family_get_metric looks up the matching metric in a metric family,
515  * allocating it if necessary. */
516 static Io__Prometheus__Client__Metric *
517 metric_family_get_metric(Io__Prometheus__Client__MetricFamily *fam,
518                          value_list_t const *vl) {
519   Io__Prometheus__Client__Metric *key = METRIC_INIT;
520   METRIC_ADD_LABELS(key, vl);
521
522   /* Metrics are sorted in metric_family_add_metric() so that we can do a binary
523    * search here. */
524   Io__Prometheus__Client__Metric **m = bsearch(
525       &key, fam->metric, fam->n_metric, sizeof(*fam->metric), metric_cmp);
526
527   if (m != NULL) {
528     return *m;
529   }
530
531   Io__Prometheus__Client__Metric *new_metric = metric_clone(key);
532   if (new_metric == NULL)
533     return NULL;
534
535   DEBUG("write_prometheus plugin: created new metric in family");
536   int status = metric_family_add_metric(fam, new_metric);
537   if (status != 0) {
538     metric_destroy(new_metric);
539     return NULL;
540   }
541
542   return new_metric;
543 }
544
545 /* metric_family_update looks up the matching metric in a metric family,
546  * allocating it if necessary, and updates the metric to the latest value. */
547 static int metric_family_update(Io__Prometheus__Client__MetricFamily *fam,
548                                 data_set_t const *ds, value_list_t const *vl,
549                                 size_t ds_index) {
550   Io__Prometheus__Client__Metric *m = metric_family_get_metric(fam, vl);
551   if (m == NULL)
552     return -1;
553
554   return metric_update(m, vl->values[ds_index], ds->ds[ds_index].type, vl->time,
555                        vl->interval);
556 }
557
558 /* metric_family_destroy frees the memory used by a metric family. */
559 static void metric_family_destroy(Io__Prometheus__Client__MetricFamily *msg) {
560   if (msg == NULL)
561     return;
562
563   sfree(msg->name);
564   sfree(msg->help);
565
566   for (size_t i = 0; i < msg->n_metric; i++) {
567     metric_destroy(msg->metric[i]);
568   }
569   sfree(msg->metric);
570
571   sfree(msg);
572 }
573
574 /* metric_family_create allocates and initializes a new metric family. */
575 static Io__Prometheus__Client__MetricFamily *
576 metric_family_create(char *name, data_set_t const *ds, value_list_t const *vl,
577                      size_t ds_index) {
578   Io__Prometheus__Client__MetricFamily *msg = calloc(1, sizeof(*msg));
579   if (msg == NULL)
580     return NULL;
581   io__prometheus__client__metric_family__init(msg);
582
583   msg->name = name;
584
585   char help[1024];
586   ssnprintf(
587       help, sizeof(help),
588       "write_prometheus plugin: '%s' Type: '%s', Dstype: '%s', Dsname: '%s'",
589       vl->plugin, vl->type, DS_TYPE_TO_STRING(ds->ds[ds_index].type),
590       ds->ds[ds_index].name);
591   msg->help = strdup(help);
592
593   msg->type = (ds->ds[ds_index].type == DS_TYPE_GAUGE)
594                   ? IO__PROMETHEUS__CLIENT__METRIC_TYPE__GAUGE
595                   : IO__PROMETHEUS__CLIENT__METRIC_TYPE__COUNTER;
596   msg->has_type = 1;
597
598   return msg;
599 }
600
601 /* metric_family_name creates a metric family's name from a data source. This is
602  * done in the same way as done by the "collectd_exporter" for best possible
603  * compatibility. In essence, the plugin, type and data source name go in the
604  * metric family name, while hostname, plugin instance and type instance go into
605  * the labels of a metric. */
606 static char *metric_family_name(data_set_t const *ds, value_list_t const *vl,
607                                 size_t ds_index) {
608   char const *fields[5] = {"collectd"};
609   size_t fields_num = 1;
610
611   if (strcmp(vl->plugin, vl->type) != 0) {
612     fields[fields_num] = vl->plugin;
613     fields_num++;
614   }
615   fields[fields_num] = vl->type;
616   fields_num++;
617
618   if (strcmp("value", ds->ds[ds_index].name) != 0) {
619     fields[fields_num] = ds->ds[ds_index].name;
620     fields_num++;
621   }
622
623   /* Prometheus best practices:
624    * cumulative metrics should have a "total" suffix. */
625   if ((ds->ds[ds_index].type == DS_TYPE_COUNTER) ||
626       (ds->ds[ds_index].type == DS_TYPE_DERIVE)) {
627     fields[fields_num] = "total";
628     fields_num++;
629   }
630
631   char name[5 * DATA_MAX_NAME_LEN];
632   strjoin(name, sizeof(name), (char **)fields, fields_num, "_");
633   return strdup(name);
634 }
635
636 /* metric_family_get looks up the matching metric family, allocating it if
637  * necessary. */
638 static Io__Prometheus__Client__MetricFamily *
639 metric_family_get(data_set_t const *ds, value_list_t const *vl, size_t ds_index,
640                   _Bool allocate) {
641   char *name = metric_family_name(ds, vl, ds_index);
642   if (name == NULL) {
643     ERROR("write_prometheus plugin: Allocating metric family name failed.");
644     return NULL;
645   }
646
647   Io__Prometheus__Client__MetricFamily *fam = NULL;
648   if (c_avl_get(metrics, name, (void *)&fam) == 0) {
649     sfree(name);
650     assert(fam != NULL);
651     return fam;
652   }
653
654   if (!allocate)
655     return NULL;
656
657   fam = metric_family_create(name, ds, vl, ds_index);
658   if (fam == NULL) {
659     ERROR("write_prometheus plugin: Allocating metric family failed.");
660     sfree(name);
661     return NULL;
662   }
663
664   /* If successful, "name" is owned by "fam", i.e. don't free it here. */
665   DEBUG("write_prometheus plugin: metric family \"%s\" has been created.",
666         name);
667   name = NULL;
668
669   int status = c_avl_insert(metrics, fam->name, fam);
670   if (status != 0) {
671     ERROR("write_prometheus plugin: Adding \"%s\" failed.", name);
672     metric_family_destroy(fam);
673     return NULL;
674   }
675
676   return fam;
677 }
678 /* }}} */
679
680 /*
681  * collectd callbacks
682  */
683 static int prom_config(oconfig_item_t *ci) {
684   for (int i = 0; i < ci->children_num; i++) {
685     oconfig_item_t *child = ci->children + i;
686
687     if (strcasecmp("Port", child->key) == 0) {
688       int status = cf_util_get_port_number(child);
689       if (status > 0)
690         httpd_port = (unsigned short)status;
691     } else if (strcasecmp("StalenessDelta", child->key) == 0) {
692       cf_util_get_cdtime(child, &staleness_delta);
693     } else {
694       WARNING("write_prometheus plugin: Ignoring unknown configuration option "
695               "\"%s\".",
696               child->key);
697     }
698   }
699
700   return 0;
701 }
702
703 static int prom_init() {
704   if (metrics == NULL) {
705     metrics = c_avl_create((void *)strcmp);
706     if (metrics == NULL) {
707       ERROR("write_prometheus plugin: c_avl_create() failed.");
708       return -1;
709     }
710   }
711
712   if (httpd == NULL) {
713     unsigned int flags = MHD_USE_THREAD_PER_CONNECTION;
714 #if MHD_VERSION >= 0x00093300
715     flags |= MHD_USE_DUAL_STACK;
716 #endif
717
718     httpd = MHD_start_daemon(flags, httpd_port,
719                              /* MHD_AcceptPolicyCallback = */ NULL,
720                              /* MHD_AcceptPolicyCallback arg = */ NULL,
721                              http_handler, NULL, MHD_OPTION_END);
722     if (httpd == NULL) {
723       ERROR("write_prometheus plugin: MHD_start_daemon() failed.");
724       return -1;
725     }
726     DEBUG("write_prometheus plugin: Successfully started microhttpd %s",
727           MHD_get_version());
728   }
729
730   return 0;
731 }
732
733 static int prom_write(data_set_t const *ds, value_list_t const *vl,
734                       __attribute__((unused)) user_data_t *ud) {
735   pthread_mutex_lock(&metrics_lock);
736
737   for (size_t i = 0; i < ds->ds_num; i++) {
738     Io__Prometheus__Client__MetricFamily *fam =
739         metric_family_get(ds, vl, i, /* allocate = */ 1);
740     if (fam == NULL)
741       continue;
742
743     int status = metric_family_update(fam, ds, vl, i);
744     if (status != 0) {
745       ERROR("write_prometheus plugin: Updating metric \"%s\" failed with "
746             "status %d",
747             fam->name, status);
748       continue;
749     }
750   }
751
752   pthread_mutex_unlock(&metrics_lock);
753   return 0;
754 }
755
756 static int prom_missing(value_list_t const *vl,
757                         __attribute__((unused)) user_data_t *ud) {
758   data_set_t const *ds = plugin_get_ds(vl->type);
759   if (ds == NULL)
760     return ENOENT;
761
762   pthread_mutex_lock(&metrics_lock);
763
764   for (size_t i = 0; i < ds->ds_num; i++) {
765     Io__Prometheus__Client__MetricFamily *fam =
766         metric_family_get(ds, vl, i, /* allocate = */ 0);
767     if (fam == NULL)
768       continue;
769
770     int status = metric_family_delete_metric(fam, vl);
771     if (status != 0) {
772       ERROR("write_prometheus plugin: Deleting a metric in family \"%s\" "
773             "failed with status %d",
774             fam->name, status);
775
776       continue;
777     }
778
779     if (fam->n_metric == 0) {
780       int status = c_avl_remove(metrics, fam->name, NULL, NULL);
781       if (status != 0) {
782         ERROR("write_prometheus plugin: Deleting metric family \"%s\" failed "
783               "with status %d",
784               fam->name, status);
785         continue;
786       }
787       metric_family_destroy(fam);
788     }
789   }
790
791   pthread_mutex_unlock(&metrics_lock);
792   return 0;
793 }
794
795 static int prom_shutdown() {
796   if (httpd != NULL) {
797     MHD_stop_daemon(httpd);
798     httpd = NULL;
799   }
800
801   pthread_mutex_lock(&metrics_lock);
802   if (metrics != NULL) {
803     char *name;
804     Io__Prometheus__Client__MetricFamily *fam;
805     while (c_avl_pick(metrics, (void *)&name, (void *)&fam) == 0) {
806       assert(name == fam->name);
807       name = NULL;
808
809       metric_family_destroy(fam);
810     }
811     c_avl_destroy(metrics);
812     metrics = NULL;
813   }
814   pthread_mutex_unlock(&metrics_lock);
815
816   return 0;
817 }
818
819 void module_register() {
820   plugin_register_complex_config("write_prometheus", prom_config);
821   plugin_register_init("write_prometheus", prom_init);
822   plugin_register_write("write_prometheus", prom_write,
823                         /* user data = */ NULL);
824   plugin_register_missing("write_prometheus", prom_missing,
825                           /* user data = */ NULL);
826   plugin_register_shutdown("write_prometheus", prom_shutdown);
827 }
828
829 /* vim: set sw=2 sts=2 et fdm=marker : */