File tree Expand file tree Collapse file tree 2 files changed +21
-15
lines changed
Expand file tree Collapse file tree 2 files changed +21
-15
lines changed Original file line number Diff line number Diff line change 4343from app_config import config
4444
4545# # Check if the Application Insights Instrumentation Key is set in the environment variables
46- # instrumentation_key = os.getenv("APPLICATIONINSIGHTS_INSTRUMENTATION_KEY")
47- # if instrumentation_key:
48- # # Configure Application Insights if the Instrumentation Key is found
49- # configure_azure_monitor(connection_string=instrumentation_key)
50- # logging.info("Application Insights configured with the provided Instrumentation Key")
51- # else:
52- # # Log a warning if the Instrumentation Key is not found
53- # logging.warning("No Application Insights Instrumentation Key found. Skipping configuration")
46+ instrumentation_key = os .getenv ("APPLICATIONINSIGHTS_INSTRUMENTATION_KEY" )
47+ if instrumentation_key :
48+ # Configure Application Insights if the Instrumentation Key is found
49+ configure_azure_monitor (connection_string = instrumentation_key )
50+ logging .info (
51+ "Application Insights configured with the provided Instrumentation Key"
52+ )
53+ else :
54+ # Log a warning if the Instrumentation Key is not found
55+ logging .warning (
56+ "No Application Insights Instrumentation Key found. Skipping configuration"
57+ )
5458
5559# Configure logging
5660logging .basicConfig (level = logging .INFO )
6266logging .getLogger ("azure.identity.aio._internal" ).setLevel (logging .WARNING )
6367
6468# # Suppress info logs from OpenTelemetry exporter
65- # logging.getLogger("azure.monitor.opentelemetry.exporter.export._base").setLevel(
66- # logging.WARNING
67- # )
69+ logging .getLogger ("azure.monitor.opentelemetry.exporter.export._base" ).setLevel (
70+ logging .WARNING
71+ )
6872
6973# Initialize the FastAPI app
7074app = FastAPI ()
Original file line number Diff line number Diff line change 55
66def track_event_if_configured (event_name : str , event_data : dict ):
77 """Track an event if Application Insights is configured.
8-
8+
99 This function safely wraps the Azure Monitor track_event function
1010 to handle potential errors with the ProxyLogger.
11-
11+
1212 Args:
1313 event_name: The name of the event to track
1414 event_data: Dictionary of event data/dimensions
@@ -17,8 +17,10 @@ def track_event_if_configured(event_name: str, event_data: dict):
1717 instrumentation_key = os .getenv ("APPLICATIONINSIGHTS_CONNECTION_STRING" )
1818 if instrumentation_key :
1919 track_event (event_name , event_data )
20- # else:
21- # logging.warning(f"Skipping track_event for {event_name} as Application Insights is not configured")
20+ else :
21+ logging .warning (
22+ f"Skipping track_event for { event_name } as Application Insights is not configured"
23+ )
2224 except AttributeError as e :
2325 # Handle the 'ProxyLogger' object has no attribute 'resource' error
2426 logging .warning (f"ProxyLogger error in track_event: { e } " )
You can’t perform that action at this time.
0 commit comments