I would like to insert a middleware to Azure Durable Functions v4 (.NET 6) which gets a correlation id from the HttpTrigger and registers that into logger factory so that it is visible in application insights. Also vice versa; attaches correlation id to all outgoing requests. I do have multiple Azure Functions (some call each other) so I want to track a particular request by its CorrelationId.
I have tried guides here and here. However all of them has Program.cs class and register middleware by using that class. I only have startup and it looks like this:
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services
.AddLogging()
.AddHttpClient();
}
}
How do I create a solution which fetches/attaches correlation id to requests/responses?
Something like: ...UseMiddleware<CorrelationIdFactory>()
2
Answers
Middlewares can only be registered in Azure Isolated Functions but not durable functions. Because host builder cannot be configured in Durable functions. However, MS team has just released support for Isolated Durable functions which can register middlewares and configure the host builder.
Here is the released library for Azure Isolated Durable functions.
Here is how to create an Azure Isolated Durable function which sets correlationId on every http request.
Program.cs
file:StampHttpHeaderMiddleware.cs
file:And this is our Http Trigger which you can get/set correlationId into:
To insert a middleware to Azure Durable Functions that attaches a correlation ID to the logs.
To use the middleware you need to register it in the
Startup.cs
fileYou can then access the correlation ID in your functions as shown below
For the requests, you can add the correlation ID to the headers as shown below.
References taken from
Durable Functions