|
| 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