From 56ac9f514566d0cbfa6125a28c96b759d4de1ff9 Mon Sep 17 00:00:00 2001 From: Chris Akritidis <43294513+cakrit@users.noreply.github.com> Date: Mon, 4 Mar 2019 18:36:08 +0100 Subject: Support legacy Prometheus metric names for source average (#5531) * Support older prometheus metric unit naming and allow removal of units from metrics * Update swagger * Added bats tests, improved efficiency of checking units --- backends/prometheus/README.md | 8 ++++++ backends/prometheus/backend_prometheus.c | 45 +++++++++++++++++++++++++++++--- backends/prometheus/backend_prometheus.h | 4 ++- 3 files changed, 53 insertions(+), 4 deletions(-) (limited to 'backends') diff --git a/backends/prometheus/README.md b/backends/prometheus/README.md index 8cf83cbf49..4a6cbfee2d 100644 --- a/backends/prometheus/README.md +++ b/backends/prometheus/README.md @@ -373,6 +373,14 @@ netdata sends all metrics prefixed with `netdata_`. You can change this in `netd It can also be changed from the URL, by appending `&prefix=netdata`. +### Metric Units + +The default source `average` adds the unit of measurement to the name of each metric (e.g. `_KiB_persec`). +To hide the units and get the same metric names as with the other sources, append to the URL `&hideunits=yes`. + +The units were standardized in v1.12, with the effect of changing the metric names. +To get the metric names as they were before v1.12, append to the URL `&oldunits=yes` + ### Accuracy of `average` and `sum` data sources When the data source is set to `average` or `sum`, netdata remembers the last access of each client accessing prometheus metrics and uses this last access time to respond with the `average` or `sum` of all the entries in the database since that. This means that prometheus servers are not losing data when they access netdata with data source = `average` or `sum`. diff --git a/backends/prometheus/backend_prometheus.c b/backends/prometheus/backend_prometheus.c index 1c36a9703a..f05da06972 100644 --- a/backends/prometheus/backend_prometheus.c +++ b/backends/prometheus/backend_prometheus.c @@ -78,11 +78,50 @@ static inline size_t prometheus_label_copy(char *d, const char *s, size_t usable return n; } -static inline char *prometheus_units_copy(char *d, const char *s, size_t usable) { +static inline char *prometheus_units_copy(char *d, const char *s, size_t usable, int showoldunits) { const char *sorig = s; char *ret = d; size_t n; + // Fix for issue 5227 + if (unlikely(showoldunits)) { + static struct { + const char *newunit; + uint32_t hash; + const char *oldunit; + } units[] = { + {"KiB/s", 0, "kilobytes/s"} + , {"MiB/s", 0, "MB/s"} + , {"GiB/s", 0, "GB/s"} + , {"KiB" , 0, "KB"} + , {"MiB" , 0, "MB"} + , {"GiB" , 0, "GB"} + , {"inodes" , 0, "Inodes"} + , {"percentage" , 0, "percent"} + , {"faults/s" , 0, "page faults/s"} + , {"KiB/operation", 0, "kilobytes per operation"} + , {"milliseconds/operation", 0, "ms per operation"} + , {NULL, 0, NULL} + }; + static int initialized = 0; + int i; + + if(unlikely(!initialized)) { + for (i = 0; units[i].newunit; i++) + units[i].hash = simple_hash(units[i].newunit); + initialized = 1; + } + + uint32_t hash = simple_hash(s); + for(i = 0; units[i].newunit ; i++) { + if(unlikely(hash == units[i].hash && !strcmp(s, units[i].newunit))) { + // info("matched extension for filename '%s': '%s'", filename, last_dot); + s=units[i].oldunit; + sorig = s; + break; + } + } + } *d++ = '_'; for(n = 1; *s && n < usable ; d++, s++, n++) { register char c = *s; @@ -275,8 +314,8 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(RRDHOST *host, BUFFER homogeneus = 0; } else { - if(BACKEND_OPTIONS_DATA_SOURCE(backend_options) == BACKEND_SOURCE_DATA_AVERAGE) - prometheus_units_copy(units, st->units, PROMETHEUS_ELEMENT_MAX); + if(BACKEND_OPTIONS_DATA_SOURCE(backend_options) == BACKEND_SOURCE_DATA_AVERAGE && !(output_options & PROMETHEUS_OUTPUT_HIDEUNITS)) + prometheus_units_copy(units, st->units, PROMETHEUS_ELEMENT_MAX, output_options & PROMETHEUS_OUTPUT_OLDUNITS); } if(unlikely(output_options & PROMETHEUS_OUTPUT_HELP)) diff --git a/backends/prometheus/backend_prometheus.h b/backends/prometheus/backend_prometheus.h index dc4ec753f2..72b65a22ba 100644 --- a/backends/prometheus/backend_prometheus.h +++ b/backends/prometheus/backend_prometheus.h @@ -11,7 +11,9 @@ typedef enum prometheus_output_flags { PROMETHEUS_OUTPUT_TYPES = (1 << 1), PROMETHEUS_OUTPUT_NAMES = (1 << 2), PROMETHEUS_OUTPUT_TIMESTAMPS = (1 << 3), - PROMETHEUS_OUTPUT_VARIABLES = (1 << 4) + PROMETHEUS_OUTPUT_VARIABLES = (1 << 4), + PROMETHEUS_OUTPUT_OLDUNITS = (1 << 5), + PROMETHEUS_OUTPUT_HIDEUNITS = (1 << 6) } PROMETHEUS_OUTPUT_OPTIONS; extern void rrd_stats_api_v1_charts_allmetrics_prometheus_single_host(RRDHOST *host, BUFFER *wb, const char *server, const char *prefix, BACKEND_OPTIONS backend_options, PROMETHEUS_OUTPUT_OPTIONS output_options); -- cgit v1.2.3