From 7cb92d3b6dbad169048e5aa07939ef613f2587e2 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Mon, 18 Sep 2023 12:36:47 -0700 Subject: [PATCH] [integration test] otlp log exporter (#4854) --- examples/Console/TestLogs.cs | 2 +- .../OtlpLogExporterHelperExtensions.cs | 49 ++++++------- .../IntegrationTest/IntegrationTests.cs | 72 +++++++++++++++++++ .../otel-collector-config.yaml | 3 + 4 files changed, 99 insertions(+), 27 deletions(-) diff --git a/examples/Console/TestLogs.cs b/examples/Console/TestLogs.cs index 06a8576f963..991b7d0e71e 100644 --- a/examples/Console/TestLogs.cs +++ b/examples/Console/TestLogs.cs @@ -79,7 +79,7 @@ internal static object Run(LogsOptions options) { processorType = ExportProcessorType.Batch; } - else if (options.Protocol.Trim().ToLower().Equals("simple")) + else if (options.ProcessorType.Trim().ToLower().Equals("simple")) { processorType = ExportProcessorType.Simple; } diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpLogExporterHelperExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpLogExporterHelperExtensions.cs index e5a9b69b3a0..83bdd6907a2 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpLogExporterHelperExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpLogExporterHelperExtensions.cs @@ -59,49 +59,46 @@ public static OpenTelemetryLoggerOptions AddOtlpExporter( configureExporterAndProcessor?.Invoke(exporterOptions, processorOptions); - return AddOtlpLogExporterInternal( - loggerOptions, - exporterOptions: exporterOptions, - processorOptions: processorOptions); + return loggerOptions.AddProcessor(BuildOtlpLogExporter(exporterOptions, processorOptions)); } - private static OpenTelemetryLoggerOptions AddOtlpExporterInternal( - OpenTelemetryLoggerOptions loggerOptions, - Action configure) - { - var exporterOptions = new OtlpExporterOptions(); - - configure?.Invoke(exporterOptions); - - return AddOtlpLogExporterInternal( - loggerOptions, - exporterOptions: exporterOptions, - processorOptions: new()); - } - - private static OpenTelemetryLoggerOptions AddOtlpLogExporterInternal( - OpenTelemetryLoggerOptions loggerOptions, + internal static BaseProcessor BuildOtlpLogExporter( OtlpExporterOptions exporterOptions, - LogRecordExportProcessorOptions processorOptions) + LogRecordExportProcessorOptions processorOptions, + Func, BaseExporter> configureExporterInstance = null) { - var otlpExporter = new OtlpLogExporter(exporterOptions); + BaseExporter otlpExporter = new OtlpLogExporter(exporterOptions); + + if (configureExporterInstance != null) + { + otlpExporter = configureExporterInstance(otlpExporter); + } if (processorOptions.ExportProcessorType == ExportProcessorType.Simple) { - loggerOptions.AddProcessor(new SimpleLogRecordExportProcessor(otlpExporter)); + return new SimpleLogRecordExportProcessor(otlpExporter); } else { var batchOptions = processorOptions.BatchExportProcessorOptions; - loggerOptions.AddProcessor(new BatchLogRecordExportProcessor( + return new BatchLogRecordExportProcessor( otlpExporter, batchOptions.MaxQueueSize, batchOptions.ScheduledDelayMilliseconds, batchOptions.ExporterTimeoutMilliseconds, - batchOptions.MaxExportBatchSize)); + batchOptions.MaxExportBatchSize); } + } + + private static OpenTelemetryLoggerOptions AddOtlpExporterInternal( + OpenTelemetryLoggerOptions loggerOptions, + Action configure) + { + var exporterOptions = new OtlpExporterOptions(); + + configure?.Invoke(exporterOptions); - return loggerOptions; + return loggerOptions.AddProcessor(BuildOtlpLogExporter(exporterOptions, new())); } } diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/IntegrationTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/IntegrationTests.cs index 63e28e501a2..e72b54bd3db 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/IntegrationTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/IntegrationTests.cs @@ -17,7 +17,10 @@ using System.Diagnostics; using System.Diagnostics.Metrics; using System.Diagnostics.Tracing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation; +using OpenTelemetry.Logs; using OpenTelemetry.Metrics; using OpenTelemetry.Tests; using OpenTelemetry.Trace; @@ -210,6 +213,75 @@ public void MetricExportResultIsSuccess(OtlpExportProtocol protocol, string endp } } + [InlineData(OtlpExportProtocol.Grpc, ":4317", ExportProcessorType.Batch)] + [InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/logs", ExportProcessorType.Batch)] + [InlineData(OtlpExportProtocol.Grpc, ":4317", ExportProcessorType.Simple)] + [InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/logs", ExportProcessorType.Simple)] + [InlineData(OtlpExportProtocol.Grpc, ":5317", ExportProcessorType.Simple, "https")] + [InlineData(OtlpExportProtocol.HttpProtobuf, ":5318/v1/logs", ExportProcessorType.Simple, "https")] + [Trait("CategoryName", "CollectorIntegrationTests")] + [SkipUnlessEnvVarFoundTheory(CollectorHostnameEnvVarName)] + public void LogExportResultIsSuccess(OtlpExportProtocol protocol, string endpoint, ExportProcessorType exportProcessorType, string scheme = "http") + { + using EventWaitHandle handle = new ManualResetEvent(false); + + var exporterOptions = new OtlpExporterOptions + { + Endpoint = new Uri($"{scheme}://{CollectorHostname}{endpoint}"), + Protocol = protocol, + }; + + DelegatingExporter delegatingExporter = null; + var exportResults = new List(); + var processorOptions = new LogRecordExportProcessorOptions(); + processorOptions.ExportProcessorType = exportProcessorType; + processorOptions.BatchExportProcessorOptions = new() + { + ScheduledDelayMilliseconds = ExportIntervalMilliseconds, + }; + + using var loggerFactory = LoggerFactory.Create(builder => + { + builder + .AddOpenTelemetry(options => options + .AddProcessor(OtlpLogExporterHelperExtensions.BuildOtlpLogExporter( + exporterOptions, + processorOptions, + configureExporterInstance: otlpExporter => + { + delegatingExporter = new DelegatingExporter + { + OnExportFunc = (batch) => + { + var result = otlpExporter.Export(batch); + exportResults.Add(result); + handle.Set(); + return result; + }, + }; + return delegatingExporter; + }))); + }); + + var logger = loggerFactory.CreateLogger("OtlpLogExporterTests"); + logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99); + + switch (processorOptions.ExportProcessorType) + { + case ExportProcessorType.Batch: + Assert.True(handle.WaitOne(ExportIntervalMilliseconds * 2)); + Assert.Single(exportResults); + Assert.Equal(ExportResult.Success, exportResults[0]); + break; + case ExportProcessorType.Simple: + Assert.Single(exportResults); + Assert.Equal(ExportResult.Success, exportResults[0]); + break; + default: + throw new NotSupportedException("Unexpected processor type encountered."); + } + } + [Trait("CategoryName", "CollectorIntegrationTests")] [SkipUnlessEnvVarFoundFact(CollectorHostnameEnvVarName)] public void ConstructingGrpcExporterFailsWhenHttp2UnencryptedSupportIsDisabledForNetcoreapp31() diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/otel-collector-config.yaml b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/otel-collector-config.yaml index 8dc68e98793..477de40fe74 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/otel-collector-config.yaml +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/otel-collector-config.yaml @@ -36,3 +36,6 @@ service: metrics: receivers: [otlp, otlp/tls] exporters: [logging] + logs: + receivers: [otlp, otlp/tls] + exporters: [logging]