public static void TransactionTraceHasAttributes(IEnumerable <string> expectedAttributes, TransactionTraceAttributeType attributeType, TransactionSample sample)
        {
            var succeeded        = true;
            var builder          = new StringBuilder();
            var actualAttributes = sample.TraceData.Attributes.GetByType(attributeType);

            foreach (var expectedAttribute in expectedAttributes)
            {
                if (!actualAttributes.ContainsKey(expectedAttribute))
                {
                    builder.AppendFormat("Attribute named {0} was not found in the transaction sample.", expectedAttribute);
                    builder.AppendLine();
                    succeeded = false;
                }
            }

            Assert.True(succeeded, builder.ToString());
        }
        public static void TransactionTraceHasAttributes(IEnumerable <KeyValuePair <string, string> > expectedAttributes, TransactionTraceAttributeType attributeType, TransactionSample sample)
        {
            var succeeded        = true;
            var builder          = new StringBuilder();
            var actualAttributes = sample.TraceData.Attributes.GetByType(attributeType);

            foreach (var expectedAttribute in expectedAttributes)
            {
                if (!actualAttributes.ContainsKey(expectedAttribute.Key))
                {
                    builder.AppendFormat("Attribute named {0} was not found in the transaction sample.", expectedAttribute);
                    builder.AppendLine();
                    succeeded = false;
                    continue;
                }

                var actualValue = actualAttributes[expectedAttribute.Key] as string;
                if (!actualValue.Equals(expectedAttribute.Value, StringComparison.OrdinalIgnoreCase))
                {
                    builder.AppendFormat("Attribute named {0} in the transaction sample had an unexpected value.  Expected: {1}, Actual: {2}", expectedAttribute.Key, expectedAttribute.Value, actualValue);
                    builder.AppendLine();
                    succeeded = false;
                    continue;
                }
            }

            Assert.True(succeeded, builder.ToString());
        }
        public static void TransactionTraceSegmentQueryParametersExist(ExpectedSegmentQueryParameters expectedQueryParameters, TransactionSample sample)
        {
            var segments = sample.TraceData.RootSegment.Flatten(node => node.ChildSegments);

            var segment = segments.First(x => x.Name == expectedQueryParameters.segmentName);

            Assert.Contains(expectedQueryParameters.parameterName, segment.Parameters);

            var actualQueryParameters = JsonConvert.DeserializeObject <Dictionary <string, object> >(segment.Parameters[expectedQueryParameters.parameterName].ToString());

            SqlTraceQueryParametersAreEquivalent(expectedQueryParameters.QueryParameters, actualQueryParameters);
        }
        public static void TransactionTraceSegmentParametersExist(IEnumerable <ExpectedSegmentParameter> expectedParameters, TransactionSample sample)
        {
            var segments = sample.TraceData.RootSegment.Flatten(node => node.ChildSegments);

            var succeeded = true;
            var builder   = new StringBuilder();

            foreach (var expectedParameter in expectedParameters)
            {
                var segment = segments.FirstOrDefault(
                    x => expectedParameter.IsRegexSegmentName && Regex.IsMatch(x.Name, expectedParameter.segmentName) ||
                    !expectedParameter.IsRegexSegmentName && x.Name == expectedParameter.segmentName
                    );
                if (segment == null)
                {
                    builder.AppendFormat("Segment {0} was not found on the transaction.", expectedParameter.segmentName);
                    builder.AppendLine();
                    succeeded = false;
                    continue;
                }

                if (!segment.Parameters.ContainsKey(expectedParameter.parameterName))
                {
                    builder.AppendFormat("Segment parameter {0} was not found in segment named {1}.", expectedParameter.parameterName, expectedParameter.segmentName);
                    builder.AppendLine();
                    succeeded = false;
                    continue;
                }

                var actualValue = segment.Parameters[expectedParameter.parameterName] as string;
                if (expectedParameter.parameterValue != null && actualValue != expectedParameter.parameterValue)
                {
                    builder.AppendFormat("Segment parameter {0} had the value {1} instead of {2} in segment named {3}.", expectedParameter.parameterName, actualValue, expectedParameter.parameterValue, expectedParameter.segmentName);
                    builder.AppendLine();
                    succeeded = false;
                    continue;
                }
            }

            Assert.True(succeeded, builder.ToString());
        }
        public static void TransactionTraceSegmentsNotExist(IEnumerable <string> unexpectedTraceSegmentNames, TransactionSample sample, bool areRegexNames = false)
        {
            var allSegments = sample.TraceData.RootSegment.Flatten(node => node.ChildSegments);

            var succeeded = true;
            var builder   = new StringBuilder();

            foreach (var unexpectedSegmentName in unexpectedTraceSegmentNames)
            {
                if (allSegments.Any(
                        segment => areRegexNames && Regex.IsMatch(segment.Name, unexpectedSegmentName) ||
                        !areRegexNames && segment.Name == unexpectedSegmentName)
                    )
                {
                    builder.AppendFormat("Segment named {0} was found in the transaction sample, but shouldn't be.", unexpectedSegmentName);
                    builder.AppendLine();
                    succeeded = false;
                }
            }

            Assert.True(succeeded, builder.ToString());
        }
        public void Test()
        {
            var expectedMetrics = new List <Assertions.ExpectedMetric>
            {
                new Assertions.ExpectedMetric {
                    metricName = @"DotNet/Owin Middleware Pipeline", callCount = 5
                },
                new Assertions.ExpectedMetric {
                    metricName = @"WebTransaction", callCount = 5
                },
                new Assertions.ExpectedMetric {
                    metricName = @"WebTransaction/WebAPI/Values/Get", callCount = 3
                },
                new Assertions.ExpectedMetric {
                    metricName = @"WebTransaction/WebAPI/Values/Post", callCount = 1
                },
                new Assertions.ExpectedMetric {
                    metricName = @"WebTransaction/WebAPI/Values/Get404", callCount = 1
                },
                new Assertions.ExpectedMetric {
                    metricName = @"DotNet/Values/Get", callCount = 3
                },
                new Assertions.ExpectedMetric {
                    metricName = @"DotNet/Values/Post", callCount = 1
                },
                new Assertions.ExpectedMetric {
                    metricName = @"DotNet/Values/Get404", callCount = 1
                },
                new Assertions.ExpectedMetric {
                    metricName = @"External/all", callCount = 1
                },
                new Assertions.ExpectedMetric {
                    metricName = @"External/allWeb", callCount = 1
                },
                new Assertions.ExpectedMetric {
                    metricName = @"External/www.google.com/all", callCount = 1
                },
                new Assertions.ExpectedMetric {
                    metricName = @"External/www.google.com/Stream/GET", callCount = 1
                }
            };
            var unexpectedMetrics = new List <Assertions.ExpectedMetric>
            {
                new Assertions.ExpectedMetric {
                    metricName = @"OtherTransaction/all", callCount = 5
                },
            };

            var expectedAttributes = new Dictionary <string, string>
            {
                { "request.parameters.data", "mything" },
            };

            var metrics = _fixture.AgentLog.GetMetrics().ToList();

            var transactionSamples = _fixture.AgentLog.GetTransactionSamples();
            //this is the transaction trace that is generally returned, but this
            //is not necessarily always the case
            var getTransactionSample = transactionSamples
                                       .Where(sample => sample.Path == "WebTransaction/WebAPI/Values/Get")
                                       .FirstOrDefault();
            var get404TransactionSample = transactionSamples
                                          .Where(sample => sample.Path == "WebTransaction/WebAPI/Values/Get404")
                                          .FirstOrDefault();
            var postTransactionSample = transactionSamples
                                        .Where(sample => sample.Path == "WebTransaction/WebAPI/Values/Post")
                                        .FirstOrDefault();

            var transactionEventWithExternal = _fixture.AgentLog.GetTransactionEvents()
                                               .Where(e => e.IntrinsicAttributes.ContainsKey("externalDuration"))
                                               .FirstOrDefault();

            NrAssert.Multiple(
                () => Assertions.MetricsExist(expectedMetrics, metrics),
                () => Assertions.MetricsDoNotExist(unexpectedMetrics, metrics),
                () => Assert.NotNull(transactionEventWithExternal),
                () => Assert.Empty(_fixture.AgentLog.GetErrorTraces()),
                () => Assert.Empty(_fixture.AgentLog.GetErrorEvents())
                );

            // check the transaction trace samples
            TransactionSample traceToCheck = null;
            List <string>     expectedTransactionTraceSegments = null;
            List <string>     doNotExistTraceSegments          = null;

            if (getTransactionSample != null)
            {
                traceToCheck = getTransactionSample;
                expectedTransactionTraceSegments = new List <string>
                {
                    @"Owin Middleware Pipeline",
                    @"DotNet/Values/Get"
                };
                doNotExistTraceSegments = new List <string>
                {
                    @"DotNet/Values/Get404",
                    @"DotNet/Values/Post"
                };
                expectedAttributes.Add("request.uri", "/api/values");
            }
            else if (get404TransactionSample != null)
            {
                traceToCheck = get404TransactionSample;
                expectedTransactionTraceSegments = new List <string>
                {
                    @"Owin Middleware Pipeline",
                    @"DotNet/Values/Get404"
                };
                doNotExistTraceSegments = new List <string>
                {
                    @"External/www.google.com/Stream/GET",
                    @"DotNet/Values/Get",
                    @"DotNet/Values/Post"
                };
                expectedAttributes.Add("request.uri", "/api/404");
            }
            else if (postTransactionSample != null)
            {
                traceToCheck = postTransactionSample;
                expectedTransactionTraceSegments = new List <string>
                {
                    @"Owin Middleware Pipeline",
                    @"DotNet/Values/Post"
                };
                doNotExistTraceSegments = new List <string>
                {
                    @"External/www.google.com/Stream/GET",
                    @"DotNet/Values/Get404",
                    @"DotNet/Values/Get"
                };
                expectedAttributes.Add("request.uri", "/api/values");
            }

            NrAssert.Multiple(
                () => Assert.NotNull(traceToCheck),
                () => Assertions.TransactionTraceSegmentsExist(expectedTransactionTraceSegments, traceToCheck),
                () => Assertions.TransactionTraceSegmentsNotExist(doNotExistTraceSegments, traceToCheck),
                () => Assertions.TransactionTraceHasAttributes(expectedAttributes, TransactionTraceAttributeType.Agent,
                                                               traceToCheck));
        }