Skip to content

Commit a0725ca

Browse files
authored
Emit warning on incorrect Objective name (#99)
1 parent 35798a1 commit a0725ca

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
2323
- Prometheus exporters are now configured via `init` function (#89)
2424
- Updated examples to call `init` function (#94)
2525
- Updated `docker compose` / `tilt` config in repo to include grafana with our dashboards (#94)
26+
- `Objective`s will now emit a warning when name contains characters other than alphanumeric and dash (#99)
2627

2728
### Deprecated
2829

src/autometrics/objectives.py

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import logging
2+
13
from enum import Enum
4+
from re import match
25
from typing import Optional, Tuple
36

47

@@ -89,3 +92,9 @@ def __init__(
8992
self.name = name
9093
self.success_rate = success_rate
9194
self.latency = latency
95+
96+
# Check that name only contains alphanumeric characters and hyphens
97+
if match(r"^[\w-]+$", name) is None:
98+
logging.getLogger().warning(
99+
f"Objective name '{name}' contains invalid characters. Only alphanumeric characters and hyphens are allowed."
100+
)

src/autometrics/test_objectives.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import logging
2+
3+
from autometrics.objectives import Objective
4+
5+
6+
def test_objective_name_warning(caplog):
7+
"""Test that a warning is logged when an objective name contains invalid characters."""
8+
caplog.set_level(logging.WARNING)
9+
caplog.clear()
10+
Objective("Incorrect name.")
11+
assert len(caplog.records) == 1
12+
assert caplog.records[0].levelname == "WARNING"
13+
assert "contains invalid characters" in caplog.records[0].message
14+
caplog.clear()
15+
Objective("correct-name-123")
16+
assert len(caplog.records) == 0

0 commit comments

Comments
 (0)