From 2d3de46a48ca24b092b46b2bee839374423217f0 Mon Sep 17 00:00:00 2001 From: Anton Zhurba Date: Sat, 21 Oct 2023 22:51:12 +0300 Subject: [PATCH] Add expire time to metrics in prometheus_exporter --- include/fluent-bit/flb_hash_table.h | 2 ++ plugins/out_prometheus_exporter/prom.c | 12 +++++++++++- plugins/out_prometheus_exporter/prom.h | 3 +++ src/flb_hash_table.c | 27 ++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/include/fluent-bit/flb_hash_table.h b/include/fluent-bit/flb_hash_table.h index 115088d4f6e..cf711024ecd 100644 --- a/include/fluent-bit/flb_hash_table.h +++ b/include/fluent-bit/flb_hash_table.h @@ -86,4 +86,6 @@ int flb_hash_table_del(struct flb_hash_table *ht, const char *key); int flb_hash_table_del_ptr(struct flb_hash_table *ht, const char *key, int key_len, void *ptr); +void flb_hash_table_clear(struct flb_hash_table *ht); + #endif diff --git a/plugins/out_prometheus_exporter/prom.c b/plugins/out_prometheus_exporter/prom.c index d471d2bab57..deb12e9425c 100644 --- a/plugins/out_prometheus_exporter/prom.c +++ b/plugins/out_prometheus_exporter/prom.c @@ -97,7 +97,7 @@ static int cb_prom_init(struct flb_output_instance *ins, } /* Hash table for metrics */ - ctx->ht_metrics = flb_hash_table_create(FLB_HASH_TABLE_EVICT_NONE, 32, 0); + ctx->ht_metrics = flb_hash_table_create_with_ttl(ctx->ttl, FLB_HASH_TABLE_EVICT_NONE, 32, 0); if (!ctx->ht_metrics) { flb_plg_error(ctx->ins, "could not initialize hash table for metrics"); return -1; @@ -179,6 +179,9 @@ static void cb_prom_flush(struct flb_event_chunk *event_chunk, struct cmt *cmt; struct prom_exporter *ctx = out_context; + /* Clear old metrics */ + flb_hash_table_clear(ctx->ht_metrics); + /* * A new set of metrics has arrived, perform decoding, apply labels, * convert to Prometheus text format and store the output in the @@ -229,6 +232,7 @@ static void cb_prom_flush(struct flb_event_chunk *event_chunk, /* retrieve a full copy of all metrics */ metrics = hash_format_metrics(ctx); + if (!metrics) { flb_plg_error(ctx->ins, "could not retrieve metrics"); FLB_OUTPUT_RETURN(FLB_ERROR); @@ -281,6 +285,12 @@ static struct flb_config_map config_map[] = { "TCP port for listening for HTTP connections." }, + { + FLB_CONFIG_MAP_TIME, "ttl", "0s", + 0, FLB_TRUE, offsetof(struct prom_exporter, ttl), + "Expiring time for metrics" + }, + /* EOF */ {0} }; diff --git a/plugins/out_prometheus_exporter/prom.h b/plugins/out_prometheus_exporter/prom.h index 1cbab6a5940..5da4bfe2ac6 100644 --- a/plugins/out_prometheus_exporter/prom.h +++ b/plugins/out_prometheus_exporter/prom.h @@ -33,6 +33,9 @@ struct prom_exporter { /* add timestamp to every metric */ int add_timestamp; + /* expiry time for metrics in the hash table */ + time_t ttl; + /* config reader for 'add_label' */ struct mk_list *add_labels; diff --git a/src/flb_hash_table.c b/src/flb_hash_table.c index d31c4259179..0b01e75030f 100644 --- a/src/flb_hash_table.c +++ b/src/flb_hash_table.c @@ -537,3 +537,30 @@ int flb_hash_table_del(struct flb_hash_table *ht, const char *key) return 0; } + +void flb_hash_table_clear(struct flb_hash_table *ht) +{ + int i; + struct mk_list *tmp; + struct mk_list *head; + struct flb_hash_table_entry *entry; + struct flb_hash_table_chain *table; + time_t expiration; + + if (ht->cache_ttl <= 0) { + return; + } + + for (i = 0; i < ht->size; i++) { + table = &ht->table[i]; + mk_list_foreach_safe(head, tmp, &table->chains) { + entry = mk_list_entry(head, struct flb_hash_table_entry, _head); + + expiration = entry->created + ht->cache_ttl; + + if (time(NULL) > expiration) { + flb_hash_table_entry_free(ht, entry); + } + } + } +}