Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 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
16 changes: 14 additions & 2 deletions src/backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,20 @@ 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):
Comment thread
Harmanpreet-Microsoft marked this conversation as resolved.
root_logger.removeHandler(handler)
Comment thread
Harmanpreet-Microsoft marked this conversation as resolved.
# Close the handler to release any associated exporter/background resources
try:
handler.close()
except Exception as exc:
# Guard against unexpected close failures
logging.error("Error while closing logging handler %r: %s", handler, exc)
# Add a fresh handler bound to the current logger_provider
root_logger.addHandler(LoggingHandler(logger_provider=logger_provider))
Comment thread
Harmanpreet-Microsoft marked this conversation as resolved.

# Instrument ONLY FastAPI for HTTP request/response tracing
# This is safe because it only wraps HTTP handlers, not internal async operations
Expand Down
48 changes: 48 additions & 0 deletions src/tests/backend/app_test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# pylint: disable=redefined-outer-name
"""Tests for the FastAPI application."""

import logging
Comment thread
github-code-quality[bot] marked this conversation as resolved.
Fixed
import os
Comment thread
github-code-quality[bot] marked this conversation as resolved.
Fixed

from backend.app import create_app

from fastapi import FastAPI

from httpx import ASGITransport
from httpx import AsyncClient

from opentelemetry.sdk._logs import LoggingHandler
Comment thread
github-code-quality[bot] marked this conversation as resolved.
Fixed

Comment thread
Harmanpreet-Microsoft marked this conversation as resolved.
import pytest


Expand All @@ -34,3 +39,46 @@ async def test_backend_routes_exist(app: FastAPI):
routes = [route.path for route in app.router.routes]
backend_routes = [r for r in routes if r.startswith("/api")]
assert backend_routes, "No backend routes found under /api prefix"


def test_logging_handler_deduplication():
"""Test that creating multiple apps doesn't accumulate LoggingHandler instances."""
# Set up Application Insights connection string to trigger telemetry setup
connection_string = "InstrumentationKey=test-key;IngestionEndpoint=https://test.com"
original_env = os.environ.get("APPLICATIONINSIGHTS_CONNECTION_STRING")

try:
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] = connection_string

# Get root logger and count existing LoggingHandlers
root_logger = logging.getLogger()
initial_handler_count = sum(1 for h in root_logger.handlers if isinstance(h, LoggingHandler))

# Create first app
app1 = create_app()
Comment thread
github-code-quality[bot] marked this conversation as resolved.
Fixed
Comment thread
github-code-quality[bot] marked this conversation as resolved.
Fixed
handler_count_after_first = sum(1 for h in root_logger.handlers if isinstance(h, LoggingHandler))

# Create second app
app2 = create_app()
Comment thread
github-code-quality[bot] marked this conversation as resolved.
Fixed
Comment thread
github-code-quality[bot] marked this conversation as resolved.
Fixed
handler_count_after_second = sum(1 for h in root_logger.handlers if isinstance(h, LoggingHandler))

# Assert only one LoggingHandler exists after multiple create_app() calls
assert handler_count_after_first == initial_handler_count + 1, \
"First create_app() should add one LoggingHandler"
assert handler_count_after_second == handler_count_after_first, \
"Second create_app() should not add another LoggingHandler (de-duplication should work)"

# Clean up - remove the handler we added
for handler in list(root_logger.handlers):
if isinstance(handler, LoggingHandler):
root_logger.removeHandler(handler)
try:
handler.close()
except Exception:
Comment thread
github-code-quality[bot] marked this conversation as resolved.
Fixed
Comment thread
github-code-quality[bot] marked this conversation as resolved.
Fixed
pass
finally:
# Restore original environment
if original_env is not None:
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] = original_env
else:
os.environ.pop("APPLICATIONINSIGHTS_CONNECTION_STRING", None)
Loading