refactor: optimize Application Insights telemetry and prevent duplicate logging handlers#367
refactor: optimize Application Insights telemetry and prevent duplicate logging handlers#367Harmanpreet-Microsoft wants to merge 11 commits intodevfrom
Conversation
chore: sync psl-abhar with dev
There was a problem hiding this comment.
Pull request overview
Refactors the backend’s Application Insights / OpenTelemetry telemetry setup to simplify client initialization and avoid duplicate log exporting during repeated app creation, and updates event-tracking tests to match the new behavior.
Changes:
- Simplifies Application Insights
TelemetryClientinitialization by removing explicit synchronous channel setup. - Adds a guard to prevent adding duplicate OpenTelemetry
LoggingHandlerinstances to the root logger. - Updates
track_event_if_configuredtests by removingflush()assertions.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/backend/api/event_utils.py |
Simplifies TelemetryClient initialization and tweaks flush() call/comment formatting. |
src/backend/app.py |
Prevents duplicate OpenTelemetry logging handlers on the root logger. |
src/tests/backend/api/event_utils_test.py |
Removes flush() assertions from telemetry event tracking tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…ensure flush is called
There was a problem hiding this comment.
Pull request overview
Refactors backend telemetry wiring to simplify Application Insights client initialization and reduce duplicated OpenTelemetry log handlers, aiming to improve runtime behavior during reload/testing and streamline telemetry configuration.
Changes:
- Removed explicit synchronous Application Insights channel setup and rely on
TelemetryClientdefaults. - Added root logger
LoggingHandlerde-duplication logic to avoid accumulating duplicate OTel handlers. - Adjusted comments around event flushing behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/backend/app.py |
Removes existing OTel LoggingHandler instances from the root logger before adding a fresh one tied to the current logger_provider. |
src/backend/api/event_utils.py |
Simplifies TelemetryClient initialization by removing the explicit synchronous channel and keeps explicit flush() after event tracking. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Refactors backend telemetry and logging initialization to simplify Application Insights client setup and avoid duplicated OpenTelemetry log handlers when the app is created multiple times (e.g., tests/hot-reload).
Changes:
- Simplified Application Insights
TelemetryClientinitialization by removing explicit synchronous channel wiring. - Updated
create_app()logging setup to remove existing OpenTelemetryLoggingHandlerinstances before adding a new one. - Minor refactor/comment tweak around event flushing; test module imports were updated (but currently appear unused).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/backend/app.py |
Deduplicates OpenTelemetry LoggingHandler on the root logger during app startup. |
src/backend/api/event_utils.py |
Uses default TelemetryClient channel config instead of explicit synchronous channel types; keeps explicit flush. |
src/tests/backend/app_test.py |
Adds imports intended for telemetry/logging-related testing, but no assertions yet. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…eduplication test
… variable assignments and enhance error logging during cleanup
This pull request improves the reliability and correctness of telemetry and logging integration in the backend, focusing on Application Insights and OpenTelemetry. It ensures proper deduplication of logging handlers, simplifies telemetry client initialization, and updates tests for better coverage and realistic configuration.
Telemetry and logging improvements:
event_utils.pyto use the default (buffered/async) channel configuration, removing custom synchronous channel setup. This simplifies the code and aligns with best practices.app.pyto remove stale handlers before adding a new one, preventing duplicateLoggingHandlerinstances and potential resource leaks. The handler is safely closed if removed.Testing enhancements:
event_utils_test.pyto use a valid UUID format for the Application Insights instrumentation key, making tests more realistic and robust. [1] [2] [3] [4] [5]app_test.pyto verify that repeated calls tocreate_app()do not accumulate multipleLoggingHandlerinstances, ensuring proper deduplication and cleanup.app_test.pyfor logging and OpenTelemetry.Other minor changes:
event_utils.pyrelated to Application Insights channel configuration.track_event_if_configuredfor immediate sending of telemetry events.Does this introduce a breaking change?
Golden Path Validation
Deployment Validation
What to Check
Verify that the following are valid
Other Information