Esempio n. 1
0
        public async Task HttpCallWithoutRegisteredListener()
        {
            var mockPayloadSender = new MockPayloadSender();
            var agent             = new ApmAgent(new TestAgentComponents(payloadSender: mockPayloadSender));

            using (var localServer = LocalServer.Create())
            {
                await agent.Tracer.CaptureTransaction("TestTransaction", "TestType", async t =>
                {
                    Thread.Sleep(5);

                    var httpClient = new HttpClient();
                    try
                    {
                        await httpClient.GetAsync(localServer.Uri);
                    }
                    catch (Exception e)
                    {
                        t.CaptureException(e);
                    }
                });

                mockPayloadSender.WaitForTransactions();
                mockPayloadSender.Transactions.Should().NotBeEmpty();
                mockPayloadSender.SignalEndSpans();
                mockPayloadSender.WaitForSpans();
                mockPayloadSender.SpansOnFirstTransaction.Should().BeEmpty();
            }
        }
Esempio n. 2
0
        public void CallToEndDoesNotChangeAlreadySetDuration(bool isSampled)
        {
            var payloadSender      = new MockPayloadSender();
            var expectedSpansCount = isSampled ? 1 : 0;

            using (var agent = new ApmAgent(new TestAgentComponents(payloadSender: payloadSender,
                                                                    config: new MockConfigSnapshot(transactionSampleRate: isSampled ? "1" : "0"))))
            {
                var transaction = agent.Tracer.StartTransaction(TestTransaction, UnitTest);
                var span        = transaction.StartSpan(TestSpan1, ApiConstants.TypeExternal);

                payloadSender.Spans.Should().HaveCount(0);
                span.Duration = 123456.789;
                span.End();

                payloadSender.SignalEndSpans();
                payloadSender.WaitForSpans();
                payloadSender.Spans.Should().HaveCount(expectedSpansCount);
                if (isSampled)
                {
                    payloadSender.FirstSpan.Duration.Should().Be(123456.789);
                }

                payloadSender.Transactions.Should().HaveCount(0);
                transaction.Duration = 987654.321;
                transaction.End();
                payloadSender.SignalEndTransactions();
                payloadSender.WaitForTransactions();
                payloadSender.Transactions.Should().HaveCount(1);
                payloadSender.FirstTransaction.Duration.Should().Be(987654.321);
            }
        }
Esempio n. 3
0
        public void TransactionWithSpanWithoutEnd()
        {
            const string transactionName = TestTransaction;
            const string transactionType = UnitTest;
            const string spanName        = "TestSpan";
            var          payloadSender   = new MockPayloadSender();

            using (var agent = new ApmAgent(new TestAgentComponents(payloadSender: payloadSender)))
            {
                var transaction = agent.Tracer.StartTransaction(transactionName, transactionType);

                var unused = transaction.StartSpan(spanName, ApiConstants.TypeExternal);

                Thread.Sleep(5);                 //Make sure we have duration > 0

                transaction.End();               //Ends transaction, but doesn't end span.

                payloadSender.WaitForTransactions();
                payloadSender.Transactions.Should().NotBeEmpty();

                payloadSender.SignalEndSpans();
                payloadSender.WaitForSpans();
                payloadSender.SpansOnFirstTransaction.Should().BeEmpty();

                agent.Service.Should().NotBeNull();
            }
        }
Esempio n. 4
0
        public async Task SubscribeUnsubscribe()
        {
            var mockPayloadSender = new MockPayloadSender();
            var agent             = new ApmAgent(new TestAgentComponents(payloadSender: mockPayloadSender));
            var subscriber        = new HttpDiagnosticsSubscriber();

            using (var localServer = LocalServer.Create())
            {
                var url = localServer.Uri;
                using (agent.Subscribe(subscriber))                 //subscribe
                {
                    await agent.Tracer.CaptureTransaction("TestTransaction", "TestType", async t =>
                    {
                        Thread.Sleep(5);

                        var httpClient = new HttpClient();
                        try
                        {
                            await httpClient.GetAsync(url);
                        }
                        catch (Exception e)
                        {
                            t.CaptureException(e);
                        }
                    });
                }                 //and then unsubscribe

                mockPayloadSender.WaitForAny();
                mockPayloadSender.Clear();

                await agent.Tracer.CaptureTransaction("TestTransaction", "TestType", async t =>
                {
                    Thread.Sleep(5);

                    var httpClient = new HttpClient();
                    try
                    {
                        await httpClient.GetAsync(url);
                    }
                    catch (Exception e)
                    {
                        t.CaptureException(e);
                    }
                });

                mockPayloadSender.WaitForTransactions();
                mockPayloadSender.FirstTransaction.Should().NotBeNull();
                mockPayloadSender.SignalEndSpans();
                mockPayloadSender.WaitForSpans();
                mockPayloadSender.SpansOnFirstTransaction.Should().BeEmpty();
            }
        }
Esempio n. 5
0
        public void CallingEndMoreThanOnceIsNoop(bool isSampled)
        {
            var payloadSender      = new MockPayloadSender();
            var expectedSpansCount = isSampled ? 1 : 0;

            using (var agent = new ApmAgent(new TestAgentComponents(payloadSender: payloadSender,
                                                                    config: new MockConfigSnapshot(transactionSampleRate: isSampled ? "1" : "0"))))
            {
                var transaction = agent.Tracer.StartTransaction(TestTransaction, UnitTest);
                var span        = transaction.StartSpan(TestSpan1, ApiConstants.TypeExternal);

                payloadSender.Spans.Should().HaveCount(0);
                span.End();

                payloadSender.SignalEndSpans();
                payloadSender.WaitForSpans();
                payloadSender.Spans.Should().HaveCount(expectedSpansCount);
                if (isSampled)
                {
                    payloadSender.FirstSpan.Name.Should().Be(TestSpan1);
                }
                span.End();

                payloadSender.SignalEndSpans();
                payloadSender.WaitForSpans();
                payloadSender.Spans.Should().HaveCount(expectedSpansCount);

                payloadSender.Transactions.Should().HaveCount(0);
                transaction.End();
                payloadSender.WaitForTransactions();
                payloadSender.Transactions.Should().HaveCount(1);
                payloadSender.FirstTransaction.Name.Should().Be(TestTransaction);
                transaction.End();
                payloadSender.SignalEndTransactions();
                payloadSender.WaitForTransactions();
                payloadSender.Transactions.Should().HaveCount(1);
            }
        }
        public async Task Does_Not_Capture_Span_When_QueueName_Matches_IgnoreMessageQueues()
        {
            await using var scope = await QueueScope.CreateWithQueue(_adminClient);

            var sender = _client.CreateSender(scope.QueueName);

            _agent.ConfigStore.CurrentSnapshot = new MockConfigSnapshot(ignoreMessageQueues: scope.QueueName);

            await _agent.Tracer.CaptureTransaction("Send AzureServiceBus Message", "message", async() =>
            {
                await sender.SendMessageAsync(new ServiceBusMessage("test message")).ConfigureAwait(false);
            });

            _sender.SignalEndSpans();
            _sender.WaitForSpans();
            _sender.Spans.Should().HaveCount(0);
        }