diff --git a/lib/filterx/filterx-metrics.c b/lib/filterx/filterx-metrics.c index 7622793e2..65b376c88 100644 --- a/lib/filterx/filterx-metrics.c +++ b/lib/filterx/filterx-metrics.c @@ -33,7 +33,6 @@ #include "stats/stats-registry.h" #include "metrics/dyn-metrics-cache.h" #include "scratch-buffers.h" -#include "atomic.h" struct _FilterXMetrics { @@ -46,7 +45,6 @@ struct _FilterXMetrics FilterXMetricsLabels *labels; gint level; - GAtomicCounter is_optimized; StatsCluster *const_cluster; }; @@ -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; @@ -136,7 +131,6 @@ _optimize(FilterXMetrics *self) self->key.str = NULL; exit: - g_atomic_counter_set(&self->is_optimized, TRUE); stats_unlock(); } @@ -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); @@ -191,6 +178,8 @@ filterx_metrics_init(FilterXMetrics *self, GlobalConfig *cfg) return FALSE; } + _optimize(self); + return TRUE; } @@ -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: diff --git a/modules/metrics-probe/tests/test_filterx_func_update_metric.c b/modules/metrics-probe/tests/test_filterx_func_update_metric.c index 3d8525ad8..f0f538a43 100644 --- a/modules/metrics-probe/tests/test_filterx_func_update_metric.c +++ b/modules/metrics-probe/tests/test_filterx_func_update_metric.c @@ -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[] = { @@ -115,6 +116,7 @@ Test(filterx_func_update_metric, key_and_labels) G_N_ELEMENTS(expected_labels), 2); + filterx_expr_deinit(func, configuration); filterx_expr_unref(func); } @@ -122,6 +124,7 @@ 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[] = {}; @@ -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); } @@ -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)); @@ -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)); @@ -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,