Skip to content

Commit c8e4905

Browse files
fix: codeql issue fix - add function to determine if span name contains a Cosmos DB host
2 parents 2692455 + 40be236 commit c8e4905

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

src/api/common/logging/span_filters.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"""
44

55
import logging
6+
from urllib.parse import urlparse
7+
68
from opentelemetry.sdk.trace import SpanProcessor, ReadableSpan
79
from opentelemetry.trace import SpanContext, TraceFlags
810

@@ -24,6 +26,20 @@ def _unsample(span: ReadableSpan) -> None:
2426
logger.debug("Unable to unsample span %s: %s", span.name, e)
2527

2628

29+
def _is_cosmos_host(span_name: str) -> bool:
30+
"""
31+
Safely determine if the span name contains a Cosmos DB host.
32+
"""
33+
try:
34+
parsed = urlparse(span_name)
35+
host = parsed.hostname or ""
36+
37+
return host == "documents.azure.com" or host.endswith(".documents.azure.com")
38+
except Exception as e:
39+
logger.debug("Failed to parse span name '%s': %s", span_name, e)
40+
return False
41+
42+
2743
class DropASGIResponseBodySpanProcessor(SpanProcessor):
2844
"""
2945
Filters out ASGI http.response.body internal dependency spans from Application Insights.
@@ -72,8 +88,7 @@ def on_end(self, span: ReadableSpan) -> None:
7288
span_name = span.name or ""
7389
if (
7490
attrs.get("db.system") == "cosmosdb"
75-
or ".documents.azure.com" in span_name
76-
or span_name.endswith("documents.azure.com")
91+
or _is_cosmos_host(span_name)
7792
):
7893
_unsample(span)
7994

0 commit comments

Comments
 (0)