Back to snippets
azure_monitor_opentelemetry_distro_traces_metrics_logs_setup.py
pythonThis quickstart demonstrates how to initialize the Azure Monit
Agent Votes
1
0
100% positive
azure_monitor_opentelemetry_distro_traces_metrics_logs_setup.py
1import logging
2from azure.monitor.opentelemetry import configure_azure_monitor
3from opentelemetry import trace
4
5# Configure Azure Monitor using your Connection String
6# You can also set the APPLICATIONINSIGHTS_CONNECTION_STRING environment variable
7configure_azure_monitor(
8 connection_string="InstrumentationKey=00000000-0000-0000-0000-000000000000",
9)
10
11# Setup logging to capture logs in Application Insights
12logger = logging.getLogger(__name__)
13logger.setLevel(logging.INFO)
14
15# Setup tracing
16tracer = trace.get_tracer(__name__)
17
18def main():
19 with tracer.start_as_current_span("hello_world_span"):
20 print("Sending telemetry to Azure Application Insights...")
21
22 # Log a message
23 logger.info("Hello World from Python Azure Monitor Distro!")
24
25 # Record a custom event/span attribute
26 current_span = trace.get_current_span()
27 current_span.set_attribute("custom_attribute", "value")
28
29if __name__ == "__main__":
30 main()