Skip to content

Commit 943fe53

Browse files
bm1549claude
andauthored
Fix flaky runtime-metrics tests with condition-based collection wait (#7231)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 7128a92 commit 943fe53

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

tests/test_config_consistency.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ class Test_Config_RuntimeMetrics_Enabled:
662662

663663
def setup_main(self):
664664
self.req = weblog.get("/")
665+
wait_for_runtime_metrics()
665666

666667
def test_main(self):
667668
assert self.req.status_code == 200
@@ -696,6 +697,7 @@ class Test_Config_RuntimeMetrics_Enabled_WithRuntimeId:
696697

697698
def setup_main(self):
698699
self.req = weblog.get("/")
700+
wait_for_runtime_metrics()
699701

700702
def test_main(self):
701703
assert self.req.status_code == 200
@@ -746,6 +748,18 @@ def get_runtime_metrics():
746748
return runtime_metrics_gauges, runtime_metrics_sketches
747749

748750

751+
RUNTIME_METRICS_WAIT_TIMEOUT = 60
752+
753+
754+
def wait_for_runtime_metrics() -> None:
755+
# Runtime metrics are emitted on an interval, so wait (while the weblog is still up) until at least one
756+
# reaches the agent instead of relying on the fixed collection window, which is racy.
757+
interfaces.agent.wait_for(
758+
lambda _: any(len(metrics) > 0 for metrics in get_runtime_metrics()),
759+
timeout=RUNTIME_METRICS_WAIT_TIMEOUT,
760+
)
761+
762+
749763
def parse_log_injection_message(log_message: str) -> dict:
750764
if context.library == "ruby":
751765
# TODO: Update ruby weblog app to support structured logs

tests/test_otlp_runtime_metrics.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,30 @@ def get_runtime_metrics_by_name() -> dict[str, list[dict[str, str]]]:
251251
return result
252252

253253

254+
RUNTIME_METRICS_WAIT_TIMEOUT = 60
255+
256+
257+
def wait_for_runtime_metrics(library: str) -> None:
258+
# OTLP metrics are exported on an interval, so wait (while the weblog is still up) until every expected
259+
# metric for this library reaches the agent instead of relying on the fixed collection window.
260+
expected = EXPECTED_METRICS.get(library)
261+
if not expected:
262+
return
263+
264+
interfaces.agent.wait_for(
265+
lambda _: expected.keys() <= get_runtime_metrics_by_name().keys(),
266+
timeout=RUNTIME_METRICS_WAIT_TIMEOUT,
267+
)
268+
269+
254270
@scenarios.otlp_runtime_metrics
255271
@features.runtime_metrics
256272
class Test_OtlpRuntimeMetrics:
257273
"""Verify runtime metrics are sent via OTLP with OTel names, not DD-proprietary names."""
258274

259275
def setup_otel_metrics_are_present_and_attributed(self) -> None:
260276
self.req = weblog.get("/")
277+
wait_for_runtime_metrics(context.library.name)
261278

262279
def test_otel_metrics_are_present_and_attributed(self) -> None:
263280
assert self.req.status_code == 200

0 commit comments

Comments
 (0)