summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 78e4df3)
raw | patch | inline | side by side (parent: 78e4df3)
author | Florian Forster <octo@collectd.org> | |
Tue, 15 Nov 2016 08:34:57 +0000 (09:34 +0100) | ||
committer | Florian Forster <octo@collectd.org> | |
Tue, 15 Nov 2016 08:34:57 +0000 (09:34 +0100) |
Fixes: #2035
src/write_prometheus.c | patch | blob | history |
diff --git a/src/write_prometheus.c b/src/write_prometheus.c
index fe7a5b5996edab97ee12b34d08558d6a26c522ed..991415ef08713ddb7939e63fd7d5d196a3d35981 100644 (file)
--- a/src/write_prometheus.c
+++ b/src/write_prometheus.c
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;