dynamically change dimension name of statsd synthetic charts based on added dimensions
Costa Tsaousis (ktsaou) committed
Dec 10, 2017 at 00:24 UTC
6d8432fae2fc360a5e4430ad6206c5b7f7f81822
1 file changed
+47
-32
src/statsd.c
+47
-32
@@ -979,39 +979,9 @@ 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
-
982
STATSD_APP_CHART_DIM *dim = callocz(sizeof(STATSD_APP_CHART_DIM), 1);
983
1014
- dim->metric = strdupz(metric);
984
+ dim->metric = strdupz(metric_name);
985
dim->metric_hash = simple_hash(dim->metric);
986
987
dim->name = strdupz((dim_name)?dim_name:"");
@@ -1026,6 +996,7 @@ static STATSD_APP_CHART_DIM *add_dimension_to_app_chart(
996
dim->divisor = 1;
997
998
// append it to the list of dimension
999
+ STATSD_APP_CHART_DIM *tdim;
1000
for(tdim = chart->dimensions; tdim && tdim->next ; tdim = tdim->next) ;
1001
if(!tdim) {
1002
dim->next = chart->dimensions;
@@ -1826,6 +1797,50 @@ static inline void check_if_metric_is_for_app(STATSD_INDEX *index, STATSD_METRIC
1797
}
1798
}
1799
1800
+static inline RRDDIM *statsd_add_dim_to_app_chart(STATSD_APP *app, STATSD_APP_CHART *chart, STATSD_APP_CHART_DIM *dim) {
1801
+ (void)app;
1802
+
1803
+ // allow the same statsd metric to be added multiple times to the same chart
1804
+
1805
+ STATSD_APP_CHART_DIM *tdim;
1806
+ size_t count_same_metric = 0, count_same_metric_value_type = 0;
1807
+ size_t pos_same_metric_value_type = 0;
1808
+
1809
+ for (tdim = chart->dimensions; tdim && tdim->next; tdim = tdim->next) {
1810
+ if (dim->metric_hash == tdim->metric_hash && !strcmp(dim->metric, tdim->metric)) {
1811
+ count_same_metric++;
1812
+
1813
+ if(dim->value_type == tdim->value_type) {
1814
+ count_same_metric_value_type++;
1815
+ if (tdim == dim)
1816
+ pos_same_metric_value_type = count_same_metric_value_type;
1817
+ }
1818
+ }
1819
+ }
1820
+
1821
+ if(count_same_metric > 1) {
1822
+ // the same metric is found multiple times
1823
+
1824
+ size_t len = strlen(dim->metric) + 100;
1825
+ char metric[ len + 1 ];
1826
+
1827
+ if(count_same_metric_value_type > 1) {
1828
+ // the same metric, with the same value type, is added multiple times
1829
+ snprintfz(metric, len, "%s_%s%zu", dim->metric, valuetype2string(dim->value_type), pos_same_metric_value_type);
1830
+ }
1831
+ else {
1832
+ // the same metric, with different value type is added
1833
+ snprintfz(metric, len, "%s_%s", dim->metric, valuetype2string(dim->value_type));
1834
+ }
1835
+
1836
+ dim->rd = rrddim_add(chart->st, metric, dim->name, dim->multiplier, dim->divisor, dim->algorithm);
1837
+ return dim->rd;
1838
+ }
1839
+
1840
+ dim->rd = rrddim_add(chart->st, dim->metric, dim->name, dim->multiplier, dim->divisor, dim->algorithm);
1841
+ return dim->rd;
1842
+}
1843
+
1844
static inline void statsd_update_app_chart(STATSD_APP *app, STATSD_APP_CHART *chart) {
1845
debug(D_STATSD, "updating chart '%s' for app '%s'", chart->id, app->name);
1846
@@ -1857,7 +1872,7 @@ static inline void statsd_update_app_chart(STATSD_APP *app, STATSD_APP_CHART *ch
1872
for(dim = chart->dimensions; dim ;dim = dim->next) {
1873
if(likely(!dim->metric_pattern)) {
1874
if (unlikely(!dim->rd))
1860
- dim->rd = rrddim_add(chart->st, dim->metric, dim->name, dim->multiplier, dim->divisor, dim->algorithm);
1875
+ statsd_add_dim_to_app_chart(app, chart, dim);
1876
1877
if (unlikely(dim->value_ptr)) {
1878
debug(D_STATSD, "updating dimension '%s' (%s) of chart '%s' (%s) for app '%s' with value " COLLECTED_NUMBER_FORMAT, dim->name, dim->rd->id, chart->id, chart->st->id, app->name, *dim->value_ptr);