allow the same statsd metric added multiple times to the same synthetic chart
Costa Tsaousis (ktsaou) committed
Dec 9, 2017 at 23:55 UTC
ac6c14e6c6abebf75cadcdeb9b862ec9124b266b
1 file changed
+47
-2
src/statsd.c
+47
-2
@@ -954,6 +954,22 @@ static STATSD_APP_CHART_DIM_VALUE_TYPE string2valuetype(const char *type, size_t
954
return STATSD_APP_CHART_DIM_VALUE_TYPE_LAST;
955
}
956
957
+static const char *valuetype2string(STATSD_APP_CHART_DIM_VALUE_TYPE type) {
958
+ switch(type) {
959
+ case STATSD_APP_CHART_DIM_VALUE_TYPE_EVENTS: return "events";
960
+ case STATSD_APP_CHART_DIM_VALUE_TYPE_LAST: return "last";
961
+ case STATSD_APP_CHART_DIM_VALUE_TYPE_MIN: return "min";
962
+ case STATSD_APP_CHART_DIM_VALUE_TYPE_MAX: return "max";
963
+ case STATSD_APP_CHART_DIM_VALUE_TYPE_SUM: return "sum";
964
+ case STATSD_APP_CHART_DIM_VALUE_TYPE_AVERAGE: return "average";
965
+ case STATSD_APP_CHART_DIM_VALUE_TYPE_MEDIAN: return "median";
966
+ case STATSD_APP_CHART_DIM_VALUE_TYPE_STDDEV: return "stddev";
967
+ case STATSD_APP_CHART_DIM_VALUE_TYPE_PERCENTILE: return "percentile";
968
+ }
969
+
970
+ return "unknown";
971
+}
972
+
973
static STATSD_APP_CHART_DIM *add_dimension_to_app_chart(
974
STATSD_APP *app
975
, STATSD_APP_CHART *chart
@@ -963,9 +979,39 @@ static STATSD_APP_CHART_DIM *add_dimension_to_app_chart(
979
, collected_number divisor
980
, STATSD_APP_CHART_DIM_VALUE_TYPE value_type
981
) {
982
+ size_t len = strlen(metric_name) + 100;
983
+ char metric[ len + 1 ];
984
+ strcpy(metric, metric_name);
985
+ uint32_t hash = simple_hash(metric);
986
+
987
+ // check if the metric already exists in this chart
988
+ // if it is found, append the value type
989
+ // if it is still found, append a counter
990
+
991
+ STATSD_APP_CHART_DIM *tdim;
992
+ size_t count = 0, found = 1;
993
+ while(found) {
994
+ found = 0;
995
+ for (tdim = chart->dimensions; tdim && tdim->next; tdim = tdim->next) {
996
+ if (hash == tdim->metric_hash && !strcmp(tdim->metric, metric)) {
997
+ count++;
998
+
999
+ // the same metric!
1000
+ if(count > 1)
1001
+ snprintfz(metric, len, "%s_%s%zu", metric_name, valuetype2string(value_type), count);
1002
+ else
1003
+ snprintfz(metric, len, "%s_%s", metric_name, valuetype2string(value_type));
1004
+
1005
+ hash = simple_hash(metric);
1006
+ found = 1;
1007
+ break;
1008
+ }
1009
+ }
1010
+ }
1011
+
1012
STATSD_APP_CHART_DIM *dim = callocz(sizeof(STATSD_APP_CHART_DIM), 1);
1013
968
- dim->metric = strdupz(metric_name);
1014
+ dim->metric = strdupz(metric);
1015
dim->metric_hash = simple_hash(dim->metric);
1016
1017
dim->name = strdupz((dim_name)?dim_name:"");
@@ -980,7 +1026,6 @@ static STATSD_APP_CHART_DIM *add_dimension_to_app_chart(
1026
dim->divisor = 1;
1027
1028
// append it to the list of dimension
983
- STATSD_APP_CHART_DIM *tdim;
1029
for(tdim = chart->dimensions; tdim && tdim->next ; tdim = tdim->next) ;
1030
if(!tdim) {
1031
dim->next = chart->dimensions;