public void TransactionContextShouldBeSerializedOnlyWhenSampled() { var agent = new TestAgentComponents(); // Create a transaction that is sampled (because the sampler is constant sampling-everything sampler var sampledTransaction = new Transaction(agent.Logger, "dummy_name", "dumm_type", new Sampler(1.0), /* distributedTracingData: */ null, agent.PayloadSender, new MockConfigSnapshot(new NoopLogger()), agent.TracerInternal.CurrentExecutionSegmentsContainer, MockApmServerInfo.Version710); sampledTransaction.Context.Request = new Request("GET", new Url { Full = "https://elastic.co", Raw = "https://elastic.co", HostName = "elastic.co", Protocol = "HTTP" }); // Create a transaction that is not sampled (because the sampler is constant not-sampling-anything sampler var nonSampledTransaction = new Transaction(agent.Logger, "dummy_name", "dumm_type", new Sampler(0.0), /* distributedTracingData: */ null, agent.PayloadSender, new MockConfigSnapshot(new NoopLogger()), agent.TracerInternal.CurrentExecutionSegmentsContainer, MockApmServerInfo.Version710); nonSampledTransaction.Context.Request = sampledTransaction.Context.Request; var serializedSampledTransaction = _payloadItemSerializer.Serialize(sampledTransaction); var deserializedSampledTransaction = JsonConvert.DeserializeObject <JObject>(serializedSampledTransaction); var serializedNonSampledTransaction = _payloadItemSerializer.Serialize(nonSampledTransaction); var deserializedNonSampledTransaction = JsonConvert.DeserializeObject <JObject>(serializedNonSampledTransaction); // ReSharper disable once PossibleNullReferenceException deserializedSampledTransaction["sampled"].Value <bool>().Should().BeTrue(); deserializedSampledTransaction["context"]["request"]["url"]["full"] .Value <string>() .Should() .Be("https://elastic.co"); // ReSharper disable once PossibleNullReferenceException deserializedNonSampledTransaction["sampled"].Value <bool>().Should().BeFalse(); deserializedNonSampledTransaction.Should().NotContainKey("context"); }
public void TransactionContextShouldBeSerializedOnlyWhenSampled() { var agent = new TestAgentComponents(); // Create a transaction that is sampled (because the sampler is constant sampling-everything sampler var sampledTransaction = new Transaction(agent.Logger, "dummy_name", "dumm_type", new Sampler(1.0), null, agent.PayloadSender); sampledTransaction.Context.Request = new Request("GET", new Url { Full = "https://elastic.co", Raw = "https://elastic.co", HostName = "elastic.co", Protocol = "HTTP" }); // Create a transaction that is not sampled (because the sampler is constant not-sampling-anything sampler var nonSampledTransaction = new Transaction(agent.Logger, "dummy_name", "dumm_type", new Sampler(0.0), null, agent.PayloadSender); nonSampledTransaction.Context.Request = sampledTransaction.Context.Request; var serializedSampledTransaction = SerializePayloadItem(sampledTransaction); var deserializedSampledTransaction = JsonConvert.DeserializeObject(serializedSampledTransaction) as JObject; var serializedNonSampledTransaction = SerializePayloadItem(nonSampledTransaction); var deserializedNonSampledTransaction = JsonConvert.DeserializeObject(serializedNonSampledTransaction) as JObject; deserializedSampledTransaction["sampled"].Value <bool>().Should().BeTrue(); deserializedSampledTransaction["context"].Value <JObject>()["request"].Value <JObject>()["url"].Value <JObject>()["full"] .Should() .Equals("https://elastic.co"); deserializedNonSampledTransaction["sampled"].Value <bool>().Should().BeFalse(); deserializedNonSampledTransaction.Should().NotContainKey("context"); }
public void IsConfigured() { Agent.IsConfigured.Should().BeFalse(); using var agentComponents = new TestAgentComponents(); Agent.Setup(agentComponents); Agent.IsConfigured.Should().BeTrue(); }
public MongoApmTests(MongoFixture <MongoConfiguration, BsonDocument> fixture) { _documents = fixture.Collection ?? throw new ArgumentNullException(nameof(fixture.Collection)); _payloadSender = new MockPayloadSender(); var config = new TestAgentComponents(configuration: new MockConfiguration(transactionSampleRate: "1.0", transactionMaxSpans: "50"), payloadSender: _payloadSender); _agent = new ApmAgent(config); _agent.Subscribe(new MongoDbDiagnosticsSubscriber()); }
internal static (IDisposable, MockPayloadSender, ApmAgent) RegisterListenerAndStartTransaction() { var payloadSender = new MockPayloadSender(); var agentComponents = new TestAgentComponents(payloadSender: payloadSender, config: new MockConfigSnapshot(logLevel: "Debug", stackTraceLimit: "-1", spanFramesMinDurationInMilliseconds: "-1ms")); var agent = new ApmAgent(agentComponents); var sub = agent.Subscribe(new HttpDiagnosticsSubscriber()); StartTransaction(agent); return (sub, payloadSender, agent); }
public async Task ChangeTransactionIgnoreUrlsAfterStart(bool useDiagnosticSourceOnly) { // Start with default config 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.Request.Url.Full.ToLower().Should().Contain("simplepage"); //change config to ignore urls with SimplePage var updateConfigSnapshot = new MockConfigSnapshot( new NoopLogger() , transactionIgnoreUrls: "*SimplePage*" ); _agent.ConfigStore.CurrentSnapshot = updateConfigSnapshot; await _client.GetAsync("/Home/SimplePage"); _capturedPayload.WaitForTransactions(TimeSpan.FromSeconds(5)); //assert that no more transaction is captured - so still 1 captured transaction _capturedPayload.Transactions.Should().ContainSingle(); //update config again updateConfigSnapshot = new MockConfigSnapshot( new NoopLogger() , transactionIgnoreUrls: "FooBar" ); _agent.ConfigStore.CurrentSnapshot = updateConfigSnapshot; await _client.GetAsync("/Home/SimplePage"); _capturedPayload.WaitForTransactions(); //assert that the number of captured transaction increased _capturedPayload.Transactions.Count.Should().Be(2); }
private void CreateAgent(string sanitizeFieldNames = null) { var configSnapshot = sanitizeFieldNames == null ? new MockConfigSnapshot(_logger, captureBody: "all") : new MockConfigSnapshot(_logger, captureBody: "all", sanitizeFieldNames: sanitizeFieldNames); _capturedPayload = new SerializerMockPayloadSender(configSnapshot); var agentComponents = new TestAgentComponents( _logger, configSnapshot, _capturedPayload, new CurrentExecutionSegmentsContainer()); _agent = new ApmAgent(agentComponents); _client = Helper.GetClient(_agent, _factory); }
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]"); }