File: live_metrics.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (40 lines) | stat: -rw-r--r-- 1,263 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""
This example shows how configure live metrics to be enabled. It sets up a minimal example of sending dependency,
trace and exception telemetry to demonstrate the capabilities and collection set of live metrics.
"""
import logging
import requests
import time

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace

from opentelemetry.sdk.resources import Resource

configure_azure_monitor(
    resource=Resource.create(
        {
            "service.name": "live_metrics_service",
            "service.instance.id": "qp_instance_id",
        }
    ),
    logger_name=__name__,
    enable_live_metrics=True,  # Enable live metrics configuration
)

tracer = trace.get_tracer(__name__)
logger = logging.getLogger(__name__)

# Continuously send metrics
while True:
    with tracer.start_as_current_span("parent"):
        logger.warning("sending request")
        response = requests.get("https://azure.microsoft.com/", timeout=5)
        try:
            val = 1 / 0
            print(val)
        except ZeroDivisionError:
            logger.error("Error: Division by zero", stack_info=True, exc_info=True)
    time.sleep(2)