write_graphite plugin: Set a (hopefully unique) callback name.
[collectd.git] / src / write_graphite.c
index b7924ba..3ff8e04 100644 (file)
@@ -449,7 +449,6 @@ static int wg_send_message (const char* key, const char* value,
         return (-1);
     }
 
-
     pthread_mutex_lock (&cb->send_lock);
 
     if (cb->sock_fd < 0)
@@ -472,6 +471,8 @@ static int wg_send_message (const char* key, const char* value,
             return (status);
         }
     }
+
+    /* Assert that we have enough space for this message. */
     assert (message_len < cb->send_buf_free);
 
     /* `message_len + 1' because `message_len' does not include the
@@ -481,14 +482,13 @@ static int wg_send_message (const char* key, const char* value,
     cb->send_buf_fill += message_len;
     cb->send_buf_free -= message_len;
 
-    DEBUG ("write_graphite plugin: <%s:%s> buf %zu/%zu (%g%%) \"%s\"",
+    DEBUG ("write_graphite plugin: [%s]:%s buf %zu/%zu (%.1f %%) \"%s\"",
             cb->node,
             cb->service,
             cb->send_buf_fill, sizeof (cb->send_buf),
             100.0 * ((double) cb->send_buf_fill) / ((double) sizeof (cb->send_buf)),
             message);
 
-    /* Check if we have enough space for this message. */
     pthread_mutex_unlock (&cb->send_lock);
 
     return (0);
@@ -509,45 +509,15 @@ static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
         return -1;
     }
 
-    if (ds->ds_num > 1)
-    {
-        for (i = 0; i < ds->ds_num; i++)
-        {
-            /* Copy the identifier to `key' and escape it. */
-            status = wg_format_name (key, sizeof (key), vl, cb, ds->ds[i].name);
-            if (status != 0)
-            {
-                ERROR ("write_graphite plugin: error with format_name");
-                return (status);
-            }
-
-            escape_string (key, sizeof (key));
-            /* Convert the values to an ASCII representation and put that
-             * into `values'. */
-            status = wg_format_values (values, sizeof (values), i, ds, vl,
-                    cb->store_rates);
-            if (status != 0)
-            {
-                ERROR ("write_graphite plugin: error with "
-                        "wg_format_values");
-                return (status);
-            }
-
-            /* Send the message to graphite */
-            status = wg_send_message (key, values, vl->time, cb);
-            if (status != 0)
-            {
-                ERROR ("write_graphite plugin: error with "
-                        "wg_send_message");
-                return (status);
-            }
-        }
-    }
-    else
+    for (i = 0; i < ds->ds_num; i++)
     {
+        const char *ds_name = NULL;
+
+        if (cb->always_append_ds || (ds->ds_num > 1))
+            ds_name = ds->ds[i].name;
+
         /* Copy the identifier to `key' and escape it. */
-        status = wg_format_name (key, sizeof (key), vl, cb,
-            cb->always_append_ds ? ds->ds[0].name : NULL);
+        status = wg_format_name (key, sizeof (key), vl, cb, ds_name);
         if (status != 0)
         {
             ERROR ("write_graphite plugin: error with format_name");
@@ -557,7 +527,7 @@ static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
         escape_string (key, sizeof (key));
         /* Convert the values to an ASCII representation and put that into
          * `values'. */
-        status = wg_format_values (values, sizeof (values), 0, ds, vl,
+        status = wg_format_values (values, sizeof (values), i, ds, vl,
                     cb->store_rates);
         if (status != 0)
         {
@@ -630,6 +600,7 @@ static int wg_config_carbon (oconfig_item_t *ci)
 {
     struct wg_callback *cb;
     user_data_t user_data;
+    char callback_name[DATA_MAX_NAME_LEN];
     int i;
 
     cb = malloc (sizeof (*cb));
@@ -673,17 +644,17 @@ static int wg_config_carbon (oconfig_item_t *ci)
         }
     }
 
-    DEBUG ("write_graphite: Registering write callback to carbon agent %s:%s",
-            cb->node ? cb->node : WG_DEFAULT_NODE,
-            cb->service ? cb->service : WG_DEFAULT_SERVICE);
+    ssnprintf (callback_name, sizeof (callback_name), "write_graphite/%s/%s",
+            cb->node != NULL ? cb->node : WG_DEFAULT_NODE,
+            cb->service != NULL ? cb->service : WG_DEFAULT_SERVICE);
 
     memset (&user_data, 0, sizeof (user_data));
     user_data.data = cb;
-    user_data.free_func = NULL;
-    plugin_register_flush ("write_graphite", wg_flush, &user_data);
-
     user_data.free_func = wg_callback_free;
-    plugin_register_write ("write_graphite", wg_write, &user_data);
+    plugin_register_write (callback_name, wg_write, &user_data);
+
+    user_data.free_func = NULL;
+    plugin_register_flush (callback_name, wg_flush, &user_data);
 
     return (0);
 }