Trace Azure Functions with OpenTelemetry .NET

The OpenTelemetry Protocol (OTLP) exporters for .NET currently support gRPC and HTTP 1.1 with binary Protocol Buffers (Protobuf) payload transports. Supported corresponding protocol values are grpc and http/protobuf. Configuration options can be set either via environment variables or explicitly in code.

Prerequisites

The following prerequisites and limitations apply:

  • Dynatrace version 1.222+
  • W3C Trace Context is enabled
    1. Go to Settings > Preferences > OneAgent features.
    2. Turn on Send W3C Trace Context HTTP headers.

Instrument Azure Functions

Dynatrace uses OpenTelemetry Trace Ingest to provide end-to-end visibility to your Azure Functions.

To instrument your Azure Functions

Step 1 Set up export

To ingest gRPC via the Dynatrace Trace API, you need to use an OpenTelemetry collector between Dynatrace and the exporter.

If you use environment variables for setup, you need to set the following value:

  • For OTEL_EXPORTER_OTLP_PROTOCOL: grpc

Configure the OpenTelemetry Collector

To ingest gRPC via the Dynatrace Trace API, you need to use an OpenTelemetry collector between Dynatrace and the exporter.

The OpenTelemetry Collector is available as a Docker image. To use this collector to export trace data to Dynatrace, you need to customize the configuration using the OpenTelemetry OTLP exporter.

Here is a sample configuration file:

receivers:
otlp:
protocols:
grpc:
exporters:
otlphttp:
endpoint: "https://<YOUR-TENANT-ID>.live.dynatrace.com/api/v2/otlp"
headers: {"Authorization": "Api-Token <YOUR-DYNATRACE-API-TOKEN>"}
service:
pipelines:
traces:
receivers: [otlp]
exporters: [otlphttp]

For further details on configuration, see Best practices for OpenTelemetry traces.

Step 2 Add dependencies

Add the following dependencies via NuGet to your project:

OpenTelemetry.Exporter.OpenTelemetryProtocol

OpenTelemetry also provides other auto-instrumentation libraries available as NuGet packages.

Step 3 Instrument code with OpenTelemetry

If you don't set the Protocol property of the OtlpExporterOptions class via environment variables or in code, it will be initialized as OtlpExportProtocol.Grpc by default.

public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
string activitySource = "[activitySource]";
string serviceName = "[serviceName]";
string collectorUrl = "[collectorUrl]" // Points to the running collector, configured before.
builder.Services.AddSingleton((builder) =>
{
return Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.AddSource(activitySource)
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName))
.AddHttpClientInstrumentation(op =>
{
// Exclude frequent calls generated by Azure Application Insights
op.FilterHttpRequestMessage = (req) => !req.RequestUri.AbsoluteUri.Contains("visualstudio");
})
.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri(collectorUrl);
})
.Build();
});
}
}