Skip to content

Commit d47437e

Browse files
📝 Add docstrings to codex/extend-.perf-budget.yml-with-navigation-thresholds (#285)
Docstrings generation was requested by @shayancoin. * #126 (comment) The following files were modified: * `tools/perf/check-canary-metrics.py` Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 238a3d4 commit d47437e

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

‎tools/perf/check-canary-metrics.py‎

100755100644
Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,23 @@
3636

3737

3838
def exit_success(message: str) -> None:
39+
"""
40+
Prints a message to standard output and exits the process with status code 0.
41+
42+
Parameters:
43+
message (str): Text to print before exiting.
44+
"""
3945
print(message)
4046
sys.exit(0)
4147

4248

4349
def exit_failure(message: str) -> None:
50+
"""
51+
Prints an error message to standard error and terminates the process with exit code 1.
52+
53+
Parameters:
54+
message (str): Error message to emit to stderr before exiting.
55+
"""
4456
print(message, file=sys.stderr)
4557
sys.exit(1)
4658

@@ -56,6 +68,15 @@ def exit_failure(message: str) -> None:
5668

5769

5870
def query_prometheus(url: str, query: str) -> Optional[float]:
71+
"""
72+
Query a Prometheus HTTP API for an instant vector and extract the first numeric value.
73+
74+
Returns:
75+
The numeric value from the first result row as a float, or `None` if Prometheus returned no results.
76+
77+
Raises:
78+
RuntimeError: If the HTTP request fails, Prometheus responds with a non-"success" status, or the response payload has an unexpected format.
79+
"""
5980
encoded_query = urllib.parse.urlencode({'query': query})
6081
endpoint = f"{url.rstrip('/')}/api/v1/query?{encoded_query}"
6182
try:
@@ -77,6 +98,16 @@ def query_prometheus(url: str, query: str) -> Optional[float]:
7798

7899

79100
def render_query(template: str, build: str) -> str:
101+
"""
102+
Substitutes the `$BUILD` placeholder in a query template with the provided build identifier.
103+
104+
Parameters:
105+
template (str): Prometheus query template containing the `$BUILD` placeholder.
106+
build (str): Build identifier to substitute into the template.
107+
108+
Returns:
109+
rendered (str): The query string with `$BUILD` replaced by `build`.
110+
"""
80111
return template.replace('$BUILD', build)
81112

82113

@@ -104,6 +135,20 @@ def evaluate_metric(
104135
unit: str,
105136
description: str,
106137
) -> MetricResult:
138+
"""
139+
Evaluate a single canary metric against an absolute threshold and an optional regression allowance.
140+
141+
Parameters:
142+
name (str): Human-readable metric identifier used in messages and the resulting MetricResult.name.
143+
template (str): Prometheus query template; the placeholder `$BUILD` will be replaced with the build SHA.
144+
threshold (float): Absolute budget value the current metric must be less than or equal to.
145+
regression_pct (float): Allowed relative increase over the previous build (e.g., 0.1 for 10%); ignored when previous baseline is near zero or unavailable.
146+
unit (str): Unit string appended to numeric values in human-readable messages (e.g., "ms", or empty).
147+
description (str): Short textual description stored on the MetricResult.description for reporting.
148+
149+
Returns:
150+
MetricResult: Populated result containing current and optional previous values, threshold and regression parameters, a pass/fail flag, and a human-readable message explaining the outcome.
151+
"""
107152
current_query = render_query(template, CURRENT_BUILD)
108153
current_value = query_prometheus(PROMETHEUS_URL, current_query)
109154
previous_value = None
@@ -161,6 +206,15 @@ def evaluate_metric(
161206

162207

163208
def maybe_check_tempo() -> Optional[MetricResult]:
209+
"""
210+
Check Tempo for traces slower than the configured threshold for the canary service.
211+
212+
If TEMPO_URL is unset the function prints a skip message and returns None. Otherwise it queries Tempo for any trace with duration at or above TEMPO_SLOW_TRACE_THRESHOLD_MS within TEMPO_LOOKBACK_SECONDS for TEMPO_SERVICE and produces a MetricResult summarizing whether slow traces were found.
213+
214+
Returns:
215+
MetricResult: a result named 'tempo_slow_traces' where `passed` is `False` if a slow trace was found and `current_value` is the slow-threshold in milliseconds (or `0.0` if none was found).
216+
None: if TEMPO_URL is not configured.
217+
"""
164218
if not TEMPO_URL:
165219
print('TEMPO_URL not provided; skipping trace regression checks.')
166220
return None
@@ -237,11 +291,28 @@ def maybe_check_tempo() -> Optional[MetricResult]:
237291

238292

239293
def write_junit(results_list: list[MetricResult]) -> None:
294+
"""
295+
Write a JUnit-format XML report summarizing the provided MetricResult entries and save it to the configured artifacts location.
296+
297+
Each MetricResult becomes a <testcase>; passing results include a <system-out> block with metric details, failing results include a <failure> element and the same details. The file is written to RESULT_DIR/JUNIT_FILENAME and a message with the written path is printed.
298+
299+
Parameters:
300+
results_list (list[MetricResult]): List of metric results to include in the JUnit report.
301+
"""
240302
tests = len(results_list)
241303
failures_count = len([result for result in results_list if not result.passed])
242304
suite_name = 'canary-budget'
243305

244306
def xml_escape(value: str) -> str:
307+
"""
308+
Escape characters in a string so it is safe to include in XML content.
309+
310+
Parameters:
311+
value (str): The raw string to escape.
312+
313+
Returns:
314+
str: The input string with XML special characters replaced by their entity equivalents (`&amp;`, `&quot;`, `&apos;`, `&lt;`, `&gt;`).
315+
"""
245316
return (
246317
value.replace('&', '&amp;')
247318
.replace('"', '&quot;')
@@ -292,4 +363,4 @@ def xml_escape(value: str) -> str:
292363
if failures:
293364
exit_failure('Canary metrics exceeded budgets; see log for details.')
294365

295-
exit_success('Canary metrics are within budgets.')
366+
exit_success('Canary metrics are within budgets.')

0 commit comments

Comments
 (0)