Ejemplo n.º 1
0
        public void OnBeginRequestWithW3CHeadersAndRequestIdIsTrackedCorrectly(bool isAspNetCore2)
        {
            var configuration = TelemetryConfiguration.CreateDefault();

            configuration.TelemetryInitializers.Add(new W3COperationCorrelationTelemetryInitializer());
            using (var hostingListener = new HostingDiagnosticListener(
                       CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry), configuration),
                       CommonMocks.GetMockApplicationIdProvider(),
                       injectResponseHeaders: true,
                       trackExceptions: true,
                       enableW3CHeaders: true,
                       enableNewDiagnosticEvents: isAspNetCore2))
            {
                hostingListener.OnSubscribe();
                var context = CreateContext(HttpRequestScheme, HttpRequestHost, "/Test", method: "POST");

                context.Request.Headers[RequestResponseHeaders.RequestIdHeader] = "|abc.1.2.3.";
                context.Request.Headers[W3C.W3CConstants.TraceParentHeader]     =
                    "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01";
                context.Request.Headers[W3C.W3CConstants.TraceStateHeader] = "state=some";
                context.Request.Headers[RequestResponseHeaders.CorrelationContextHeader] = "k=v";
                context.Request.Headers[RequestResponseHeaders.RequestContextHeader]     = "appId=something";

                HandleRequestBegin(hostingListener, context, 0, isAspNetCore2);

                var activityInitializedByW3CHeader = Activity.Current;

                if (isAspNetCore2)
                {
                    Assert.Equal("|abc.1.2.3.", activityInitializedByW3CHeader.ParentId);
                }

                Assert.Equal("4bf92f3577b34da6a3ce929d0e0e4736", activityInitializedByW3CHeader.GetTraceId());
                Assert.Equal("00f067aa0ba902b7", activityInitializedByW3CHeader.GetParentSpanId());
                Assert.Equal(16, activityInitializedByW3CHeader.GetSpanId().Length);
                Assert.Equal("state=some", activityInitializedByW3CHeader.GetTracestate());
                Assert.Equal("v", activityInitializedByW3CHeader.Baggage.Single(t => t.Key == "k").Value);

                HandleRequestEnd(hostingListener, context, 0, isAspNetCore2);

                Assert.Single(sentTelemetry);
                var requestTelemetry = (RequestTelemetry)this.sentTelemetry.Single();

                Assert.Equal($"|4bf92f3577b34da6a3ce929d0e0e4736.{activityInitializedByW3CHeader.GetSpanId()}.",
                             requestTelemetry.Id);
                Assert.Equal("4bf92f3577b34da6a3ce929d0e0e4736", requestTelemetry.Context.Operation.Id);
                Assert.Equal("|4bf92f3577b34da6a3ce929d0e0e4736.00f067aa0ba902b7.",
                             requestTelemetry.Context.Operation.ParentId);

                Assert.True(context.Response.Headers.TryGetValue(RequestResponseHeaders.RequestContextHeader,
                                                                 out var appId));
                Assert.Equal($"appId={CommonMocks.TestApplicationId}", appId);

                if (isAspNetCore2)
                {
                    Assert.Equal("abc", requestTelemetry.Properties["ai_legacyRootId"]);
                    Assert.StartsWith("|abc.1.2.3.", requestTelemetry.Properties["ai_legacyRequestId"]);
                }
            }
        }
        public void OnBeginRequestWithNoW3CHeadersAndRequestIdIsTrackedCorrectly()
        {
            var configuration = TelemetryConfiguration.CreateDefault();

            configuration.TelemetryInitializers.Add(new W3COperationCorrelationTelemetryInitializer());
            this.middleware = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry), configuration),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: true,
                enableW3CHeaders: true);

            var context = CreateContext(HttpRequestScheme, HttpRequestHost, "/Test", method: "POST");

            context.Request.Headers[RequestResponseHeaders.RequestIdHeader]          = "|abc.1.2.3.";
            context.Request.Headers[RequestResponseHeaders.CorrelationContextHeader] = "k=v";

            middleware.OnBeginRequest(context, Stopwatch.GetTimestamp());
            var activityInitializedByW3CHeader = Activity.Current;

            Assert.Equal("|abc.1.2.3.", activityInitializedByW3CHeader.ParentId);
            middleware.OnEndRequest(context, Stopwatch.GetTimestamp());

            Assert.Single(sentTelemetry);
            var requestTelemetry = (RequestTelemetry)this.sentTelemetry.Single();

            Assert.Equal($"|{activityInitializedByW3CHeader.GetTraceId()}.{activityInitializedByW3CHeader.GetSpanId()}.", requestTelemetry.Id);
            Assert.Equal(activityInitializedByW3CHeader.GetTraceId(), requestTelemetry.Context.Operation.Id);
            Assert.Equal("|abc.1.2.3.", requestTelemetry.Context.Operation.ParentId);

            Assert.Equal("abc", requestTelemetry.Properties["ai_legacyRootId"]);
            Assert.StartsWith("|abc.1.2.3.", requestTelemetry.Properties["ai_legacyRequestId"]);
        }
Ejemplo n.º 3
0
        private HostingDiagnosticListener CreateHostingListener(bool aspNetCore2, TelemetryConfiguration config = null)
        {
            HostingDiagnosticListener hostingListener;

            if (config != null)
            {
                hostingListener = new HostingDiagnosticListener(
                    config,
                    CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)),
                    CommonMocks.GetMockApplicationIdProvider(),
                    injectResponseHeaders: true,
                    trackExceptions: true,
                    enableW3CHeaders: false,
                    enableNewDiagnosticEvents: aspNetCore2);
            }
            else
            {
                hostingListener = new HostingDiagnosticListener(
                    CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)),
                    CommonMocks.GetMockApplicationIdProvider(),
                    injectResponseHeaders: true,
                    trackExceptions: true,
                    enableW3CHeaders: false,
                    enableNewDiagnosticEvents: aspNetCore2);
            }

            hostingListener.OnSubscribe();
            return(hostingListener);
        }
        public void OnBeginRequestWithW3CHeadersIsTrackedCorrectly()
        {
            var configuration = TelemetryConfiguration.CreateDefault();

            configuration.TelemetryInitializers.Add(new W3COperationCorrelationTelemetryInitializer());
            this.middleware = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry), configuration),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: true,
                enableW3CHeaders: true);

            var context = CreateContext(HttpRequestScheme, HttpRequestHost, "/Test", method: "POST");

            context.Request.Headers[W3CConstants.TraceParentHeader] = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01";
            context.Request.Headers[W3CConstants.TraceStateHeader]  = "state=some";
            context.Request.Headers[RequestResponseHeaders.CorrelationContextHeader] = "k=v";

            if (HostingDiagnosticListener.IsAspNetCore20)
            {
                var activity = new Activity("operation");
                activity.Start();

                middleware.OnHttpRequestInStart(context);

                Assert.NotEqual(Activity.Current, activity);
            }
            else
            {
                middleware.OnBeginRequest(context, Stopwatch.GetTimestamp());
            }

            var activityInitializedByW3CHeader = Activity.Current;

            Assert.Equal("4bf92f3577b34da6a3ce929d0e0e4736", activityInitializedByW3CHeader.GetTraceId());
            Assert.Equal("00f067aa0ba902b7", activityInitializedByW3CHeader.GetParentSpanId());
            Assert.Equal(16, activityInitializedByW3CHeader.GetSpanId().Length);
            Assert.Equal("state=some", activityInitializedByW3CHeader.GetTracestate());
            Assert.Equal("v", activityInitializedByW3CHeader.Baggage.Single(t => t.Key == "k").Value);

            if (HostingDiagnosticListener.IsAspNetCore20)
            {
                middleware.OnHttpRequestInStop(context);
            }
            else
            {
                middleware.OnEndRequest(context, Stopwatch.GetTimestamp());
            }

            Assert.Single(sentTelemetry);
            var requestTelemetry = (RequestTelemetry)this.sentTelemetry.Single();

            Assert.Equal($"|4bf92f3577b34da6a3ce929d0e0e4736.{activityInitializedByW3CHeader.GetSpanId()}.", requestTelemetry.Id);
            Assert.Equal("4bf92f3577b34da6a3ce929d0e0e4736", requestTelemetry.Context.Operation.Id);
            Assert.Equal("|4bf92f3577b34da6a3ce929d0e0e4736.00f067aa0ba902b7.", requestTelemetry.Context.Operation.ParentId);

            Assert.True(context.Response.Headers.TryGetValue(RequestResponseHeaders.RequestContextHeader, out var appId));
            Assert.Equal($"appId={CommonMocks.TestApplicationId}", appId);
        }
 public RequestTrackingMiddlewareTest()
 {
     this.middleware = new HostingDiagnosticListener(
         CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)),
         CommonMocks.GetMockApplicationIdProvider(),
         injectResponseHeaders: true,
         trackExceptions: true);
 }
Ejemplo n.º 6
0
        public void SdkVersionIsPopulatedByMiddleware()
        {
            var middleware = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: true);

            middleware.OnHostingException(null, null);

            Assert.NotEmpty(sentTelemetry.Context.GetInternalContext().SdkVersion);
            Assert.Contains(SdkVersionTestUtils.VersionPrefix, sentTelemetry.Context.GetInternalContext().SdkVersion);
        }
        private HostingDiagnosticListener CreateHostingListener(bool aspNetCore2)
        {
            var hostingListener = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)), 
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: true,
                enableW3CHeaders: false,
                enableNewDiagnosticEvents: aspNetCore2);
            hostingListener.OnSubscribe();

            return hostingListener;
        }
Ejemplo n.º 8
0
        public void InvokeTracksExceptionThrownByNextMiddlewareAsHandledByPlatform()
        {
            var middleware = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: true);

            middleware.OnHostingException(null, null);

            Assert.NotNull(sentTelemetry);
            Assert.IsType <ExceptionTelemetry>(sentTelemetry);
            Assert.Equal(ExceptionHandledAt.Platform, ((ExceptionTelemetry)sentTelemetry).HandledAt);
        }
Ejemplo n.º 9
0
        private HostingDiagnosticListener CreateHostingListener(AspNetCoreMajorVersion aspNetCoreMajorVersion)
        {
            var hostingListener = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => {}),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: true,
                enableW3CHeaders: false,
                aspNetCoreMajorVersion: aspNetCoreMajorVersion);

            hostingListener.OnSubscribe();

            return(hostingListener);
        }
        public void OnBeginRequestWithW3CSupportAndNoHeadersIsTrackedCorrectly(bool isAspNetCore2)
        {
            var configuration = TelemetryConfiguration.CreateDefault();
            configuration.TelemetryInitializers.Add(new W3COperationCorrelationTelemetryInitializer());
            using (var hostingListener = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry), configuration),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: true,
                enableW3CHeaders: true,
                enableNewDiagnosticEvents: isAspNetCore2))
            {
                hostingListener.OnSubscribe();

                var context = CreateContext(HttpRequestScheme, HttpRequestHost, "/Test", method: "POST");

                HandleRequestBegin(hostingListener, context, 0, isAspNetCore2);

                var activityInitializedByW3CHeader = Activity.Current;

                if (!isAspNetCore2)
                { 
                Assert.Null(activityInitializedByW3CHeader.ParentId);
                    }
                Assert.NotNull(activityInitializedByW3CHeader.GetTraceId());
                Assert.Equal(32, activityInitializedByW3CHeader.GetTraceId().Length);
                Assert.Equal(16, activityInitializedByW3CHeader.GetSpanId().Length);
                Assert.Equal(
                    $"00-{activityInitializedByW3CHeader.GetTraceId()}-{activityInitializedByW3CHeader.GetSpanId()}-02",
                    activityInitializedByW3CHeader.GetTraceparent());
                Assert.Null(activityInitializedByW3CHeader.GetTracestate());
                Assert.Empty(activityInitializedByW3CHeader.Baggage);

                HandleRequestEnd(hostingListener, context, 0, isAspNetCore2);

                Assert.Single(sentTelemetry);
                var requestTelemetry = (RequestTelemetry) this.sentTelemetry.Single();

                Assert.Equal(
                    $"|{activityInitializedByW3CHeader.GetTraceId()}.{activityInitializedByW3CHeader.GetSpanId()}.",
                    requestTelemetry.Id);
                Assert.Equal(activityInitializedByW3CHeader.GetTraceId(), requestTelemetry.Context.Operation.Id);
                Assert.Null(requestTelemetry.Context.Operation.ParentId);

                Assert.True(context.Response.Headers.TryGetValue(RequestResponseHeaders.RequestContextHeader,
                    out var appId));
                Assert.Equal($"appId={CommonMocks.TestApplicationId}", appId);
            }
        }
        public void ExceptionsAreNotTrackedInjectedWhenDisabled()
        {
            HttpContext context = CreateContext(HttpRequestScheme, HttpRequestHost);

            var noExceptionsMiddleware = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: false);

            noExceptionsMiddleware.OnHostingException(context, new Exception("HostingException"));
            noExceptionsMiddleware.OnDiagnosticsHandledException(context, new Exception("DiagnosticsHandledException"));
            noExceptionsMiddleware.OnDiagnosticsUnhandledException(context, new Exception("UnhandledException"));

            Assert.Empty(sentTelemetry);
        }
        public void ResponseHeadersAreNotInjectedWhenDisabled()
        {
            HttpContext context = CreateContext(HttpRequestScheme, HttpRequestHost);

            var noHeadersMiddleware = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: false,
                trackExceptions: true);

            noHeadersMiddleware.OnBeginRequest(context, 0);
            Assert.False(context.Response.Headers.ContainsKey(RequestResponseHeaders.RequestContextHeader));
            noHeadersMiddleware.OnEndRequest(context, 0);
            Assert.False(context.Response.Headers.ContainsKey(RequestResponseHeaders.RequestContextHeader));

            Assert.Single(sentTelemetry);
            Assert.IsType <RequestTelemetry>(this.sentTelemetry.First());
        }
Ejemplo n.º 13
0
        public void SdkVersionIsPopulatedByMiddleware()
        {
            using (var hostingListener = new HostingDiagnosticListener(
                       CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry),
                       CommonMocks.GetMockApplicationIdProvider(),
                       injectResponseHeaders: true,
                       trackExceptions: true,
                       enableW3CHeaders: false,
                       enableNewDiagnosticEvents: true))
            {
                hostingListener.OnSubscribe();
                hostingListener.OnHostingException(null, null);

                Assert.NotEmpty(sentTelemetry.Context.GetInternalContext().SdkVersion);
                Assert.Contains(SdkVersionTestUtils.VersionPrefix,
                                sentTelemetry.Context.GetInternalContext().SdkVersion);
            }
        }
Ejemplo n.º 14
0
        public void InvokeTracksExceptionThrownByNextMiddlewareAsHandledByPlatform()
        {
            using (var hostingListener = new HostingDiagnosticListener(
                       CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry),
                       CommonMocks.GetMockApplicationIdProvider(),
                       injectResponseHeaders: true,
                       trackExceptions: true,
                       enableW3CHeaders: false,
                       enableNewDiagnosticEvents: true))
            {
                hostingListener.OnSubscribe();
                hostingListener.OnHostingException(null, null);

                Assert.NotNull(sentTelemetry);
                Assert.IsType <ExceptionTelemetry>(sentTelemetry);
                Assert.Equal(ExceptionHandledAt.Platform, ((ExceptionTelemetry)sentTelemetry).HandledAt);
            }
        }
Ejemplo n.º 15
0
        public void OnBeginRequestWithW3CHeadersAndAppIdInState(bool isAspNetCore2)
        {
            var configuration = TelemetryConfiguration.CreateDefault();

            configuration.TelemetryInitializers.Add(new W3COperationCorrelationTelemetryInitializer());
            using (var hostingListener = new HostingDiagnosticListener(
                       CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry), configuration),
                       CommonMocks.GetMockApplicationIdProvider(),
                       injectResponseHeaders: true,
                       trackExceptions: true,
                       enableW3CHeaders: true,
                       enableNewDiagnosticEvents: isAspNetCore2))
            {
                hostingListener.OnSubscribe();

                var context = CreateContext(HttpRequestScheme, HttpRequestHost, "/Test", method: "POST");

                context.Request.Headers[W3C.W3CConstants.TraceParentHeader] =
                    "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00";
                context.Request.Headers[W3C.W3CConstants.TraceStateHeader] =
                    $"state=some,{W3C.W3CConstants.AzureTracestateNamespace}={ExpectedAppId}";

                HandleRequestBegin(hostingListener, context, 0, isAspNetCore2);
                var activityInitializedByW3CHeader = Activity.Current;

                Assert.Equal("state=some", activityInitializedByW3CHeader.GetTracestate());

                HandleRequestEnd(hostingListener, context, 0, isAspNetCore2);

                Assert.Single(sentTelemetry);
                var requestTelemetry = (RequestTelemetry)this.sentTelemetry.Single();

                Assert.Equal(ExpectedAppId, requestTelemetry.Source);

                Assert.True(context.Response.Headers.TryGetValue(RequestResponseHeaders.RequestContextHeader,
                                                                 out var appId));
                Assert.Equal($"appId={CommonMocks.TestApplicationId}", appId);
            }
        }
        public void ResponseHeadersAreNotInjectedWhenDisabled(bool isAspNetCore2)
        {
            HttpContext context = CreateContext(HttpRequestScheme, HttpRequestHost);

            using (var noHeadersMiddleware = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: false,
                trackExceptions: true,
                enableW3CHeaders: false,
                enableNewDiagnosticEvents: isAspNetCore2))
            {
                noHeadersMiddleware.OnSubscribe();

                HandleRequestBegin(noHeadersMiddleware, context, 0, isAspNetCore2);
                Assert.False(context.Response.Headers.ContainsKey(RequestResponseHeaders.RequestContextHeader));

                HandleRequestEnd(noHeadersMiddleware, context, 0, isAspNetCore2);
                Assert.False(context.Response.Headers.ContainsKey(RequestResponseHeaders.RequestContextHeader));

                Assert.Single(sentTelemetry);
                Assert.IsType<RequestTelemetry>(this.sentTelemetry.First());
            }
        }
 public RequestTrackingMiddlewareTest()
 {
     this.middleware = new HostingDiagnosticListener(CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)), CommonMocks.GetMockApplicationIdProvider());
 }
 private static OperationCorrelationTelemetryInitializer CreateInitializer(IHttpContextAccessor contextAccessor)
 {
     return(new OperationCorrelationTelemetryInitializer(contextAccessor, CommonMocks.GetMockApplicationIdProvider()));
 }