Skip to content

This specific library offers a lightweight approach to implementing Correlation Ids for ASP.NET

License

Notifications You must be signed in to change notification settings

nmakhmutov/Elwark.CorrelationId

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Elwark Correlation Id

A Correlation Id is a tool used in distributed systems to trace requests across multiple services. This specific library offers a lightweight approach to implementing Correlation Ids. When enabled, it checks for the presence of a Correlation Id in request headers and attaches it to the Correlation Context, which can then be used for logging and other purposes. Additionally, this Correlation Id can also be attached to downstream HTTP calls made through an IHttpClientFactory. However, it should be noted that this library may not be fully comprehensive and that built-in tracing options for .NET apps may be more suitable for more extensive tracing needs.

Installation

You should install Elwark.CorrelationId from NuGet:

Install-Package Elwark.CorrelationId

NuGet

This command from Package Manager Console will download and install CorrelationId and all required dependencies.

All stable and some pre-release packages are available on NuGet.

Quick Start

Register with DI

Inside ConfigureServices add the required correlation id services, with common defaults.

services.AddCorrelationId()

or you can define with options

services.AddCorrelationId(options =>
{ 
    options.AddToLoggingScope = true;
    options.EnforceHeader = true;
    options.IgnoreRequestHeader = false;
    options.IncludeInResponse = true;
    options.RequestHeader = "X-Correlation-Id";
    options.ResponseHeader = "X-Correlation-Id";
    options.UpdateTraceIdentifier = false;
});

This registers a correlation id provider which generates new ids based on a random GUID.

Minimal API / MVC

You should install Elwark.CorrelationId.Http from NuGet:

Install-Package Elwark.CorrelationId.Http

NuGet

Add the middleware

Register the middleware into the pipeline. This should occur before any downstream middleware which requires the correlation ID. Normally this will be registered very early in the middleware pipeline.

app.UseCorrelationId();

Also Elwark.CorrelationId.Http contains TraceIdentifier Correlation id provider. You can replace GUID provider by TraceId just following code:

services.AddCorrelationId()
   .WithTraceIdentifierProvider();

After your correlation id will be trace identifier from HttpContext.

Add HttpClient forwarding

Forwarding your correlation id to another service by http call available by adding:

builder.Services
    .AddHttpClient<ITestClient, TestClient>()
    .AddHttpCorrelationIdForwarding();

gRPC

You should install Elwark.CorrelationId.Grpc from NuGet:

Install-Package Elwark.CorrelationId.Grpc

NuGet

Add the interceptor

Register the interceptor into the pipeline.

builder.Services
    .AddGrpc(options => options.UseCorrelationId());

Add GrpcClient forwarding

Forwarding your correlation id to another service by gRPC call available by adding:

builder.Services
    .AddGrpcClient<Greeter.GreeterClient>()
    .AddGrpcCorrelationIdForwarding();

or

builder.Services
    .AddGrpcClient<Greeter.GreeterClient>(options =>
    {
        options.AddCorrelationIdForwarding();
    });

Where you need to access the correlation Id, you may request the ICorrelationContextAccessor from DI.

public class MyClass
{
   private readonly ICorrelationContextAccessor _accessor;

   public MyClass(ICorrelationContextAccessor accessor)
   {
	  _accessor = accessor;
   }

   ...
}

About

This specific library offers a lightweight approach to implementing Correlation Ids for ASP.NET

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages