Skip to content

Commit 32ddf35

Browse files
committed
improve tag normalization
1 parent a123350 commit 32ddf35

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

ddtrace/internal/process_tags/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616

1717
log = get_logger(__name__)
1818

19+
_INVALID_CHARS_PATTERN = re.compile(r"[^a-z0-9/._-]")
20+
_CONSECUTIVE_UNDERSCORES_PATTERN = re.compile(r"_{2,}")
21+
1922

2023
def normalize_tag(value: str) -> str:
21-
return re.sub(r"[^a-z0-9/._-]", "_", value.lower())
24+
normalized = _INVALID_CHARS_PATTERN.sub("_", value.lower())
25+
normalized = _CONSECUTIVE_UNDERSCORES_PATTERN.sub("_", normalized)
26+
return normalized.strip("_")
2227

2328

2429
def generate_process_tags() -> Optional[str]:

tests/internal/test_process_tags.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,22 @@
1414
"input_tag,expected",
1515
[
1616
("HelloWorld", "helloworld"),
17-
("Hello@World!", "hello_world_"),
17+
("Hello@World!", "hello_world"),
1818
("HeLLo123", "hello123"),
1919
("hello world", "hello_world"),
2020
("a/b.c_d-e", "a/b.c_d-e"),
21-
("héllø", "h_ll_"),
21+
("héllø", "h_ll"),
2222
("", ""),
23-
("💡⚡️", "___"),
24-
("!foo@", "_foo_"),
23+
("💡⚡️", ""),
24+
("!foo@", "foo"),
2525
("123_abc.DEF-ghi/jkl", "123_abc.def-ghi/jkl"),
2626
("Env:Prod-Server#1", "env_prod-server_1"),
27+
("__hello__world__", "hello_world"),
28+
("___test___", "test"),
29+
("_leading", "leading"),
30+
("trailing_", "trailing"),
31+
("double__underscore", "double_underscore"),
32+
("test\x99\x8faaa", "test_aaa"),
2733
],
2834
)
2935
def test_normalize_tag(input_tag, expected):

0 commit comments

Comments
 (0)