Skip to content

Commit

Permalink
Added missing logic in TracingMiddleware to actually get correlation …
Browse files Browse the repository at this point in the history
…and causation ids from request headers
  • Loading branch information
oskardudycz committed Mar 5, 2022
1 parent 9cef9cb commit 283e47c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Core.WebApi/Swagger/MetadataOperationFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)

operation.Parameters.Add(new OpenApiParameter
{
Name = "X-CorrelationId",
Name = "X-Correlation-ID",
In = ParameterLocation.Header,
Description = "Correlation Id",
Required = false
});

operation.Parameters.Add(new OpenApiParameter
{
Name = "X-CausationId",
Name = "X-Causation-ID",
In = ParameterLocation.Header,
Description = "Causation Id",
Required = false
Expand Down
31 changes: 31 additions & 0 deletions Core.WebApi/Tracing/TracingHeadersExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Core.Tracing.Causation;
using Core.Tracing.Correlation;
using Microsoft.AspNetCore.Http;

namespace Core.WebApi.Tracing;

public static class TracingHeadersExtensions
{
private const string CorrelationIdHeaderName = "X-Correlation-ID";
private const string CausationIdHeaderName = "X-Causation-ID";

public static CorrelationId? GetCorrelationId(this HttpRequest request) =>
request.Headers.TryGetValue(CorrelationIdHeaderName, out var correlationId)
? new CorrelationId(correlationId)
: null;


public static CausationId? GetCausationId(this HttpRequest request) =>
request.Headers.TryGetValue(CausationIdHeaderName, out var correlationId)
? new CausationId(correlationId)
: null;

public static void SetCorrelationId(this HttpResponse response, CorrelationId correlationId) =>
response.Headers.Add(CorrelationIdHeaderName, new[] { correlationId.Value });


public static void SetCausationId(this HttpResponse response, CausationId causationId) =>
response.Headers.Add(CausationIdHeaderName, new[] { causationId.Value });


}
23 changes: 13 additions & 10 deletions Core.WebApi/Tracing/TracingMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Core.Tracing;
using Core.Events;
using Core.Tracing;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -10,32 +11,34 @@ namespace Core.WebApi.Tracing;

public class TracingMiddleware
{
private const string CorrelationIdHeaderName = "X-Correlation-ID";
private const string CausationIdHeaderName = "X-Causation-ID";

private readonly RequestDelegate next;
private readonly ILogger<TracingMiddleware> logger;

public TracingMiddleware(RequestDelegate next, ILogger<TracingMiddleware> logger)
{
this.next = next ?? throw new ArgumentNullException(nameof(next));
this.logger = logger;
}

public async Task Invoke(
HttpContext context,
Func<IServiceProvider, TracingScope> createTracingScope
Func<IServiceProvider, TraceMetadata?, TracingScope> createTracingScope
)
{
using var tracingScope = createTracingScope(context.RequestServices);
var traceMetadataFromHeaders = new TraceMetadata(
context.Request.GetCorrelationId(),
context.Request.GetCausationId()
);
using var tracingScope = createTracingScope(context.RequestServices, traceMetadataFromHeaders);

var correlationId = tracingScope.CorrelationId;
var causationId = tracingScope.CausationId;

context.TraceIdentifier = correlationId.Value;

// apply the correlation ID to the response header for client side tracking
context.Response.OnStarting(() =>
{
context.Response.Headers.Add(CorrelationIdHeaderName, new[] { correlationId.Value });
context.Response.Headers.Add(CausationIdHeaderName, new[] { causationId.Value });
context.Response.SetCorrelationId(correlationId);
context.Response.SetCausationId(causationId);
return Task.CompletedTask;
});

Expand Down
6 changes: 3 additions & 3 deletions Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public static IServiceCollection AddTracing(this IServiceCollection services)
services.TryAddScoped<ITraceMetadataProvider, TraceMetadataProvider>();
services.TryAddScoped<ITracingScopeFactory, TracingScopeFactory>();

services.TryAddScoped<Func<IServiceProvider, TracingScope>>(sp =>
scopedServiceProvider =>
sp.GetRequiredService<ITracingScopeFactory>().CreateTraceScope(scopedServiceProvider)
services.TryAddScoped<Func<IServiceProvider, TraceMetadata?, TracingScope>>(sp =>
(scopedServiceProvider, traceMetadata) =>
sp.GetRequiredService<ITracingScopeFactory>().CreateTraceScope(scopedServiceProvider, traceMetadata)
);

services.TryAddScoped<Func<IServiceProvider, EventEnvelope?, TracingScope>>(sp =>
Expand Down
3 changes: 2 additions & 1 deletion Core/Tracing/TracingScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public TracingScope CreateTraceScope(IServiceProvider serviceProvider, TraceMeta

public static class TraceScopeFactoryExtensions
{
public static TracingScope CreateTraceScope(this ITracingScopeFactory tracingScopeFactory,
public static TracingScope CreateTraceScope(
this ITracingScopeFactory tracingScopeFactory,
IServiceProvider serviceProvider, EventEnvelope? eventEnvelope)
{
if (eventEnvelope == null)
Expand Down

0 comments on commit 283e47c

Please sign in to comment.