Skip to content

Commit

Permalink
Add expire time to metrics in prometheus_exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
bdjjiiik committed Oct 21, 2023
1 parent 92b9053 commit 2d3de46
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/fluent-bit/flb_hash_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 11 additions & 1 deletion plugins/out_prometheus_exporter/prom.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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}
};
Expand Down
3 changes: 3 additions & 0 deletions plugins/out_prometheus_exporter/prom.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
27 changes: 27 additions & 0 deletions src/flb_hash_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}

0 comments on commit 2d3de46

Please sign in to comment.