private void ValidateIntrinsics(TraceContextTestData testData) { if (testData.IntrinsicSettings != null) { foreach (var eventType in testData.IntrinsicSettings?.TargetEvents) { switch (eventType) { case "Transaction": var transactionEvent = _compositeTestAgent.TransactionEvents?.First(); var txAttrs = testData.IntrinsicSettings.Events != null? testData.IntrinsicSettings.Events.ContainsKey("Transaction") ? testData.IntrinsicSettings.Events?["Transaction"] : null : null; ValidateAttributes(transactionEvent.IntrinsicAttributes(), testData, txAttrs); break; case "Span": Assert.That(_compositeTestAgent.SpanEvents.Count, Is.EqualTo(2)); var spanEvent = _compositeTestAgent.SpanEvents.First(); ValidateAttributes(spanEvent.IntrinsicAttributes(), testData, testData.IntrinsicSettings.Events?["Span"]); break; case "Error": var errorEvent = _compositeTestAgent.ErrorEvents.First(); ValidateAttributes(errorEvent.IntrinsicAttributes(), testData, testData.IntrinsicSettings.Events?["Error"]); break; default: break; } } } }
List <IEnumerable <KeyValuePair <string, string> > > MakeHeaders(TraceContextTestData testData) { List <IEnumerable <KeyValuePair <string, string> > > inboundHeaderSets = new List <IEnumerable <KeyValuePair <string, string> > >(); testData.InboundHeaders?.ToList().ForEach (headerSet => { var ingestHeaderSet = new List <KeyValuePair <string, string> >(); headerSet.Headers.ForEach (header => { if (header.Key.Equals("newrelic")) { var settingPayload = header.Value.ToObject <string>(); ingestHeaderSet.Add(new KeyValuePair <string, string>(header.Key, settingPayload)); } else { ingestHeaderSet.Add(new KeyValuePair <string, string>(header.Key, header.Value.ToString())); } }); inboundHeaderSets.Add(ingestHeaderSet); }); if (inboundHeaderSets.Count == 0) { // test requires AcceptPayloads to be called even if there are no headers inboundHeaderSets.Add(new List <KeyValuePair <string, string> >()); } return(inboundHeaderSets); }
public void TraceContext_CrossAgentTests(TraceContextTestData testData) { InitializeSettings(testData); MakeTransaction(testData); _compositeTestAgent.Harvest(); ValidateIntrinsics(testData); ValidateMetrics(testData); }
private void ValidateMetrics(TraceContextTestData testData) { var expectedMetrics = new List <ExpectedMetric>(); // convert json ExpectedMetrics for CompositeTests.MetricAssertions foreach (var metric in testData.ExpectedMetrics) { expectedMetrics.Add(new ExpectedTimeMetric() { Name = metric.Name, CallCount = metric.Count }); } MetricAssertions.MetricsExist(expectedMetrics, _compositeTestAgent.Metrics); }
void MakeTransaction(TraceContextTestData testData) { var testDataInboundHeaderSets = MakeHeaders(testData); var transaction = _agent.CreateTransaction( isWeb: testData.WebTransaction, category: testData.WebTransaction ? "Action" : "Other", transactionDisplayName: "name", doNotTrackAsUnitOfWork: true); testDataInboundHeaderSets?.ForEach (headerSet => { AcceptPayloads(headerSet, testData); }); var segment = _agent.StartTransactionSegmentOrThrow("segmentName"); if (testData.RaisesException) { transaction.NoticeError(new Exception("This is a new exception.")); } Dictionary <string, string> insertedHeaders = new Dictionary <string, string>(); var setHeaders = new Action <Dictionary <string, string>, string, string>((carrier, key, value) => { carrier.Add(key, value); }); testData.OutboundPayloadsSettings?.ForEach(payloadSettings => { _agent.CurrentTransaction.InsertDistributedTraceHeaders(insertedHeaders, setHeaders); if (testData.OutboundPayloadsSettings != null) { ValidateOutboundHeaders(payloadSettings, insertedHeaders, testData.TrustedAccountKey); } insertedHeaders.Clear(); }); segment.End(); transaction.End(); }
private static void InitializeSettings(TraceContextTestData testData) { if (testData.ForceSampledTrue) { var priority = 1.0f; Mock.Arrange(() => _adaptiveSampler.ComputeSampled(ref priority)).IgnoreArguments().Returns(true); } _compositeTestAgent.LocalConfiguration.spanEvents.enabled = testData.SpanEventsEnabled; var defaultTransactionEventsEnabled = _compositeTestAgent.LocalConfiguration.transactionEvents.enabled; _compositeTestAgent.LocalConfiguration.transactionEvents.enabled = testData.TransactionEventsEnabled.HasValue ? testData.TransactionEventsEnabled.Value : defaultTransactionEventsEnabled; _compositeTestAgent.ServerConfiguration.TrustedAccountKey = testData.TrustedAccountKey; _compositeTestAgent.ServerConfiguration.AccountId = testData.AccountId; _compositeTestAgent.ServerConfiguration.PrimaryApplicationId = "primaryApplicationId"; _compositeTestAgent.PushConfiguration(); }
private void ValidateAttributes(IDictionary <string, object> actualAttributes, TraceContextTestData testData, JToken eventSpecificAttributes = null) { // Common (for all target_events) ValidateAttributeSettings(testData.IntrinsicSettings.CommonAttributes, actualAttributes); // event-specific attrs ValidateAttributeSettings(eventSpecificAttributes?.ToObject <AttributesSettings>(), actualAttributes); }
private void AcceptPayloads(IEnumerable <KeyValuePair <string, string> > testDataInboundHeaders, TraceContextTestData testData) { var isValidEnumValue = Enum.TryParse(testData.TransportType, ignoreCase: false, result: out TransportType transportType); if (!isValidEnumValue) { transportType = (TransportType)(-1); } _agent.CurrentTransaction.AcceptDistributedTraceHeaders(testDataInboundHeaders, GetHeaderValue, transportType); IEnumerable <string> GetHeaderValue(IEnumerable <KeyValuePair <string, string> > carrier, string key) { List <string> results = new List <string>(); foreach (KeyValuePair <string, string> header in carrier) { if (header.Key == key) { results.Add(header.Value); } } return(results.Any() ? results : null); } }