Code

statsd plugin: Dont'a call common.c:parse_value().
[collectd.git] / src / statsd.c
index 46575845e31be5b911c7e191d36ec7061cb0f02c..a83cc9a2ece6907c9f2009286f04e3e5259b920e 100644 (file)
@@ -80,10 +80,9 @@ static double *conf_timer_percentile = NULL;
 static size_t  conf_timer_percentile_num = 0;
 
 /* Must hold metrics_lock when calling this function. */
-static statsd_metric_t *statsd_metric_lookup_unsafe (char const *name,
+static statsd_metric_t *statsd_metric_lookup_unsafe (char const *name, /* {{{ */
     metric_type_t type)
 {
-  char const *prefix;
   char key[DATA_MAX_NAME_LEN + 2];
   char *key_copy;
   statsd_metric_t *metric;
@@ -91,14 +90,15 @@ static statsd_metric_t *statsd_metric_lookup_unsafe (char const *name,
 
   switch (type)
   {
-    case STATSD_COUNTER: prefix = "c"; break;
-    case STATSD_TIMER:   prefix = "t"; break;
-    case STATSD_GAUGE:   prefix = "g"; break;
-    case STATSD_SET:     prefix = "s"; break;
+    case STATSD_COUNTER: key[0] = 'c'; break;
+    case STATSD_TIMER:   key[0] = 't'; break;
+    case STATSD_GAUGE:   key[0] = 'g'; break;
+    case STATSD_SET:     key[0] = 's'; break;
     default: return (NULL);
   }
 
-  ssnprintf (key, sizeof (key), "%s:%s", prefix, name);
+  key[1] = ':';
+  sstrncpy (&key[2], name, sizeof (key) - 2);
 
   status = c_avl_get (metrics_tree, key, (void *) &metric);
   if (status == 0)
@@ -180,11 +180,21 @@ static int statsd_metric_add (char const *name, int64_t delta, /* {{{ */
   return (0);
 } /* }}} int statsd_metric_add */
 
+static int statsd_parse_value (char const *str, value_t *ret_value) /* {{{ */
+{
+  char *endptr = NULL;
+
+  ret_value->derive = (derive_t) strtoll (str, &endptr, /* base = */ 0);
+  if ((str == endptr) || ((endptr != NULL) && (*endptr != 0)))
+    return (-1);
+
+  return (0);
+} /* }}} int statsd_parse_value */
+
 static int statsd_handle_counter (char const *name, /* {{{ */
     char const *value_str,
     char const *extra)
 {
-  char key[DATA_MAX_NAME_LEN + 2];
   value_t value;
   value_t scale;
   int status;
@@ -204,16 +214,14 @@ static int statsd_handle_counter (char const *name, /* {{{ */
   }
 
   value.derive = 1;
-  status = parse_value (value_str, &value, DS_TYPE_DERIVE);
+  status = statsd_parse_value (value_str, &value);
   if (status != 0)
     return (status);
 
   if (value.derive < 1)
     return (-1);
 
-  ssnprintf (key, sizeof (key), "c:%s", name);
-
-  return (statsd_metric_add (key,
+  return (statsd_metric_add (name,
         (int64_t) (((gauge_t) value.derive) / scale.gauge),
         STATSD_COUNTER));
 } /* }}} int statsd_handle_counter */
@@ -221,21 +229,18 @@ static int statsd_handle_counter (char const *name, /* {{{ */
 static int statsd_handle_gauge (char const *name, /* {{{ */
     char const *value_str)
 {
-  char key[DATA_MAX_NAME_LEN + 2];
   value_t value;
   int status;
 
   value.derive = 0;
-  status = parse_value (value_str, &value, DS_TYPE_DERIVE);
+  status = statsd_parse_value (value_str, &value);
   if (status != 0)
     return (status);
 
-  ssnprintf (key, sizeof (key), "g:%s", name);
-
   if ((value_str[0] == '+') || (value_str[0] == '-'))
-    return (statsd_metric_add (key, (int64_t) value.derive, STATSD_GAUGE));
+    return (statsd_metric_add (name, (int64_t) value.derive, STATSD_GAUGE));
   else
-    return (statsd_metric_set (key, (int64_t) value.derive, STATSD_GAUGE));
+    return (statsd_metric_set (name, (int64_t) value.derive, STATSD_GAUGE));
 } /* }}} int statsd_handle_gauge */
 
 static int statsd_handle_timer (char const *name, /* {{{ */
@@ -247,7 +252,7 @@ static int statsd_handle_timer (char const *name, /* {{{ */
   int status;
 
   value_ms.derive = 0;
-  status = parse_value (value_str, &value_ms, DS_TYPE_DERIVE);
+  status = statsd_parse_value (value_str, &value_ms);
   if (status != 0)
     return (status);