Skip to content

Commit

Permalink
Emit warning on incorrect Objective name (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
actualwitch authored Nov 14, 2023
1 parent 35798a1 commit a0725ca
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Prometheus exporters are now configured via `init` function (#89)
- Updated examples to call `init` function (#94)
- Updated `docker compose` / `tilt` config in repo to include grafana with our dashboards (#94)
- `Objective`s will now emit a warning when name contains characters other than alphanumeric and dash (#99)

### Deprecated

Expand Down
9 changes: 9 additions & 0 deletions src/autometrics/objectives.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import logging

from enum import Enum
from re import match
from typing import Optional, Tuple


Expand Down Expand Up @@ -89,3 +92,9 @@ def __init__(
self.name = name
self.success_rate = success_rate
self.latency = latency

# Check that name only contains alphanumeric characters and hyphens
if match(r"^[\w-]+$", name) is None:
logging.getLogger().warning(
f"Objective name '{name}' contains invalid characters. Only alphanumeric characters and hyphens are allowed."
)
16 changes: 16 additions & 0 deletions src/autometrics/test_objectives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import logging

from autometrics.objectives import Objective


def test_objective_name_warning(caplog):
"""Test that a warning is logged when an objective name contains invalid characters."""
caplog.set_level(logging.WARNING)
caplog.clear()
Objective("Incorrect name.")
assert len(caplog.records) == 1
assert caplog.records[0].levelname == "WARNING"
assert "contains invalid characters" in caplog.records[0].message
caplog.clear()
Objective("correct-name-123")
assert len(caplog.records) == 0

0 comments on commit a0725ca

Please sign in to comment.