[Python] Azure function Open Telemetry

Discussão em 'Python' iniciado por Stack, Março 3, 2025.

  1. Stack

    Stack Membro Participativo

    I currently have an Azure APIM which uses the event-metric policy to send custom metrics to application insights:

    <emit-metric name="appid-info" namespace="app-stats">
    <dimension name="api-name" value="@(context.Api.Name)" />
    <dimension name="app-id" value="@(context.Variables.GetValueOrDefault<string>("appid"))" />
    </emit-metric>


    This is just a small sample.

    I am starting to using a function app which is called from the APIM, and I want to emit the same metrics in the python function app as I do in the APIM:

    https://learn.microsoft.com/en-us/p...=azure-python-preview#metric-instrument-usage

    I start by using the import:

    from azure.monitor.opentelemetry.exporter import AzureMonitorLogExporter


    Then I created a function which can be called:

    def log_to_app_insights():
    """Log data to Azure Application Insights"""
    try:
    exporter = AzureMonitorMetricExporter(
    connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
    )

    except Exception as e:
    logging.error(f"Failed to log to Application Insights: {str(e)}")


    Note that I have set the connection string as an env variable on the function.

    What do I need to do next - I want to be able to call this function at the end of the function execution (preferably async).

    But the MS example above mentioned Periodic Readers and timers etc. I just want to emit the custom metrics to my namespace from the function so they appear as the same way as the emit-metrics.

    Is this possible?

    Continue reading...

Compartilhe esta Página