Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In-memory Exporter: Buffer log scopes #3360

Merged
Merged
Show file tree
Hide file tree
Changes from all 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

* `InMemoryExporter` will now 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 = this.DefaultExport;
}

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

internal delegate ExportResult ExportFunc(in Batch<T> batch);
utpilla marked this conversation as resolved.
Show resolved Hide resolved

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();
utpilla marked this conversation as resolved.
Show resolved Hide resolved

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
22 changes: 3 additions & 19 deletions test/OpenTelemetry.Tests/Logs/LogRecordTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -746,15 +746,14 @@ public void ParseStateValuesUsingCustomTest()

private static ILoggerFactory InitializeLoggerFactory(out List<LogRecord> exportedItems, Action<OpenTelemetryLoggerOptions> configure = null)
{
exportedItems = new List<LogRecord>();
var exporter = new InMemoryExporter<LogRecord>(exportedItems);
var processor = new TestLogRecordProcessor(exporter);
var items = exportedItems = new List<LogRecord>();

return LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
configure?.Invoke(options);
options.AddProcessor(processor);
options.AddInMemoryExporter(items);
});
builder.AddFilter(typeof(LogRecordTest).FullName, LogLevel.Trace);
});
Expand Down Expand Up @@ -841,21 +840,6 @@ private class CustomState
{
public string Property { get; set; }
}

private class TestLogRecordProcessor : SimpleExportProcessor<LogRecord>
{
public TestLogRecordProcessor(BaseExporter<LogRecord> exporter)
: base(exporter)
{
}

public override void OnEnd(LogRecord data)
{
data.BufferLogScopes();

base.OnEnd(data);
}
}
}
}
#endif