X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fwrite_prometheus.c;h=61c43fed7ae921265e049c663804a92e0273a060;hb=a9e50e9e30ecde17e167e271060c8183bfcbf407;hp=c91f012ff79cdb262ea0e3fb3bed618f82a90763;hpb=cc893903f8453dc96a797b319bdd4e294052de6f;p=collectd.git diff --git a/src/write_prometheus.c b/src/write_prometheus.c index c91f012f..26b0b4d4 100644 --- a/src/write_prometheus.c +++ b/src/write_prometheus.c @@ -36,8 +36,12 @@ #include +#include +#include +#include + #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 @@ -55,7 +59,7 @@ static struct MHD_Daemon *httpd; static cdtime_t staleness_delta = PROMETHEUS_DEFAULT_STALENESS_DELTA; -/* Unfortunately, protoc-c doesn't export it's implementation of varint, so we +/* Unfortunately, protoc-c doesn't export its implementation of varint, so we * need to implement our own. */ static size_t varint(uint8_t buffer[static VARINT_UINT32_BYTES], uint32_t value) { @@ -98,6 +102,43 @@ static void format_protobuf(ProtobufCBuffer *buffer) { pthread_mutex_unlock(&metrics_lock); } +static char const *escape_label_value(char *buffer, size_t buffer_size, + char const *value) { + /* shortcut for values that don't need escaping. */ + if (strpbrk(value, "\n\"\\") == NULL) + return value; + + size_t value_len = strlen(value); + size_t buffer_len = 0; + + for (size_t i = 0; i < value_len; i++) { + switch (value[i]) { + case '\n': + case '"': + case '\\': + if ((buffer_size - buffer_len) < 3) { + break; + } + buffer[buffer_len] = '\\'; + buffer[buffer_len + 1] = (value[i] == '\n') ? 'n' : value[i]; + buffer_len += 2; + break; + + default: + if ((buffer_size - buffer_len) < 2) { + break; + } + buffer[buffer_len] = value[i]; + buffer_len++; + break; + } + } + + assert(buffer_len < buffer_size); + buffer[buffer_len] = 0; + return buffer; +} + /* format_labels formats a metric's labels in Prometheus-compatible format. This * format looks like this: * @@ -109,16 +150,22 @@ static char *format_labels(char *buffer, size_t buffer_size, assert(m->n_label >= 1); assert(m->n_label <= 3); -#define LABEL_BUFFER_SIZE (2 * DATA_MAX_NAME_LEN + 4) +#define LABEL_KEY_SIZE DATA_MAX_NAME_LEN +#define LABEL_VALUE_SIZE (2 * DATA_MAX_NAME_LEN - 1) +#define LABEL_BUFFER_SIZE (LABEL_KEY_SIZE + LABEL_VALUE_SIZE + 4) char *labels[3] = { (char[LABEL_BUFFER_SIZE]){0}, (char[LABEL_BUFFER_SIZE]){0}, (char[LABEL_BUFFER_SIZE]){0}, }; - for (size_t i = 0; i < m->n_label; i++) - ssnprintf(labels[i], LABEL_BUFFER_SIZE, "%s=\"%s\"", m->label[i]->name, - m->label[i]->value); + /* N.B.: the label *names* are hard-coded by this plugin and therefore we + * know that they are sane. */ + for (size_t i = 0; i < m->n_label; i++) { + char value[LABEL_VALUE_SIZE]; + snprintf(labels[i], LABEL_BUFFER_SIZE, "%s=\"%s\"", m->label[i]->name, + escape_label_value(value, sizeof(value), m->label[i]->value)); + } strjoin(buffer, buffer_size, labels, m->n_label, ","); return buffer; @@ -135,13 +182,13 @@ static void format_text(ProtobufCBuffer *buffer) { while (c_avl_iterator_next(iter, (void *)&unused_name, (void *)&fam) == 0) { char line[1024]; /* 4x DATA_MAX_NAME_LEN? */ - ssnprintf(line, sizeof(line), "# HELP %s %s\n", fam->name, fam->help); + snprintf(line, sizeof(line), "# HELP %s %s\n", fam->name, fam->help); buffer->append(buffer, strlen(line), (uint8_t *)line); - ssnprintf(line, sizeof(line), "# TYPE %s %s\n", fam->name, - (fam->type == IO__PROMETHEUS__CLIENT__METRIC_TYPE__GAUGE) - ? "gauge" - : "counter"); + snprintf(line, sizeof(line), "# TYPE %s %s\n", fam->name, + (fam->type == IO__PROMETHEUS__CLIENT__METRIC_TYPE__GAUGE) + ? "gauge" + : "counter"); buffer->append(buffer, strlen(line), (uint8_t *)line); for (size_t i = 0; i < fam->n_metric; i++) { @@ -151,17 +198,17 @@ static void format_text(ProtobufCBuffer *buffer) { char timestamp_ms[24] = ""; if (m->has_timestamp_ms) - ssnprintf(timestamp_ms, sizeof(timestamp_ms), " %" PRIi64, - m->timestamp_ms); + snprintf(timestamp_ms, sizeof(timestamp_ms), " %" PRIi64, + m->timestamp_ms); if (fam->type == IO__PROMETHEUS__CLIENT__METRIC_TYPE__GAUGE) - ssnprintf(line, sizeof(line), "%s{%s} " GAUGE_FORMAT "%s\n", fam->name, - format_labels(labels, sizeof(labels), m), m->gauge->value, - timestamp_ms); + snprintf(line, sizeof(line), "%s{%s} " GAUGE_FORMAT "%s\n", fam->name, + format_labels(labels, sizeof(labels), m), m->gauge->value, + timestamp_ms); else /* if (fam->type == IO__PROMETHEUS__CLIENT__METRIC_TYPE__COUNTER) */ - ssnprintf(line, sizeof(line), "%s{%s} %.0f%s\n", fam->name, - format_labels(labels, sizeof(labels), m), m->counter->value, - timestamp_ms); + snprintf(line, sizeof(line), "%s{%s} %.0f%s\n", fam->name, + format_labels(labels, sizeof(labels), m), m->counter->value, + timestamp_ms); buffer->append(buffer, strlen(line), (uint8_t *)line); } @@ -169,8 +216,8 @@ static void format_text(ProtobufCBuffer *buffer) { c_avl_iterator_destroy(iter); char server[1024]; - ssnprintf(server, sizeof(server), "\n# collectd/write_prometheus %s at %s\n", - PACKAGE_VERSION, hostname_g); + snprintf(server, sizeof(server), "\n# collectd/write_prometheus %s at %s\n", + PACKAGE_VERSION, hostname_g); buffer->append(buffer, strlen(server), (uint8_t *)server); pthread_mutex_unlock(&metrics_lock); @@ -210,8 +257,13 @@ static int http_handler(void *cls, struct MHD_Connection *connection, else format_text(buffer); +#if defined(MHD_VERSION) && MHD_VERSION >= 0x00090500 + struct MHD_Response *res = MHD_create_response_from_buffer( + simple.len, simple.data, MHD_RESPMEM_MUST_COPY); +#else 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, want_proto ? CONTENT_TYPE_PROTO : CONTENT_TYPE_TEXT); @@ -249,22 +301,22 @@ static void label_pair_destroy(Io__Prometheus__Client__LabelPair *msg) { sfree(msg); } -/* label_pair_create allocates and initializes a new label pair. */ -static Io__Prometheus__Client__LabelPair *label_pair_create(char const *name, - char const *value) { - Io__Prometheus__Client__LabelPair *msg = calloc(1, sizeof(*msg)); - if (msg == NULL) +/* label_pair_clone allocates and initializes a new label pair. */ +static Io__Prometheus__Client__LabelPair * +label_pair_clone(Io__Prometheus__Client__LabelPair const *orig) { + Io__Prometheus__Client__LabelPair *copy = calloc(1, sizeof(*copy)); + if (copy == NULL) return NULL; - io__prometheus__client__label_pair__init(msg); + io__prometheus__client__label_pair__init(copy); - msg->name = strdup(name); - msg->value = strdup(value); - if ((msg->name == NULL) || (msg->value == NULL)) { - label_pair_destroy(msg); + copy->name = strdup(orig->name); + copy->value = strdup(orig->value); + if ((copy->name == NULL) || (copy->value == NULL)) { + label_pair_destroy(copy); return NULL; } - return msg; + return copy; } /* metric_destroy frees the memory used by a metric. */ @@ -283,47 +335,6 @@ static void metric_destroy(Io__Prometheus__Client__Metric *msg) { sfree(msg); } -/* metric_add_labels adds the labels that identify this metric to m. - * The logic is copied from the "collectd_exporter". Essentially, the labels - * contain the hostname, the plugin instance and the type instance of a - * value_list_t. */ -static int metric_add_labels(Io__Prometheus__Client__Metric *m, - value_list_t const *vl) { - size_t n_label = 1; - if (strlen(vl->plugin_instance) != 0) - n_label++; - if (strlen(vl->type_instance) != 0) - n_label++; - - m->label = calloc(n_label, sizeof(*m->label)); - if (m->label == NULL) - return ENOMEM; - - if (strlen(vl->plugin_instance) != 0) { - m->label[m->n_label] = label_pair_create(vl->plugin, vl->plugin_instance); - m->n_label++; - } - - if (strlen(vl->type_instance) != 0) { - char const *name = "type"; - if (strlen(vl->plugin_instance) == 0) - name = vl->plugin; - - m->label[m->n_label] = label_pair_create(name, vl->type_instance); - m->n_label++; - } - - m->label[m->n_label] = label_pair_create("instance", vl->host); - m->n_label++; - - for (size_t i = 0; i < m->n_label; i++) { - if (m->label[i] == NULL) - return ENOMEM; - } - - return 0; -} - /* metric_cmp compares two metrics. It's prototype makes it easy to use with * qsort(3) and bsearch(3). */ static int metric_cmp(void const *a, void const *b) { @@ -338,35 +349,104 @@ static int metric_cmp(void const *a, void const *b) { return 1; /* Prometheus does not care about the order of labels. All labels in this - * plugin are created by metric_add_labels(), though, and therefore always + * plugin are created by METRIC_ADD_LABELS(), though, and therefore always * appear in the same order. We take advantage of this and simplify the check - * by making sure all labels are the same in each position. */ + * by making sure all labels are the same in each position. + * + * We also only need to check the label values, because the label names are + * the same for all metrics in a metric family. + * + * 3 labels: + * [0] $plugin="$plugin_instance" => $plugin is the same within a family + * [1] type="$type_instance" => "type" is a static string + * [2] instance="$host" => "instance" is a static string + * + * 2 labels, variant 1: + * [0] $plugin="$plugin_instance" => $plugin is the same within a family + * [1] instance="$host" => "instance" is a static string + * + * 2 labels, variant 2: + * [0] $plugin="$type_instance" => $plugin is the same within a family + * [1] instance="$host" => "instance" is a static string + * + * 1 label: + * [1] instance="$host" => "instance" is a static string + */ for (size_t i = 0; i < m_a->n_label; i++) { - int status = strcmp(m_a->label[i]->name, m_b->label[i]->name); + int status = strcmp(m_a->label[i]->value, m_b->label[i]->value); if (status != 0) return status; - status = strcmp(m_a->label[i]->value, m_b->label[i]->value); - if (status != 0) - return status; +#if COLLECT_DEBUG + assert(strcmp(m_a->label[i]->name, m_b->label[i]->name) == 0); +#endif } return 0; } -/* metric_create allocates and initializes a new metric. */ -static Io__Prometheus__Client__Metric *metric_create(value_list_t const *vl) { - Io__Prometheus__Client__Metric *msg = calloc(1, sizeof(*msg)); - if (msg == NULL) +#define METRIC_INIT \ + &(Io__Prometheus__Client__Metric) { \ + .label = \ + (Io__Prometheus__Client__LabelPair *[]){ \ + &(Io__Prometheus__Client__LabelPair){ \ + .name = NULL, \ + }, \ + &(Io__Prometheus__Client__LabelPair){ \ + .name = NULL, \ + }, \ + &(Io__Prometheus__Client__LabelPair){ \ + .name = NULL, \ + }, \ + }, \ + .n_label = 0, \ + } + +#define METRIC_ADD_LABELS(m, vl) \ + do { \ + if (strlen((vl)->plugin_instance) != 0) { \ + (m)->label[(m)->n_label]->name = (char *)(vl)->plugin; \ + (m)->label[(m)->n_label]->value = (char *)(vl)->plugin_instance; \ + (m)->n_label++; \ + } \ + \ + if (strlen((vl)->type_instance) != 0) { \ + (m)->label[(m)->n_label]->name = "type"; \ + if (strlen((vl)->plugin_instance) == 0) \ + (m)->label[(m)->n_label]->name = (char *)(vl)->plugin; \ + (m)->label[(m)->n_label]->value = (char *)(vl)->type_instance; \ + (m)->n_label++; \ + } \ + \ + (m)->label[(m)->n_label]->name = "instance"; \ + (m)->label[(m)->n_label]->value = (char *)(vl)->host; \ + (m)->n_label++; \ + } while (0) + +/* metric_clone allocates and initializes a new metric based on orig. */ +static Io__Prometheus__Client__Metric * +metric_clone(Io__Prometheus__Client__Metric const *orig) { + Io__Prometheus__Client__Metric *copy = calloc(1, sizeof(*copy)); + if (copy == NULL) return NULL; - io__prometheus__client__metric__init(msg); + io__prometheus__client__metric__init(copy); - if (metric_add_labels(msg, vl) != 0) { - metric_destroy(msg); + copy->n_label = orig->n_label; + copy->label = calloc(copy->n_label, sizeof(*copy->label)); + if (copy->label == NULL) { + sfree(copy); return NULL; } - return msg; + for (size_t i = 0; i < copy->n_label; i++) { + copy->label[i] = label_pair_clone(orig->label[i]); + if (copy->label[i] == NULL) { + metric_destroy(copy); + return NULL; + } + } + + return copy; } /* metric_update stores the new value and timestamp in m. */ @@ -452,9 +532,8 @@ static int metric_family_add_metric(Io__Prometheus__Client__MetricFamily *fam, static int metric_family_delete_metric(Io__Prometheus__Client__MetricFamily *fam, value_list_t const *vl) { - Io__Prometheus__Client__Metric *key = metric_create(vl); - if (key == NULL) - return ENOMEM; + Io__Prometheus__Client__Metric *key = METRIC_INIT; + METRIC_ADD_LABELS(key, vl); size_t i; for (i = 0; i < fam->n_metric; i++) { @@ -471,9 +550,14 @@ metric_family_delete_metric(Io__Prometheus__Client__MetricFamily *fam, ((fam->n_metric - 1) - i) * sizeof(fam->metric[i])); fam->n_metric--; + if (fam->n_metric == 0) { + sfree(fam->metric); + return 0; + } + Io__Prometheus__Client__Metric **tmp = realloc(fam->metric, fam->n_metric * sizeof(*fam->metric)); - if ((tmp != NULL) || (fam->n_metric == 0)) + if (tmp != NULL) fam->metric = tmp; return 0; @@ -484,9 +568,8 @@ metric_family_delete_metric(Io__Prometheus__Client__MetricFamily *fam, static Io__Prometheus__Client__Metric * metric_family_get_metric(Io__Prometheus__Client__MetricFamily *fam, value_list_t const *vl) { - Io__Prometheus__Client__Metric *key = metric_create(vl); - if (key == NULL) - return NULL; + Io__Prometheus__Client__Metric *key = METRIC_INIT; + METRIC_ADD_LABELS(key, vl); /* Metrics are sorted in metric_family_add_metric() so that we can do a binary * search here. */ @@ -494,18 +577,21 @@ metric_family_get_metric(Io__Prometheus__Client__MetricFamily *fam, &key, fam->metric, fam->n_metric, sizeof(*fam->metric), metric_cmp); if (m != NULL) { - metric_destroy(key); return *m; } + Io__Prometheus__Client__Metric *new_metric = metric_clone(key); + if (new_metric == NULL) + return NULL; + DEBUG("write_prometheus plugin: created new metric in family"); - int status = metric_family_add_metric(fam, key); + int status = metric_family_add_metric(fam, new_metric); if (status != 0) { - metric_destroy(key); + metric_destroy(new_metric); return NULL; } - return key; + return new_metric; } /* metric_family_update looks up the matching metric in a metric family, @@ -549,7 +635,7 @@ metric_family_create(char *name, data_set_t const *ds, value_list_t const *vl, msg->name = name; char help[1024]; - ssnprintf( + snprintf( help, sizeof(help), "write_prometheus plugin: '%s' Type: '%s', Dstype: '%s', Dsname: '%s'", vl->plugin, vl->type, DS_TYPE_TO_STRING(ds->ds[ds_index].type), @@ -602,8 +688,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."); @@ -617,6 +703,11 @@ metric_family_get(data_set_t const *ds, value_list_t const *vl, return fam; } + if (!allocate) { + sfree(name); + return NULL; + } + fam = metric_family_create(name, ds, vl, ds_index); if (fam == NULL) { ERROR("write_prometheus plugin: Allocating metric family failed."); @@ -640,6 +731,109 @@ metric_family_get(data_set_t const *ds, value_list_t const *vl, } /* }}} */ +static void prom_logger(__attribute__((unused)) void *arg, char const *fmt, + va_list ap) { + /* {{{ */ + char errbuf[1024]; + vsnprintf(errbuf, sizeof(errbuf), fmt, ap); + + ERROR("write_prometheus plugin: %s", errbuf); +} /* }}} prom_logger */ + +#if MHD_VERSION >= 0x00090000 +static int prom_open_socket(int addrfamily) { + /* {{{ */ + char service[NI_MAXSERV]; + snprintf(service, sizeof(service), "%hu", httpd_port); + + struct addrinfo *res; + int status = getaddrinfo(NULL, service, + &(struct addrinfo){ + .ai_flags = AI_PASSIVE | AI_ADDRCONFIG, + .ai_family = addrfamily, + .ai_socktype = SOCK_STREAM, + }, + &res); + if (status != 0) { + return -1; + } + + int fd = -1; + for (struct addrinfo *ai = res; ai != NULL; ai = ai->ai_next) { + fd = socket(ai->ai_family, ai->ai_socktype | SOCK_CLOEXEC, 0); + if (fd == -1) + continue; + + int tmp = 1; + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp)) != 0) { + WARNING("write_prometheus: setsockopt(SO_REUSEADDR) failed: %s", + STRERRNO); + close(fd); + fd = -1; + continue; + } + + if (bind(fd, ai->ai_addr, ai->ai_addrlen) != 0) { + close(fd); + fd = -1; + continue; + } + + if (listen(fd, /* backlog = */ 16) != 0) { + close(fd); + fd = -1; + continue; + } + + break; + } + + freeaddrinfo(res); + + return fd; +} /* }}} int prom_open_socket */ + +static struct MHD_Daemon *prom_start_daemon() { + /* {{{ */ + int fd = prom_open_socket(PF_INET6); + if (fd == -1) + fd = prom_open_socket(PF_INET); + if (fd == -1) { + ERROR("write_prometheus plugin: Opening a listening socket failed."); + return NULL; + } + + struct MHD_Daemon *d = MHD_start_daemon( + MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, httpd_port, + /* MHD_AcceptPolicyCallback = */ NULL, + /* MHD_AcceptPolicyCallback arg = */ NULL, http_handler, NULL, + MHD_OPTION_LISTEN_SOCKET, fd, MHD_OPTION_EXTERNAL_LOGGER, prom_logger, + NULL, MHD_OPTION_END); + if (d == NULL) { + ERROR("write_prometheus plugin: MHD_start_daemon() failed."); + close(fd); + return NULL; + } + + return d; +} /* }}} struct MHD_Daemon *prom_start_daemon */ +#else /* if MHD_VERSION < 0x00090000 */ +static struct MHD_Daemon *prom_start_daemon() { + /* {{{ */ + struct MHD_Daemon *d = MHD_start_daemon( + MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, httpd_port, + /* MHD_AcceptPolicyCallback = */ NULL, + /* MHD_AcceptPolicyCallback arg = */ NULL, http_handler, NULL, + MHD_OPTION_EXTERNAL_LOGGER, prom_logger, NULL, MHD_OPTION_END); + if (d == NULL) { + ERROR("write_prometheus plugin: MHD_start_daemon() failed."); + return NULL; + } + + return d; +} /* }}} struct MHD_Daemon *prom_start_daemon */ +#endif + /* * collectd callbacks */ @@ -673,15 +867,7 @@ static int prom_init() { } if (httpd == NULL) { - unsigned int flags = MHD_USE_THREAD_PER_CONNECTION; -#if MHD_VERSION >= 0x00093300 - flags |= MHD_USE_DUAL_STACK; -#endif - - httpd = MHD_start_daemon(flags, httpd_port, - /* MHD_AcceptPolicyCallback = */ NULL, - /* MHD_AcceptPolicyCallback arg = */ NULL, - http_handler, NULL, MHD_OPTION_END); + httpd = prom_start_daemon(); if (httpd == NULL) { ERROR("write_prometheus plugin: MHD_start_daemon() failed."); return -1; @@ -698,7 +884,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; @@ -724,7 +911,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; @@ -733,6 +921,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; } @@ -785,5 +974,3 @@ void module_register() { /* user data = */ NULL); plugin_register_shutdown("write_prometheus", prom_shutdown); } - -/* vim: set sw=2 sts=2 et fdm=marker : */