Monitoring Azure Functionsedit

Prerequisitesedit

You need an APM Server to which you can send APM data. Follow the APM Quick start if you have not set one up yet. For the best-possible performance, we recommend setting up APM on Elastic Cloud in the same Azure region as your Azure Functions app.

Currently, only HTTP and timer triggers are supported. Other trigger types may be captured as well, but the amount of captured contextual data may differ.

Step 1: Enable Worker Extensionsedit

Elastic APM uses Worker Extensions to instrument Azure Functions. This feature is not enabled by default, and must be enabled in your Azure Functions App. Please follow the instructions in the Azure docs.

Once you have enabled Worker Extensions, these two lines of code will enable Elastic APM’s extension:

from elasticapm.contrib.serverless.azure import ElasticAPMExtension

ElasticAPMExtension.configure()

Put them somewhere at the top of your Python file, before the function definitions.

Step 2: Install the APM Python Agentedit

You need to add elastic-apm as a dependency for your Functions app. Simply add elastic-apm to your requirements.txt file. We recommend pinning the version to the current newest version of the agent, and periodically updating the version.

Step 3: Configure APM on Azure Functionsedit

The APM Python agent is configured through App Settings. These are then picked up by the agent as environment variables.

For the minimal configuration, you will need the ELASTIC_APM_SERVER_URL to set the destination for APM data and a ELASTIC_APM_SECRET_TOKEN. If you prefer to use an APM API key instead of the APM secret token, use the ELASTIC_APM_API_KEY environment variable instead of ELASTIC_APM_SECRET_TOKEN in the following example configuration.

$ az functionapp config appsettings set --settings ELASTIC_APM_SERVER_URL=https://example.apm.northeurope.azure.elastic-cloud.com:443
$ az functionapp config appsettings set --settings ELASTIC_APM_SECRET_TOKEN=verysecurerandomstring

You can optionally fine-tune the Python agent.

That’s it; Once the agent is installed and working, spans will be captured for supported technologies. You can also use capture_span to capture custom spans, and you can retrieve the Client object for capturing exceptions/messages using get_client.