public async Task LEGACY_Can_Send_Request_Telemetry() { // given var context = new MockOwinContextBuilder().Build(); var configuration = new TelemetryConfigurationBuilder().Build(); var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new NoopMiddleware(), configuration), new OperationIdContextMiddlewareConfiguration()); // when await sut.Invoke(context); // then var channel = configuration.TelemetryChannel as MockTelemetryChannel; channel.SentTelemetries.Count.Should().Be(1); var telemetry = channel.SentTelemetries.First() as RequestTelemetry; telemetry.Should().NotBeNull(); VerifyDefaultsOnRequestTelemetry(telemetry); telemetry.ResponseCode.Should().Be("200"); telemetry.Success.Should().BeTrue(); }
public async Task Can_Send_Request_Telemetry(int statusCode, bool expectedSuccess) { // given var channel = new MockTelemetryChannel(); var response = Mock.Of <IOwinResponse>(r => r.StatusCode == statusCode); var context = new MockOwinContextBuilder() .WithResponse(response) .Build(); var configuration = new TelemetryConfigurationBuilder() .WithChannel(channel) .Build(); var sut = new HttpRequestTrackingMiddleware(new NoopMiddleware(), configuration); // when await sut.Invoke(context); // then var telemetry = channel.SentTelemetries.First() as RequestTelemetry; telemetry.Success.Should().Be(expectedSuccess); }
public async Task Can_Pass_Request_Details_For_Filtering() { // given var context = new MockOwinContextBuilder().Build(); var configuration = new TelemetryConfigurationBuilder().Build(); IOwinContext filteredContext = null; var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new NoopMiddleware(), new RequestTrackingConfiguration { TelemetryConfiguration = configuration, ShouldTrackRequest = ctx => { filteredContext = ctx; return(Task.FromResult(false)); } }), new OperationIdContextMiddlewareConfiguration()); // when await sut.Invoke(context); // then filteredContext.ShouldBeEquivalentTo(context); }
public async Task LEGACY_Can_Pass_Request_Details_For_Filtering() { // given var context = new MockOwinContextBuilder().Build(); var configuration = new TelemetryConfigurationBuilder().Build(); IOwinRequest filteredRequest = null; IOwinResponse filteredResponse = null; var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new NoopMiddleware(), configuration, (req, resp) => { filteredRequest = req; filteredResponse = resp; return(false); }), new OperationIdContextMiddlewareConfiguration()); // when await sut.Invoke(context); // then filteredRequest.ShouldBeEquivalentTo(context.Request); filteredResponse.ShouldBeEquivalentTo(context.Response); }
public async Task Can_Filter_Exceptions_To_Trace() { // given var context = new MockOwinContextBuilder().Build(); var configuration = new TelemetryConfigurationBuilder() .WithTelemetryInitializer(new OperationIdTelemetryInitializer()) .Build(); var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new ThrowingMiddleware(), new RequestTrackingConfiguration { TelemetryConfiguration = configuration, ShouldTrackException = async(ctx, e) => await DefaultFilters.ShouldTrackException(ctx, e) && !(e is OlabogaException) }), new OperationIdContextMiddlewareConfiguration()); // when try { await sut.Invoke(context); } catch (OlabogaException) { // ignored } // then var channel = configuration.TelemetryChannel as MockTelemetryChannel; channel.SentTelemetries.Count(t => t is ExceptionTelemetry).Should().Be(0); }
public async Task Should_Skip_Request_Telemetry_When_Filtered_Out() { // given var context = new MockOwinContextBuilder().Build(); var configuration = new TelemetryConfigurationBuilder().Build(); var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new NoopMiddleware(), new RequestTrackingConfiguration { TelemetryConfiguration = configuration, ShouldTrackRequest = ctx => Task.FromResult(false) }), new OperationIdContextMiddlewareConfiguration()); // when await sut.Invoke(context); // then var channel = configuration.TelemetryChannel as MockTelemetryChannel; channel.SentTelemetries.Count.Should().Be(0); }
public async Task Can_Handle_Async_Filtering_Delegate() { // given var context = new MockOwinContextBuilder().Build(); var configuration = new TelemetryConfigurationBuilder().Build(); var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new NoopMiddleware(), new RequestTrackingConfiguration { TelemetryConfiguration = configuration, ShouldTrackRequest = async ctx => { await Task.Delay(100); return(false); } }), new OperationIdContextMiddlewareConfiguration()); // when await sut.Invoke(context); // then var channel = configuration.TelemetryChannel as MockTelemetryChannel; channel.SentTelemetries.Count.Should().Be(0); }
public async Task Should_Skip_Request_Telemetry_When_Filtered_Out() { // given var channel = new MockTelemetryChannel(); var request = Mock.Of <IOwinRequest>(r => r.Method == "GET" && r.Path == new PathString("/path") && r.Uri == new Uri("http://google.com/path") ); var response = Mock.Of <IOwinResponse>(r => r.StatusCode == 200); var context = new MockOwinContextBuilder() .WithRequest(request) .WithResponse(response) .Build(); var configuration = new TelemetryConfigurationBuilder() .WithChannel(channel) .Build(); var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new NoopMiddleware(), configuration, (req, resp) => false), new OperationIdContextMiddlewareConfiguration()); // when await sut.Invoke(context); // then channel.SentTelemetries.Count.Should().Be(0); }
public async Task LEGACY_Should_Send_Request_Telemetry_When_Not_Filtered_Out() { // given var context = new MockOwinContextBuilder().Build(); var configuration = new TelemetryConfigurationBuilder().Build(); var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new NoopMiddleware(), configuration, (req, resp) => true), new OperationIdContextMiddlewareConfiguration()); // when await sut.Invoke(context); // then var channel = configuration.TelemetryChannel as MockTelemetryChannel; channel.SentTelemetries.Count.Should().Be(1); var telemetry = channel.SentTelemetries.First() as RequestTelemetry; telemetry.Should().NotBeNull(); }
public void Can_Clear_Operation_Context_In_Owin_Environment() { var owinContext = new MockOwinContextBuilder().Build(); using (new OperationContextScope("opid", "parentid")) using (new OperationContextStoredInOwinContextScope(owinContext)) { } owinContext.Get <string>(Consts.OperationIdContextKey).Should().BeNull(); owinContext.Get <string>(Consts.ParentOperationIdContextKey).Should().BeNull(); }
public async Task Should_Add_Properties_To_Request_Telemetry_Context_When_They_Are_Provided() { // given var channel = new MockTelemetryChannel(); var request = Mock.Of <IOwinRequest>(r => r.Method == "GET" && r.Path == new PathString("/path") && r.Uri == new Uri("http://google.com/path") ); var response = Mock.Of <IOwinResponse>(r => r.StatusCode == 200); var context = new MockOwinContextBuilder() .WithRequest(request) .WithResponse(response) .Build(); var configuration = new TelemetryConfigurationBuilder() .WithChannel(channel) .Build(); var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new NoopMiddleware(), configuration, getContextProperties: (req, res) => new[] { new KeyValuePair <string, string>("key1", "val1"), new KeyValuePair <string, string>("key2", "val2"), }), new OperationIdContextMiddlewareConfiguration()); // when await sut.Invoke(context); // then channel.SentTelemetries.Count.Should().Be(1); var telemetry = channel.SentTelemetries.First() as RequestTelemetry; telemetry.Should().NotBeNull(); telemetry.HttpMethod.Should().Be("GET"); telemetry.Name.Should().Be("GET /path"); telemetry.Context.Operation.Name.Should().Be("GET /path"); telemetry.Id.Should().NotBeNullOrEmpty(); telemetry.Success.Should().BeTrue(); telemetry.Url.Should().Be(new Uri("http://google.com/path")); telemetry.StartTime.Date.Should().Be(DateTimeOffset.Now.Date); telemetry.Context.Properties.Should().Contain(new[] { new KeyValuePair <string, string>("key1", "val1"), new KeyValuePair <string, string>("key2", "val2"), }); }
public async Task Can_Clean_Context() { var context = new MockOwinContextBuilder().Build(); var sut = new OperationIdContextMiddleware( new NoopMiddleware(), new OperationIdContextMiddlewareConfiguration()); await sut.Invoke(context); context.Get <string>(Consts.OperationIdContextKey).Should().BeNull(); context.Get <string>(Consts.ParentOperationIdContextKey).Should().BeNull(); OperationContext.Get().Should().BeNull(); }
public async Task LEGACY_On_Exception_Should_Pass_It_Further() { // given var configuration = new TelemetryConfigurationBuilder().Build(); var context = new MockOwinContextBuilder().Build(); var sut = new HttpRequestTrackingMiddleware(new ThrowingMiddleware(), configuration); // when / then Func <Task> sutAction = async() => await sut.Invoke(context); sutAction.ShouldThrow <OlabogaException>(); }
public async Task Can_Handle_Async_Additional_Properties_Delegate() { // given var context = new MockOwinContextBuilder().Build(); var configuration = new TelemetryConfigurationBuilder().Build(); var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new NoopMiddleware(), new RequestTrackingConfiguration { TelemetryConfiguration = configuration, GetAdditionalContextProperties = async ctx => { await Task.Delay(100); return(new[] { new KeyValuePair <string, string>("key1", "val1"), new KeyValuePair <string, string>("key2", "val2") }); } }), new OperationIdContextMiddlewareConfiguration()); // when await sut.Invoke(context); // then var channel = configuration.TelemetryChannel as MockTelemetryChannel; channel.SentTelemetries.Count.Should().Be(1); var telemetry = channel.SentTelemetries.First() as RequestTelemetry; telemetry.Should().NotBeNull(); telemetry.Context.Properties.Should().Contain(new[] { new KeyValuePair <string, string>("key1", "val1"), new KeyValuePair <string, string>("key2", "val2"), }); VerifyDefaultsOnRequestTelemetry(telemetry); telemetry.ResponseCode.Should().Be("200"); telemetry.Success.Should().BeTrue(); }
public async Task Can_Send_Request_Telemetry() { // given var channel = new MockTelemetryChannel(); var request = Mock.Of <IOwinRequest>(r => r.Method == "GET" && r.Path == new PathString("/path") && r.Uri == new Uri("http://google.com/path") ); var response = Mock.Of <IOwinResponse>(r => r.StatusCode == 200); var context = new MockOwinContextBuilder() .WithRequest(request) .WithResponse(response) .Build(); var configuration = new TelemetryConfigurationBuilder() .WithChannel(channel) .Build(); var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new NoopMiddleware(), configuration), new OperationIdContextMiddlewareConfiguration()); // when await sut.Invoke(context); // then channel.SentTelemetries.Count.Should().Be(1); var telemetry = channel.SentTelemetries.First() as RequestTelemetry; telemetry.Should().NotBeNull(); telemetry.HttpMethod.Should().Be("GET"); telemetry.Name.Should().Be("GET /path"); telemetry.Context.Operation.Name.Should().Be("GET /path"); telemetry.Id.Should().NotBeNullOrEmpty(); telemetry.Success.Should().BeTrue(); telemetry.Url.Should().Be(new Uri("http://google.com/path")); telemetry.StartTime.Date.Should().Be(DateTimeOffset.Now.Date); }
public async Task Can_Send_Request_Telemetry() { // given var channel = new MockTelemetryChannel(); var request = Mock.Of<IOwinRequest>(r => r.Method == "GET" && r.Path == new PathString("/path") && r.Uri == new Uri("http://google.com/path") ); var response = Mock.Of<IOwinResponse>(r => r.StatusCode == 200); var context = new MockOwinContextBuilder() .WithRequest(request) .WithResponse(response) .Build(); var configuration = new TelemetryConfigurationBuilder() .WithChannel(channel) .Build(); var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new NoopMiddleware(), configuration), new OperationIdContextMiddlewareConfiguration()); // when await sut.Invoke(context); // then channel.SentTelemetries.Count.Should().Be(1); var telemetry = channel.SentTelemetries.First() as RequestTelemetry; telemetry.Should().NotBeNull(); telemetry.HttpMethod.Should().Be("GET"); telemetry.Name.Should().Be("GET /path"); telemetry.Context.Operation.Name.Should().Be("GET /path"); telemetry.Id.Should().NotBeNullOrEmpty(); telemetry.Success.Should().BeTrue(); telemetry.Url.Should().Be(new Uri("http://google.com/path")); telemetry.StartTime.Date.Should().Be(DateTimeOffset.Now.Date); }
public async Task On_Exception_Should_Log_500_Request_And_Exception_Telemetry() { // given var context = new MockOwinContextBuilder().Build(); var configuration = new TelemetryConfigurationBuilder() .WithTelemetryInitializer(new OperationIdTelemetryInitializer()) .Build(); var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new ThrowingMiddleware(), new RequestTrackingConfiguration { TelemetryConfiguration = configuration }), new OperationIdContextMiddlewareConfiguration()); // when try { await sut.Invoke(context); } catch (OlabogaException) { // ignored } // then var channel = configuration.TelemetryChannel as MockTelemetryChannel; channel.SentTelemetries.Count.Should().Be(2); var requestTelemetry = channel.SentTelemetries.First(t => t is RequestTelemetry) as RequestTelemetry; requestTelemetry.Success.Should().BeFalse(); requestTelemetry.ResponseCode.Should().Be("500"); VerifyDefaultsOnRequestTelemetry(requestTelemetry); var exceptionTelemetry = channel.SentTelemetries.First(t => t is ExceptionTelemetry) as ExceptionTelemetry; exceptionTelemetry.Context.Operation.Id.Should().NotBeNullOrEmpty(); exceptionTelemetry.Exception.Should().BeOfType <OlabogaException>(); }
public async Task Should_Add_Properties_To_Request_Telemetry_Context_When_They_Are_Provided() { // given var context = new MockOwinContextBuilder().Build(); var configuration = new TelemetryConfigurationBuilder().Build(); var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new NoopMiddleware(), new RequestTrackingConfiguration { TelemetryConfiguration = configuration, GetAdditionalContextProperties = ctx => Task.FromResult(new[] { new KeyValuePair <string, string>("key1", "val1"), new KeyValuePair <string, string>("key2", "val2") }.AsEnumerable()) }), new OperationIdContextMiddlewareConfiguration()); // when await sut.Invoke(context); // then var channel = configuration.TelemetryChannel as MockTelemetryChannel; channel.SentTelemetries.Count.Should().Be(1); var telemetry = channel.SentTelemetries.First() as RequestTelemetry; telemetry.Should().NotBeNull(); telemetry.Context.Properties.Should().Contain(new[] { new KeyValuePair <string, string>("key1", "val1"), new KeyValuePair <string, string>("key2", "val2"), }); VerifyDefaultsOnRequestTelemetry(telemetry); telemetry.ResponseCode.Should().Be("200"); telemetry.Success.Should().BeTrue(); }
public async Task Can_Pass_Request_Details_For_Filtering() { // given var channel = new MockTelemetryChannel(); var request = Mock.Of <IOwinRequest>(r => r.Method == "GET" && r.Path == new PathString("/path") && r.Uri == new Uri("http://google.com/path") ); var response = Mock.Of <IOwinResponse>(r => r.StatusCode == 200); var context = new MockOwinContextBuilder() .WithRequest(request) .WithResponse(response) .Build(); var configuration = new TelemetryConfigurationBuilder() .WithChannel(channel) .Build(); IOwinRequest filteredRequest = null; IOwinResponse filteredResponse = null; var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new NoopMiddleware(), configuration, (req, resp) => { filteredRequest = req; filteredResponse = resp; return(false); }), new OperationIdContextMiddlewareConfiguration()); // when await sut.Invoke(context); // then filteredRequest.ShouldBeEquivalentTo(request); filteredResponse.ShouldBeEquivalentTo(response); }
public async Task Should_Establish_New_OperationContext_With_ReqestId_As_ParentId() { // given var context = new MockOwinContextBuilder().Build(); var configuration = new TelemetryConfigurationBuilder().Build(); var actual = new OperationContextCollectingMiddleware(); var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( actual, new RequestTrackingConfiguration { TelemetryConfiguration = configuration, RequestIdFactory = _ => "requestid" }), new OperationIdContextMiddlewareConfiguration { OperationIdFactory = _ => "operationid" }); // when await sut.Invoke(context); // then var channel = configuration.TelemetryChannel as MockTelemetryChannel; var telemetry = channel.SentTelemetries.First() as RequestTelemetry; telemetry.Should().NotBeNull(); telemetry.Id.Should().Be("requestid"); actual.ParentOperationIdFromAmbientContext.Should().Be("requestid"); actual.ParentOperationIdFromEnvironment.Should().Be("requestid"); actual.OperationIdFromEnvironment.Should().Be("operationid"); actual.OperationIdFromAmbientContext.Should().Be("operationid"); }
public void Can_Work_With_Subscopes() { var owinContext = new MockOwinContextBuilder().Build(); using (new OperationContextScope("opid", "op1")) using (new OperationContextStoredInOwinContextScope(owinContext)) { owinContext.Get <string>(Consts.OperationIdContextKey).Should().Be("opid"); owinContext.Get <string>(Consts.ParentOperationIdContextKey).Should().Be("op1"); using (new OperationContextScope("opid", "op2")) using (new OperationContextStoredInOwinContextScope(owinContext)) { owinContext.Get <string>(Consts.OperationIdContextKey).Should().Be("opid"); owinContext.Get <string>(Consts.ParentOperationIdContextKey).Should().Be("op2"); } owinContext.Get <string>(Consts.OperationIdContextKey).Should().Be("opid"); owinContext.Get <string>(Consts.ParentOperationIdContextKey).Should().Be("op1"); } owinContext.Get <string>(Consts.OperationIdContextKey).Should().BeNull(); owinContext.Get <string>(Consts.ParentOperationIdContextKey).Should().BeNull(); }
public async Task Should_Send_Request_Telemetry_When_Not_Filtered_Out() { // given var channel = new MockTelemetryChannel(); var request = Mock.Of<IOwinRequest>(r => r.Method == "GET" && r.Path == new PathString("/path") && r.Uri == new Uri("http://google.com/path") ); var response = Mock.Of<IOwinResponse>(r => r.StatusCode == 200); var context = new MockOwinContextBuilder() .WithRequest(request) .WithResponse(response) .Build(); var configuration = new TelemetryConfigurationBuilder() .WithChannel(channel) .Build(); var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new NoopMiddleware(), configuration, (req, resp) => true), new OperationIdContextMiddlewareConfiguration()); // when await sut.Invoke(context); // then channel.SentTelemetries.Count.Should().Be(1); var telemetry = channel.SentTelemetries.First() as RequestTelemetry; telemetry.Should().NotBeNull(); }
public async Task Can_Pass_Request_Details_For_Filtering() { // given var channel = new MockTelemetryChannel(); var request = Mock.Of<IOwinRequest>(r => r.Method == "GET" && r.Path == new PathString("/path") && r.Uri == new Uri("http://google.com/path") ); var response = Mock.Of<IOwinResponse>(r => r.StatusCode == 200); var context = new MockOwinContextBuilder() .WithRequest(request) .WithResponse(response) .Build(); var configuration = new TelemetryConfigurationBuilder() .WithChannel(channel) .Build(); IOwinRequest filteredRequest = null; IOwinResponse filteredResponse = null; var sut = new OperationIdContextMiddleware( new HttpRequestTrackingMiddleware( new NoopMiddleware(), configuration, (req, resp) => { filteredRequest = req; filteredResponse = resp; return false; }), new OperationIdContextMiddlewareConfiguration()); // when await sut.Invoke(context); // then filteredRequest.ShouldBeEquivalentTo(request); filteredResponse.ShouldBeEquivalentTo(response); }
public async Task Can_Send_Request_Telemetry(int statusCode, bool expectedSuccess) { // given var channel = new MockTelemetryChannel(); var response = Mock.Of<IOwinResponse>(r => r.StatusCode == statusCode); var context = new MockOwinContextBuilder() .WithResponse(response) .Build(); var configuration = new TelemetryConfigurationBuilder() .WithChannel(channel) .Build(); var sut = new HttpRequestTrackingMiddleware(new NoopMiddleware(), configuration); // when await sut.Invoke(context); // then var telemetry = channel.SentTelemetries.First() as RequestTelemetry; telemetry.Success.Should().Be(expectedSuccess); }