Code

src/utils_format_graphite.[ch]: Implement the GRAPHITE_PRESERVE_SEPARATOR flag.
authorFlorian Forster <octo@collectd.org>
Sat, 26 Nov 2016 17:46:36 +0000 (18:46 +0100)
committerFlorian Forster <octo@collectd.org>
Sat, 26 Nov 2016 17:46:36 +0000 (18:46 +0100)
src/utils_format_graphite.c
src/utils_format_graphite.h
src/utils_format_graphite_test.c

index 69c619f425702ffc6c329738673c6887bdac875a..85f5917c8ea807f849476b29f91f139824f4abdb 100644 (file)
@@ -83,7 +83,7 @@ static int gr_format_values (char *ret, size_t ret_len,
 }
 
 static void gr_copy_escape_part (char *dst, const char *src, size_t dst_len,
-    char escape_char)
+    char escape_char, _Bool preserve_separator)
 {
     memset (dst, 0, dst_len);
 
@@ -98,7 +98,7 @@ static void gr_copy_escape_part (char *dst, const char *src, size_t dst_len,
             break;
         }
 
-        if ((src[i] == '.')
+        if ((!preserve_separator && (src[i] == '.'))
                 || isspace ((int) src[i])
                 || iscntrl ((int) src[i]))
             dst[i] = escape_char;
@@ -130,16 +130,18 @@ static int gr_format_name (char *ret, int ret_len,
     if (postfix == NULL)
         postfix = "";
 
+    _Bool preserve_separator = (flags & GRAPHITE_PRESERVE_SEPARATOR) ? 1 : 0;
+
     gr_copy_escape_part (n_host, vl->host,
-            sizeof (n_host), escape_char);
+            sizeof (n_host), escape_char, preserve_separator);
     gr_copy_escape_part (n_plugin, vl->plugin,
-            sizeof (n_plugin), escape_char);
+            sizeof (n_plugin), escape_char, preserve_separator);
     gr_copy_escape_part (n_plugin_instance, vl->plugin_instance,
-            sizeof (n_plugin_instance), escape_char);
+            sizeof (n_plugin_instance), escape_char, preserve_separator);
     gr_copy_escape_part (n_type, vl->type,
-            sizeof (n_type), escape_char);
+            sizeof (n_type), escape_char, preserve_separator);
     gr_copy_escape_part (n_type_instance, vl->type_instance,
-            sizeof (n_type_instance), escape_char);
+            sizeof (n_type_instance), escape_char, preserve_separator);
 
     if (n_plugin_instance[0] != '\0')
         ssnprintf (tmp_plugin, sizeof (tmp_plugin), "%s%c%s",
index 5165f9e65f113fb5c8c64703afffcd6f8940eec5..ebc50802475f20de6d581ae219676f5422ebbaaa 100644 (file)
@@ -30,6 +30,7 @@
 #define GRAPHITE_SEPARATE_INSTANCES 0x02
 #define GRAPHITE_ALWAYS_APPEND_DS   0x04
 #define GRAPHITE_DROP_DUPE_FIELDS   0x08
+#define GRAPHITE_PRESERVE_SEPARATOR 0x10
 
 int format_graphite (char *buffer,
     size_t buffer_size, const data_set_t *ds,
index 30cdd7a420fc7e1d4d4377d0745e58b7737194f0..f349db1f12362a09440c11c431203159bf198f4c 100644 (file)
@@ -96,6 +96,19 @@ DEF_TEST(metric_name)
       .flags = GRAPHITE_ALWAYS_APPEND_DS,
       .want_name = "example@com.test-foo.single-bar.value",
     },
+    /* flag GRAPHITE_PRESERVE_SEPARATOR */
+    {
+      .plugin_instance = "f.o.o",
+      .type_instance = "b.a.r",
+      .flags = 0,
+      .want_name = "example@com.test-f@o@o.single-b@a@r",
+    },
+    {
+      .plugin_instance = "f.o.o",
+      .type_instance = "b.a.r",
+      .flags = GRAPHITE_PRESERVE_SEPARATOR,
+      .want_name = "example.com.test-f.o.o.single-b.a.r",
+    },
     /* prefix and suffix */
     {
       .prefix = "foo.",