コード例 #1
0
        private void ValidateOutboundPayload(OutboundPayloadSettings payloadSettings, string serializedPayload)
        {
            var jObjectActual = JObject.Parse(serializedPayload);

            var expectedFields = payloadSettings.Exact;

            if (expectedFields != null)
            {
                foreach (var key in expectedFields.Keys)
                {
                    var expectedValue = expectedFields[key];
                    var actualValue   = jObjectActual.SelectToken(key);
                    Assert.That(actualValue, Is.EqualTo(expectedValue), $"{key}");
                }
            }

            payloadSettings.Expected?.ForEach(expected =>
            {
                Assert.That(jObjectActual.SelectToken(expected), Is.Not.Null, $"{expected}");
            });

            payloadSettings.Unexpected?.ForEach(unexpected =>
            {
                Assert.That(jObjectActual.SelectToken(unexpected), Is.Null, $"{unexpected}");
            });
        }
コード例 #2
0
        private void ValidateOutboundHeaders(OutboundPayloadSettings payloadSettings, Dictionary <string, string> actualOutboundHeaders, string trustedAccountKey)
        {
            JObject newrelicHeaderValue = null;
            JObject newrelicJson        = null;
            JObject traceparentJson     = null;
            JObject tracestateJson      = null;

            if (actualOutboundHeaders.ContainsKey("newrelic"))
            {
                newrelicHeaderValue = JObject.Parse(Strings.Base64Decode(actualOutboundHeaders["newrelic"]));

                newrelicJson = new JObject
                {
                    { "newrelic", newrelicHeaderValue }
                };
            }
            if (actualOutboundHeaders.ContainsKey("traceparent"))
            {
                var fields = actualOutboundHeaders["traceparent"].Split('-');
                traceparentJson = new JObject
                {
                    { "traceparent", new JObject
                      {
                          { "version", fields[0] },
                          { "trace_id", fields[1] },
                          { "parent_id", fields[2] },
                          { "trace_flags", fields[3] }
                      } }
                };
            }
            if (actualOutboundHeaders.ContainsKey("tracestate"))
            {
                var tracestate  = W3CTracestate.GetW3CTracestateFromHeaders(new string[] { actualOutboundHeaders["tracestate"] }, trustedAccountKey);
                var headerValue = actualOutboundHeaders["tracestate"];
                var tenantId    = headerValue.Substring(0, headerValue.IndexOf('@'));
                tracestateJson = new JObject
                {
                    { "tracestate", new JObject
                      {
                          { "version", tracestate.Version },
                          { "parent_type", (int)tracestate.ParentType },
                          { "parent_account_id", tracestate.AccountId },
                          { "parent_application_id", tracestate.AppId },
                          { "span_id", tracestate.SpanId },
                          { "transaction_id", tracestate.TransactionId },
                          { "sampled", tracestate.Sampled },
                          { "priority", string.Format("{0:0.######}", tracestate.Priority) },  // cheating here: priority is stored as float, which may show in scientific notation; this formatting is performed when creating a new tracestate header in the agent so it will not be transmitted in scientific notation
                          { "timestamp", tracestate.Timestamp },
                          { "tenant_id", tenantId },
                          { "vendors", new JArray(tracestate.VendorstateEntries.Select(vse => vse.Split('=')[0]).ToList()) }
                      } }
                };
            }

            JToken actualValue = null;

            var exactFields = payloadSettings.Exact;

            if (exactFields != null)
            {
                foreach (var key in exactFields.Keys)
                {
                    var expectedValue = exactFields[key];

                    switch (key.Substring(0, key.IndexOf('.')))
                    {
                    case "newrelic":
                        actualValue = newrelicJson.SelectToken(key);
                        break;

                    case "traceparent":
                        actualValue = traceparentJson.SelectToken(key);
                        break;

                    case "tracestate":
                        actualValue = tracestateJson.SelectToken(key);
                        break;

                    default:
                        break;
                    }

                    Assert.That(actualValue.IsEqualTo(expectedValue), $"{key}, expected: {expectedValue}, actual: {actualValue}");
                }
            }

            payloadSettings.Expected?.ForEach(expected =>
            {
                switch (expected.Substring(0, expected.IndexOf('.')))
                {
                case "newrelic":
                    Assert.That(newrelicJson.SelectToken(expected), Is.Not.Null, $"Missing expected: {expected}");
                    break;

                case "traceparent":
                    Assert.That(traceparentJson.SelectToken(expected), Is.Not.Null, $"Missing expected: {expected}");
                    break;

                case "tracestate":
                    Assert.That(tracestateJson.SelectToken(expected), Is.Not.Null, $"Missing expected: {expected}");
                    break;

                default:
                    break;
                }
            });

            payloadSettings.Unexpected?.ForEach(unexpected =>
            {
                switch (unexpected.Substring(0, unexpected.IndexOf('.')))
                {
                case "newrelic":
                    Assert.That(newrelicJson.SelectToken(unexpected), Is.Null, $"Unexpected exists: {unexpected}");
                    break;

                case "traceparent":
                    Assert.That(traceparentJson.SelectToken(unexpected), Is.Empty, $"Unexpected exists: {unexpected}");
                    break;

                case "tracestate":
                    Assert.That(tracestateJson.SelectToken(unexpected), Is.Empty, $"Unexpected exists: {unexpected}");
                    break;

                default:
                    break;
                }
            });

            var notequalFields = payloadSettings.Notequal;

            if (notequalFields != null)
            {
                foreach (var key in notequalFields.Keys)
                {
                    var notValue = notequalFields[key];

                    switch (key.Substring(0, key.IndexOf('.')))
                    {
                    case "newrelic":
                        actualValue = newrelicJson.SelectToken(key);
                        break;

                    case "traceparent":
                        actualValue = traceparentJson.SelectToken(key);
                        break;

                    case "tracestate":
                        actualValue = tracestateJson.SelectToken(key);
                        break;

                    default:
                        break;
                    }

                    Assert.That(actualValue.IsNotEqualTo(notValue), $"Expected not equal {key}, but was equal {notValue}");
                }
            }

            if (payloadSettings.Vendors != null)
            {
                JArray actualVendors = (JArray)tracestateJson["tracestate"]["vendors"];

                Assert.That(JToken.DeepEquals(actualVendors, payloadSettings.Vendors), $"Expected vendors {payloadSettings.Vendors}, actual: {actualVendors}");
            }
        }