Skip to content

Commit

Permalink
Merge pull request #243 from MrAnno/fix-metrics-probe-cntr-lifetime
Browse files Browse the repository at this point in the history
Fix disappearing `metrics-probe()` counters
  • Loading branch information
MrAnno authored Aug 13, 2024
2 parents 243a51d + 10376b4 commit d356f61
Show file tree
Hide file tree
Showing 24 changed files with 291 additions and 222 deletions.
2 changes: 1 addition & 1 deletion lib/cfg-grammar-internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ LogRewrite *last_rewrite;
CfgArgs *last_block_args;
DNSCacheOptions *last_dns_cache_options;
MultiLineOptions *last_multi_line_options;
MetricsTemplate *last_metrics_template;
DynMetricsTemplate *last_dyn_metrics_template;
4 changes: 2 additions & 2 deletions lib/cfg-grammar-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#include "cfg-block.h"
#include "cfg-path.h"
#include "multi-line/multi-line-factory.h"
#include "metrics/metrics-template.h"
#include "metrics/dyn-metrics-template.h"

#include "logthrsource/logthrfetcherdrv.h"
#include "logthrdest/logthrdestdrv.h"
Expand Down Expand Up @@ -91,7 +91,7 @@ extern LogRewrite *last_rewrite;
extern CfgArgs *last_block_args;
extern DNSCacheOptions *last_dns_cache_options;
extern MultiLineOptions *last_multi_line_options;
extern MetricsTemplate *last_metrics_template;
extern DynMetricsTemplate *last_dyn_metrics_template;


#endif
22 changes: 11 additions & 11 deletions lib/cfg-grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -1674,25 +1674,25 @@ vp_rekey_option
| KW_LOWER '(' ')' { value_pairs_transform_set_add_func(last_vp_transset, value_pairs_new_transform_lower()); }
;

metrics_template_opt
: KW_KEY '(' string ')' { metrics_template_set_key(last_metrics_template, $3); g_free($3); }
| KW_LABELS '(' metrics_template_labels_opts ')'
| KW_LEVEL '(' nonnegative_integer ')' { metrics_template_set_level(last_metrics_template, $3); }
dyn_metrics_template_opt
: KW_KEY '(' string ')' { dyn_metrics_template_set_key(last_dyn_metrics_template, $3); g_free($3); }
| KW_LABELS '(' dyn_metrics_template_labels_opts ')'
| KW_LEVEL '(' nonnegative_integer ')' { dyn_metrics_template_set_level(last_dyn_metrics_template, $3); }

metrics_template_labels_opts
: metrics_template_labels_opt metrics_template_labels_opts
dyn_metrics_template_labels_opts
: dyn_metrics_template_labels_opt dyn_metrics_template_labels_opts
|
;

metrics_template_labels_opt
: metrics_template_label_template
| { last_value_pairs = metrics_template_get_value_pairs(last_metrics_template); } vp_option
dyn_metrics_template_labels_opt
: dyn_metrics_template_label_template
| { last_value_pairs = dyn_metrics_template_get_value_pairs(last_dyn_metrics_template); } vp_option
;

metrics_template_label_template
dyn_metrics_template_label_template
: string LL_ARROW template_content
{
metrics_template_add_label_template(last_metrics_template, $1, $3);
dyn_metrics_template_add_label_template(last_dyn_metrics_template, $1, $3);
free($1);
log_template_unref($3);
}
Expand Down
22 changes: 11 additions & 11 deletions lib/filterx/filterx-metrics-labels.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "expr-literal-generator.h"
#include "object-string.h"
#include "object-dict-interface.h"
#include "metrics/metrics-tls-cache.h"
#include "metrics/dyn-metrics-cache.h"
#include "stats/stats-cluster-single.h"
#include "scratch-buffers.h"

Expand Down Expand Up @@ -192,7 +192,7 @@ filterx_metrics_labels_is_const(FilterXMetricsLabels *self)
static gboolean
_format_dict_elem_to_cache(FilterXObject *key, FilterXObject *value, gpointer user_data)
{
MetricsCache *cache = (MetricsCache *) user_data;
DynMetricsStore *cache = (DynMetricsStore *) user_data;

const gchar *name_str = _format_str_obj(key);
if (!name_str)
Expand All @@ -210,15 +210,15 @@ _format_dict_elem_to_cache(FilterXObject *key, FilterXObject *value, gpointer us
return FALSE;
}

StatsClusterLabel *label = metrics_cache_alloc_label(cache);
StatsClusterLabel *label = dyn_metrics_store_cache_label(cache);
label->name = name_str;
label->value = value_str;

return TRUE;
}

static gboolean
_format_expr_to_cache(FilterXExpr *expr, MetricsCache *cache)
_format_expr_to_cache(FilterXExpr *expr, DynMetricsStore *cache)
{
FilterXObject *obj = filterx_expr_eval_typed(expr);
if (!obj)
Expand All @@ -237,7 +237,7 @@ _format_expr_to_cache(FilterXExpr *expr, MetricsCache *cache)
if (!success)
goto exit;

metrics_cache_sort_labels(cache);
dyn_metrics_store_sort_cached_labels(cache);

exit:
filterx_object_unref(obj);
Expand All @@ -249,20 +249,20 @@ _format_label_to_cache(gpointer data, gpointer user_data)
{
FilterXMetricsLabel *label = (FilterXMetricsLabel *) data;
gboolean *success = ((gpointer *) user_data)[0];
MetricsCache *cache = ((gpointer *) user_data)[1];
DynMetricsStore *cache = ((gpointer *) user_data)[1];

if (!(*success))
return;

*success = _label_format(label, metrics_cache_alloc_label(cache));
*success = _label_format(label, dyn_metrics_store_cache_label(cache));
}

gboolean
filterx_metrics_labels_format(FilterXMetricsLabels *self, StatsClusterLabel **labels, gsize *len)
{
MetricsCache *cache = metrics_tls_cache();
DynMetricsStore *cache = dyn_metrics_cache();

metrics_cache_reset_labels(cache);
dyn_metrics_store_reset_labels_cache(cache);

gboolean success;
if (self->expr)
Expand All @@ -279,8 +279,8 @@ filterx_metrics_labels_format(FilterXMetricsLabels *self, StatsClusterLabel **la
if (!success)
return FALSE;

*labels = metrics_cache_get_labels(cache);
*len = metrics_cache_get_labels_len(cache);
*labels = dyn_metrics_store_get_cached_labels(cache);
*len = dyn_metrics_store_get_cached_labels_len(cache);
return TRUE;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/filterx-metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "filterx-eval.h"
#include "stats/stats-cluster-single.h"
#include "stats/stats-registry.h"
#include "metrics/metrics-tls-cache.h"
#include "metrics/dyn-metrics-cache.h"
#include "scratch-buffers.h"
#include "atomic.h"

Expand Down Expand Up @@ -173,7 +173,7 @@ filterx_metrics_get_stats_counter(FilterXMetrics *self, StatsCounterItem **count
if (!_format_sck(self, &sck))
goto exit;

*counter = metrics_cache_get_counter(metrics_tls_cache(), &sck, self->level);
*counter = dyn_metrics_store_retrieve_counter(dyn_metrics_cache(), &sck, self->level);
success = TRUE;

exit:
Expand Down
12 changes: 6 additions & 6 deletions lib/metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
set(METRICS_HEADERS
metrics/metrics.h
metrics/metrics-cache.h
metrics/metrics-tls-cache.h
metrics/metrics-template.h
metrics/dyn-metrics-store.h
metrics/dyn-metrics-cache.h
metrics/dyn-metrics-template.h
metrics/label-template.h
PARENT_SCOPE)

set(METRICS_SOURCES
metrics/metrics.c
metrics/metrics-cache.c
metrics/metrics-tls-cache.c
metrics/metrics-template.c
metrics/dyn-metrics-store.c
metrics/dyn-metrics-cache.c
metrics/dyn-metrics-template.c
metrics/label-template.c
PARENT_SCOPE)

Expand Down
12 changes: 6 additions & 6 deletions lib/metrics/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ EXTRA_DIST += lib/metrics/CMakeLists.txt

metricsinclude_HEADERS = \
lib/metrics/metrics.h \
lib/metrics/metrics-cache.h \
lib/metrics/metrics-tls-cache.h \
lib/metrics/metrics-template.h \
lib/metrics/dyn-metrics-store.h \
lib/metrics/dyn-metrics-cache.h \
lib/metrics/dyn-metrics-template.h \
lib/metrics/label-template.h

metrics_sources = \
lib/metrics/metrics.c \
lib/metrics/metrics-cache.c \
lib/metrics/metrics-tls-cache.c \
lib/metrics/metrics-template.c \
lib/metrics/dyn-metrics-store.c \
lib/metrics/dyn-metrics-cache.c \
lib/metrics/dyn-metrics-template.c \
lib/metrics/label-template.c

#include lib/metrics/tests/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2023-2024 Attila Szakacs <[email protected]>
* Copyright (c) 2024 Balazs Scheidler <[email protected]>
* Copyright (c) 2024 László Várady
* Copyright (c) 2024 Axoflow
*
* This library is free software; you can redistribute it and/or
Expand All @@ -23,49 +24,79 @@
*
*/

#include "metrics-tls-cache.h"
#include "dyn-metrics-cache.h"
#include "syslog-ng.h"
#include "apphook.h"
#include "tls-support.h"

TLS_BLOCK_START
{
MetricsCache *metrics_cache;
DynMetricsStore *metrics_cache;
}
TLS_BLOCK_END;

#define metrics_cache __tls_deref(metrics_cache)

static DynMetricsStore *global_metrics_cache;
static GMutex global_metrics_cache_lock;

static void
_sync_with_global_cache(DynMetricsStore *store)
{
g_mutex_lock(&global_metrics_cache_lock);
dyn_metrics_store_merge(global_metrics_cache, store);
g_mutex_unlock(&global_metrics_cache_lock);
}

static void
_purge_global_cache(gint type, gpointer c)
{
g_mutex_lock(&global_metrics_cache_lock);
dyn_metrics_store_reset(global_metrics_cache);
g_mutex_unlock(&global_metrics_cache_lock);
}

static void
_init_tls_cache(gpointer user_data)
{
g_assert(!metrics_cache);

metrics_cache = metrics_cache_new();
metrics_cache = dyn_metrics_store_new();
}

static void
_deinit_tls_cache(gpointer user_data)
{
metrics_cache_free(metrics_cache);
_sync_with_global_cache(metrics_cache);
dyn_metrics_store_free(metrics_cache);
}

MetricsCache *
metrics_tls_cache(void)
DynMetricsStore *
dyn_metrics_cache(void)
{
return metrics_cache;
}

void
metrics_tls_cache_global_init(void)
dyn_metrics_cache_global_init(void)
{
g_assert(!global_metrics_cache);
g_mutex_init(&global_metrics_cache_lock);
global_metrics_cache = dyn_metrics_store_new();

register_application_thread_init_hook(_init_tls_cache, NULL);
register_application_thread_deinit_hook(_deinit_tls_cache, NULL);

register_application_hook(AH_CONFIG_CHANGED, _purge_global_cache, NULL, AHM_RUN_REPEAT);

_init_tls_cache(NULL);
}

void
metrics_tls_cache_global_deinit(void)
dyn_metrics_cache_global_deinit(void)
{
_deinit_tls_cache(NULL);

dyn_metrics_store_free(global_metrics_cache);
g_mutex_clear(&global_metrics_cache_lock);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2023-2024 Attila Szakacs <[email protected]>
* Copyright (c) 2024 Balazs Scheidler <[email protected]>
* Copyright (c) 2024 László Várady
* Copyright (c) 2024 Axoflow
*
* This library is free software; you can redistribute it and/or
Expand All @@ -23,15 +24,15 @@
*
*/

#ifndef METRICS_TLS_CACHE_H_INCLUDED
#define METRICS_TLS_CACHE_H_INCLUDED
#ifndef DYN_METRICS_CACHE_H_INCLUDED
#define DYN_METRICS_CACHE_H_INCLUDED

#include "stats/stats-registry.h"
#include "metrics-cache.h"
#include "dyn-metrics-store.h"

MetricsCache *metrics_tls_cache(void);
DynMetricsStore *dyn_metrics_cache(void);

void metrics_tls_cache_global_init(void);
void metrics_tls_cache_global_deinit(void);
void dyn_metrics_cache_global_init(void);
void dyn_metrics_cache_global_deinit(void);

#endif
Loading

0 comments on commit d356f61

Please sign in to comment.