public async Task Forward_PropagateAsPredefined_ExpectPredefinedHeader(CorrelationContext correlationContext) { // arrange var incomingHeader = HttpHeaders.RequestId; var outgoindHeader = "X-MyRequest-Id"; var propagationSettings = PropagationSettings.PropagateAs(outgoindHeader); _correlationContextAccessor .Setup(a => a.CorrelationContext) .Returns(correlationContext); void AssertRequest(HttpRequestMessage r) { Assert.True(r.Headers.Contains(outgoindHeader)); Assert.Contains(TestCorrelationId.Value, r.Headers.GetValues(outgoindHeader)); } var handler = CreateMessageHandler(propagationSettings, _correlationContextAccessor, AssertRequest); // act & assert (via test delegating handler) var client = new HttpClient(handler); _ = await client .GetAsync("https://www.example.com/") .ConfigureAwait(false); }
public async Task Forward_KeepIncomingHeaderWithGeneratedCorrelationId_ExpectPredefinedIncomingHeader() { // arrange var incomingHeader = HttpHeaders.RequestId; var propagationSettings = PropagationSettings.KeepIncomingHeaderName(incomingHeader); _correlationContextAccessor .Setup(a => a.CorrelationContext) .Returns(new GeneratedCorrelationContext(TestCorrelationId)); void AssertRequest(HttpRequestMessage r) { Assert.True(r.Headers.Contains(incomingHeader)); Assert.Contains(TestCorrelationId.Value, r.Headers.GetValues(incomingHeader)); }; var handler = CreateMessageHandler(propagationSettings, _correlationContextAccessor, AssertRequest); // act & assert (via test delegating handler) var client = new HttpClient(handler); _ = await client .GetAsync("https://www.example.com/") .ConfigureAwait(false); }
/// <summary> /// Initializes a new instance of the <see cref="CorrelatorHttpMessageHandler"/> class. /// </summary> /// <param name="settings">Propagation settings.</param> /// <param name="correlationContextAccessor">Correlation context accessor.</param> public CorrelatorHttpMessageHandler( PropagationSettings settings, ICorrelationContextAccessor correlationContextAccessor) { _settings = settings; _correlationContextAccessor = correlationContextAccessor ?? throw new ArgumentNullException(nameof(correlationContextAccessor)); }
private static IOptions <CorrelatorOptions> CreateEmitOptions(PropagationSettings emitSettings) { var options = new CorrelatorOptions { Emit = emitSettings }; return(new OptionsWrapper <CorrelatorOptions>(options)); }
private static CorrelatorHttpMessageHandler CreateMessageHandler( PropagationSettings propagationSettings, Mock <ICorrelationContextAccessor> contextAccessor, Action <HttpRequestMessage> assertRequest) { return(new CorrelatorHttpMessageHandler(propagationSettings, contextAccessor.Object) { InnerHandler = new TestDelegatingHandler(assertRequest) }); }
public void ConfigureServices(IServiceCollection services) { services.AddDefaultCorrelator(options => { options.ReadFrom.Clear(); options.ReadFrom.Add("X-Correlation-Id"); options.Factory = (_) => CorrelationId.FromString("le_correlation"); options.Emit = PropagationSettings.PropagateAs("X-Correlation-Id"); options.ReplaceTraceIdentifier = false; options.LoggingScope = LoggingScopeSettings.IncludeLoggingScope("Correlation"); }); }
public void ConfigureServices(IServiceCollection services) { services.AddDefaultCorrelator(o => { o.Forward = PropagationSettings.PropagateAs("X-Request-Id"); }); services .AddHttpClient( BetaClientName, httpClient => httpClient.BaseAddress = new Uri("http://localhost:8082")) .WithCorrelation(); }
public void ConfigureServices(IServiceCollection services) { services.AddDefaultCorrelator(options => { options.Emit = PropagationSettings.KeepIncomingHeaderName(); options.ReplaceTraceIdentifier = true; options.LoggingScope = LoggingScopeSettings.IncludeLoggingScope(); options.ReadFrom.Add("X-Correlation"); options.ReadFrom.Add("X-Request"); options.ReadFrom.Add("X-Trace-Id"); }); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddDefaultCorrelator(options => { options.Forward = PropagationSettings.PropagateAs("X-Correlation-Id"); options.Emit = PropagationSettings.PropagateAs("X-Correlation-Id"); options.LoggingScope = LoggingScopeSettings.IncludeLoggingScope("Correlation"); }); services .AddHttpClient("DummyClient") .WithCorrelation(); services.AddControllers(); }
public async Task Emits_WhenKeepingIncomingHeaderButCorrelationIdGenerated_ExpectCorrelationIdEmittedWithPredefinedHeader() { // arrange var headerName = "X-Incoming-Request-Id"; var options = CreateEmitOptions(PropagationSettings.KeepIncomingHeaderName(headerName)); var httpContext = new DefaultHttpContext(); var correlationContext = new GeneratedCorrelationContext(CorrelationId.FromString("123")); // act var emitter = new CorrelationEmitter(options, _logger); await emitter .Emit(httpContext, correlationContext) .ConfigureAwait(false); // assert Assert.Contains(headerName, httpContext.Response.Headers); Assert.Equal("123", httpContext.Response.Headers[headerName].ToString()); }
public void ConfigureServices(IServiceCollection services) => services.AddDefaultCorrelator( o => { // disable correlation ID factory // (if correlation ID not sent, ASP.NET trace ID is going to be used) o.Factory = null; // read custom header o.ReadFrom.Clear(); o.ReadFrom.Add("X-CID"); // do not emit correlation ID o.Emit = PropagationSettings.KeepIncomingHeaderName(); // overwrite `HttpContext.TraceIdentifier` by correlation ID o.ReplaceTraceIdentifier = true; // enable logging scope containing correlation ID o.LoggingScope = LoggingScopeSettings.IncludeLoggingScope("Correlation"); });
public async Task Emits_WhenEmmitingUsingCustomHeader_ExpectCorrelationIdEmittedWithPredefinedHeader() { // arrange var incomingHeader = HttpHeaders.AspNetRequestId; var outgoingHeader = "X-Le-Custom-Request-Id"; var options = CreateEmitOptions(PropagationSettings.PropagateAs(outgoingHeader)); var httpContext = new DefaultHttpContext(); var correlationContext = new RequestCorrelationContext( CorrelationId.FromString("123"), incomingHeader); // act var emitter = new CorrelationEmitter(options, _logger); await emitter .Emit(httpContext, correlationContext) .ConfigureAwait(false); // assert Assert.Contains(outgoingHeader, httpContext.Response.Headers); Assert.Equal("123", httpContext.Response.Headers[outgoingHeader]); }
private static string?GetResponseHeaderName( PropagationSettings propagation, CorrelationContext correlationContext) => (propagation.Settings, correlationContext) switch
/// <summary> /// Configures HTTP client with configured <see cref="CorrelatorHttpMessageHandler"/>. /// </summary> /// <param name="builder">HTTP client builder.</param> /// <param name="propagationSettings">Correlation propagation settings.</param> /// <returns> /// HTTP client builder configured to use correlator message handler. /// </returns> public static IHttpClientBuilder WithCorrelation( this IHttpClientBuilder builder, PropagationSettings propagationSettings) => builder.AddHttpMessageHandler(sp => new CorrelatorHttpMessageHandler( propagationSettings, sp.GetRequiredService <ICorrelationContextAccessor>()));