11import os
2- import time
32
43from utils .http_matrix import run_curl_request
54from utils .test_service import FluentBitTestService
65
76
8- COUNTER_NAME = "nginx_request_nginx_request_status_code_total "
7+ COUNTER_NAME = "nginx_request_status_code_total "
98COUNTER_LABELS = (
109 'request_method="GET"' ,
1110 'status="200"' ,
1211 'host="example.com"' ,
1312 'endpoint="/"' ,
1413 'hostname="host-a"' ,
1514)
15+ EXPECTED_SAMPLE_COUNT = 100
1616
1717
1818class Service :
@@ -41,23 +41,28 @@ def _counter_value(metrics_text):
4141 if not line .startswith (f"{ COUNTER_NAME } {{" ):
4242 continue
4343 if all (label in line for label in COUNTER_LABELS ):
44- return float (line .rsplit (" " , 1 )[1 ])
44+ fields = line .split ()
45+ if len (fields ) < 2 :
46+ continue
47+ value_field = fields [- 2 ] if len (fields ) > 2 else fields [- 1 ]
48+ return float (value_field )
4549 return None
4650
4751
4852def test_log_to_metrics_counter_timer_emits_repeated_metric_chunks ():
4953 service = Service ("counter_timer_prometheus.yaml" )
50- service .start ()
5154
5255 try :
56+ service .start ()
57+
5358 first_value = service .service .wait_for_condition (
5459 lambda : _counter_value (service .scrape_metrics ()["body" ]),
5560 timeout = 15 ,
5661 interval = 1 ,
5762 description = "initial log_to_metrics counter scrape" ,
5863 )
5964
60- second_value = service .service .wait_for_condition (
65+ service .service .wait_for_condition (
6166 lambda : (
6267 value
6368 if (value := _counter_value (service .scrape_metrics ()["body" ])) is not None
@@ -69,7 +74,16 @@ def test_log_to_metrics_counter_timer_emits_repeated_metric_chunks():
6974 description = "increasing log_to_metrics counter scrape" ,
7075 )
7176
72- assert second_value > first_value
73- time .sleep (3 )
77+ service .service .wait_for_condition (
78+ lambda : (
79+ value
80+ if (value := _counter_value (service .scrape_metrics ()["body" ])) is not None
81+ and value >= EXPECTED_SAMPLE_COUNT
82+ else None
83+ ),
84+ timeout = 15 ,
85+ interval = 1 ,
86+ description = "complete log_to_metrics counter scrape" ,
87+ )
7488 finally :
7589 service .stop ()
0 commit comments