added dictionary to rename dimensions of synthetic charts
Costa Tsaousis (ktsaou) committed
Dec 9, 2017 at 21:41 UTC
84b642c5813174587a02a8e1e7ead527474a5902
1 file changed
+74
-20
src/statsd.c
+74
-20
@@ -207,6 +207,7 @@ typedef struct statsd_app {
207
SIMPLE_PATTERN *metrics;
208
STATS_METRIC_OPTIONS default_options;
209
RRD_MEMORY_MODE rrd_memory_mode;
210
+ DICTIONARY *dict;
211
long rrd_history_entries;
212
213
const char *source;
@@ -967,7 +968,7 @@ static STATSD_APP_CHART_DIM *add_dimension_to_app_chart(
968
dim->metric = strdupz(metric_name);
969
dim->metric_hash = simple_hash(dim->metric);
970
970
- dim->name = strdupz((dim_name && *dim_name)?dim_name:metric_name);
971
+ dim->name = strdupz((dim_name)?dim_name:"");
972
dim->multiplier = multiplier;
973
dim->divisor = divisor;
974
dim->value_type = value_type;
@@ -1012,6 +1013,7 @@ int statsd_readfile(const char *path, const char *filename) {
1013
1014
STATSD_APP *app = NULL;
1015
STATSD_APP_CHART *chart = NULL;
1016
+ DICTIONARY *dict = NULL;
1017
1018
size_t line = 0;
1019
char *s;
@@ -1042,22 +1044,33 @@ int statsd_readfile(const char *path, const char *filename) {
1044
app->next = statsd.apps;
1045
statsd.apps = app;
1046
chart = NULL;
1047
+ dict = NULL;
1048
}
1049
else if(app) {
1047
- // a new chart
1048
- chart = callocz(sizeof(STATSD_APP_CHART), 1);
1049
- netdata_fix_chart_id(s);
1050
- chart->id = strdupz(s);
1051
- chart->name = strdupz(s);
1052
- chart->title = strdupz("Statsd chart");
1053
- chart->context = strdupz(s);
1054
- chart->family = strdupz("overview");
1055
- chart->units = strdupz("value");
1056
- chart->priority = STATSD_CHART_PRIORITY;
1057
- chart->chart_type = RRDSET_TYPE_LINE;
1058
-
1059
- chart->next = app->charts;
1060
- app->charts = chart;
1050
+ if(!strcmp(s, "dictionary")) {
1051
+ if(!app->dict)
1052
+ app->dict = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED);
1053
+
1054
+ dict = app->dict;
1055
+ }
1056
+ else {
1057
+ dict = NULL;
1058
+
1059
+ // a new chart
1060
+ chart = callocz(sizeof(STATSD_APP_CHART), 1);
1061
+ netdata_fix_chart_id(s);
1062
+ chart->id = strdupz(s);
1063
+ chart->name = strdupz(s);
1064
+ chart->title = strdupz("Statsd chart");
1065
+ chart->context = strdupz(s);
1066
+ chart->family = strdupz("overview");
1067
+ chart->units = strdupz("value");
1068
+ chart->priority = STATSD_CHART_PRIORITY;
1069
+ chart->chart_type = RRDSET_TYPE_LINE;
1070
+
1071
+ chart->next = app->charts;
1072
+ app->charts = chart;
1073
+ }
1074
}
1075
else
1076
error("STATSD: ignoring line %zu ('%s') of file '%s/%s', [app] is not defined.", line, s, path, filename);
@@ -1091,7 +1104,14 @@ int statsd_readfile(const char *path, const char *filename) {
1104
continue;
1105
}
1106
1094
- if(!chart) {
1107
+ if(unlikely(dict)) {
1108
+ // parse [dictionary] members
1109
+
1110
+ dictionary_set(dict, name, value, strlen(value) + 1);
1111
+ }
1112
+ else if(!chart) {
1113
+ // parse [app] members
1114
+
1115
if(!strcmp(name, "name")) {
1116
freez((void *)app->name);
1117
netdata_fix_chart_name(value);
@@ -1125,6 +1145,8 @@ int statsd_readfile(const char *path, const char *filename) {
1145
}
1146
}
1147
else {
1148
+ // parse [chart] members
1149
+
1150
if(!strcmp(name, "name")) {
1151
freez((void *)chart->name);
1152
netdata_fix_chart_id(value);
@@ -1172,6 +1194,13 @@ int statsd_readfile(const char *path, const char *filename) {
1194
char *multipler = words[i++];
1195
char *divisor = words[i++];
1196
1197
+ if(!pattern && app->dict && (!dim_name || !*dim_name)) {
1198
+ dim_name = dictionary_get(app->dict, metric_name);
1199
+ }
1200
+
1201
+ if(!pattern && (!dim_name || !*dim_name))
1202
+ dim_name = metric_name;
1203
+
1204
STATSD_APP_CHART_DIM *dim = add_dimension_to_app_chart(
1205
app
1206
, chart
@@ -1693,14 +1722,39 @@ static inline void check_if_metric_is_for_app(STATSD_INDEX *index, STATSD_METRIC
1722
STATSD_APP_CHART_DIM *dim;
1723
for(dim = chart->dimensions; dim ; dim = dim->next) {
1724
if(unlikely(dim->metric_pattern)) {
1696
- size_t len = strlen(m->name) + 1;
1697
- char name[len];
1698
- if(simple_pattern_matches_extract(dim->metric_pattern, m->name, name, len)) {
1725
+ size_t wildcarded_len = strlen(m->name) + 1;
1726
+ char wildcarded[wildcarded_len];
1727
+ if(simple_pattern_matches_extract(dim->metric_pattern, m->name, wildcarded, wildcarded_len)) {
1728
+
1729
+ char *final_name = NULL;
1730
+
1731
+ if(app->dict) {
1732
+ // use the name of the metric
1733
+ final_name = dictionary_get(app->dict, m->name);
1734
+
1735
+ if(unlikely(!final_name)) {
1736
+ // use the name given at the dynamic dimension + the wildcarded string
1737
+ size_t key_len = strlen(dim->name) + wildcarded_len + 10;
1738
+ char key[key_len + 1];
1739
+
1740
+ snprintfz(key, key_len, "%s%s", dim->name, wildcarded);
1741
+ final_name = dictionary_get(app->dict, key);
1742
+ }
1743
+
1744
+ if(unlikely(!final_name && *wildcarded)) {
1745
+ // use the name of the wildcarded string
1746
+ final_name = dictionary_get(app->dict, m->name);
1747
+ }
1748
+ }
1749
+
1750
+ if(unlikely(!final_name))
1751
+ final_name = wildcarded;
1752
+
1753
add_dimension_to_app_chart(
1754
app
1755
, chart
1756
, m->name
1703
- , name
1757
+ , final_name
1758
, dim->multiplier
1759
, dim->divisor
1760
, dim->value_type