Skip to content

Commit

Permalink
Add sanitize_metric_name
Browse files Browse the repository at this point in the history
  • Loading branch information
idrissneumann committed Sep 11, 2024
1 parent 0183eaa commit 894e589
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.1
4.0.2
4 changes: 4 additions & 0 deletions src/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ def is_uuid (var):

def is_not_uuid (var):
return not is_uuid(var)

_allowed_chars_pattern = re.compile(r'[^a-zA-Z0-9_\-\.]')
def sanitize_metric_name(name: str):
return re.sub(_allowed_chars_pattern, '_', name)
2 changes: 2 additions & 0 deletions src/utils/counter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from prometheus_client import Counter

from utils.cid import get_current_cid
from utils.common import sanitize_metric_name
from utils.otel import get_otel_meter

def create_counter(name, description):
name = sanitize_metric_name(name)
return {
'otel': get_otel_meter().create_counter(
name = name,
Expand Down
2 changes: 2 additions & 0 deletions src/utils/gauge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from prometheus_client import Gauge
from opentelemetry.metrics import Observation

from utils.common import sanitize_metric_name
from utils.otel import get_otel_meter

_numeric_value_pattern = r"-?\d+\.\d+"
_current_gauge_values = {}

def create_gauge(name, description):
name = sanitize_metric_name(name)
_current_gauge_values[name] = 0.0

def observable_gauge_func(_):
Expand Down

0 comments on commit 894e589

Please sign in to comment.