Skip to content

Commit 22b3e93

Browse files
committed
Telemetry E2E test
Add an E2E test to test telemetry metric scraping. The test sends a request to the Lightspeed /query endpoint and then sends a query to Prometheus and checks if ls_llm_calls_total metrics is greater than zero.
1 parent ba1b358 commit 22b3e93

7 files changed

Lines changed: 131 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,51 @@ for the `lightspeed-app-server` service. For example:
4444
oc port-forward -n openstack-lightspeed svc/lightspeed-app-server 8443:8443
4545
```
4646

47+
Telemetry tests are excluded by default. To run them, deploy Lightspeed and
48+
enable its metrics collection through telemetry-operator. Configure the
49+
Lightspeed URL and token above, then set a reachable Prometheus URL. TLS
50+
verification is enabled by default. For an internal CA, set
51+
`RHOS_LIGHTSPEED_PROMETHEUS_CA_CERT_PATH` (`prometheus.ca_cert_path`) to its
52+
PEM CA bundle:
53+
54+
```bash
55+
export RHOS_LIGHTSPEED_PROMETHEUS_URL="https://metric-storage-prometheus.openstack.svc:9090"
56+
export RHOS_LIGHTSPEED_PROMETHEUS_CA_CERT_PATH="/path/to/prometheus-ca.pem"
57+
lightspeed-tests -m telemetry
58+
```
59+
60+
If you run the telemetry test outside of OpenShift, you need to have a running port forward
61+
for the `metric-storage-prometheus` service. For example:
62+
63+
```
64+
oc port-forward -n openstack svc/metric-storage-prometheus 9090:9090
65+
```
66+
67+
Map `metric-storage-prometheus.openstack.svc` to `127.0.0.1` in your local
68+
hosts file and keep the service hostname in the URL above so TLS hostname
69+
verification succeeds. Export the OpenStack control plane CA bundle locally:
70+
71+
```bash
72+
oc -n openstack get secret combined-ca-bundle \
73+
-o jsonpath='{.data.tls-ca-bundle\.pem}' | base64 --decode > prometheus-ca.pem
74+
export RHOS_LIGHTSPEED_PROMETHEUS_CA_CERT_PATH="$PWD/prometheus-ca.pem"
75+
```
76+
4777
## Running tests
4878

4979
The command accepts pytest options and returns pytest's exit status. In a uv
50-
checkout, prefix the commands below with `uv run`.
80+
checkout, prefix the commands below with `uv run`. An explicit `-m` expression
81+
replaces the default `not telemetry` filter. For example, `-m "not llm_dependent"`
82+
also includes telemetry; use `-m "not llm_dependent and not telemetry"` to exclude
83+
both groups.
5184

5285
```bash
53-
# Run all tests
86+
# Run the default suite (excludes telemetry)
5487
lightspeed-tests
5588

89+
# Run all tests, including telemetry (requires Prometheus configuration)
90+
lightspeed-tests -m ""
91+
5692
# Run integration tests only
5793
lightspeed-tests -k integration
5894

‎playbooks/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ All parameters can be overridden via the `AnsibleTest` custom resource when runn
2626
* `pytest_args`: (String) Additional arguments to pass to pytest. Default value: `-v --tb=short --color=yes`
2727
* `junit_xml_path`: (String) Path where JUnit XML test results will be written. The test-operator provides writable storage at this location. Default value: `/var/lib/AnsibleTests/external_files/test-results.xml`
2828
* `test_suite_path`: (String) Path to the test suite directory. The test-operator clones git repositories to this location. Default value: `{{ ansible_env.HOME }}/ansible`
29+
* `prometheus_url`: (String) Telemetry Prometheus URL. Empty by default; required when selecting telemetry tests.
30+
* `prometheus_ca_cert_path`: (String) Path inside the test pod to a PEM CA bundle for Prometheus. Empty by default, which uses Requests' default trust store. TLS verification is always enabled.
31+
* `prometheus_api_timeout`: (Number) Timeout in seconds for each Prometheus API request. Default: `10`.
32+
* `prometheus_scrape_timeout`: (Number) Time in seconds to poll for a positive collected LLM call counter. Default: `120`.
2933

3034
### Usage
3135

‎playbooks/run_lightspeed_tests.yaml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
lightspeed_timeout: 30
99
# Match the Goose container's LIGHTSPEED_MODEL for the Goose API tests.
1010
lightspeed_model: ""
11+
# Required when pytest_args selects telemetry tests.
12+
prometheus_url: ""
13+
prometheus_ca_cert_path: ""
14+
prometheus_api_timeout: 10
15+
prometheus_scrape_timeout: 120
1116
test_question: "What is OpenStack?"
1217
pytest_args: "-v --tb=short --color=yes"
1318
# Test-operator provides writable storage at this location
@@ -94,6 +99,10 @@
9499
RHOS_LIGHTSPEED_URL: "{{ lightspeed_url }}"
95100
RHOS_LIGHTSPEED_TOKEN: "{{ lightspeed_token }}"
96101
RHOS_LIGHTSPEED_MODEL: "{{ lightspeed_model }}"
102+
RHOS_LIGHTSPEED_PROMETHEUS_URL: "{{ prometheus_url }}"
103+
RHOS_LIGHTSPEED_PROMETHEUS_CA_CERT_PATH: "{{ prometheus_ca_cert_path }}"
104+
RHOS_LIGHTSPEED_PROMETHEUS_API_TIMEOUT: "{{ prometheus_api_timeout | string }}"
105+
RHOS_LIGHTSPEED_PROMETHEUS_SCRAPE_TIMEOUT: "{{ prometheus_scrape_timeout | string }}"
97106
PYTHONUNBUFFERED: "1"
98107
PATH: "{{ ansible_env.HOME }}/.local/bin:{{ ansible_env.PATH }}"
99108

‎src/rhos_lightspeed_tests/config.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,25 @@ def load_config(config_path: str | None = None) -> dict:
2626
goose = config.setdefault("goose", {})
2727
goose["model"] = os.environ.get("RHOS_LIGHTSPEED_MODEL", goose.get("model", ""))
2828

29+
prometheus = config.setdefault("prometheus", {})
30+
prometheus["url"] = os.environ.get(
31+
"RHOS_LIGHTSPEED_PROMETHEUS_URL", prometheus.get("url", "")
32+
)
33+
prometheus["ca_cert_path"] = os.environ.get(
34+
"RHOS_LIGHTSPEED_PROMETHEUS_CA_CERT_PATH",
35+
prometheus.get("ca_cert_path", ""),
36+
)
37+
prometheus["api_timeout"] = float(
38+
os.environ.get(
39+
"RHOS_LIGHTSPEED_PROMETHEUS_API_TIMEOUT",
40+
prometheus.get("api_timeout", 10),
41+
)
42+
)
43+
prometheus["scrape_timeout"] = float(
44+
os.environ.get(
45+
"RHOS_LIGHTSPEED_PROMETHEUS_SCRAPE_TIMEOUT",
46+
prometheus.get("scrape_timeout", 120),
47+
)
48+
)
49+
2950
return config

‎src/rhos_lightspeed_tests/default.yaml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ query:
1010
goose:
1111
# Set RHOS_LIGHTSPEED_MODEL to the Goose container's LIGHTSPEED_MODEL value.
1212
model: ""
13+
14+
prometheus:
15+
# Required when running tests with -m telemetry.
16+
url: ""
17+
# Optional PEM CA bundle; empty uses Requests' default trust store.
18+
ca_cert_path: ""
19+
# Timeout in seconds for each Prometheus API request.
20+
api_timeout: 10
21+
scrape_timeout: 120
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
[pytest]
22
# Used only when no test path, module or node ID is supplied on the command line.
33
testpaths = rhos_lightspeed_tests.tests
4-
addopts = --verbose --junitxml=test-results.xml
4+
addopts = --verbose --junitxml=test-results.xml -m "not telemetry"
55
python_files = test_*.py
66
python_functions = test_*
77
markers =
8+
telemetry: requires telemetry-operator metrics collection and a configured Prometheus URL
89
llm_dependent: requires generated answer content from a real LLM
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""Verify that telemetry's Prometheus collects metrics from a Lightspeed query."""
2+
3+
import time
4+
5+
import pytest
6+
import requests
7+
8+
9+
def _get_llm_calls_total(prometheus: dict) -> float:
10+
"""Read the total collected LLM calls from telemetry's Prometheus."""
11+
query_url = prometheus["url"].rstrip("/") + "/api/v1/query"
12+
13+
with requests.get(
14+
query_url,
15+
params={"query": "sum(ls_llm_calls_total)"},
16+
verify=prometheus["ca_cert_path"] or True,
17+
timeout=prometheus["api_timeout"],
18+
) as response:
19+
response.raise_for_status()
20+
samples = response.json()["data"]["result"]
21+
22+
if not samples:
23+
return 0
24+
25+
_timestamp, value = samples[0]["value"]
26+
return float(value)
27+
28+
29+
@pytest.mark.telemetry
30+
def test_query_metrics_collected_by_telemetry(client, config):
31+
"""Send a query and check that telemetry has collected a positive LLM counter."""
32+
prometheus = config["prometheus"]
33+
34+
question = config["query"]["default_question"]
35+
with client.query(question) as response:
36+
response.raise_for_status()
37+
38+
deadline = time.monotonic() + prometheus["scrape_timeout"]
39+
llm_calls_total = 0
40+
while time.monotonic() < deadline:
41+
llm_calls_total = _get_llm_calls_total(prometheus)
42+
if llm_calls_total > 0:
43+
break
44+
time.sleep(5)
45+
46+
assert llm_calls_total > 0, (
47+
"Prometheus did not collect a positive ls_llm_calls_total"
48+
)

0 commit comments

Comments
 (0)