public static OpenTelemetryLoggerOptions AddAzureMonitorLogExporter(this OpenTelemetryLoggerOptions loggerOptions, Action <AzureMonitorExporterOptions> configure = null)
        {
            if (loggerOptions == null)
            {
                throw new ArgumentNullException(nameof(loggerOptions));
            }

            var options = new AzureMonitorExporterOptions();

            configure?.Invoke(options);

            return(loggerOptions.AddProcessor(new BatchLogRecordExportProcessor(new AzureMonitorLogExporter(options))));
        }
예제 #2
0
        /// <remarks>
        /// TODO: The <see cref="AzureMonitorLogExporter" /> is currently INTERNAL while under development.
        /// When this Exporter is finished, this method can be changed to PUBLIC.
        /// </remarks>
        internal AzureMonitorLogExporter GetAzureMonitorLogExporter()
        {
            // TODO: I WOULD LIKE TO TAKE ADVANTAGE OF THIS: https://github.com/open-telemetry/opentelemetry-dotnet/pull/1837
            // In these tests we have to manually build the Exporter and processor so we can call ForceFlush in the tests.
            // But we would expect customers to use the Extension method.

            var exporterOptions = new AzureMonitorExporterOptions
            {
                ConnectionString = TestEnvironment.ConnectionString,
            };

            var clientOptions = this.InstrumentClientOptions(exporterOptions);

            return(new AzureMonitorLogExporter(clientOptions));
        }
        private static AzureMonitorTransmitter GetTransmitter(MockResponse mockResponse)
        {
            MockTransport mockTransport         = new MockTransport(mockResponse);
            AzureMonitorExporterOptions options = new AzureMonitorExporterOptions();

            options.ConnectionString = $"InstrumentationKey={testIkey};IngestionEndpoint={testEndpoint}";
            options.StorageDirectory = StorageHelper.GetDefaultStorageDirectory() + "\\test";
            options.Transport        = mockTransport;
            AzureMonitorTransmitter transmitter = new AzureMonitorTransmitter(options);

            // Overwrite storage with mock
            transmitter._storage = new MockFileStorage();

            return(transmitter);
        }
예제 #4
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 BatchActivityExportProcessor(new AzureMonitorTraceExporter(
                                                                  options: azureMonitorExporterOptions,
                                                                  transmitter: mockTransmitter));

            var processor2 = new BatchLogRecordExportProcessor(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"]);
        }