Skip to content

feat: improve Application Insights logging and telemetry handling#811

Open
Abdul-Microsoft wants to merge 5 commits intodevfrom
psl-logging-improvements
Open

feat: improve Application Insights logging and telemetry handling#811
Abdul-Microsoft wants to merge 5 commits intodevfrom
psl-logging-improvements

Conversation

@Abdul-Microsoft
Copy link
Contributor

Purpose

  • This pull request enhances observability and error tracking across the API by adding detailed telemetry events and improving trace correlation, while also centralizing and improving the configuration of Azure Monitor and OpenTelemetry. Additionally, it standardizes logging practices for better maintainability and debugging.

Telemetry and Error Tracking Enhancements:

  • Added track_event_if_configured calls to all major API endpoints in both api_routes.py and history_routes.py to log structured telemetry events on errors, improving monitoring and diagnostics. Each event includes error details and type for better filtering and analysis. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
  • Tracked successful chat request initiation and attached conversation_id as a span attribute for correlation in conversation and relevant history endpoints, enabling better traceability in Application Insights. [1] [2] [3] [4] [5] [6]

Centralized Azure Monitor & OpenTelemetry Configuration:

  • Moved Azure Monitor and FastAPI OpenTelemetry instrumentation from individual route files (api_routes.py, history_routes.py) to a centralized location in app.py. Now, configuration is performed once at app startup, enabling live metrics, automatic request tracing, and reducing redundant/noisy logs. [1] [2]
  • Suppressed noisy logs from OpenTelemetry and Azure Monitor libraries to improve log clarity.

Logging Improvements:

  • Standardized logging statements to use parameterized logging instead of f-strings, improving performance and readability across helpers and routes. [1] [2] [3] [4]
  • Removed redundant or outdated Azure Monitor configuration code from route files, reducing duplication and potential misconfiguration. [1] [2]

Overall, these changes improve the reliability, observability, and maintainability of the API codebase.

Does this introduce a breaking change?

  • Yes
  • No

Golden Path Validation

  • I have tested the primary workflows (the "golden path") to ensure they function correctly without errors.

Deployment Validation

  • I have validated the deployment process successfully and all services are running as expected with this change.

What to Check

Verify that the following are valid

  • ...

Other Information

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves API observability by centralizing Azure Monitor/OpenTelemetry setup in the FastAPI app startup and adding richer, structured telemetry events (success + error) across key API and history endpoints, while also standardizing logging to parameterized formats.

Changes:

  • Centralizes configure_azure_monitor + FastAPI OpenTelemetry instrumentation in app.py and removes per-route configuration.
  • Adds track_event_if_configured(...) calls to multiple endpoints (including error paths) and adds conversation_id as an OpenTelemetry span attribute for correlation.
  • Replaces f-string logging with parameterized logging in services/helpers/routes.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/api/app.py Centralizes Azure Monitor + FastAPI OpenTelemetry configuration and suppresses noisy library loggers.
src/api/api/api_routes.py Adds telemetry events on error paths and correlates traces via conversation_id; logging tweaks.
src/api/api/history_routes.py Adds error telemetry events and correlates traces via conversation_id; removes per-route AI config.
src/api/helpers/chat_helper.py Converts some logs to parameterized logging in chart processing helper.
src/api/services/history_service.py Converts service logging to parameterized logging and adjusts exception logging.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves observability in the FastAPI API by centralizing Azure Monitor/OpenTelemetry setup and adding structured Application Insights custom events + span correlation (conversation_id) across key endpoints, while also standardizing logging to parameterized style.

Changes:

  • Centralizes Azure Monitor configuration and FastAPI OpenTelemetry instrumentation in app.py, and suppresses noisy Azure/OTel logs.
  • Adds track_event_if_configured calls for error tracking (and some success/init events) across api_routes.py and history_routes.py, plus span attributes for correlation.
  • Replaces f-string logging with parameterized logging in several helpers/services.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
src/api/app.py Centralized Azure Monitor + FastAPI instrumentation and log-level suppression.
src/api/common/logging/event_utils.py Removes stdout print, adds docstring, parameterizes warning log.
src/api/api/api_routes.py Adds custom telemetry events and improves logging formatting for key endpoints.
src/api/api/history_routes.py Adds telemetry events and sets conversation_id on spans across history endpoints.
src/api/services/history_service.py Standardizes logging to parameterized form in history service methods.
src/api/helpers/chat_helper.py Standardizes logging to parameterized form for chart-generation flow.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants