diff options
author | Vladimir Kobal <vlad@prokk.net> | 2022-05-05 19:32:57 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-05 19:32:57 +0300 |
commit | 464695b41069aa9ac39c5daecdd15b1d89b75bdd (patch) | |
tree | 2c46fb6620072f4587fa4a3ffff8ee3d028c9114 /exporting/prometheus | |
parent | 4f3d90a405d297105597f3b840b85a62f8cc7fd2 (diff) |
Add chart filtering parameter to the allmetrics API query (#12820)
* Add chart filtering in the allmetrics API call
* Fix compilation warnings
* Remove unnecessary function
* Update the documentation
* Apply suggestions from code review
* Check for filter instead of filter_string
* Do not check both - chart id and name for prometheus and shell formats
* Fix unit tests
Co-authored-by: Ilya Mashchenko <ilya@netdata.cloud>
Diffstat (limited to 'exporting/prometheus')
-rw-r--r-- | exporting/prometheus/prometheus.c | 53 | ||||
-rw-r--r-- | exporting/prometheus/prometheus.h | 6 |
2 files changed, 39 insertions, 20 deletions
diff --git a/exporting/prometheus/prometheus.c b/exporting/prometheus/prometheus.c index 97c3a29f0e..dc8e870ef2 100644 --- a/exporting/prometheus/prometheus.c +++ b/exporting/prometheus/prometheus.c @@ -12,9 +12,10 @@ * * @param instance an instance data structure. * @param st a chart. + * @param filter a simple pattern to match against. * @return Returns 1 if the chart can be sent, 0 otherwise. */ -inline int can_send_rrdset(struct instance *instance, RRDSET *st) +inline int can_send_rrdset(struct instance *instance, RRDSET *st, SIMPLE_PATTERN *filter) { #ifdef NETDATA_INTERNAL_CHECKS RRDHOST *host = st->rrdhost; @@ -27,19 +28,29 @@ inline int can_send_rrdset(struct instance *instance, RRDSET *st) if (unlikely(rrdset_flag_check(st, RRDSET_FLAG_EXPORTING_IGNORE))) return 0; - if (unlikely(!rrdset_flag_check(st, RRDSET_FLAG_EXPORTING_SEND))) { - // we have not checked this chart - if (simple_pattern_matches(instance->config.charts_pattern, st->id) || - simple_pattern_matches(instance->config.charts_pattern, st->name)) - rrdset_flag_set(st, RRDSET_FLAG_EXPORTING_SEND); - else { - rrdset_flag_set(st, RRDSET_FLAG_EXPORTING_IGNORE); - debug( - D_EXPORTING, - "EXPORTING: not sending chart '%s' of host '%s', because it is disabled for exporting.", - st->id, - host->hostname); - return 0; + if (filter) { + if (instance->config.options & EXPORTING_OPTION_SEND_NAMES) { + if (!simple_pattern_matches(filter, st->name)) + return 0; + } else { + if (!simple_pattern_matches(filter, st->id)) + return 0; + } + } else { + if (unlikely(!rrdset_flag_check(st, RRDSET_FLAG_EXPORTING_SEND))) { + // we have not checked this chart + if (simple_pattern_matches(instance->config.charts_pattern, st->id) || + simple_pattern_matches(instance->config.charts_pattern, st->name)) + rrdset_flag_set(st, RRDSET_FLAG_EXPORTING_SEND); + else { + rrdset_flag_set(st, RRDSET_FLAG_EXPORTING_IGNORE); + debug( + D_EXPORTING, + "EXPORTING: not sending chart '%s' of host '%s', because it is disabled for exporting.", + st->id, + host->hostname); + return 0; + } } } @@ -480,6 +491,7 @@ static void generate_as_collected_prom_metric(BUFFER *wb, struct gen_parameters * * @param instance an instance data structure. * @param host a data collecting host. + * @param filter_string a simple pattern filter. * @param wb the buffer to fill with metrics. * @param prefix a prefix for every metric. * @param exporting_options options to configure what data is exported. @@ -489,12 +501,14 @@ static void generate_as_collected_prom_metric(BUFFER *wb, struct gen_parameters static void rrd_stats_api_v1_charts_allmetrics_prometheus( struct instance *instance, RRDHOST *host, + const char *filter_string, BUFFER *wb, const char *prefix, EXPORTING_OPTIONS exporting_options, int allhosts, PROMETHEUS_OUTPUT_OPTIONS output_options) { + SIMPLE_PATTERN *filter = simple_pattern_create(filter_string, NULL, SIMPLE_PATTERN_EXACT); rrdhost_rdlock(host); char hostname[PROMETHEUS_ELEMENT_MAX + 1]; @@ -592,7 +606,7 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus( rrdset_foreach_read(st, host) { - if (likely(can_send_rrdset(instance, st))) { + if (likely(can_send_rrdset(instance, st, filter))) { rrdset_rdlock(st); char chart[PROMETHEUS_ELEMENT_MAX + 1]; @@ -777,6 +791,7 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus( } rrdhost_unlock(host); + simple_pattern_free(filter); } /** @@ -850,6 +865,7 @@ static inline time_t prometheus_preparation( * Write metrics and auxiliary information for one host to a buffer. * * @param host a data collecting host. + * @param filter_string a simple pattern filter. * @param wb the buffer to write to. * @param server the name of a Prometheus server. * @param prefix a prefix for every metric. @@ -858,6 +874,7 @@ static inline time_t prometheus_preparation( */ void rrd_stats_api_v1_charts_allmetrics_prometheus_single_host( RRDHOST *host, + const char *filter_string, BUFFER *wb, const char *server, const char *prefix, @@ -880,13 +897,14 @@ void rrd_stats_api_v1_charts_allmetrics_prometheus_single_host( output_options); rrd_stats_api_v1_charts_allmetrics_prometheus( - prometheus_exporter_instance, host, wb, prefix, exporting_options, 0, output_options); + prometheus_exporter_instance, host, filter_string, wb, prefix, exporting_options, 0, output_options); } /** * Write metrics and auxiliary information for all hosts to a buffer. * * @param host a data collecting host. + * @param filter_string a simple pattern filter. * @param wb the buffer to write to. * @param server the name of a Prometheus server. * @param prefix a prefix for every metric. @@ -895,6 +913,7 @@ void rrd_stats_api_v1_charts_allmetrics_prometheus_single_host( */ void rrd_stats_api_v1_charts_allmetrics_prometheus_all_hosts( RRDHOST *host, + const char *filter_string, BUFFER *wb, const char *server, const char *prefix, @@ -920,7 +939,7 @@ void rrd_stats_api_v1_charts_allmetrics_prometheus_all_hosts( rrdhost_foreach_read(host) { rrd_stats_api_v1_charts_allmetrics_prometheus( - prometheus_exporter_instance, host, wb, prefix, exporting_options, 1, output_options); + prometheus_exporter_instance, host, filter_string, wb, prefix, exporting_options, 1, output_options); } rrd_unlock(); } diff --git a/exporting/prometheus/prometheus.h b/exporting/prometheus/prometheus.h index 2f0845ce98..4b8860ded8 100644 --- a/exporting/prometheus/prometheus.h +++ b/exporting/prometheus/prometheus.h @@ -23,13 +23,13 @@ typedef enum prometheus_output_flags { } PROMETHEUS_OUTPUT_OPTIONS; extern void rrd_stats_api_v1_charts_allmetrics_prometheus_single_host( - RRDHOST *host, BUFFER *wb, const char *server, const char *prefix, + RRDHOST *host, const char *filter_string, BUFFER *wb, const char *server, const char *prefix, EXPORTING_OPTIONS exporting_options, PROMETHEUS_OUTPUT_OPTIONS output_options); extern void rrd_stats_api_v1_charts_allmetrics_prometheus_all_hosts( - RRDHOST *host, BUFFER *wb, const char *server, const char *prefix, + RRDHOST *host, const char *filter_string, BUFFER *wb, const char *server, const char *prefix, EXPORTING_OPTIONS exporting_options, PROMETHEUS_OUTPUT_OPTIONS output_options); -int can_send_rrdset(struct instance *instance, RRDSET *st); +int can_send_rrdset(struct instance *instance, RRDSET *st, SIMPLE_PATTERN *filter); size_t prometheus_name_copy(char *d, const char *s, size_t usable); size_t prometheus_label_copy(char *d, const char *s, size_t usable); char *prometheus_units_copy(char *d, const char *s, size_t usable, int showoldunits); |