@cryptotaxi247 / netdata-1 / commits / b5dd1e33c

prometheus: allow prefix to be given on the URL

Costa Tsaousis (ktsaou) committed Jul 10, 2017 at 23:36 UTC b5dd1e33c3a36d9ca3790c9709b064c9ba9dfdd3
3 files changed +19 -15
src/backend_prometheus.c
+11 -11
@@ -70,7 +70,7 @@ static inline size_t prometheus_label_copy(char *d, const char *s, size_t usable
70 #define PROMETHEUS_ELEMENT_MAX 256
71 #define PROMETHEUS_LABELS_MAX 1024
72
73 -void rrd_stats_api_v1_charts_allmetrics_prometheus(RRDHOST *host, BUFFER *wb, uint32_t options, time_t after, time_t before, int allhosts, int help, int types, int names) {
73 +static void rrd_stats_api_v1_charts_allmetrics_prometheus(RRDHOST *host, BUFFER *wb, const char *prefix, uint32_t options, time_t after, time_t before, int allhosts, int help, int types, int names) {
74 rrdhost_rdlock(host);
75
76 char hostname[PROMETHEUS_ELEMENT_MAX + 1];
@@ -125,7 +125,7 @@ void rrd_stats_api_v1_charts_allmetrics_prometheus(RRDHOST *host, BUFFER *wb, ui
125
126 if (unlikely(help))
127 buffer_sprintf(wb, "# COMMENT HELP %s_%s_%s netdata chart \"%s\", context \"%s\", family \"%s\", dimension \"%s\", value * " COLLECTED_NUMBER_FORMAT " / " COLLECTED_NUMBER_FORMAT " %s %s (%s)\n",
128 - backend_prefix, context, dimension,
128 + prefix, context, dimension,
129 (names && st->name) ? st->name : st->id, st->context,
130 st->family,
131 (names && rd->name) ? rd->name : rd->id,
@@ -134,10 +134,10 @@ void rrd_stats_api_v1_charts_allmetrics_prometheus(RRDHOST *host, BUFFER *wb, ui
134 );
135
136 if (unlikely(types))
137 - buffer_sprintf(wb, "# COMMENT TYPE %s_%s_%s %s\n", backend_prefix, context, dimension, t);
137 + buffer_sprintf(wb, "# COMMENT TYPE %s_%s_%s %s\n", prefix, context, dimension, t);
138
139 buffer_sprintf(wb, "%s_%s_%s{chart=\"%s\",family=\"%s\"%s} " COLLECTED_NUMBER_FORMAT " %llu\n",
140 - backend_prefix, context, dimension,
140 + prefix, context, dimension,
141 chart, family, labels,
142 rd->last_collected_value, timeval_msec(&rd->last_collected_time)
143 );
@@ -152,7 +152,7 @@ void rrd_stats_api_v1_charts_allmetrics_prometheus(RRDHOST *host, BUFFER *wb, ui
152
153 if (unlikely(help))
154 buffer_sprintf(wb, "# COMMENT HELP %s_%s netdata chart \"%s\", context \"%s\", family \"%s\", dimension \"%s\", value gives %s (gauge)\n",
155 - backend_prefix, context,
155 + prefix, context,
156 (names && st->name) ? st->name : st->id, st->context,
157 st->family,
158 (names && rd->name) ? rd->name : rd->id,
@@ -160,10 +160,10 @@ void rrd_stats_api_v1_charts_allmetrics_prometheus(RRDHOST *host, BUFFER *wb, ui
160 );
161
162 if (unlikely(types))
163 - buffer_sprintf(wb, "# COMMENT TYPE %s_%s gauge\n", backend_prefix, context);
163 + buffer_sprintf(wb, "# COMMENT TYPE %s_%s gauge\n", prefix, context);
164
165 buffer_sprintf(wb, "%s_%s{chart=\"%s\",family=\"%s\",dimension=\"%s\"%s} " CALCULATED_NUMBER_FORMAT " %llu\n",
166 - backend_prefix, context,
166 + prefix, context,
167 chart, family, dimension, labels,
168 value, timeval_msec(&rd->last_collected_time)
169 );
@@ -222,20 +222,20 @@ static inline time_t prometheus_preparation(RRDHOST *host, BUFFER *wb, uint32_t
222 return after;
223 }
224
225 -void rrd_stats_api_v1_charts_allmetrics_prometheus_single_host(RRDHOST *host, BUFFER *wb, const char *server, uint32_t options, int help, int types, int names) {
225 +void rrd_stats_api_v1_charts_allmetrics_prometheus_single_host(RRDHOST *host, BUFFER *wb, const char *server, const char *prefix, uint32_t options, int help, int types, int names) {
226 time_t before = now_realtime_sec();
227 time_t after = prometheus_preparation(host, wb, options, server, before, help);
228
229 - rrd_stats_api_v1_charts_allmetrics_prometheus(host, wb, options, after, before, 0, help, types, names);
229 + rrd_stats_api_v1_charts_allmetrics_prometheus(host, wb, prefix, options, after, before, 0, help, types, names);
230 }
231
232 -void rrd_stats_api_v1_charts_allmetrics_prometheus_all_hosts(RRDHOST *host, BUFFER *wb, const char *server, uint32_t options, int help, int types, int names) {
232 +void rrd_stats_api_v1_charts_allmetrics_prometheus_all_hosts(RRDHOST *host, BUFFER *wb, const char *server, const char *prefix, uint32_t options, int help, int types, int names) {
233 time_t before = now_realtime_sec();
234 time_t after = prometheus_preparation(host, wb, options, server, before, help);
235
236 rrd_rdlock();
237 rrdhost_foreach_read(host) {
238 - rrd_stats_api_v1_charts_allmetrics_prometheus(host, wb, options, after, before, 1, help, types, names);
238 + rrd_stats_api_v1_charts_allmetrics_prometheus(host, wb, prefix, options, after, before, 1, help, types, names);
239 }
240 rrd_unlock();
241 }
src/backend_prometheus.h
+2 -2
@@ -5,7 +5,7 @@
5 #ifndef NETDATA_BACKEND_PROMETHEUS_H
6 #define NETDATA_BACKEND_PROMETHEUS_H
7
8 -extern void rrd_stats_api_v1_charts_allmetrics_prometheus_single_host(RRDHOST *host, BUFFER *wb, const char *server, uint32_t options, int help, int types, int names);
9 -extern void rrd_stats_api_v1_charts_allmetrics_prometheus_all_hosts(RRDHOST *host, BUFFER *wb, const char *server, uint32_t options, int help, int types, int names);
8 +extern void rrd_stats_api_v1_charts_allmetrics_prometheus_single_host(RRDHOST *host, BUFFER *wb, const char *server, const char *prefix, uint32_t options, int help, int types, int names);
9 +extern void rrd_stats_api_v1_charts_allmetrics_prometheus_all_hosts(RRDHOST *host, BUFFER *wb, const char *server, const char *prefix, uint32_t options, int help, int types, int names);
10
11 #endif //NETDATA_BACKEND_PROMETHEUS_H
src/web_api_v1.c
+6 -2
@@ -211,6 +211,7 @@ inline int web_client_api_request_v1_allmetrics(RRDHOST *host, struct web_client
211 int help = 0, types = 0, names = backend_send_names; // prometheus options
212 const char *prometheus_server = w->client_ip;
213 uint32_t prometheus_options = backend_options;
214 + const char *prometheus_prefix = backend_prefix;
215
216 while(url) {
217 char *value = mystrsep(&url, "?&");
@@ -253,6 +254,9 @@ inline int web_client_api_request_v1_allmetrics(RRDHOST *host, struct web_client
254 else if(!strcmp(name, "server")) {
255 prometheus_server = value;
256 }
257 + else if(!strcmp(name, "prefix")) {
258 + prometheus_prefix = value;
259 + }
260 else if(!strcmp(name, "source")) {
261 prometheus_options = backend_parse_data_source(value, prometheus_options);
262 }
@@ -274,12 +278,12 @@ inline int web_client_api_request_v1_allmetrics(RRDHOST *host, struct web_client
278
279 case ALLMETRICS_PROMETHEUS:
280 w->response.data->contenttype = CT_PROMETHEUS;
277 - rrd_stats_api_v1_charts_allmetrics_prometheus_single_host(host, w->response.data, prometheus_server, prometheus_options, help, types, names);
281 + rrd_stats_api_v1_charts_allmetrics_prometheus_single_host(host, w->response.data, prometheus_server, prometheus_prefix, prometheus_options, help, types, names);
282 return 200;
283
284 case ALLMETRICS_PROMETHEUS_ALL_HOSTS:
285 w->response.data->contenttype = CT_PROMETHEUS;
282 - rrd_stats_api_v1_charts_allmetrics_prometheus_all_hosts(host, w->response.data, prometheus_server, prometheus_options, help, types, names);
286 + rrd_stats_api_v1_charts_allmetrics_prometheus_all_hosts(host, w->response.data, prometheus_server, prometheus_prefix, prometheus_options, help, types, names);
287 return 200;
288
289 default: