Exemple #1
0
        public async Task AsyncTrackingInterceptor_GivenTrackingContext_WhenReturnAsync_ThenSameTrackingContextReturned()
        {
            var instance    = new TestTrackingService();
            var interceptor = new AsyncTrackingInterceptor();

            ITestTrackingService proxy = s_ProxyGenerator.CreateInterfaceProxyWithTargetInterface <ITestTrackingService>(instance, interceptor.ToInterceptor());

            TrackingContext.NewCurrent();
            TrackingContext currentTrackingContext = TrackingContext.Current;

            currentTrackingContext.Should().NotBeNull();

            TrackingContext returnedTrackingContext = null;
            await proxy.ReturnAsync(() =>
            {
                returnedTrackingContext = TrackingContext.Current;
            }).ConfigureAwait(false);

            TrackingContext.Current.Should().NotBeNull();
            TrackingContext.Current.CallChainId.Should().Be(currentTrackingContext.CallChainId);
            TrackingContext.Current.OriginatorUtcTimestamp.Should().Be(currentTrackingContext.OriginatorUtcTimestamp);

            returnedTrackingContext.Should().NotBeNull();
            returnedTrackingContext.CallChainId.Should().Be(currentTrackingContext.CallChainId);
            returnedTrackingContext.OriginatorUtcTimestamp.Should().Be(currentTrackingContext.OriginatorUtcTimestamp);
        }
Exemple #2
0
        public void TrackingContextEnricher_GivenNoTrackingContext_WhenEnricherApplied_ThenNoPropertiesAdded()
        {
            TrackingContext currentTrackingContext = TrackingContext.Current;

            currentTrackingContext.Should().BeNull();

            var trackingContextEnricher = new TrackingContextEnricher();
            var logEvent = new LogEvent(DateTimeOffset.UtcNow, LogEventLevel.Information, null, new MessageTemplate(new List <MessageTemplateToken>()), new List <LogEventProperty>());

            trackingContextEnricher.Enrich(logEvent, null);

            logEvent.Properties.Should().BeEmpty();
        }
Exemple #3
0
        public async Task AsyncTrackingInterceptor_GivenNoTrackingContext_WhenReturnTrackingContextAsync_ThenNewTrackingContextReturned()
        {
            var instance    = new TestTrackingService();
            var interceptor = new AsyncTrackingInterceptor();

            ITestTrackingService proxy = s_ProxyGenerator.CreateInterfaceProxyWithTargetInterface <ITestTrackingService>(instance, interceptor.ToInterceptor());

            TrackingContext.Current.Should().BeNull();

            TrackingContext returnedTrackingContext = await proxy.ReturnTrackingContextAsync().ConfigureAwait(false);

            returnedTrackingContext.Should().NotBeNull();
            TrackingContext.Current.Should().BeNull();
        }
Exemple #4
0
        public void TrackingContextEnricher_GivenTrackingContext_WhenEnricherApplied_ThenPropertiesAdded()
        {
            TrackingContext.NewCurrent();
            TrackingContext currentTrackingContext = TrackingContext.Current;

            currentTrackingContext.Should().NotBeNull();

            var trackingContextEnricher = new TrackingContextEnricher();
            var logEvent = new LogEvent(DateTimeOffset.UtcNow, LogEventLevel.Information, null, new MessageTemplate(new List <MessageTemplateToken>()), new List <LogEventProperty>());

            trackingContextEnricher.Enrich(logEvent, null);

            logEvent.Properties.Count.Should().Be(2);
            logEvent.Properties[TrackingContextEnricher.CallChainIdPropertyName].ToString().Should().Be($"\"{currentTrackingContext.CallChainId}\"");
            logEvent.Properties[TrackingContextEnricher.OriginatorUtcTimestampPropertyName].ToString().Should().Be($"\"{currentTrackingContext.OriginatorUtcTimestamp.ToString("o")}\"");
        }
Exemple #5
0
        public async Task Startup_GivenTrackingContextMiddleware_WhenTrackingContextSent_ThenNoTrackingContextReturned()
        {
            var textWriterSink = new StringWriter();
            var hostBuilder    = new HostBuilder()
                                 .ConfigureWebHost(webHost =>
            {
                webHost.UseTestServer();
                webHost.UseStartup <Startup>();
                webHost.ConfigureServices((services) => StartupBase.ConfigureServicesWithLogSink(
                                              services,
                                              textWriterSink,
                                              $"{{{nameof(TrackingContext.CallChainId)}}}{textWriterSink.NewLine}"));
            });

            var host = await hostBuilder.StartAsync();

            var client = host.GetTestClient();

            TrackingContext.NewCurrentIfEmpty();
            TrackingContext currentContext = TrackingContext.Current;

            currentContext.Should().NotBeNull();

            var requestMessage = new HttpRequestMessage(HttpMethod.Get, @"/api/values");

            string tcHeader = TrackingContext.Serialize(currentContext).ByteArrayToBase64String();

            requestMessage.Headers.Add(TrackingContextHelper.TrackingContextKeyName, tcHeader);

            var response = await client.SendAsync(requestMessage);

            response.EnsureSuccessStatusCode();
            response.Headers.TryGetValues(TrackingContextHelper.TrackingContextKeyName, out IEnumerable <string> values).Should().BeFalse();

            TrackingContext.Current.Should().NotBeNull();
            TrackingContext.Current.Should().BeEquivalentTo(currentContext);

            IList <string> callChainIds = textWriterSink
                                          .ToString()
                                          .Split(textWriterSink.NewLine, StringSplitOptions.RemoveEmptyEntries)
                                          .ToList();

            callChainIds.Count().Should().Be(10);

            // The call chain ID should be different on the server side.
            callChainIds.All(x => !x.Equals(currentContext.CallChainId.ToDashedString())).Should().BeTrue();
        }
Exemple #6
0
        public void TrackingContextEnricher_GivenTrackingContextWithExtraHeaders_WhenEnricherApplied_ThenPropertiesAdded()
        {
            var extraHeaders = new Dictionary <string, string> {
                { "FirstKey", "FirstValue" },
                { "SecondKey", "SecondValue" }
            };

            TrackingContext.NewCurrent(extraHeaders);
            TrackingContext currentTrackingContext = TrackingContext.Current;

            currentTrackingContext.Should().NotBeNull();

            var trackingContextEnricher = new TrackingContextEnricher();
            var logEvent = new LogEvent(DateTimeOffset.UtcNow, LogEventLevel.Information, null, new MessageTemplate(new List <MessageTemplateToken>()), new List <LogEventProperty>());

            trackingContextEnricher.Enrich(logEvent, null);

            logEvent.Properties.Count.Should().Be(4);
            logEvent.Properties[TrackingContextEnricher.CallChainIdPropertyName].ToString().Should().Be($@"""{currentTrackingContext.CallChainId}""");
            logEvent.Properties[TrackingContextEnricher.OriginatorUtcTimestampPropertyName].ToString().Should().Be($@"""{currentTrackingContext.OriginatorUtcTimestamp.ToString("o")}""");
            logEvent.Properties["FirstKey"].ToString().Should().Be(@"""FirstValue""");
            logEvent.Properties["SecondKey"].ToString().Should().Be(@"""SecondValue""");
        }
Exemple #7
0
        public async Task Startup_GivenTrackingContextWithMergeAndOverwriteMiddleware_WhenTrackingContextSent_ThenTrackingContextReturned()
        {
            var textWriterSink = new StringWriter();
            var hostBuilder    = new HostBuilder()
                                 .ConfigureWebHost(webHost =>
            {
                webHost.UseTestServer();
                webHost.UseStartup <StartupWithMergeAndOverwrite>();
                webHost.ConfigureServices((services) => StartupBase.ConfigureServicesWithLogSink(
                                              services,
                                              textWriterSink,
                                              $"{{{StartupBase.CountryOfOriginName}}}{textWriterSink.NewLine}"));
            });

            var host = await hostBuilder.StartAsync();

            var client = host.GetTestClient();

            string headerKey   = Guid.NewGuid().ToFlatString();
            string headerValue = Guid.NewGuid().ToFlatString();

            TrackingContext.NewCurrentIfEmpty(new Dictionary <string, string>()
            {
                { headerKey, headerValue },
                { StartupBase.CountryOfOriginName, @"Germany" }
            });
            TrackingContext currentContext = TrackingContext.Current;

            currentContext.Should().NotBeNull();

            var requestMessage = new HttpRequestMessage(HttpMethod.Get, @"/api/values");

            string tcHeader = TrackingContext.Serialize(currentContext).ByteArrayToBase64String();

            requestMessage.Headers.Add(TrackingContextHelper.TrackingContextKeyName, tcHeader);

            var response = await client.SendAsync(requestMessage);

            response.EnsureSuccessStatusCode();
            response.Headers.TryGetValues(TrackingContextHelper.TrackingContextKeyName, out IEnumerable <string> values).Should().BeTrue();

            string          tcBase64 = values.First();
            TrackingContext tc       = TrackingContext.DeSerialize(tcBase64.Base64StringToByteArray());

            tc.CallChainId.Should().NotBeEmpty();
            tc.OriginatorUtcTimestamp.Should().BeCloseTo(DateTime.UtcNow, 10000);
            tc.ExtraHeaders.Count.Should().Be(4);
            tc.ExtraHeaders[headerKey].Should().Be(headerValue);
            tc.ExtraHeaders[StartupBase.TraceIdentifierName].Should().NotBeNullOrWhiteSpace();
            tc.ExtraHeaders[StartupBase.CountryOfOriginName].Should().Be(@"France");
            tc.ExtraHeaders[StartupBase.RandomStringGeneratedWitEachCallName].Should().NotBeNullOrWhiteSpace();

            TrackingContext.Current.Should().NotBeNull();
            TrackingContext.Current.Should().BeEquivalentTo(currentContext);

            IList <string> countriesOfOrigin = textWriterSink
                                               .ToString()
                                               .Split(textWriterSink.NewLine, StringSplitOptions.RemoveEmptyEntries)
                                               .ToList();

            countriesOfOrigin.Count().Should().Be(10);

            countriesOfOrigin.All(x => x.Equals(tc.ExtraHeaders[StartupBase.CountryOfOriginName])).Should().BeTrue();
        }