From 899c75f428fd7cb923d313b57c04d6e6e6b345cb Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Mon, 13 Jun 2022 12:24:34 -0700 Subject: [PATCH 1/5] Buffer log scopes when using in-memory exporter. --- .../InMemoryExporter.cs | 9 ++++---- .../InMemoryExporterLoggingExtensions.cs | 22 ++++++++++++++++++- .../InMemoryExporterMetricsExtensions.cs | 2 +- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs index 8dab7b51d96..2f6d7a4c594 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs @@ -14,7 +14,6 @@ // limitations under the License. // -using System; using System.Collections.Generic; namespace OpenTelemetry.Exporter @@ -23,19 +22,21 @@ public class InMemoryExporter : BaseExporter where T : class { private readonly ICollection exportedItems; - private readonly Func, ExportResult> onExport; + private readonly ExportFunc onExport; public InMemoryExporter(ICollection exportedItems) { this.exportedItems = exportedItems; - this.onExport = (Batch batch) => this.DefaultExport(batch); + this.onExport = (in Batch batch) => this.DefaultExport(in batch); } - internal InMemoryExporter(Func, ExportResult> exportFunc) + internal InMemoryExporter(ExportFunc exportFunc) { this.onExport = exportFunc; } + internal delegate ExportResult ExportFunc(in Batch batch); + public override ExportResult Export(in Batch batch) => this.onExport(batch); private ExportResult DefaultExport(in Batch batch) diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterLoggingExtensions.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterLoggingExtensions.cs index 9cbde894ced..ca67d4f3be5 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterLoggingExtensions.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterLoggingExtensions.cs @@ -27,7 +27,27 @@ public static OpenTelemetryLoggerOptions AddInMemoryExporter(this OpenTelemetryL Guard.ThrowIfNull(loggerOptions); Guard.ThrowIfNull(exportedItems); - return loggerOptions.AddProcessor(new SimpleLogRecordExportProcessor(new InMemoryExporter(exportedItems))); + var logExporter = new InMemoryExporter( + exportFunc: (in Batch batch) => ExportLogRecord(in batch, exportedItems)); + + return loggerOptions.AddProcessor(new SimpleLogRecordExportProcessor(logExporter)); + } + + private static ExportResult ExportLogRecord(in Batch batch, ICollection exportedItems) + { + if (exportedItems == null) + { + return ExportResult.Failure; + } + + foreach (var log in batch) + { + log.BufferLogScopes(); + + exportedItems.Add(log); + } + + return ExportResult.Success; } } } diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs index f817f5744fa..613067d5e8a 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs @@ -145,7 +145,7 @@ private static MeterProviderBuilder AddInMemoryExporter( configureMetricReader?.Invoke(metricReaderOptions); var metricExporter = new InMemoryExporter( - exportFunc: metricBatch => ExportMetricSnapshot(metricBatch, exportedItems)); + exportFunc: (in Batch metricBatch) => ExportMetricSnapshot(in metricBatch, exportedItems)); var metricReader = PeriodicExportingMetricReaderHelper.CreatePeriodicExportingMetricReader( metricExporter, From 1f255ce93278b1607f934e2272b9cdc6a6f2a018 Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Mon, 13 Jun 2022 12:28:33 -0700 Subject: [PATCH 2/5] CHANGELOG update. --- src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md index 17fa5bd8c7d..20633fa850d 100644 --- a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +* Buffer scopes when exporting `LogRecord` + ([#3360](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3360)) + ## 1.3.0 Released 2022-Jun-03 From 567ac48a6dd0ba335fb5f2d58aa57b918cd7a9f5 Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Mon, 13 Jun 2022 16:37:17 -0700 Subject: [PATCH 3/5] Code review. --- src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs index 2f6d7a4c594..9715119d6b2 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs @@ -27,7 +27,7 @@ public class InMemoryExporter : BaseExporter public InMemoryExporter(ICollection exportedItems) { this.exportedItems = exportedItems; - this.onExport = (in Batch batch) => this.DefaultExport(in batch); + this.onExport = this.DefaultExport; } internal InMemoryExporter(ExportFunc exportFunc) From 07be8ada4262ac3a1420e3231f314092d6fe901a Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Tue, 14 Jun 2022 16:28:35 -0700 Subject: [PATCH 4/5] Tests. --- .../OpenTelemetry.Tests/Logs/LogRecordTest.cs | 22 +++---------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/test/OpenTelemetry.Tests/Logs/LogRecordTest.cs b/test/OpenTelemetry.Tests/Logs/LogRecordTest.cs index 37dd5615106..05305be10ff 100644 --- a/test/OpenTelemetry.Tests/Logs/LogRecordTest.cs +++ b/test/OpenTelemetry.Tests/Logs/LogRecordTest.cs @@ -746,15 +746,14 @@ public void ParseStateValuesUsingCustomTest() private static ILoggerFactory InitializeLoggerFactory(out List exportedItems, Action configure = null) { - exportedItems = new List(); - var exporter = new InMemoryExporter(exportedItems); - var processor = new TestLogRecordProcessor(exporter); + var items = exportedItems = new List(); + return LoggerFactory.Create(builder => { builder.AddOpenTelemetry(options => { configure?.Invoke(options); - options.AddProcessor(processor); + options.AddInMemoryExporter(items); }); builder.AddFilter(typeof(LogRecordTest).FullName, LogLevel.Trace); }); @@ -841,21 +840,6 @@ private class CustomState { public string Property { get; set; } } - - private class TestLogRecordProcessor : SimpleExportProcessor - { - public TestLogRecordProcessor(BaseExporter exporter) - : base(exporter) - { - } - - public override void OnEnd(LogRecord data) - { - data.BufferLogScopes(); - - base.OnEnd(data); - } - } } } #endif From 7f6e5449f92c8a6c5b5692d8f5c3c1d15d088c7c Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Wed, 15 Jun 2022 09:49:00 -0700 Subject: [PATCH 5/5] CHANGELOG tweak. --- src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md index 20633fa850d..7b528d516d4 100644 --- a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md @@ -2,7 +2,7 @@ ## Unreleased -* Buffer scopes when exporting `LogRecord` +* `InMemoryExporter` will now buffer scopes when exporting `LogRecord` ([#3360](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3360)) ## 1.3.0