Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion src/api/common/logging/event_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def track_event_if_configured(event_name: str, event_data: dict):
"""
instrumentation_key = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")
if instrumentation_key:
track_event(event_name, event_data)
try:
track_event(event_name, event_data)
except Exception:
logger.warning("Failed to track event '%s'", event_name, exc_info=True)
else:
logger.warning("Skipping track_event for %s: Application Insights is not configured", event_name)
25 changes: 17 additions & 8 deletions src/api/common/logging/span_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
Custom OpenTelemetry SpanProcessor filters to reduce telemetry noise in Application Insights.
"""

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

logger = logging.getLogger(__name__)


def _unsample(span: ReadableSpan) -> None:
"""Set trace_flags to 0 so BatchSpanProcessor skips exporting this span."""
span._context = SpanContext(
trace_id=span.context.trace_id,
span_id=span.context.span_id,
is_remote=span.context.is_remote,
trace_flags=TraceFlags(0),
trace_state=span.context.trace_state,
)
try:
span._context = SpanContext(
trace_id=span.context.trace_id,
span_id=span.context.span_id,
is_remote=span.context.is_remote,
trace_flags=TraceFlags(0),
trace_state=span.context.trace_state,
)
except (AttributeError, TypeError) as e:
# Gracefully handle SDK changes where _context might not be mutable
logger.debug("Unable to unsample span %s: %s", span.name, e)


class DropASGIResponseBodySpanProcessor(SpanProcessor):
Expand Down Expand Up @@ -62,9 +69,11 @@ def on_start(self, span, parent_context=None):

def on_end(self, span: ReadableSpan) -> None:
attrs = span.attributes or {}
span_name = span.name or ""
if (
attrs.get("db.system") == "cosmosdb"
or "documents.azure.com" in (span.name or "")
or ".documents.azure.com" in span_name
or span_name.endswith("documents.azure.com")
):
_unsample(span)

Expand Down
Loading