#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
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:
*
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++)
+ /* 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];
ssnprintf(labels[i], LABEL_BUFFER_SIZE, "%s=\"%s\"", m->label[i]->name,
- m->label[i]->value);
+ escape_label_value(value, sizeof(value), m->label[i]->value));
+ }
strjoin(buffer, buffer_size, labels, m->n_label, ",");
return buffer;
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);
((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;
if (m == NULL)
return -1;
- return metric_update(m, vl->values[ds_index], ds->ds[ds_index].type, vl->time,
- vl->interval);
+ return metric_update(m, vl->values[ds_index], ds->ds[ds_index].type,
+ vl->time, vl->interval);
}
/* metric_family_destroy frees the memory used by a metric family. */
/* 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.");
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.");
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;
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;
ERROR("write_prometheus plugin: Deleting a metric in family \"%s\" "
"failed with status %d",
fam->name, status);
+
continue;
}
/* user data = */ NULL);
plugin_register_shutdown("write_prometheus", prom_shutdown);
}
-
-/* vim: set sw=2 sts=2 et fdm=marker : */