Skip to content

Emit warning on incorrect Objective name #99

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

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


@@ -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