fix: fixed app insights logging issue#753
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds Azure Application Insights (via Azure Monitor OpenTelemetry) initialization to the backend entrypoint so the service can emit telemetry when an Application Insights connection string is provided.
Changes:
- Import
configure_azure_monitorand conditionally configure Azure Monitor at startup based onAPPLICATIONINSIGHTS_CONNECTION_STRING. - Add startup log lines indicating whether Application Insights was configured.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
content-gen/src/backend/app.py
Outdated
| # Check if the Application Insights Instrumentation Key is set in the environment variables | ||
| instrumentation_key = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING") | ||
| if instrumentation_key: | ||
| # Configure Application Insights if the Instrumentation Key is found | ||
| configure_azure_monitor(connection_string=instrumentation_key) | ||
| logging.info("Application Insights configured with the provided Instrumentation Key") | ||
| else: | ||
| # Log a warning if the Instrumentation Key is not found | ||
| logging.warning("No Application Insights Instrumentation Key found. Skipping configuration") |
There was a problem hiding this comment.
The comments/log messages and variable name refer to an "Instrumentation Key", but the code is reading APPLICATIONINSIGHTS_CONNECTION_STRING and passes it as connection_string. Please rename instrumentation_key to something like appinsights_connection_string and update the comments/messages to consistently refer to a connection string (or change the env var if you truly intend to use an instrumentation key).
| # Check if the Application Insights Instrumentation Key is set in the environment variables | |
| instrumentation_key = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING") | |
| if instrumentation_key: | |
| # Configure Application Insights if the Instrumentation Key is found | |
| configure_azure_monitor(connection_string=instrumentation_key) | |
| logging.info("Application Insights configured with the provided Instrumentation Key") | |
| else: | |
| # Log a warning if the Instrumentation Key is not found | |
| logging.warning("No Application Insights Instrumentation Key found. Skipping configuration") | |
| # Check if the Application Insights connection string is set in the environment variables | |
| appinsights_connection_string = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING") | |
| if appinsights_connection_string: | |
| # Configure Application Insights if the connection string is found | |
| configure_azure_monitor(connection_string=appinsights_connection_string) | |
| logging.info("Application Insights configured with the provided connection string") | |
| else: | |
| # Log a warning if the connection string is not found | |
| logging.warning("No Application Insights connection string found. Skipping configuration") |
content-gen/src/backend/app.py
Outdated
| logging.info("Application Insights configured with the provided Instrumentation Key") | ||
| else: | ||
| # Log a warning if the Instrumentation Key is not found | ||
| logging.warning("No Application Insights Instrumentation Key found. Skipping configuration") |
There was a problem hiding this comment.
This file defines a module logger (logger = logging.getLogger(__name__)), but these new lines log via the root logging module. Please use the module logger (logger.info / logger.warning) so log records have the correct logger name and can be filtered/structured consistently with the rest of the file.
| logging.info("Application Insights configured with the provided Instrumentation Key") | |
| else: | |
| # Log a warning if the Instrumentation Key is not found | |
| logging.warning("No Application Insights Instrumentation Key found. Skipping configuration") | |
| logger.info("Application Insights configured with the provided Instrumentation Key") | |
| else: | |
| # Log a warning if the Instrumentation Key is not found | |
| logger.warning("No Application Insights Instrumentation Key found. Skipping configuration") |
Purpose
Azure Application Insights integration:
configure_azure_monitorfromazure.monitor.opentelemetryinapp.pyto support telemetry.APPLICATIONINSIGHTS_CONNECTION_STRINGenvironment variable; if present, the app configures Azure Application Insights for monitoring, otherwise it logs a warning.Does this introduce a breaking change?
Golden Path Validation
Deployment Validation
What to Check
Verify that the following are valid
Other Information