2 * collectd - src/write_prometheus.c
3 * Copyright (C) 2016 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
24 * Florian octo Forster <octo at collectd.org>
30 #include "utils/avltree/avltree.h"
31 #include "utils/common/common.h"
32 #include "utils_complain.h"
33 #include "utils_time.h"
35 #include "prometheus.pb-c.h"
37 #include <microhttpd.h>
40 #include <sys/socket.h>
41 #include <sys/types.h>
43 #ifndef PROMETHEUS_DEFAULT_STALENESS_DELTA
44 #define PROMETHEUS_DEFAULT_STALENESS_DELTA TIME_T_TO_CDTIME_T_STATIC(300)
47 #define VARINT_UINT32_BYTES 5
49 #define CONTENT_TYPE_PROTO \
50 "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; " \
52 #define CONTENT_TYPE_TEXT "text/plain; version=0.0.4"
54 static c_avl_tree_t *metrics;
55 static pthread_mutex_t metrics_lock = PTHREAD_MUTEX_INITIALIZER;
57 static char *httpd_host = NULL;
58 static unsigned short httpd_port = 9103;
59 static struct MHD_Daemon *httpd;
61 static cdtime_t staleness_delta = PROMETHEUS_DEFAULT_STALENESS_DELTA;
63 /* Unfortunately, protoc-c doesn't export its implementation of varint, so we
64 * need to implement our own. */
65 static size_t varint(uint8_t buffer[static VARINT_UINT32_BYTES],
67 for (size_t i = 0; i < VARINT_UINT32_BYTES; i++) {
68 buffer[i] = (uint8_t)(value & 0x7f);
80 /* format_protobuf iterates over all metric families in "metrics" and adds them
81 * to a buffer in ProtoBuf format. It prefixes each protobuf with its encoded
82 * size, the so called "delimited" format. */
83 static void format_protobuf(ProtobufCBuffer *buffer) {
84 pthread_mutex_lock(&metrics_lock);
87 Io__Prometheus__Client__MetricFamily *fam;
88 c_avl_iterator_t *iter = c_avl_get_iterator(metrics);
89 while (c_avl_iterator_next(iter, (void *)&unused_name, (void *)&fam) == 0) {
90 /* Prometheus uses a message length prefix to determine where one
91 * MetricFamily ends and the next begins. This delimiter is encoded as a
92 * "varint", which is common in Protobufs. */
93 uint8_t delim[VARINT_UINT32_BYTES] = {0};
94 size_t delim_len = varint(
96 (uint32_t)io__prometheus__client__metric_family__get_packed_size(fam));
97 buffer->append(buffer, delim_len, delim);
99 io__prometheus__client__metric_family__pack_to_buffer(fam, buffer);
101 c_avl_iterator_destroy(iter);
103 pthread_mutex_unlock(&metrics_lock);
106 static char const *escape_label_value(char *buffer, size_t buffer_size,
108 /* shortcut for values that don't need escaping. */
109 if (strpbrk(value, "\n\"\\") == NULL)
112 size_t value_len = strlen(value);
113 size_t buffer_len = 0;
115 for (size_t i = 0; i < value_len; i++) {
120 if ((buffer_size - buffer_len) < 3) {
123 buffer[buffer_len] = '\\';
124 buffer[buffer_len + 1] = (value[i] == '\n') ? 'n' : value[i];
129 if ((buffer_size - buffer_len) < 2) {
132 buffer[buffer_len] = value[i];
138 assert(buffer_len < buffer_size);
139 buffer[buffer_len] = 0;
143 /* format_labels formats a metric's labels in Prometheus-compatible format. This
144 * format looks like this:
146 * key0="value0",key1="value1"
148 static char *format_labels(char *buffer, size_t buffer_size,
149 Io__Prometheus__Client__Metric const *m) {
150 /* our metrics always have at least one and at most three labels. */
151 assert(m->n_label >= 1);
152 assert(m->n_label <= 3);
154 #define LABEL_KEY_SIZE DATA_MAX_NAME_LEN
155 #define LABEL_VALUE_SIZE (2 * DATA_MAX_NAME_LEN - 1)
156 #define LABEL_BUFFER_SIZE (LABEL_KEY_SIZE + LABEL_VALUE_SIZE + 4)
159 (char[LABEL_BUFFER_SIZE]){0},
160 (char[LABEL_BUFFER_SIZE]){0},
161 (char[LABEL_BUFFER_SIZE]){0},
164 /* N.B.: the label *names* are hard-coded by this plugin and therefore we
165 * know that they are sane. */
166 for (size_t i = 0; i < m->n_label; i++) {
167 char value[LABEL_VALUE_SIZE];
168 ssnprintf(labels[i], LABEL_BUFFER_SIZE, "%s=\"%s\"", m->label[i]->name,
169 escape_label_value(value, sizeof(value), m->label[i]->value));
172 strjoin(buffer, buffer_size, labels, m->n_label, ",");
176 /* format_protobuf iterates over all metric families in "metrics" and adds them
177 * to a buffer in plain text format. */
178 static void format_text(ProtobufCBuffer *buffer) {
179 pthread_mutex_lock(&metrics_lock);
182 Io__Prometheus__Client__MetricFamily *fam;
183 c_avl_iterator_t *iter = c_avl_get_iterator(metrics);
184 while (c_avl_iterator_next(iter, (void *)&unused_name, (void *)&fam) == 0) {
185 char line[1024]; /* 4x DATA_MAX_NAME_LEN? */
187 ssnprintf(line, sizeof(line), "# HELP %s %s\n", fam->name, fam->help);
188 buffer->append(buffer, strlen(line), (uint8_t *)line);
190 ssnprintf(line, sizeof(line), "# TYPE %s %s\n", fam->name,
191 (fam->type == IO__PROMETHEUS__CLIENT__METRIC_TYPE__GAUGE)
194 buffer->append(buffer, strlen(line), (uint8_t *)line);
196 for (size_t i = 0; i < fam->n_metric; i++) {
197 Io__Prometheus__Client__Metric *m = fam->metric[i];
201 char timestamp_ms[24] = "";
202 if (m->has_timestamp_ms)
203 ssnprintf(timestamp_ms, sizeof(timestamp_ms), " %" PRIi64,
206 if (fam->type == IO__PROMETHEUS__CLIENT__METRIC_TYPE__GAUGE)
207 ssnprintf(line, sizeof(line), "%s{%s} " GAUGE_FORMAT "%s\n", fam->name,
208 format_labels(labels, sizeof(labels), m), m->gauge->value,
210 else /* if (fam->type == IO__PROMETHEUS__CLIENT__METRIC_TYPE__COUNTER) */
211 ssnprintf(line, sizeof(line), "%s{%s} %.0f%s\n", fam->name,
212 format_labels(labels, sizeof(labels), m), m->counter->value,
215 buffer->append(buffer, strlen(line), (uint8_t *)line);
218 c_avl_iterator_destroy(iter);
221 ssnprintf(server, sizeof(server), "\n# collectd/write_prometheus %s at %s\n",
222 PACKAGE_VERSION, hostname_g);
223 buffer->append(buffer, strlen(server), (uint8_t *)server);
225 pthread_mutex_unlock(&metrics_lock);
228 /* http_handler is the callback called by the microhttpd library. It essentially
229 * handles all HTTP request aspects and creates an HTTP response. */
230 static int http_handler(void *cls, struct MHD_Connection *connection,
231 const char *url, const char *method,
232 const char *version, const char *upload_data,
233 size_t *upload_data_size, void **connection_state) {
234 if (strcmp(method, MHD_HTTP_METHOD_GET) != 0) {
238 /* On the first call for each connection, return without anything further.
239 * Apparently not everything has been initialized yet or so; the docs are not
240 * very specific on the issue. */
241 if (*connection_state == NULL) {
242 /* set to a random non-NULL pointer. */
243 *connection_state = &(int){42};
247 char const *accept = MHD_lookup_connection_value(connection, MHD_HEADER_KIND,
248 MHD_HTTP_HEADER_ACCEPT);
249 bool want_proto = (accept != NULL) &&
250 (strstr(accept, "application/vnd.google.protobuf") != NULL);
252 uint8_t scratch[4096] = {0};
253 ProtobufCBufferSimple simple = PROTOBUF_C_BUFFER_SIMPLE_INIT(scratch);
254 ProtobufCBuffer *buffer = (ProtobufCBuffer *)&simple;
257 format_protobuf(buffer);
261 #if defined(MHD_VERSION) && MHD_VERSION >= 0x00090500
262 struct MHD_Response *res = MHD_create_response_from_buffer(
263 simple.len, simple.data, MHD_RESPMEM_MUST_COPY);
265 struct MHD_Response *res = MHD_create_response_from_data(
266 simple.len, simple.data, /* must_free = */ 0, /* must_copy = */ 1);
268 MHD_add_response_header(res, MHD_HTTP_HEADER_CONTENT_TYPE,
269 want_proto ? CONTENT_TYPE_PROTO : CONTENT_TYPE_TEXT);
271 int status = MHD_queue_response(connection, MHD_HTTP_OK, res);
273 MHD_destroy_response(res);
274 PROTOBUF_C_BUFFER_SIMPLE_CLEAR(&simple);
279 * Functions for manipulating the global state in "metrics". This is organized
280 * in two tiers: the global "metrics" tree holds "metric families", which are
281 * identified by a name (a string). Each metric family has one or more
282 * "metrics", which are identified by a unique set of key-value-pairs. For
286 * {cpu="0",type="idle"}
287 * {cpu="0",type="user"}
294 /* label_pair_destroy frees the memory used by a label pair. */
295 static void label_pair_destroy(Io__Prometheus__Client__LabelPair *msg) {
305 /* label_pair_clone allocates and initializes a new label pair. */
306 static Io__Prometheus__Client__LabelPair *
307 label_pair_clone(Io__Prometheus__Client__LabelPair const *orig) {
308 Io__Prometheus__Client__LabelPair *copy = calloc(1, sizeof(*copy));
311 io__prometheus__client__label_pair__init(copy);
313 copy->name = strdup(orig->name);
314 copy->value = strdup(orig->value);
315 if ((copy->name == NULL) || (copy->value == NULL)) {
316 label_pair_destroy(copy);
323 /* metric_destroy frees the memory used by a metric. */
324 static void metric_destroy(Io__Prometheus__Client__Metric *msg) {
328 for (size_t i = 0; i < msg->n_label; i++) {
329 label_pair_destroy(msg->label[i]);
339 /* metric_cmp compares two metrics. It's prototype makes it easy to use with
340 * qsort(3) and bsearch(3). */
341 static int metric_cmp(void const *a, void const *b) {
342 Io__Prometheus__Client__Metric const *m_a =
343 *((Io__Prometheus__Client__Metric **)a);
344 Io__Prometheus__Client__Metric const *m_b =
345 *((Io__Prometheus__Client__Metric **)b);
347 if (m_a->n_label < m_b->n_label)
349 else if (m_a->n_label > m_b->n_label)
352 /* Prometheus does not care about the order of labels. All labels in this
353 * plugin are created by METRIC_ADD_LABELS(), though, and therefore always
354 * appear in the same order. We take advantage of this and simplify the check
355 * by making sure all labels are the same in each position.
357 * We also only need to check the label values, because the label names are
358 * the same for all metrics in a metric family.
361 * [0] $plugin="$plugin_instance" => $plugin is the same within a family
362 * [1] type="$type_instance" => "type" is a static string
363 * [2] instance="$host" => "instance" is a static string
365 * 2 labels, variant 1:
366 * [0] $plugin="$plugin_instance" => $plugin is the same within a family
367 * [1] instance="$host" => "instance" is a static string
369 * 2 labels, variant 2:
370 * [0] $plugin="$type_instance" => $plugin is the same within a family
371 * [1] instance="$host" => "instance" is a static string
374 * [1] instance="$host" => "instance" is a static string
376 for (size_t i = 0; i < m_a->n_label; i++) {
377 int status = strcmp(m_a->label[i]->value, m_b->label[i]->value);
382 assert(strcmp(m_a->label[i]->name, m_b->label[i]->name) == 0);
389 #define METRIC_INIT \
390 &(Io__Prometheus__Client__Metric) { \
392 (Io__Prometheus__Client__LabelPair *[]){ \
393 &(Io__Prometheus__Client__LabelPair){ \
396 &(Io__Prometheus__Client__LabelPair){ \
399 &(Io__Prometheus__Client__LabelPair){ \
406 #define METRIC_ADD_LABELS(m, vl) \
408 if (strlen((vl)->plugin_instance) != 0) { \
409 (m)->label[(m)->n_label]->name = (char *)(vl)->plugin; \
410 (m)->label[(m)->n_label]->value = (char *)(vl)->plugin_instance; \
414 if (strlen((vl)->type_instance) != 0) { \
415 (m)->label[(m)->n_label]->name = "type"; \
416 if (strlen((vl)->plugin_instance) == 0) \
417 (m)->label[(m)->n_label]->name = (char *)(vl)->plugin; \
418 (m)->label[(m)->n_label]->value = (char *)(vl)->type_instance; \
422 (m)->label[(m)->n_label]->name = "instance"; \
423 (m)->label[(m)->n_label]->value = (char *)(vl)->host; \
427 /* metric_clone allocates and initializes a new metric based on orig. */
428 static Io__Prometheus__Client__Metric *
429 metric_clone(Io__Prometheus__Client__Metric const *orig) {
430 Io__Prometheus__Client__Metric *copy = calloc(1, sizeof(*copy));
433 io__prometheus__client__metric__init(copy);
435 copy->n_label = orig->n_label;
436 copy->label = calloc(copy->n_label, sizeof(*copy->label));
437 if (copy->label == NULL) {
442 for (size_t i = 0; i < copy->n_label; i++) {
443 copy->label[i] = label_pair_clone(orig->label[i]);
444 if (copy->label[i] == NULL) {
445 metric_destroy(copy);
453 /* metric_update stores the new value and timestamp in m. */
454 static int metric_update(Io__Prometheus__Client__Metric *m, value_t value,
455 int ds_type, cdtime_t t, cdtime_t interval) {
456 if (ds_type == DS_TYPE_GAUGE) {
458 if (m->gauge == NULL) {
459 m->gauge = calloc(1, sizeof(*m->gauge));
460 if (m->gauge == NULL)
462 io__prometheus__client__gauge__init(m->gauge);
465 m->gauge->value = (double)value.gauge;
466 m->gauge->has_value = 1;
467 } else { /* not gauge */
469 if (m->counter == NULL) {
470 m->counter = calloc(1, sizeof(*m->counter));
471 if (m->counter == NULL)
473 io__prometheus__client__counter__init(m->counter);
477 case DS_TYPE_ABSOLUTE:
478 m->counter->value = (double)value.absolute;
480 case DS_TYPE_COUNTER:
481 m->counter->value = (double)value.counter;
484 m->counter->value = (double)value.derive;
487 m->counter->has_value = 1;
490 /* Prometheus has a globally configured timeout after which metrics are
491 * considered stale. This causes problems when metrics have an interval
492 * exceeding that limit. We emulate the behavior of "pushgateway" and *not*
493 * send a timestamp value – Prometheus will fill in the current time. */
494 if (interval <= staleness_delta) {
495 m->timestamp_ms = CDTIME_T_TO_MS(t);
496 m->has_timestamp_ms = 1;
498 static c_complain_t long_metric = C_COMPLAIN_INIT_STATIC;
500 LOG_NOTICE, &long_metric,
501 "write_prometheus plugin: You have metrics with an interval exceeding "
502 "\"StalenessDelta\" setting (%.3fs). This is suboptimal, please check "
503 "the collectd.conf(5) manual page to understand what's going on.",
504 CDTIME_T_TO_DOUBLE(staleness_delta));
507 m->has_timestamp_ms = 0;
513 /* metric_family_add_metric adds m to the metric list of fam. */
514 static int metric_family_add_metric(Io__Prometheus__Client__MetricFamily *fam,
515 Io__Prometheus__Client__Metric *m) {
516 Io__Prometheus__Client__Metric **tmp =
517 realloc(fam->metric, (fam->n_metric + 1) * sizeof(*fam->metric));
522 fam->metric[fam->n_metric] = m;
525 /* Sort the metrics so that lookup is fast. */
526 qsort(fam->metric, fam->n_metric, sizeof(*fam->metric), metric_cmp);
531 /* metric_family_delete_metric looks up and deletes the metric corresponding to
534 metric_family_delete_metric(Io__Prometheus__Client__MetricFamily *fam,
535 value_list_t const *vl) {
536 Io__Prometheus__Client__Metric *key = METRIC_INIT;
537 METRIC_ADD_LABELS(key, vl);
540 for (i = 0; i < fam->n_metric; i++) {
541 if (metric_cmp(&key, &fam->metric[i]) == 0)
545 if (i >= fam->n_metric)
548 metric_destroy(fam->metric[i]);
549 if ((fam->n_metric - 1) > i)
550 memmove(&fam->metric[i], &fam->metric[i + 1],
551 ((fam->n_metric - 1) - i) * sizeof(fam->metric[i]));
554 if (fam->n_metric == 0) {
559 Io__Prometheus__Client__Metric **tmp =
560 realloc(fam->metric, fam->n_metric * sizeof(*fam->metric));
567 /* metric_family_get_metric looks up the matching metric in a metric family,
568 * allocating it if necessary. */
569 static Io__Prometheus__Client__Metric *
570 metric_family_get_metric(Io__Prometheus__Client__MetricFamily *fam,
571 value_list_t const *vl) {
572 Io__Prometheus__Client__Metric *key = METRIC_INIT;
573 METRIC_ADD_LABELS(key, vl);
575 /* Metrics are sorted in metric_family_add_metric() so that we can do a binary
577 Io__Prometheus__Client__Metric **m = bsearch(
578 &key, fam->metric, fam->n_metric, sizeof(*fam->metric), metric_cmp);
584 Io__Prometheus__Client__Metric *new_metric = metric_clone(key);
585 if (new_metric == NULL)
588 DEBUG("write_prometheus plugin: created new metric in family");
589 int status = metric_family_add_metric(fam, new_metric);
591 metric_destroy(new_metric);
598 /* metric_family_update looks up the matching metric in a metric family,
599 * allocating it if necessary, and updates the metric to the latest value. */
600 static int metric_family_update(Io__Prometheus__Client__MetricFamily *fam,
601 data_set_t const *ds, value_list_t const *vl,
603 Io__Prometheus__Client__Metric *m = metric_family_get_metric(fam, vl);
607 return metric_update(m, vl->values[ds_index], ds->ds[ds_index].type, vl->time,
611 /* metric_family_destroy frees the memory used by a metric family. */
612 static void metric_family_destroy(Io__Prometheus__Client__MetricFamily *msg) {
619 for (size_t i = 0; i < msg->n_metric; i++) {
620 metric_destroy(msg->metric[i]);
627 /* metric_family_create allocates and initializes a new metric family. */
628 static Io__Prometheus__Client__MetricFamily *
629 metric_family_create(char *name, data_set_t const *ds, value_list_t const *vl,
631 Io__Prometheus__Client__MetricFamily *msg = calloc(1, sizeof(*msg));
634 io__prometheus__client__metric_family__init(msg);
641 "write_prometheus plugin: '%s' Type: '%s', Dstype: '%s', Dsname: '%s'",
642 vl->plugin, vl->type, DS_TYPE_TO_STRING(ds->ds[ds_index].type),
643 ds->ds[ds_index].name);
644 msg->help = strdup(help);
646 msg->type = (ds->ds[ds_index].type == DS_TYPE_GAUGE)
647 ? IO__PROMETHEUS__CLIENT__METRIC_TYPE__GAUGE
648 : IO__PROMETHEUS__CLIENT__METRIC_TYPE__COUNTER;
654 /* metric_family_name creates a metric family's name from a data source. This is
655 * done in the same way as done by the "collectd_exporter" for best possible
656 * compatibility. In essence, the plugin, type and data source name go in the
657 * metric family name, while hostname, plugin instance and type instance go into
658 * the labels of a metric. */
659 static char *metric_family_name(data_set_t const *ds, value_list_t const *vl,
661 char const *fields[5] = {"collectd"};
662 size_t fields_num = 1;
664 if (strcmp(vl->plugin, vl->type) != 0) {
665 fields[fields_num] = vl->plugin;
668 fields[fields_num] = vl->type;
671 if (strcmp("value", ds->ds[ds_index].name) != 0) {
672 fields[fields_num] = ds->ds[ds_index].name;
676 /* Prometheus best practices:
677 * cumulative metrics should have a "total" suffix. */
678 if ((ds->ds[ds_index].type == DS_TYPE_COUNTER) ||
679 (ds->ds[ds_index].type == DS_TYPE_DERIVE)) {
680 fields[fields_num] = "total";
684 char name[5 * DATA_MAX_NAME_LEN];
685 strjoin(name, sizeof(name), (char **)fields, fields_num, "_");
689 /* metric_family_get looks up the matching metric family, allocating it if
691 static Io__Prometheus__Client__MetricFamily *
692 metric_family_get(data_set_t const *ds, value_list_t const *vl, size_t ds_index,
694 char *name = metric_family_name(ds, vl, ds_index);
696 ERROR("write_prometheus plugin: Allocating metric family name failed.");
700 Io__Prometheus__Client__MetricFamily *fam = NULL;
701 if (c_avl_get(metrics, name, (void *)&fam) == 0) {
712 fam = metric_family_create(name, ds, vl, ds_index);
714 ERROR("write_prometheus plugin: Allocating metric family failed.");
719 /* If successful, "name" is owned by "fam", i.e. don't free it here. */
720 DEBUG("write_prometheus plugin: metric family \"%s\" has been created.",
724 int status = c_avl_insert(metrics, fam->name, fam);
726 ERROR("write_prometheus plugin: Adding \"%s\" failed.", fam->name);
727 metric_family_destroy(fam);
735 static void prom_logger(__attribute__((unused)) void *arg, char const *fmt,
739 vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
741 ERROR("write_prometheus plugin: %s", errbuf);
742 } /* }}} prom_logger */
744 #if MHD_VERSION >= 0x00090000
745 static int prom_open_socket(int addrfamily) {
747 char service[NI_MAXSERV];
748 ssnprintf(service, sizeof(service), "%hu", httpd_port);
750 struct addrinfo *res;
751 int status = getaddrinfo(httpd_host, service,
753 .ai_flags = AI_PASSIVE | AI_ADDRCONFIG,
754 .ai_family = addrfamily,
755 .ai_socktype = SOCK_STREAM,
763 for (struct addrinfo *ai = res; ai != NULL; ai = ai->ai_next) {
764 int flags = ai->ai_socktype;
766 flags |= SOCK_CLOEXEC;
769 fd = socket(ai->ai_family, flags, 0);
773 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) != 0) {
774 WARNING("write_prometheus plugin: setsockopt(SO_REUSEADDR) failed: %s",
781 if (bind(fd, ai->ai_addr, ai->ai_addrlen) != 0) {
787 if (listen(fd, /* backlog = */ 16) != 0) {
793 char str_node[NI_MAXHOST];
794 char str_service[NI_MAXSERV];
796 getnameinfo(ai->ai_addr, ai->ai_addrlen, str_node, sizeof(str_node),
797 str_service, sizeof(str_service),
798 NI_NUMERICHOST | NI_NUMERICSERV);
800 INFO("write_prometheus plugin: Listening on [%s]:%s.", str_node,
808 } /* }}} int prom_open_socket */
810 static struct MHD_Daemon *prom_start_daemon() {
812 int fd = prom_open_socket(PF_INET6);
814 fd = prom_open_socket(PF_INET);
816 ERROR("write_prometheus plugin: Opening a listening socket for [%s]:%hu "
818 (httpd_host != NULL) ? httpd_host : "::", httpd_port);
822 unsigned int flags = MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG;
823 #if MHD_VERSION >= 0x00095300
824 flags |= MHD_USE_INTERNAL_POLLING_THREAD;
827 struct MHD_Daemon *d = MHD_start_daemon(
829 /* MHD_AcceptPolicyCallback = */ NULL,
830 /* MHD_AcceptPolicyCallback arg = */ NULL, http_handler, NULL,
831 MHD_OPTION_LISTEN_SOCKET, fd, MHD_OPTION_EXTERNAL_LOGGER, prom_logger,
832 NULL, MHD_OPTION_END);
834 ERROR("write_prometheus plugin: MHD_start_daemon() failed.");
840 } /* }}} struct MHD_Daemon *prom_start_daemon */
841 #else /* if MHD_VERSION < 0x00090000 */
842 static struct MHD_Daemon *prom_start_daemon() {
844 struct MHD_Daemon *d = MHD_start_daemon(
845 MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, httpd_port,
846 /* MHD_AcceptPolicyCallback = */ NULL,
847 /* MHD_AcceptPolicyCallback arg = */ NULL, http_handler, NULL,
848 MHD_OPTION_EXTERNAL_LOGGER, prom_logger, NULL, MHD_OPTION_END);
850 ERROR("write_prometheus plugin: MHD_start_daemon() failed.");
855 } /* }}} struct MHD_Daemon *prom_start_daemon */
861 static int prom_config(oconfig_item_t *ci) {
862 for (int i = 0; i < ci->children_num; i++) {
863 oconfig_item_t *child = ci->children + i;
865 if (strcasecmp("Host", child->key) == 0) {
866 #if MHD_VERSION >= 0x00090000
867 cf_util_get_string(child, &httpd_host);
869 ERROR("write_prometheus plugin: Option `Host' not supported. Please "
870 "upgrade libmicrohttpd to at least 0.9.0");
873 } else if (strcasecmp("Port", child->key) == 0) {
874 int status = cf_util_get_port_number(child);
876 httpd_port = (unsigned short)status;
877 } else if (strcasecmp("StalenessDelta", child->key) == 0) {
878 cf_util_get_cdtime(child, &staleness_delta);
880 WARNING("write_prometheus plugin: Ignoring unknown configuration option "
889 static int prom_init() {
890 if (metrics == NULL) {
891 metrics = c_avl_create((void *)strcmp);
892 if (metrics == NULL) {
893 ERROR("write_prometheus plugin: c_avl_create() failed.");
899 httpd = prom_start_daemon();
903 DEBUG("write_prometheus plugin: Successfully started microhttpd %s",
910 static int prom_write(data_set_t const *ds, value_list_t const *vl,
911 __attribute__((unused)) user_data_t *ud) {
912 pthread_mutex_lock(&metrics_lock);
914 for (size_t i = 0; i < ds->ds_num; i++) {
915 Io__Prometheus__Client__MetricFamily *fam =
916 metric_family_get(ds, vl, i, /* allocate = */ true);
920 int status = metric_family_update(fam, ds, vl, i);
922 ERROR("write_prometheus plugin: Updating metric \"%s\" failed with "
929 pthread_mutex_unlock(&metrics_lock);
933 static int prom_missing(value_list_t const *vl,
934 __attribute__((unused)) user_data_t *ud) {
935 data_set_t const *ds = plugin_get_ds(vl->type);
939 pthread_mutex_lock(&metrics_lock);
941 for (size_t i = 0; i < ds->ds_num; i++) {
942 Io__Prometheus__Client__MetricFamily *fam =
943 metric_family_get(ds, vl, i, /* allocate = */ false);
947 int status = metric_family_delete_metric(fam, vl);
949 ERROR("write_prometheus plugin: Deleting a metric in family \"%s\" "
950 "failed with status %d",
956 if (fam->n_metric == 0) {
957 int status = c_avl_remove(metrics, fam->name, NULL, NULL);
959 ERROR("write_prometheus plugin: Deleting metric family \"%s\" failed "
964 metric_family_destroy(fam);
968 pthread_mutex_unlock(&metrics_lock);
972 static int prom_shutdown() {
974 MHD_stop_daemon(httpd);
978 pthread_mutex_lock(&metrics_lock);
979 if (metrics != NULL) {
981 Io__Prometheus__Client__MetricFamily *fam;
982 while (c_avl_pick(metrics, (void *)&name, (void *)&fam) == 0) {
983 assert(name == fam->name);
986 metric_family_destroy(fam);
988 c_avl_destroy(metrics);
991 pthread_mutex_unlock(&metrics_lock);
998 void module_register() {
999 plugin_register_complex_config("write_prometheus", prom_config);
1000 plugin_register_init("write_prometheus", prom_init);
1001 plugin_register_write("write_prometheus", prom_write,
1002 /* user data = */ NULL);
1003 plugin_register_missing("write_prometheus", prom_missing,
1004 /* user data = */ NULL);
1005 plugin_register_shutdown("write_prometheus", prom_shutdown);