azure-functions-host icon indicating copy to clipboard operation
azure-functions-host copied to clipboard

Use different HTTP header for distributed trace context

Open joostmeijles opened this issue 1 year ago • 7 comments

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?

joostmeijles avatar Sep 07 '23 09:09 joostmeijles

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 avatar Sep 12 '23 10:09 bhagyshricompany

@bhagyshricompany do you have a code example?

joostmeijles avatar Sep 12 '23 14:09 joostmeijles

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();
}

}

bhagyshricompany avatar Sep 26 '23 06:09 bhagyshricompany

Thanks for the example, do you have an example with a custom ITracerProvider and DistributedContextPropagator as well?

joostmeijles avatar Sep 26 '23 06:09 joostmeijles

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
}

}

bhagyshricompany avatar Sep 26 '23 08:09 bhagyshricompany

/bot not-stale

joostmeijles avatar Oct 02 '23 14:10 joostmeijles

Thank you joostmeijles. This issue will not be automatically closed and a member of the team will review it soon.