Skip to content

Commit

Permalink
filterx/metrics: optimize counter in init()
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Szakacs <[email protected]>
  • Loading branch information
alltilla committed Nov 12, 2024
1 parent 3d92f6a commit 63bf016
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
17 changes: 2 additions & 15 deletions lib/filterx/filterx-metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "stats/stats-registry.h"
#include "metrics/dyn-metrics-cache.h"
#include "scratch-buffers.h"
#include "atomic.h"

struct _FilterXMetrics
{
Expand All @@ -46,7 +45,6 @@ struct _FilterXMetrics
FilterXMetricsLabels *labels;
gint level;

GAtomicCounter is_optimized;
StatsCluster *const_cluster;
};

Expand Down Expand Up @@ -110,9 +108,6 @@ _optimize(FilterXMetrics *self)
{
stats_lock();

if (g_atomic_counter_get(&self->is_optimized))
goto exit;

if (!self->key.str || !filterx_metrics_labels_is_const(self->labels))
goto exit;

Expand All @@ -136,7 +131,6 @@ _optimize(FilterXMetrics *self)
self->key.str = NULL;

exit:
g_atomic_counter_set(&self->is_optimized, TRUE);
stats_unlock();
}

Expand All @@ -149,13 +143,6 @@ filterx_metrics_get_stats_counter(FilterXMetrics *self, StatsCounterItem **count
return TRUE;
}

/*
* We need to delay the optimization to the first get() call,
* as we don't have stats options when FilterXExprs are being created.
*/
if (!g_atomic_counter_get(&self->is_optimized))
_optimize(self);

if (_is_const(self))
{
*counter = stats_cluster_single_get_counter(self->const_cluster);
Expand Down Expand Up @@ -191,6 +178,8 @@ filterx_metrics_init(FilterXMetrics *self, GlobalConfig *cfg)
return FALSE;
}

_optimize(self);

return TRUE;
}

Expand Down Expand Up @@ -265,8 +254,6 @@ filterx_metrics_new(gint level, FilterXExpr *key, FilterXExpr *labels)
if (!_init_labels(self, labels))
goto error;

g_atomic_counter_set(&self->is_optimized, FALSE);

return self;

error:
Expand Down
10 changes: 10 additions & 0 deletions modules/metrics-probe/tests/test_filterx_func_update_metric.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Test(filterx_func_update_metric, key_and_labels)
_add_label(labels, "test_label_1", "foo");
_add_label(labels, "test_label_2", "bar");
FilterXExpr *func = _create_func(key, labels, NULL, NULL);
cr_assert(filterx_expr_init(func, configuration));

StatsClusterLabel expected_labels[] =
{
Expand All @@ -115,13 +116,15 @@ Test(filterx_func_update_metric, key_and_labels)
G_N_ELEMENTS(expected_labels),
2);

filterx_expr_deinit(func, configuration);
filterx_expr_unref(func);
}

Test(filterx_func_update_metric, increment)
{
FilterXExpr *key = filterx_literal_new(filterx_string_new("test_key", -1));
FilterXExpr *func = _create_func(key, NULL, filterx_non_literal_new(filterx_integer_new(42)), NULL);
cr_assert(filterx_expr_init(func, configuration));

StatsClusterLabel expected_labels[] = {};

Expand All @@ -137,6 +140,7 @@ Test(filterx_func_update_metric, increment)
G_N_ELEMENTS(expected_labels),
84);

filterx_expr_deinit(func, configuration);
filterx_expr_unref(func);
}

Expand All @@ -149,7 +153,9 @@ Test(filterx_func_update_metric, level)
cr_assert(cfg_init(configuration));
func = _create_func(filterx_literal_new(filterx_string_new("test_key", -1)), NULL, NULL,
filterx_literal_new(filterx_integer_new(2)));
cr_assert(filterx_expr_init(func, configuration));
cr_assert(_eval(func));
filterx_expr_deinit(func, configuration);
filterx_expr_unref(func);
cr_assert_not(metrics_probe_test_stats_cluster_exists("test_key", expected_labels, G_N_ELEMENTS(expected_labels)));
cr_assert(cfg_deinit(configuration));
Expand All @@ -158,7 +164,9 @@ Test(filterx_func_update_metric, level)
cr_assert(cfg_init(configuration));
func = _create_func(filterx_literal_new(filterx_string_new("test_key", -1)), NULL, NULL,
filterx_literal_new(filterx_integer_new(2)));
cr_assert(filterx_expr_init(func, configuration));
cr_assert(_eval(func));
filterx_expr_deinit(func, configuration);
filterx_expr_unref(func);
cr_assert_not(metrics_probe_test_stats_cluster_exists("test_key", expected_labels, G_N_ELEMENTS(expected_labels)));
cr_assert(cfg_deinit(configuration));
Expand All @@ -167,7 +175,9 @@ Test(filterx_func_update_metric, level)
cr_assert(cfg_init(configuration));
func = _create_func(filterx_literal_new(filterx_string_new("test_key", -1)), NULL, NULL,
filterx_literal_new(filterx_integer_new(2)));
cr_assert(filterx_expr_init(func, configuration));
cr_assert(_eval(func));
filterx_expr_deinit(func, configuration);
filterx_expr_unref(func);
metrics_probe_test_assert_counter_value("test_key",
expected_labels,
Expand Down

0 comments on commit 63bf016

Please sign in to comment.