Azure Function (Python) Application Insights
I have some Python Azure Functions and recently I decided to review and improve the logging mechanism for them (Azure Application Insights integration). What I wanted to acheave is to have visual spans for individual function calls that can be nested and give the better idea of the function call behavior.
Long story short (Solution):
Use azure-monitor-opentelemetry python package:
import logging
import requests
import azure.functions as func
import opentelemetry.trace
from azure.monitor.opentelemetry import configure_azure_monitor
configure_azure_monitor()
tracer: opentelemetry.trace.Tracer = opentelemetry.trace.get_tracer(__name__)
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
@app.timer_trigger(schedule="0 * * * * *", arg_name="timer")
async def timer_trigger(timer: func.TimerRequest) -> None:
logging.info("Call: TimerTrigger Started")
await RunBusinessLogic()
logging.info("Call: TimerTrigger Ended")
@app.function_name(name="GetRecord")
@app.route(route="GetRecord", methods=["GET"])
async def GetRecord(req: func.HttpRequest) -> func.HttpResponse:
logging.info("Call: GetRecord Started")
await RunBusinessLogic()
logging.info("Call: GetRecord Ended")
return func.HttpResponse("Done", status_code=200)
async def RunBusinessLogic() -> None:
with tracer.start_as_current_span("My Business Logic"):
with tracer.start_as_current_span("HTTP Request to Google"):
google_resp = requests.get(url='https://google.com')
logging.info(f"google response status code {google_resp.status_code}")
with tracer.start_as_current_span("HTTP Request to Microsoft"):
msft_resp = requests.get(url='https://www.microsoft.com')
logging.info(f"msft response status code {msft_resp.status_code}")
And make sure you set OTEL_LOGS_EXPORTER
Environment Variable (App Setting) to None
as it is described here: Duplicate trace logs in Azure Functions. If you do not do this your trace logs will be duplicated.
Here is how it should be:
That’s it.
Longer story (also short) with links:
There is no automatic dependency and transaction diagnostic auto collection. There are also no samples of how to track dependencies manually for python Azure Functions. The Azure Functions Python Developer Reference Guide describes how to Log Custom Telemetry using OpenCensus library. You can also find samples of how to use this library Monitor a distributed system by using Application Insights and OpenCensus. However… you should not use it because Azure Monitor support for OpenCensus will end on 30 September 2024 - transition to using Azure Monitor OpenTelemetry Python Distro. Here is the guide for Migrating from OpenCensus Python SDK and Azure Monitor OpenCensus exporter for Python to Azure Monitor OpenTelemetry Python Distro and step-by-step guide: Enable Azure Monitor OpenTelemetry for .NET, Node.js, Python, and Java applications