public async Task SubscribeUnsubscribe() { var mockPayloadSender = new MockPayloadSender(); var agent = new ApmAgent(new TestAgentComponents(payloadSender: mockPayloadSender)); var subscriber = new HttpDiagnosticsSubscriber(); using (var localServer = LocalServer.Create()) { var url = localServer.Uri; using (agent.Subscribe(subscriber)) //subscribe { await agent.Tracer.CaptureTransaction("TestTransaction", "TestType", async t => { Thread.Sleep(5); var httpClient = new HttpClient(); try { await httpClient.GetAsync(url); } catch (Exception e) { t.CaptureException(e); } }); } //and then unsubscribe mockPayloadSender.WaitForAny(); mockPayloadSender.Clear(); await agent.Tracer.CaptureTransaction("TestTransaction", "TestType", async t => { Thread.Sleep(5); var httpClient = new HttpClient(); try { await httpClient.GetAsync(url); } catch (Exception e) { t.CaptureException(e); } }); mockPayloadSender.WaitForTransactions(); mockPayloadSender.FirstTransaction.Should().NotBeNull(); mockPayloadSender.SignalEndSpans(); mockPayloadSender.WaitForSpans(); mockPayloadSender.SpansOnFirstTransaction.Should().BeEmpty(); } }
public async Task SubscribeAndUnsubscribeAspNetCoreDiagnosticListener() { //For reference: unsubscribing from AspNetCoreDiagnosticListener does not seem to work. //TODO: this should be investigated. This is more relevant for testing. // using (_agent.Subscribe(new AspNetCoreDiagnosticsSubscriber())) // { // await _client.GetAsync("/Home/TriggerError"); // // _capturedPayload.Transactions.Should().ContainSingle(); // // _capturedPayload.Errors.Should().NotBeEmpty(); // _capturedPayload.Errors.Should().ContainSingle(); // _capturedPayload.Errors[0].CapturedException.Type.Should().Be(typeof(Exception).FullName); // } //here we unsubsribe, so no errors should be captured after this line. _agent.Dispose(); _capturedPayload.Clear(); await _client.GetAsync("/Home/TriggerError"); _capturedPayload.Transactions.Should().ContainSingle(); _capturedPayload.Errors.Should().BeEmpty(); }
public async Task ChangeSanitizeFieldNamesAfterStart(bool useDiagnosticSourceOnly) { var startConfigSnapshot = new MockConfigSnapshot(new NoopLogger()); _capturedPayload = new MockPayloadSender(); var agentComponents = new TestAgentComponents( _logger, startConfigSnapshot, _capturedPayload, new CurrentExecutionSegmentsContainer()); _agent = new ApmAgent(agentComponents); _client = Helper.GetClient(_agent, _factory, useDiagnosticSourceOnly); _client.DefaultRequestHeaders.Add("foo", "bar"); await _client.GetAsync("/Home/SimplePage"); _capturedPayload.WaitForTransactions(); _capturedPayload.Transactions.Should().ContainSingle(); _capturedPayload.FirstTransaction.Context.Should().NotBeNull(); _capturedPayload.FirstTransaction.Context.Request.Should().NotBeNull(); _capturedPayload.FirstTransaction.Context.Request.Headers.Should().NotBeNull(); _capturedPayload.FirstTransaction.Context.Request.Headers["foo"].Should().Be("bar"); _capturedPayload.Clear(); //change config to sanitize headers with "foo" var updateConfigSnapshot = new MockConfigSnapshot( new NoopLogger() , sanitizeFieldNames: "foo" ); _agent.ConfigStore.CurrentSnapshot = updateConfigSnapshot; await _client.GetAsync("/Home/SimplePage"); _capturedPayload.WaitForTransactions(); _capturedPayload.Transactions.Should().ContainSingle(); _capturedPayload.FirstTransaction.Context.Should().NotBeNull(); _capturedPayload.FirstTransaction.Context.Request.Should().NotBeNull(); _capturedPayload.FirstTransaction.Context.Request.Headers.Should().NotBeNull(); _capturedPayload.FirstTransaction.Context.Request.Headers["foo"].Should().Be("[REDACTED]"); }
public async Task SubscribeAndUnsubscribeSmartSqlDiagnosticListener() { using (_agent.Subscribe(new SmartSqlDiagnosticsSubscriber())) { var response = await _client.GetAsync("/Home/Index"); _capturedPayload.Transactions.Should().ContainSingle(); _capturedPayload.SpansOnFirstTransaction.Should() .NotBeEmpty() .And.Contain(n => n.Context.Db != null); } //here we unsubscribe, so no errors should be captured after this line. _capturedPayload.Clear(); await _client.GetAsync("/Home/Index"); _capturedPayload.Transactions.Should().ContainSingle(); _capturedPayload.SpansOnFirstTransaction.Should().BeEmpty(); }