Esempio n. 1
0
        public async Task EventHubProducerCreatesDiagnosticScopeOnSend()
        {
            using var testListener = new ClientDiagnosticListener(DiagnosticSourceName);

            var eventHubName  = "SomeName";
            var endpoint      = new Uri("amqp://endpoint");
            var transportMock = new Mock <TransportEventHubProducer>();

            transportMock
            .Setup(m => m.SendAsync(It.IsAny <IEnumerable <EventData> >(), It.IsAny <SendOptions>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var producer = new EventHubProducer(transportMock.Object, endpoint, eventHubName, new EventHubProducerOptions(), Mock.Of <EventHubRetryPolicy>());

            var eventData = new EventData(ReadOnlyMemory <byte> .Empty);
            await producer.SendAsync(eventData);

            ClientDiagnosticListener.ProducedDiagnosticScope sendScope = testListener.AssertScope(DiagnosticProperty.ProducerActivityName,
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.TypeAttribute, DiagnosticProperty.EventHubProducerType),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.ServiceContextAttribute, DiagnosticProperty.EventHubsServiceContext),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EventHubAttribute, eventHubName),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EndpointAttribute, endpoint.ToString()));

            ClientDiagnosticListener.ProducedDiagnosticScope messageScope = testListener.AssertScope(DiagnosticProperty.EventActivityName);

            Assert.That(eventData.Properties[DiagnosticProperty.DiagnosticIdAttribute], Is.EqualTo(messageScope.Activity.Id), "The diagnostics identifier should match.");
            Assert.That(messageScope.Activity.Tags, Has.One.EqualTo(new KeyValuePair <string, string>(DiagnosticProperty.KindAttribute, DiagnosticProperty.InternalKind)), "The activities tag should be internal.");
            Assert.That(messageScope.Activity, Is.Not.SameAs(sendScope.Activity), "The activities should not be the same instance.");
        }
Esempio n. 2
0
        public async Task EventHubProducerCreatesDiagnosticScopeOnBatchSend()
        {
            using var testListener = new ClientDiagnosticListener(EventDataInstrumentation.DiagnosticNamespace);
            var activity = new Activity("SomeActivity").Start();

            var eventCount         = 0;
            var eventHubName       = "SomeName";
            var endpoint           = "endpoint";
            var batchEvent         = default(EventData);
            var fakeConnection     = new MockConnection(endpoint, eventHubName);
            var batchTransportMock = new Mock <TransportEventBatch>();


            batchTransportMock
            .Setup(m => m.TryAdd(It.IsAny <EventData>()))
            .Callback <EventData>(addedEvent => batchEvent = addedEvent)
            .Returns(() =>
            {
                eventCount++;
                return(eventCount <= 1);
            });

            var transportMock = new Mock <TransportProducer>();

            transportMock
            .Setup(m => m.SendAsync(It.IsAny <IEnumerable <EventData> >(), It.IsAny <SendEventOptions>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            transportMock
            .Setup(m => m.CreateBatchAsync(It.IsAny <CreateBatchOptions>(), It.IsAny <CancellationToken>()))
            .Returns(new ValueTask <TransportEventBatch>(Task.FromResult(batchTransportMock.Object)));

            var producer = new EventHubProducerClient(fakeConnection, transportMock.Object);

            var eventData = new EventData(ReadOnlyMemory <byte> .Empty);
            var batch     = await producer.CreateBatchAsync();

            Assert.True(batch.TryAdd(eventData));

            await producer.SendAsync(batch);

            activity.Stop();

            ClientDiagnosticListener.ProducedDiagnosticScope sendScope = testListener.AssertScope(DiagnosticProperty.ProducerActivityName,
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.KindAttribute, DiagnosticProperty.ClientKind),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.ServiceContextAttribute, DiagnosticProperty.EventHubsServiceContext),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EventHubAttribute, eventHubName),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EndpointAttribute, endpoint));

            ClientDiagnosticListener.ProducedDiagnosticScope messageScope = testListener.AssertScope(DiagnosticProperty.EventActivityName,
                                                                                                     new KeyValuePair <string, string>(DiagnosticProperty.EventHubAttribute, eventHubName),
                                                                                                     new KeyValuePair <string, string>(DiagnosticProperty.EndpointAttribute, endpoint));

            Assert.That(batchEvent.Properties[DiagnosticProperty.DiagnosticIdAttribute], Is.EqualTo(messageScope.Activity.Id), "The diagnostics identifier should match.");
            Assert.That(messageScope.Activity, Is.Not.SameAs(sendScope.Activity), "The activities should not be the same instance.");
            Assert.That(sendScope.Activity.ParentId, Is.EqualTo(activity.Id), "The send scope's parent identifier should match the activity in the active scope.");
            Assert.That(messageScope.Activity.ParentId, Is.EqualTo(activity.Id), "The message scope's parent identifier should match the activity in the active scope.");
        }
Esempio n. 3
0
        public async Task EventHubProducerCreatesDiagnosticScopeOnBatchSend()
        {
            using var testListener = new ClientDiagnosticListener(DiagnosticSourceName);

            var eventHubName       = "SomeName";
            var endpoint           = new Uri("amqp://endpoint");
            var fakeConnection     = new MockConnection(endpoint, eventHubName);
            var eventCount         = 0;
            var batchTransportMock = new Mock <TransportEventBatch>();

            batchTransportMock
            .Setup(m => m.TryAdd(It.IsAny <EventData>()))
            .Returns(() =>
            {
                eventCount++;
                return(eventCount <= 3);
            });

            var transportMock = new Mock <TransportProducer>();

            transportMock
            .Setup(m => m.SendAsync(It.IsAny <IEnumerable <EventData> >(), It.IsAny <SendEventOptions>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            transportMock
            .Setup(m => m.CreateBatchAsync(It.IsAny <CreateBatchOptions>(), It.IsAny <CancellationToken>()))
            .Returns(new ValueTask <TransportEventBatch>(Task.FromResult(batchTransportMock.Object)));

            var producer = new EventHubProducerClient(fakeConnection, transportMock.Object);

            var            eventData = new EventData(ReadOnlyMemory <byte> .Empty);
            EventDataBatch batch     = await producer.CreateBatchAsync();

            Assert.True(batch.TryAdd(eventData));

            await producer.SendAsync(batch);

            ClientDiagnosticListener.ProducedDiagnosticScope sendScope = testListener.AssertScope(DiagnosticProperty.ProducerActivityName,
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.TypeAttribute, DiagnosticProperty.EventHubProducerType),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.ServiceContextAttribute, DiagnosticProperty.EventHubsServiceContext),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EventHubAttribute, eventHubName),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EndpointAttribute, endpoint.ToString()));

            ClientDiagnosticListener.ProducedDiagnosticScope messageScope = testListener.AssertScope(DiagnosticProperty.EventActivityName);

            Assert.That(eventData.Properties[DiagnosticProperty.DiagnosticIdAttribute], Is.EqualTo(messageScope.Activity.Id), "The diagnostics identifier should match.");
            Assert.That(messageScope.Activity, Is.Not.SameAs(sendScope.Activity), "The activities should not be the same instance.");
        }
Esempio n. 4
0
        public async Task RecognizeContentOperationCreatesDiagnosticScopeOnUpdate()
        {
            using var testListener = new ClientDiagnosticListener(DiagnosticNamespace);
            using var stream       = new MemoryStream(Encoding.UTF8.GetBytes("{}"));

            var mockResponse = new MockResponse(200);

            mockResponse.ContentStream = stream;

            var mockTransport = new MockTransport(new[] { mockResponse, mockResponse });
            var options       = new FormRecognizerClientOptions()
            {
                Transport = mockTransport
            };
            var client = CreateFormRecognizerClient(options);

            var operation = new RecognizeContentOperation("00000000-0000-0000-0000-000000000000", client);

            if (IsAsync)
            {
                await operation.UpdateStatusAsync();
            }
            else
            {
                operation.UpdateStatus();
            }

            testListener.AssertScope($"{nameof(RecognizeContentOperation)}.{nameof(RecognizeContentOperation.UpdateStatus)}");
        }
Esempio n. 5
0
        public async Task CreateComposedModelOperationCreatesDiagnosticScopeOnUpdate()
        {
            using var testListener = new ClientDiagnosticListener(DiagnosticNamespace);
            using var stream       = new MemoryStream(Encoding.UTF8.GetBytes(@"
                {
                    ""modelInfo"": {
                        ""status"": ""creating"",
                        ""modelId"": ""00000000-0000-0000-0000-000000000000""
                    }
                }"));

            var mockResponse = new MockResponse(200);

            mockResponse.ContentStream = stream;

            var mockTransport = new MockTransport(new[] { mockResponse, mockResponse });
            var options       = new FormRecognizerClientOptions()
            {
                Transport = mockTransport
            };
            var client = CreateFormTrainingClient(options);

            var operation = new CreateComposedModelOperation("00000000-0000-0000-0000-000000000000", client);

            if (IsAsync)
            {
                await operation.UpdateStatusAsync();
            }
            else
            {
                operation.UpdateStatus();
            }

            testListener.AssertScope($"{nameof(CreateCustomFormModelOperation)}.{nameof(CreateCustomFormModelOperation.UpdateStatus)}");
        }
Esempio n. 6
0
        public async Task TestMultipleCloudEventsWithTracingMultiDispatchError()
        {
            // individual elements
            var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance);

            using var host = TestHelpers.NewHost <MyProg3>(ext);
            await host.StartAsync(); // add listener

            using var testListener = new ClientDiagnosticListener("Azure.Messaging.EventGrid");
            const string functionName = "EventGridThrowsExceptionMultiple";
            const string traceparent1 = "00-0123456789abcdef0123456789abcdef-0123456789abcdef-01";
            const string traceparent2 = "00-1123456789abcdef0123456789abcdef-1123456789abcdef-01";

            var request = CreateDispatchRequest(functionName,
                                                JObject.Parse($"{{'subject':'one','data':{{'prop':'alpha'}},'traceparent':'{traceparent1}'}}"),
                                                JObject.Parse($"{{'subject':'two','data':{{'prop':'alpha'}},'traceparent':'{traceparent2}'}}"));

            var response = await ext.ConvertAsync(request, CancellationToken.None);

            Assert.AreEqual(1, testListener.Scopes.Count);
            var executionScope = testListener.AssertScope("EventGrid.Process",
                                                          new KeyValuePair <string, string>("az.namespace", "Microsoft.EventGrid"));

            var expectedLinks = new[] { new ClientDiagnosticListener.ProducedLink(traceparent1, null),
                                        new ClientDiagnosticListener.ProducedLink(traceparent2, null) };

            Assert.That(executionScope.Links, Is.EquivalentTo(expectedLinks));
            Assert.NotNull(executionScope.Exception);
            Assert.True(_log.TryGetValue(executionScope.Activity.Id, out var activityName));
            Assert.AreEqual("EventGrid.Process", activityName);
        }
Esempio n. 7
0
        public async Task EventHubProducerCreatesDiagnosticScopeOnSend()
        {
            using var testListener = new ClientDiagnosticListener(DiagnosticSourceName);
            var activity = new Activity("SomeActivity").Start();

            var eventHubName   = "SomeName";
            var endpoint       = "endpoint";
            var fakeConnection = new MockConnection(endpoint, eventHubName);
            var transportMock  = new Mock <TransportProducer>();

            transportMock
            .Setup(m => m.SendAsync(It.IsAny <IEnumerable <EventData> >(), It.IsAny <SendEventOptions>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var producer = new EventHubProducerClient(fakeConnection, transportMock.Object);

            var eventData = new EventData(ReadOnlyMemory <byte> .Empty);
            await producer.SendAsync(eventData);

            activity.Stop();

            ClientDiagnosticListener.ProducedDiagnosticScope sendScope = testListener.AssertScope(DiagnosticProperty.ProducerActivityName,
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.KindAttribute, DiagnosticProperty.ClientKind),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.ServiceContextAttribute, DiagnosticProperty.EventHubsServiceContext),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EventHubAttribute, eventHubName),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EndpointAttribute, endpoint));

            ClientDiagnosticListener.ProducedDiagnosticScope messageScope = testListener.AssertScope(DiagnosticProperty.EventActivityName,
                                                                                                     new KeyValuePair <string, string>(DiagnosticProperty.EventHubAttribute, eventHubName),
                                                                                                     new KeyValuePair <string, string>(DiagnosticProperty.EndpointAttribute, endpoint));

            Assert.That(eventData.Properties[DiagnosticProperty.DiagnosticIdAttribute], Is.EqualTo(messageScope.Activity.Id), "The diagnostics identifier should match.");
            Assert.That(messageScope.Activity.Tags, Has.One.EqualTo(new KeyValuePair <string, string>(DiagnosticProperty.KindAttribute, DiagnosticProperty.ProducerKind)), "The activities tag should be internal.");
            Assert.That(messageScope.Activity, Is.Not.SameAs(sendScope.Activity), "The activities should not be the same instance.");
            Assert.That(sendScope.Activity.ParentId, Is.EqualTo(activity.Id), "The send scope's parent identifier should match the activity in the active scope.");
            Assert.That(messageScope.Activity.ParentId, Is.EqualTo(activity.Id), "The message scope's parent identifier should match the activity in the active scope.");
        }
Esempio n. 8
0
        public async Task TestEventGridEventBatchDispatchWithTracing(string functionName)
        {
            // individual elements
            var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance);

            using var host = TestHelpers.NewHost <MyProg1>(ext);
            await host.StartAsync(); // add listener

            using var testListener = new ClientDiagnosticListener("Azure.Messaging.EventGrid");
            var request = CreateDispatchRequest(functionName,
                                                JObject.Parse(@"{'subject':'one','eventType':'1','id':'1','dataVersion':'0','data':{'prop':'alpha'}}"),
                                                JObject.Parse(@"{'subject':'two','eventType':'2','id':'2','dataVersion':'0','data':{'prop':'alpha'}}"));

            var response = await ext.ConvertAsync(request, CancellationToken.None);

            Assert.AreEqual(1, testListener.Scopes.Count);
            var executionScope = testListener.AssertScope("EventGrid.Process",
                                                          new KeyValuePair <string, string>("az.namespace", "Microsoft.EventGrid"));

            Assert.IsEmpty(executionScope.Links);
            Assert.Null(executionScope.Exception);
            Assert.True(_log.TryGetValue(executionScope.Activity.Id, out var activityName));
            Assert.AreEqual("EventGrid.Process", activityName);
        }