Skip to content
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
19 changes: 17 additions & 2 deletions src/api/common/logging/span_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""

import logging
from urllib.parse import urlparse

from opentelemetry.sdk.trace import SpanProcessor, ReadableSpan
from opentelemetry.trace import SpanContext, TraceFlags

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


def _is_cosmos_host(span_name: str) -> bool:
"""
Safely determine if the span name contains a Cosmos DB host.
"""
try:
parsed = urlparse(span_name)
host = parsed.hostname or ""

return host == "documents.azure.com" or host.endswith(".documents.azure.com")
except Exception as e:
logger.debug("Failed to parse span name '%s': %s", span_name, e)
return False


class DropASGIResponseBodySpanProcessor(SpanProcessor):
"""
Filters out ASGI http.response.body internal dependency spans from Application Insights.
Expand Down Expand Up @@ -72,8 +88,7 @@ def on_end(self, span: ReadableSpan) -> None:
span_name = span.name or ""
if (
attrs.get("db.system") == "cosmosdb"
or ".documents.azure.com" in span_name
or span_name.endswith("documents.azure.com")
or _is_cosmos_host(span_name)
):
_unsample(span)

Expand Down
Loading