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);
        }
        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");
            });
        }
Example #3
0
        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");
        });