Skip to content

Commit 3a43df8

Browse files
committed
tests: integration: add log_to_metrics counter timer test
Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
1 parent 5a0049f commit 3a43df8

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
service:
2+
flush: 1
3+
grace: 1
4+
log_level: info
5+
http_server: on
6+
http_port: ${FLUENT_BIT_HTTP_MONITORING_PORT}
7+
8+
pipeline:
9+
inputs:
10+
- name: dummy
11+
tag: nginx.b2b.access
12+
rate: 1
13+
samples: 5
14+
dummy: '{"request_method":"GET","status":"200","host":"example.com","endpoint":"/","hostname":"host-a","upstream_response_time":"0.125"}'
15+
16+
filters:
17+
- name: log_to_metrics
18+
match: nginx.b2b.access
19+
tag: metrics.nginx
20+
emitter_name: em_status_code
21+
flush_interval_sec: 1
22+
metric_mode: counter
23+
metric_name: nginx_request_status_code_total
24+
metric_description: Counts nginx request status codes
25+
metric_namespace: nginx
26+
metric_subsystem: request
27+
label_field:
28+
- request_method
29+
- status
30+
- host
31+
- endpoint
32+
- hostname
33+
34+
outputs:
35+
- name: prometheus_exporter
36+
match: metrics.nginx
37+
host: 127.0.0.1
38+
port: ${EXPORTER_PORT}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import os
2+
import time
3+
4+
from utils.http_matrix import run_curl_request
5+
from utils.test_service import FluentBitTestService
6+
7+
8+
COUNTER_NAME = "nginx_request_nginx_request_status_code_total"
9+
COUNTER_LABELS = (
10+
'request_method="GET"',
11+
'status="200"',
12+
'host="example.com"',
13+
'endpoint="/"',
14+
'hostname="host-a"',
15+
)
16+
17+
18+
class Service:
19+
def __init__(self, config_file):
20+
self.config_file = os.path.abspath(
21+
os.path.join(os.path.dirname(__file__), "../config", config_file)
22+
)
23+
self.service = FluentBitTestService(self.config_file, pre_start=self._pre_start)
24+
25+
def _pre_start(self, service):
26+
self.exporter_port = service.allocate_port_env("EXPORTER_PORT")
27+
28+
def start(self):
29+
self.service.start()
30+
self.base_url = f"http://127.0.0.1:{self.exporter_port}"
31+
32+
def stop(self):
33+
self.service.stop()
34+
35+
def scrape_metrics(self):
36+
return run_curl_request(f"{self.base_url}/metrics", method="GET", http_mode="http1.1")
37+
38+
39+
def _counter_value(metrics_text):
40+
for line in metrics_text.splitlines():
41+
if not line.startswith(f"{COUNTER_NAME}{{"):
42+
continue
43+
if all(label in line for label in COUNTER_LABELS):
44+
return float(line.rsplit(" ", 1)[1])
45+
return None
46+
47+
48+
def test_log_to_metrics_counter_timer_emits_repeated_metric_chunks():
49+
service = Service("counter_timer_prometheus.yaml")
50+
service.start()
51+
52+
try:
53+
first_value = service.service.wait_for_condition(
54+
lambda: _counter_value(service.scrape_metrics()["body"]),
55+
timeout=15,
56+
interval=1,
57+
description="initial log_to_metrics counter scrape",
58+
)
59+
60+
second_value = service.service.wait_for_condition(
61+
lambda: (
62+
value
63+
if (value := _counter_value(service.scrape_metrics()["body"])) is not None
64+
and value > first_value
65+
else None
66+
),
67+
timeout=15,
68+
interval=1,
69+
description="increasing log_to_metrics counter scrape",
70+
)
71+
72+
assert second_value > first_value
73+
time.sleep(3)
74+
finally:
75+
service.stop()

0 commit comments

Comments
 (0)