Ejemplo n.º 1
0
        public void ErrorShouldContainTransactionData(bool isSampled, bool captureOnSpan, bool captureAsError)
        {
            var payloadSender        = new MockPayloadSender();
            var expectedErrorContext = new Context();

            expectedErrorContext.Labels["one"]        = "1";
            expectedErrorContext.Labels["twenty two"] = "22";

            ITransaction      capturedTransaction            = null;
            IExecutionSegment errorCapturingExecutionSegment = null;

            using (var agent = new ApmAgent(new TestAgentComponents(payloadSender: payloadSender)))
            {
                agent.TracerInternal.Sampler = new Sampler(isSampled ? 1 : 0);
                agent.Tracer.CaptureTransaction(TestTransaction, CustomTransactionTypeForTests, transaction =>
                {
                    capturedTransaction = transaction;

                    foreach (var keyValue in expectedErrorContext.Labels)
                    {
                        transaction.Context.Labels[keyValue.Key] = keyValue.Value;
                    }
                    ISpan span = null;
                    if (captureOnSpan)
                    {
                        span = transaction.StartSpan(TestSpan1, ApiConstants.TypeExternal);
                        errorCapturingExecutionSegment = span;
                    }
                    else
                    {
                        errorCapturingExecutionSegment = transaction;
                    }

                    if (captureAsError)
                    {
                        errorCapturingExecutionSegment.CaptureError("Test error message", "Test error culprit", new StackTrace(true).GetFrames());
                    }
                    else
                    {
                        errorCapturingExecutionSegment.CaptureException(new TestException("test exception"));
                    }

                    // Immutable snapshot of the context should be captured instead of reference to a mutable object
                    // transaction.Context.Labels["three hundred thirty three"] = "333";

                    span?.End();
                });
            }

            payloadSender.Errors.Count.Should().Be(1);
            payloadSender.FirstError.Transaction.IsSampled.Should().Be(isSampled);
            payloadSender.FirstError.Transaction.Type.Should().Be(CustomTransactionTypeForTests);
            payloadSender.FirstError.TransactionId.Should().Be(capturedTransaction.Id);
            payloadSender.FirstError.TraceId.Should().Be(capturedTransaction.TraceId);
            payloadSender.FirstError.ParentId.Should().Be(errorCapturingExecutionSegment.Id);
            payloadSender.FirstError.Context.Should().BeEquivalentTo(expectedErrorContext);
        }
Ejemplo n.º 2
0
        public void ErrorShouldContainTransactionData(bool isSampled, bool captureOnSpan, bool captureAsError)
        {
            var payloadSender        = new MockPayloadSender();
            var expectedErrorContext = new Context();

            expectedErrorContext.Labels["one"]        = "1";
            expectedErrorContext.Labels["twenty two"] = "22";

            ITransaction      capturedTransaction            = null;
            IExecutionSegment errorCapturingExecutionSegment = null;
            var mockConfig = new MockConfigSnapshot(transactionSampleRate: isSampled ? "1" : "0");

            using (var agent = new ApmAgent(new TestAgentComponents(config: mockConfig, payloadSender: payloadSender)))
            {
                agent.Tracer.CaptureTransaction(TestTransaction, CustomTransactionTypeForTests, transaction =>
                {
                    capturedTransaction = transaction;

                    foreach (var(key, value) in expectedErrorContext.Labels)
                    {
                        transaction.Context.Labels[key] = value;
                    }
                    ISpan span = null;
                    if (captureOnSpan)
                    {
                        span = transaction.StartSpan(TestSpan1, ApiConstants.TypeExternal);
                        errorCapturingExecutionSegment = span;
                    }
                    else
                    {
                        errorCapturingExecutionSegment = transaction;
                    }

                    if (captureAsError)
                    {
                        errorCapturingExecutionSegment.CaptureError("Test error message", "Test error culprit", new StackTrace(true).GetFrames());
                    }
                    else
                    {
                        errorCapturingExecutionSegment.CaptureException(new TestException("test exception"));
                    }

                    // Immutable snapshot of the context should be captured instead of reference to a mutable object
                    // transaction.Context.Labels["three hundred thirty three"] = "333";

                    span?.End();
                });
Ejemplo n.º 3
0
        public void ErrorShouldContainTransactionData(bool isSampled, bool captureOnSpan, bool captureAsError)
        {
            var payloadSender        = new MockPayloadSender();
            var expectedErrorContext = new Context();

            expectedErrorContext.InternalLabels.Value.InnerDictionary["one"]        = 1;
            expectedErrorContext.InternalLabels.Value.InnerDictionary["twenty two"] = "22";
            expectedErrorContext.InternalLabels.Value.InnerDictionary["true"]       = true;

            ITransaction      capturedTransaction            = null;
            IExecutionSegment errorCapturingExecutionSegment = null;
            var mockConfig = new MockConfigSnapshot(transactionSampleRate: isSampled ? "1" : "0");

            using (var agent = new ApmAgent(new TestAgentComponents(config: mockConfig, payloadSender: payloadSender)))
            {
                agent.Tracer.CaptureTransaction(TestTransaction, CustomTransactionTypeForTests, transaction =>
                {
                    capturedTransaction = transaction;

                    foreach (var item in expectedErrorContext.InternalLabels.Value.MergedDictionary)
                    {
                        transaction.Context.InternalLabels.Value.MergedDictionary[item.Key] = item.Value;
                    }
                    ISpan span = null;
                    if (captureOnSpan)
                    {
                        span = transaction.StartSpan(TestSpan1, ApiConstants.TypeExternal);
                        errorCapturingExecutionSegment = span;
                    }
                    else
                    {
                        errorCapturingExecutionSegment = transaction;
                    }

                    if (captureAsError)
                    {
                        errorCapturingExecutionSegment.CaptureError("Test error message", "Test error culprit", new StackTrace(true).GetFrames());
                    }
                    else
                    {
                        errorCapturingExecutionSegment.CaptureException(new TestException("test exception"));
                    }

                    // Immutable snapshot of the context should be captured instead of reference to a mutable object
                    // transaction.Context.Labels["three hundred thirty three"] = "333";

                    span?.End();
                });
            }

            payloadSender.WaitForErrors();
            payloadSender.Errors.Count.Should().Be(1);
            payloadSender.FirstError.Transaction.IsSampled.Should().Be(isSampled);
            payloadSender.FirstError.Transaction.Type.Should().Be(CustomTransactionTypeForTests);
            payloadSender.FirstError.TransactionId.Should().Be(capturedTransaction.Id);
            payloadSender.FirstError.TraceId.Should().Be(capturedTransaction.TraceId);
            payloadSender.FirstError.ParentId.Should()
            .Be(errorCapturingExecutionSegment.IsSampled ? errorCapturingExecutionSegment.Id : capturedTransaction.Id);

            payloadSender.FirstError.Transaction.IsSampled.Should().Be(isSampled);
            if (isSampled)
            {
                payloadSender.FirstError.Context.Should().NotBeNull().And.BeEquivalentTo(expectedErrorContext);
            }
            else
            {
                payloadSender.FirstError.Context.Should().BeNull();
            }
        }