Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;

namespace OpenTelemetry.Exporter
Expand All @@ -23,19 +22,21 @@ public class InMemoryExporter<T> : BaseExporter<T>
where T : class
{
private readonly ICollection<T> exportedItems;
private readonly Func<Batch<T>, ExportResult> onExport;
private readonly ExportFunc onExport;

public InMemoryExporter(ICollection<T> exportedItems)
{
this.exportedItems = exportedItems;
this.onExport = (Batch<T> batch) => this.DefaultExport(batch);
this.onExport = (in Batch<T> batch) => this.DefaultExport(in batch);
}

internal InMemoryExporter(Func<Batch<T>, ExportResult> exportFunc)
internal InMemoryExporter(ExportFunc exportFunc)
{
this.onExport = exportFunc;
}

internal delegate ExportResult ExportFunc(in Batch<T> batch);

public override ExportResult Export(in Batch<T> batch) => this.onExport(batch);

private ExportResult DefaultExport(in Batch<T> batch)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,27 @@ public static OpenTelemetryLoggerOptions AddInMemoryExporter(this OpenTelemetryL
Guard.ThrowIfNull(loggerOptions);
Guard.ThrowIfNull(exportedItems);

return loggerOptions.AddProcessor(new SimpleLogRecordExportProcessor(new InMemoryExporter<LogRecord>(exportedItems)));
var logExporter = new InMemoryExporter<LogRecord>(
exportFunc: (in Batch<LogRecord> batch) => ExportLogRecord(in batch, exportedItems));

return loggerOptions.AddProcessor(new SimpleLogRecordExportProcessor(logExporter));
}

private static ExportResult ExportLogRecord(in Batch<LogRecord> batch, ICollection<LogRecord> exportedItems)
{
if (exportedItems == null)
{
return ExportResult.Failure;
}

foreach (var log in batch)
{
log.BufferLogScopes();

exportedItems.Add(log);
}

return ExportResult.Success;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private static MeterProviderBuilder AddInMemoryExporter(
configureMetricReader?.Invoke(metricReaderOptions);

var metricExporter = new InMemoryExporter<Metric>(
exportFunc: metricBatch => ExportMetricSnapshot(metricBatch, exportedItems));
exportFunc: (in Batch<Metric> metricBatch) => ExportMetricSnapshot(in metricBatch, exportedItems));

var metricReader = PeriodicExportingMetricReaderHelper.CreatePeriodicExportingMetricReader(
metricExporter,
Expand Down