azure-functions-host
azure-functions-host copied to clipboard
Use different HTTP header for distributed trace context
Is your question related to a specific version? If so, please specify:
Azure Function (in-process), .NET 6
What language does your question apply to? (e.g. C#, JavaScript, Java, All)
C#
Question
Instead of the default traceparent
HTTP header I want to use another value, e.g. X-Correlation-Id
.
I tried using a custom DistributedContextPropagator
(and setting it in the Startup DistributedContextPropagator.Current = new MyCusomtPropagator();
), but it seems to be unused. At least its not executed when I trigger the HTTP function.
How should this be configured?
To use a custom HTTP header (e.g., X-Correlation-Id) for distributed tracing instead of the default traceparent header in an Azure Function (in-process) using .NET 6, you can customize the tracing behavior by creating a custom ITracerProvider and configuring it to use your custom DistributedContextPropagator .
@bhagyshricompany do you have a code example?
public static class MyFunction { [Function("YourFunctionName")] public static async Task<IActionResult> RunAsync( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestData req, FunctionContext context) { if (context.Items.TryGetValue("CorrelationId", out var correlationId) && correlationId is string id) { // Use the correlationId as needed }
// Your function logic here
return new OkResult();
}
}
Thanks for the example, do you have an example with a custom ITracerProvider
and DistributedContextPropagator
as well?
Here at High level code provided you can change as per required. dotnet add package OpenTelemetry.Extensions.Hosting in your startup.cs file using Microsoft.Azure.Functions.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection; using OpenTelemetry.Context.Propagation; using OpenTelemetry.Trace; using System;
[assembly: FunctionsStartup(typeof(YourNamespace.Startup))]
namespace YourNamespace { public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { // Create and configure a custom TracerProvider var tracerProvider = Sdk.CreateTracerProviderBuilder() .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("YourFunctionApp")) .AddSource("YourFunctionNamespace") .AddConsoleExporter() .Build();
builder.Services.AddSingleton(tracerProvider);
// Register a custom propagator to use "X-Correlation-Id"
builder.Services.AddSingleton<ITextMapPropagator, TraceContextPropagator>();
}
}
} configure a custom ITracerProvider and DistributedContextPropagator 2--now you can use using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.Logging; using System.Diagnostics;
public class MyFunction { private readonly ILogger<MyFunction> _logger; private readonly TracerProvider _tracerProvider;
public MyFunction(ILogger<MyFunction> logger, TracerProvider tracerProvider)
{
_logger = logger;
_tracerProvider = tracerProvider;
}
[Function("YourFunctionName")]
public void Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestData req)
{
// Create a new activity using the custom TracerProvider
using var activitySource = new ActivitySource("YourFunctionNamespace");
using var activity = activitySource.StartActivity("YourFunctionActivity");
// Access the correlation ID
var correlationId = activity.GetBaggageItem("X-Correlation-Id");
// Use the correlationId and the custom TracerProvider as needed
// Your function logic here
}
}
/bot not-stale
Thank you joostmeijles. This issue will not be automatically closed and a member of the team will review it soon.