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
- From the Dynatrace menu, go to Settings > Preferences > OneAgent features.
- 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
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:
1receivers:2 otlp:3 protocols:4 grpc:5exporters:6 otlphttp:7 endpoint: "https://<YOUR-TENANT-ID>.live.dynatrace.com/api/v2/otlp"8 headers: {"Authorization": "Api-Token <YOUR-DYNATRACE-API-TOKEN>"}9service:10 pipelines:11 traces:12 receivers: [otlp]13 exporters: [otlphttp]
For further details on configuration, see Send OpenTelemetry trace data to Dynatrace.
Add dependencies
Add the following dependencies via NuGet to your project:
1OpenTelemetry.Exporter.OpenTelemetryProtocol
OpenTelemetry also provides other auto-instrumentation libraries available as NuGet packages.
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.
1public class Startup : FunctionsStartup2{3 public override void Configure(IFunctionsHostBuilder builder)4 {5 string activitySource = "[activitySource]";6 string serviceName = "[serviceName]";7 string collectorUrl = "[collectorUrl]" // Points to the running collector, configured before.89 builder.Services.AddSingleton((builder) =>10 {11 return Sdk.CreateTracerProviderBuilder()12 .SetSampler(new AlwaysOnSampler())13 .AddSource(activitySource)14 .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName))15 .AddHttpClientInstrumentation(op =>16 {17 // Exclude frequent calls generated by Azure Application Insights18 op.FilterHttpRequestMessage = (req) => !req.RequestUri.AbsoluteUri.Contains("visualstudio");19 })20 .AddOtlpExporter(otlpOptions =>21 {22 otlpOptions.Endpoint = new Uri(collectorUrl);23 })24 .Build();25 });26 }27}