public void CheckForceFlushExport(int timeout)
        {
            var exportedItems = new List <Activity>();

            using var exporter  = new InMemoryExporter <Activity>(exportedItems);
            using var processor = new BatchExportProcessor <Activity>(
                      exporter,
                      maxQueueSize: 3,
                      maxExportBatchSize: 3,
                      exporterTimeoutMilliseconds: 30000);

            processor.OnEnd(new Activity("start1"));
            processor.OnEnd(new Activity("start2"));

            Assert.Equal(0, processor.ProcessedCount);

            // waiting to see if time is triggering the exporter
            Thread.Sleep(1_000);
            Assert.Empty(exportedItems);

            // forcing flush
            processor.ForceFlush(timeout);

            if (timeout == 0)
            {
                // ForceFlush(0) will trigger flush and return immediately, so let's sleep for a while
                Thread.Sleep(1_000);
            }

            Assert.Equal(2, exportedItems.Count);

            Assert.Equal(2, processor.ProcessedCount);
            Assert.Equal(2, processor.ReceivedCount);
            Assert.Equal(0, processor.DroppedCount);
        }
コード例 #2
0
        private TelemetryItem RunActivityTest(Action <ActivitySource> testScenario)
        {
            // SETUP
            var ActivitySourceName = "MyCompany.MyProduct.MyLibrary";

            using var activitySource = new ActivitySource(ActivitySourceName);

            var transmitter = new MockTransmitter();
            var processor   = new BatchExportProcessor <Activity>(new AzureMonitorTraceExporter(
                                                                      options: new AzureMonitorExporterOptions
            {
                ConnectionString = EmptyConnectionString,
            },
                                                                      transmitter: transmitter));

            Sdk.CreateTracerProviderBuilder()
            .SetSampler(new AlwaysOnSampler())
            .AddSource(ActivitySourceName)
            .AddProcessor(processor)
            .Build();

            // ACT
            testScenario(activitySource);

            // CLEANUP
            processor.ForceFlush();
            Task.Delay(100).Wait(); //TODO: HOW TO REMOVE THIS WAIT?

            Assert.True(transmitter.TelemetryItems.Any(), "test project did not capture telemetry");
            return(transmitter.TelemetryItems.Single());
        }
コード例 #3
0
        private TelemetryItem RunActivityTest(Action <ActivitySource> testScenario)
        {
            // SETUP
            var ActivitySourceName = $"{nameof(TelemetryItemTests)}.{nameof(RunActivityTest)}";

            using var activitySource = new ActivitySource(ActivitySourceName);

            var mockTransmitter = new MockTransmitter();
            var processor       = new BatchExportProcessor <Activity>(new AzureMonitorTraceExporter(
                                                                          options: new AzureMonitorExporterOptions
            {
                ConnectionString = EmptyConnectionString,
            },
                                                                          transmitter: mockTransmitter));

            using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                                       .SetSampler(new AlwaysOnSampler())
                                       .AddSource(ActivitySourceName)
                                       .AddProcessor(processor)
                                       .Build();

            // ACT
            testScenario(activitySource);

            // CLEANUP
            processor.ForceFlush();

            Assert.True(mockTransmitter.TelemetryItems.Any(), "test project did not capture telemetry");
            return(mockTransmitter.TelemetryItems.Single());
        }
コード例 #4
0
        private TelemetryItem RunLoggerTest(Action <ILogger <TelemetryItemTests> > testScenario)
        {
            // SETUP
            var mockTransmitter = new MockTransmitter();
            var processor       = new BatchExportProcessor <LogRecord>(new AzureMonitorLogExporter(
                                                                           options: new AzureMonitorExporterOptions
            {
                ConnectionString = EmptyConnectionString,
            },
                                                                           transmitter: mockTransmitter));

            var serviceCollection = new ServiceCollection().AddLogging(builder =>
            {
                builder.SetMinimumLevel(LogLevel.Trace)
                .AddOpenTelemetry(options => options
                                  .AddProcessor(processor));
            });

            using var serviceProvider = serviceCollection.BuildServiceProvider();
            var logger = serviceProvider.GetRequiredService <ILogger <TelemetryItemTests> >();

            // ACT
            testScenario(logger);

            // CLEANUP
            processor.ForceFlush();

            Assert.True(mockTransmitter.TelemetryItems.Any(), "test project did not capture telemetry");
            return(mockTransmitter.TelemetryItems.Single());
        }
コード例 #5
0
        public void CheckForceFlushWithInvalidTimeout()
        {
            var exportedItems = new List <Activity>();

            using var exporter  = new InMemoryExporter <Activity>(exportedItems);
            using var processor = new BatchExportProcessor <Activity>(exporter, maxQueueSize: 2, maxExportBatchSize: 1);
            Assert.Throws <ArgumentOutOfRangeException>(() => processor.ForceFlush(-2));
        }
コード例 #6
0
        public void VerifyLoggerWithActivity()
        {
            // SETUP
            var ActivitySourceName = $"{nameof(TelemetryItemTests)}.{nameof(VerifyLoggerWithActivity)}";

            using var activitySource = new ActivitySource(ActivitySourceName);

            var mockTransmitter = new MockTransmitter();

            var azureMonitorExporterOptions = new AzureMonitorExporterOptions
            {
                ConnectionString = EmptyConnectionString,
            };

            var processor1 = new BatchExportProcessor <Activity>(new AzureMonitorTraceExporter(
                                                                     options: azureMonitorExporterOptions,
                                                                     transmitter: mockTransmitter));

            var processor2 = new BatchExportProcessor <LogRecord>(new AzureMonitorLogExporter(
                                                                      options: azureMonitorExporterOptions,
                                                                      transmitter: mockTransmitter));

            using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                                       .SetSampler(new AlwaysOnSampler())
                                       .AddSource(ActivitySourceName)
                                       .AddProcessor(processor1)
                                       .Build();

            var serviceCollection = new ServiceCollection().AddLogging(builder =>
            {
                builder.SetMinimumLevel(LogLevel.Trace)
                .AddOpenTelemetry(options => options
                                  .AddProcessor(processor2));
            });

            using var serviceProvider = serviceCollection.BuildServiceProvider();
            var logger = serviceProvider.GetRequiredService <ILogger <TelemetryItemTests> >();

            // ACT
            using (var activity = activitySource.StartActivity(name: "test activity", kind: ActivityKind.Server))
            {
                activity.SetTag("message", "hello activity!");

                logger.LogWarning("hello ilogger");
            }

            // CLEANUP
            processor1.ForceFlush();
            processor2.ForceFlush();

            // VERIFY
            Assert.True(mockTransmitter.TelemetryItems.Any(), "test project did not capture telemetry");
            Assert.Equal(2, mockTransmitter.TelemetryItems.Count);

            var logTelemetry      = mockTransmitter.TelemetryItems.Single(x => x.Name == "Message");
            var activityTelemetry = mockTransmitter.TelemetryItems.Single(x => x.Name == "Request");

            var activityId  = ((RequestData)activityTelemetry.Data.BaseData).Id;
            var operationId = activityTelemetry.Tags["ai.operation.id"];

            Assert.Equal(activityId, logTelemetry.Tags["ai.operation.parentId"]);
            Assert.Equal(operationId, logTelemetry.Tags["ai.operation.id"]);
        }