Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
14 changes: 5 additions & 9 deletions src/backend/api/event_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

# Third-party
from applicationinsights import TelemetryClient
from applicationinsights.channel import SynchronousQueue, SynchronousSender, TelemetryChannel

from dotenv import load_dotenv

load_dotenv()
Expand All @@ -28,12 +26,8 @@ def _get_telemetry_client():
instrumentation_key = parts.get('InstrumentationKey')

if instrumentation_key:
# Create a synchronous channel for immediate sending
sender = SynchronousSender()
queue = SynchronousQueue(sender)
channel = TelemetryChannel(None, queue)

_telemetry_client = TelemetryClient(instrumentation_key, channel)
# Use the default (buffered/async) channel configuration
_telemetry_client = TelemetryClient(instrumentation_key)
logging.info("Application Insights TelemetryClient initialized successfully")
else:
logging.error("Could not extract InstrumentationKey from connection string")
Expand All @@ -59,7 +53,9 @@ def track_event_if_configured(event_name: str, event_data: dict):

# Track the custom event
client.track_event(event_name, properties=properties)
client.flush() # Ensure immediate sending

# Flush to ensure events are sent immediately
client.flush()

logging.debug(f"Tracked custom event: {event_name} with data: {event_data}")
else:
Expand Down
10 changes: 8 additions & 2 deletions src/backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,14 @@ def create_app() -> FastAPI:
set_logger_provider(logger_provider)

# Attach OpenTelemetry handler to Python's root logger
handler = LoggingHandler(logger_provider=logger_provider)
logging.getLogger().addHandler(handler)
# Ensure we don't accumulate stale handlers or mismatch logger providers
root_logger = logging.getLogger()
# Remove any existing OpenTelemetry logging handlers to avoid duplicates
for handler in list(root_logger.handlers):
if isinstance(handler, LoggingHandler):
root_logger.removeHandler(handler)
# Add a fresh handler bound to the current logger_provider
root_logger.addHandler(LoggingHandler(logger_provider=logger_provider))

# Instrument ONLY FastAPI for HTTP request/response tracing
# This is safe because it only wraps HTTP handlers, not internal async operations
Expand Down
Loading