Skip to content

Request correlation

moattarwork edited this page Dec 14, 2022 · 1 revision

The framework is built to support MicroService architecture. One of the challenges we face in such architectures is tracking the flow of the requests through multiple APIs. This particular feature of the API is built to address Request Correlation problem. Default behavior of the API is to expect that each INCOMING request has a correlation id, which is located in the request header. We support two header types: X-Correlation-ID and X-Request-ID. There is no difference which one you are using, however, it is MANDATORY to have one, otherwise, requests will be rejected with an error explaining that Correlation ID was not present.

If you want to override this behavior, then instead of rejecting a NON-correlated request we can generate an id ourselves and attached it to the request. In order to achieve this we should configure our API as shown below:

public sealed class Startup
{
    // Code omitted for clarity

    public void ConfigureServices(IServiceCollection services)
    {
        services.BootstrapApp<Startup>(Configuration,
            app => app
                .HandleApplicationException<MyApplicationBaseException>()
                .ConfigureCorrelation(o => o.AutoCorrelateRequests())
                .AddServices((container, config) => {})
        );
    }

    // Code omitted for clarity
}

We also have some additional options here, for example, we may want to exclude certain URL's (i.e. not correlate them), example being a PING URL or some diagnostics endpoint, to achieve this please consult an example below:

public sealed class Startup
{
    // Code omitted for clarity

    public void ConfigureServices(IServiceCollection services)
    {
        services.BootstrapApp<Startup>(Configuration,
            app => app
                .HandleApplicationException<MyApplicationBaseException>()
                .ConfigureCorrelation(o => o.ExcludeUrl(f => f.WhenContains("/diagnostics/ping")))
                .AddServices((container, config) => {})
        );
    }

    // Code omitted for clarity
}

With this configuration, all requests to URLs that contain '/diagnostics/ping' in their path will not be checked for correlation ids. We have 3 different filtering options:

  • WhenContains
  • WhenStartsWith
  • WhenEndsWith

I believe those are self-explanatory. You may chain as many URL fragments as you like.

Clone this wiki locally