Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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()
Comment thread
Harmanpreet-Microsoft marked this conversation as resolved.
Comment thread
Harmanpreet-Microsoft marked this conversation as resolved.

logging.debug(f"Tracked custom event: {event_name} with data: {event_data}")
else:
Expand Down
7 changes: 5 additions & 2 deletions src/backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ 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)
# Guard against duplicate handlers in hot-reload or test scenarios
root_logger = logging.getLogger()
if not any(isinstance(h, LoggingHandler) for h in root_logger.handlers):
handler = LoggingHandler(logger_provider=logger_provider)
root_logger.addHandler(handler)
Comment thread
Harmanpreet-Microsoft marked this conversation as resolved.
Outdated

# Instrument ONLY FastAPI for HTTP request/response tracing
# This is safe because it only wraps HTTP handlers, not internal async operations
Expand Down
3 changes: 0 additions & 3 deletions src/tests/backend/api/event_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def test_track_event_with_instrumentation_key(self):
track_event_if_configured("TestEvent", {"key": "value"})

mock_client.track_event.assert_called_once_with("TestEvent", properties={"key": "value"})
mock_client.flush.assert_called_once()

Comment thread
Harmanpreet-Microsoft marked this conversation as resolved.
def test_track_event_without_instrumentation_key(self):
"""Test tracking event when instrumentation key is not set."""
Expand All @@ -45,7 +44,6 @@ def test_track_event_with_empty_data(self):
track_event_if_configured("TestEvent", {})

mock_client.track_event.assert_called_once_with("TestEvent", properties={})
mock_client.flush.assert_called_once()

Comment thread
Harmanpreet-Microsoft marked this conversation as resolved.
def test_track_event_with_complex_data(self):
"""Test tracking event with complex data."""
Expand Down Expand Up @@ -73,7 +71,6 @@ def test_track_event_with_complex_data(self):
}

mock_client.track_event.assert_called_once_with("ComplexEvent", properties=expected_properties)
Comment thread
Harmanpreet-Microsoft marked this conversation as resolved.
mock_client.flush.assert_called_once()

def test_track_event_client_returns_none(self):
"""Test tracking event when client initialization fails."""
Expand Down
Loading