Skip to content

Commit

Permalink
metrics-probe: add filterx update_metric() E2E tests
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Szakacs <[email protected]>
  • Loading branch information
alltilla committed Aug 3, 2024
1 parent 08c44d6 commit 595a7d2
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/copyright/policy
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ tests/light/functional_tests/templates/test_template_stmt\.py
tests/light/functional_tests/filters/test_multiple_filters\.py
tests/light/functional_tests/filterx/test_filterx\.py
tests/light/functional_tests/filterx/test_filterx_scope\.py
tests/light/functional_tests/filterx/test_filterx_update_metric\.py
tests/light/functional_tests/parsers/metrics-probe/test_metrics_probe\.py
tests/light/src/syslog_ng_ctl/prometheus_stats_handler.py
tests/light/src/syslog_ng_config/statements/template/template\.py
Expand Down
191 changes: 191 additions & 0 deletions tests/light/functional_tests/filterx/test_filterx_update_metric.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
#!/usr/bin/env python
#############################################################################
# Copyright (c) 2024 Axoflow
# Copyright (c) 2024 Attila Szakacs <[email protected]>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# As an additional exemption you are allowed to compile & link against the
# OpenSSL libraries as published by the OpenSSL project. See the file
# COPYING for details.
#
#############################################################################
from src.syslog_ng_config.renderer import render_statement
from src.syslog_ng_ctl.prometheus_stats_handler import MetricFilter


def create_config(config, port_allocator, filterx, stats_level=0):
network_source = config.create_network_source(port=port_allocator(), flags="no-parse")
file_destination = config.create_file_destination(file_name="output.log")
raw_config = f"""
@version: {config.get_version()}
options {{ stats(level({stats_level})); }};
source s_network {{
{render_statement(network_source)};
}};
destination d_file {{
{render_statement(file_destination)};
}};
log {{
source(s_network);
filterx {{
{filterx}
}};
destination(d_file);
}};
"""
config.set_raw_config(raw_config)
return network_source, file_destination


def test_filterx_update_metric_labels(config, port_allocator, syslog_ng):
network_source, file_destination = create_config(
config,
port_allocator,
r"""
update_metric("literal", labels={"msg": $MSG, "foo": "foovalue"});
labels = json();
labels.msg = $MSG;
labels.foo = "foovalue";
update_metric("non_literal", labels=labels);
""",
)

syslog_ng.start(config)
network_source.write_log("msg1\nmsg2\nmsg1\nmsg3\n")
file_destination.read_logs(4)

# literal

samples = config.get_prometheus_samples([MetricFilter("syslogng_literal", {})])
assert len(samples) == 3

samples = config.get_prometheus_samples([MetricFilter("syslogng_literal", {"msg": "msg1", "foo": "foovalue"})])
assert len(samples) == 1
assert int(samples[0].value) == 2

samples = config.get_prometheus_samples([MetricFilter("syslogng_literal", {"msg": "msg2", "foo": "foovalue"})])
assert len(samples) == 1
assert int(samples[0].value) == 1

samples = config.get_prometheus_samples([MetricFilter("syslogng_literal", {"msg": "msg3", "foo": "foovalue"})])
assert len(samples) == 1
assert int(samples[0].value) == 1

# non literal

samples = config.get_prometheus_samples([MetricFilter("syslogng_non_literal", {})])
assert len(samples) == 3

samples = config.get_prometheus_samples([MetricFilter("syslogng_non_literal", {"msg": "msg1", "foo": "foovalue"})])
assert len(samples) == 1
assert int(samples[0].value) == 2

samples = config.get_prometheus_samples([MetricFilter("syslogng_non_literal", {"msg": "msg2", "foo": "foovalue"})])
assert len(samples) == 1
assert int(samples[0].value) == 1

samples = config.get_prometheus_samples([MetricFilter("syslogng_non_literal", {"msg": "msg3", "foo": "foovalue"})])
assert len(samples) == 1
assert int(samples[0].value) == 1

syslog_ng.stop()


def test_filterx_update_metric_increment(config, port_allocator, syslog_ng):
network_source, file_destination = create_config(
config,
port_allocator,
r"""
update_metric("const", increment=3);
# TODO: fix int cast from message_value
update_metric("expr", increment=int(string($MSG)));
""",
)

syslog_ng.start(config)
network_source.write_log("3\n2\n1\n0\n")
file_destination.read_logs(4)

samples = config.get_prometheus_samples([MetricFilter("syslogng_const", {})])
assert len(samples) == 1
assert int(samples[0].value) == 12

samples = config.get_prometheus_samples([MetricFilter("syslogng_expr", {})])
assert len(samples) == 1
assert int(samples[0].value) == 6

syslog_ng.stop()


def test_filterx_update_metric_level(config, port_allocator, syslog_ng):

# stats(level(0));

network_source, file_destination = create_config(
config,
port_allocator,
r"""
update_metric("metric", level=2);
""",
stats_level=0,
)

syslog_ng.start(config)
network_source.write_log("foo\n")
file_destination.read_log()

samples = config.get_prometheus_samples([MetricFilter("syslogng_metric", {})])
assert len(samples) == 0

# stats(level(1));

network_source, file_destination = create_config(
config,
port_allocator,
r"""
update_metric("metric", level=2);
""",
stats_level=1,
)

syslog_ng.reload(config)
network_source.write_log("foo\n")
file_destination.read_log()

# stats(level(2));

network_source, file_destination = create_config(
config,
port_allocator,
r"""
update_metric("metric", level=2);
""",
stats_level=2,
)

syslog_ng.reload(config)
network_source.write_log("foo\n")
file_destination.read_log()

samples = config.get_prometheus_samples([MetricFilter("syslogng_metric", {})])
assert len(samples) == 1
assert int(samples[0].value) == 1

syslog_ng.stop()

0 comments on commit 595a7d2

Please sign in to comment.