This is a cache of https://www.elastic.co/guide/en/apm/agent/dotnet/current/setup-azure-functions.html. It is a snapshot of the page at 2024-11-19T01:07:32.034+0000.
Azure Functions | APM .NET Agent Reference [1.x] | Elastic

Azure Functions

edit

The .NET APM Agent can trace function invocations in an Azure Functions app.

Prerequisites

edit

You need an APM Server to send APM data to. Follow the APM Quick start if you have not set one up yet. You will need your APM server URL and an APM server secret token (or API key) for configuring the APM agent below.

You will also need an Azure Function app to monitor. If you do not have an existing one, you can follow this Azure guide to create one.

You can also take a look at and use this Azure Functions example app with Elastic APM already integrated.

Currently, only .NET Azure Functions in an isolated worker process can be traced.

Step 1: Add the NuGet package

edit

Add the Elastic.Apm.Azure.Functions NuGet package to your Azure Functions project:

dotnet add package Elastic.Apm.Azure.Functions

Step 2: Add the tracing Middleware

edit

For the APM agent to trace Azure Functions invocations, the Elastic.Apm.Azure.Functions.ApmMiddleware must be used in your Azure Functions app.

using Elastic.Apm.Azure.Functions;
using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
	.ConfigureFunctionsWorkerDefaults(builder =>
	{
		builder.UseMiddleware<ApmMiddleware>();
	})
	.Build();

host.Run();

Step 3: Configure the APM agent

edit

The APM agent can be configured with environment variables. Using environment variables allows you to use application settings in the Azure Portal, enabling you to hide values and update settings without needing to re-deploy code.

Open Configuration > Application settings for your Function App in the Azure Portal and set:

ELASTIC_APM_SERVER_URL: <your APM server URL from the prerequisites step>
ELASTIC_APM_SECRET_TOKEN: <your APM secret token from the prerequisites step>

For example:

Configuring the APM Agent in the Azure Portal

Limitations

edit

Azure Functions instrumentation currently does not collect system metrics in the background because of a concern with unintentionally increasing Azure Functions costs (for Consumption plans).