Skip to content

Commit

Permalink
Update AddInMemoryExporter extension method signature (#1514)
Browse files Browse the repository at this point in the history
* Updated AddInMemoryExporter extension method to take ICollection<T> as a parameter

* Updated README.md

* Updated CHANGELOG.md

* Corrected markdown format

* Update InMemoryExporter ctor call in tests

* Fix public Api documents for AddInMemoryExporter for logs

* Removed InMemoryExporter options

* Update public Api documents

Co-authored-by: Cijo Thomas <[email protected]>
  • Loading branch information
utpilla and cijothomas authored Nov 13, 2020
1 parent d363df8 commit 745fccf
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
OpenTelemetry.Exporter.InMemoryExporter<T>
OpenTelemetry.Exporter.InMemoryExporter<T>.InMemoryExporter(OpenTelemetry.Exporter.InMemoryExporterOptions options = null) -> void
OpenTelemetry.Exporter.InMemoryExporterOptions
OpenTelemetry.Exporter.InMemoryExporterOptions.ExportedItems.get -> System.Collections.Generic.ICollection<object>
OpenTelemetry.Exporter.InMemoryExporterOptions.ExportedItems.set -> void
OpenTelemetry.Exporter.InMemoryExporterOptions.InMemoryExporterOptions() -> void
OpenTelemetry.Exporter.InMemoryExporter<T>.InMemoryExporter(System.Collections.Generic.ICollection<T> exportedItems) -> void
OpenTelemetry.InMemoryExporterHelperExtensions
override OpenTelemetry.Exporter.InMemoryExporter<T>.Export(in OpenTelemetry.Batch<T> batch) -> OpenTelemetry.ExportResult
static OpenTelemetry.InMemoryExporterHelperExtensions.AddInMemoryExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Exporter.InMemoryExporterOptions> configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.InMemoryExporterHelperExtensions.AddInMemoryExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Collections.Generic.ICollection<System.Diagnostics.Activity> exportedItems) -> OpenTelemetry.Trace.TracerProviderBuilder
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
OpenTelemetry.Exporter.InMemoryExporter<T>
OpenTelemetry.Exporter.InMemoryExporter<T>.InMemoryExporter(OpenTelemetry.Exporter.InMemoryExporterOptions options = null) -> void
OpenTelemetry.Exporter.InMemoryExporterOptions
OpenTelemetry.Exporter.InMemoryExporterOptions.ExportedItems.get -> System.Collections.Generic.ICollection<object>
OpenTelemetry.Exporter.InMemoryExporterOptions.ExportedItems.set -> void
OpenTelemetry.Exporter.InMemoryExporterOptions.InMemoryExporterOptions() -> void
OpenTelemetry.Exporter.InMemoryExporter<T>.InMemoryExporter(System.Collections.Generic.ICollection<T> exportedItems) -> void
OpenTelemetry.InMemoryExporterHelperExtensions
override OpenTelemetry.Exporter.InMemoryExporter<T>.Export(in OpenTelemetry.Batch<T> batch) -> OpenTelemetry.ExportResult
static OpenTelemetry.InMemoryExporterHelperExtensions.AddInMemoryExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Exporter.InMemoryExporterOptions> configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.InMemoryExporterHelperExtensions.AddInMemoryExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Collections.Generic.ICollection<System.Diagnostics.Activity> exportedItems) -> OpenTelemetry.Trace.TracerProviderBuilder
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
Microsoft.Extensions.Logging.InMemoryExporterLoggingExtensions
OpenTelemetry.Exporter.InMemoryExporter<T>
OpenTelemetry.Exporter.InMemoryExporter<T>.InMemoryExporter(OpenTelemetry.Exporter.InMemoryExporterOptions options = null) -> void
OpenTelemetry.Exporter.InMemoryExporterOptions
OpenTelemetry.Exporter.InMemoryExporterOptions.ExportedItems.get -> System.Collections.Generic.ICollection<object>
OpenTelemetry.Exporter.InMemoryExporterOptions.ExportedItems.set -> void
OpenTelemetry.Exporter.InMemoryExporterOptions.InMemoryExporterOptions() -> void
OpenTelemetry.Exporter.InMemoryExporter<T>.InMemoryExporter(System.Collections.Generic.ICollection<T> exportedItems) -> void
OpenTelemetry.InMemoryExporterHelperExtensions
override OpenTelemetry.Exporter.InMemoryExporter<T>.Export(in OpenTelemetry.Batch<T> batch) -> OpenTelemetry.ExportResult
static Microsoft.Extensions.Logging.InMemoryExporterLoggingExtensions.AddInMemoryExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Action<OpenTelemetry.Exporter.InMemoryExporterOptions> configure = null) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
static OpenTelemetry.InMemoryExporterHelperExtensions.AddInMemoryExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Exporter.InMemoryExporterOptions> configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder
static Microsoft.Extensions.Logging.InMemoryExporterLoggingExtensions.AddInMemoryExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Collections.Generic.ICollection<OpenTelemetry.Logs.LogRecord> exportedItems) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
static OpenTelemetry.InMemoryExporterHelperExtensions.AddInMemoryExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Collections.Generic.ICollection<System.Diagnostics.Activity> exportedItems) -> OpenTelemetry.Trace.TracerProviderBuilder
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
Microsoft.Extensions.Logging.InMemoryExporterLoggingExtensions
OpenTelemetry.Exporter.InMemoryExporter<T>
OpenTelemetry.Exporter.InMemoryExporter<T>.InMemoryExporter(OpenTelemetry.Exporter.InMemoryExporterOptions options = null) -> void
OpenTelemetry.Exporter.InMemoryExporterOptions
OpenTelemetry.Exporter.InMemoryExporterOptions.ExportedItems.get -> System.Collections.Generic.ICollection<object>
OpenTelemetry.Exporter.InMemoryExporterOptions.ExportedItems.set -> void
OpenTelemetry.Exporter.InMemoryExporterOptions.InMemoryExporterOptions() -> void
OpenTelemetry.Exporter.InMemoryExporter<T>.InMemoryExporter(System.Collections.Generic.ICollection<T> exportedItems) -> void
OpenTelemetry.InMemoryExporterHelperExtensions
override OpenTelemetry.Exporter.InMemoryExporter<T>.Export(in OpenTelemetry.Batch<T> batch) -> OpenTelemetry.ExportResult
static Microsoft.Extensions.Logging.InMemoryExporterLoggingExtensions.AddInMemoryExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Action<OpenTelemetry.Exporter.InMemoryExporterOptions> configure = null) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
static OpenTelemetry.InMemoryExporterHelperExtensions.AddInMemoryExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Exporter.InMemoryExporterOptions> configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder
static Microsoft.Extensions.Logging.InMemoryExporterLoggingExtensions.AddInMemoryExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Collections.Generic.ICollection<OpenTelemetry.Logs.LogRecord> exportedItems) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
static OpenTelemetry.InMemoryExporterHelperExtensions.AddInMemoryExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Collections.Generic.ICollection<System.Diagnostics.Activity> exportedItems) -> OpenTelemetry.Trace.TracerProviderBuilder
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Updated AddInMemoryExporter extension methods for TracerProviderBuilder and
OpenTelemetryLoggerOptions
([#1514](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1514))

## 0.8.0-beta.1

Released 2020-Nov-5
Expand Down
6 changes: 3 additions & 3 deletions src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace OpenTelemetry.Exporter
public class InMemoryExporter<T> : BaseExporter<T>
where T : class
{
private readonly ICollection<object> exportedItems;
private readonly ICollection<T> exportedItems;

public InMemoryExporter(InMemoryExporterOptions options = null)
public InMemoryExporter(ICollection<T> exportedItems)
{
this.exportedItems = options?.ExportedItems;
this.exportedItems = exportedItems;
}

public override ExportResult Export(in Batch<T> batch)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System;
using System.Collections.Generic;
using System.Diagnostics;
using OpenTelemetry.Exporter;
using OpenTelemetry.Trace;
Expand All @@ -27,19 +28,22 @@ public static class InMemoryExporterHelperExtensions
/// Adds InMemory exporter to the TracerProvider.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param>
/// <param name="configure">Exporter configuration options.</param>
/// <param name="exportedItems">Collection which will be populated with the exported items.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The objects should not be disposed.")]
public static TracerProviderBuilder AddInMemoryExporter(this TracerProviderBuilder builder, Action<InMemoryExporterOptions> configure = null)
public static TracerProviderBuilder AddInMemoryExporter(this TracerProviderBuilder builder, ICollection<Activity> exportedItems)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

var options = new InMemoryExporterOptions();
configure?.Invoke(options);
return builder.AddProcessor(new SimpleExportProcessor<Activity>(new InMemoryExporter<Activity>(options)));
if (exportedItems == null)
{
throw new ArgumentNullException(nameof(exportedItems));
}

return builder.AddProcessor(new SimpleExportProcessor<Activity>(new InMemoryExporter<Activity>(exportedItems)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#if NET461 || NETSTANDARD2_0
using System;
using System.Collections.Generic;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
Expand All @@ -24,17 +25,19 @@ namespace Microsoft.Extensions.Logging
{
public static class InMemoryExporterLoggingExtensions
{
public static OpenTelemetryLoggerOptions AddInMemoryExporter(this OpenTelemetryLoggerOptions loggerOptions, Action<InMemoryExporterOptions> configure = null)
public static OpenTelemetryLoggerOptions AddInMemoryExporter(this OpenTelemetryLoggerOptions loggerOptions, ICollection<LogRecord> exportedItems)
{
if (loggerOptions == null)
{
throw new ArgumentNullException(nameof(loggerOptions));
}

var options = new InMemoryExporterOptions();
configure?.Invoke(options);
if (exportedItems == null)
{
throw new ArgumentNullException(nameof(exportedItems));
}

return loggerOptions.AddProcessor(new SimpleExportProcessor<LogRecord>(new InMemoryExporter<LogRecord>(options)));
return loggerOptions.AddProcessor(new SimpleExportProcessor<LogRecord>(new InMemoryExporter<LogRecord>(exportedItems)));
}
}
}
Expand Down
25 changes: 0 additions & 25 deletions src/OpenTelemetry.Exporter.InMemory/InMemoryExporterOptions.cs

This file was deleted.

10 changes: 5 additions & 5 deletions src/OpenTelemetry.Exporter.InMemory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ dotnet add package OpenTelemetry.Exporter.InMemory
## Configuration

```csharp
var list = new List<object>();
var activityExporter = new InMemoryExporter<Activity>(
new InMemoryExporterOptions { ExportedItems = list });
var logExporter = new InMemoryExporter<LogRecord>(
new InMemoryExporterOptions { ExportedItems = list });
var activityList = new List<Activity>();
var activityExporter = new InMemoryExporter<Activity>(activityList);

var logRecordList = new List<LogRecord>();
var logExporter = new InMemoryExporter<LogRecord>(logRecordList);
```

## References
Expand Down
26 changes: 14 additions & 12 deletions test/OpenTelemetry.Tests/Trace/BatchExportActivityProcessorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ public void CheckNullExporter()
[Fact]
public void CheckConstructorWithInvalidValues()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new BatchExportProcessor<Activity>(new InMemoryExporter<Activity>(), maxQueueSize: 0));
Assert.Throws<ArgumentOutOfRangeException>(() => new BatchExportProcessor<Activity>(new InMemoryExporter<Activity>(), maxExportBatchSize: 0));
Assert.Throws<ArgumentOutOfRangeException>(() => new BatchExportProcessor<Activity>(new InMemoryExporter<Activity>(), maxQueueSize: 1, maxExportBatchSize: 2049));
Assert.Throws<ArgumentOutOfRangeException>(() => new BatchExportProcessor<Activity>(new InMemoryExporter<Activity>(), scheduledDelayMilliseconds: 0));
Assert.Throws<ArgumentOutOfRangeException>(() => new BatchExportProcessor<Activity>(new InMemoryExporter<Activity>(), exporterTimeoutMilliseconds: -1));
var exportedItems = new List<Activity>();
Assert.Throws<ArgumentOutOfRangeException>(() => new BatchExportProcessor<Activity>(new InMemoryExporter<Activity>(exportedItems), maxQueueSize: 0));
Assert.Throws<ArgumentOutOfRangeException>(() => new BatchExportProcessor<Activity>(new InMemoryExporter<Activity>(exportedItems), maxExportBatchSize: 0));
Assert.Throws<ArgumentOutOfRangeException>(() => new BatchExportProcessor<Activity>(new InMemoryExporter<Activity>(exportedItems), maxQueueSize: 1, maxExportBatchSize: 2049));
Assert.Throws<ArgumentOutOfRangeException>(() => new BatchExportProcessor<Activity>(new InMemoryExporter<Activity>(exportedItems), scheduledDelayMilliseconds: 0));
Assert.Throws<ArgumentOutOfRangeException>(() => new BatchExportProcessor<Activity>(new InMemoryExporter<Activity>(exportedItems), exporterTimeoutMilliseconds: -1));
}

[Fact]
public void CheckIfBatchIsExportingOnQueueLimit()
{
var exportedItems = new List<object>();
using var exporter = new InMemoryExporter<Activity>(new InMemoryExporterOptions { ExportedItems = exportedItems });
var exportedItems = new List<Activity>();
using var exporter = new InMemoryExporter<Activity>(exportedItems);
using var processor = new BatchExportProcessor<Activity>(
exporter,
maxQueueSize: 1,
Expand All @@ -69,7 +70,8 @@ public void CheckIfBatchIsExportingOnQueueLimit()
[Fact]
public void CheckForceFlushWithInvalidTimeout()
{
using var exporter = new InMemoryExporter<Activity>();
var exportedItems = new List<Activity>();
using var exporter = new InMemoryExporter<Activity>(exportedItems);
using var processor = new BatchExportProcessor<Activity>(exporter, maxQueueSize: 2, maxExportBatchSize: 1);
Assert.Throws<ArgumentOutOfRangeException>(() => processor.ForceFlush(-2));
}
Expand All @@ -80,8 +82,8 @@ public void CheckForceFlushWithInvalidTimeout()
[InlineData(1)]
public void CheckForceFlushExport(int timeout)
{
var exportedItems = new List<object>();
using var exporter = new InMemoryExporter<Activity>(new InMemoryExporterOptions { ExportedItems = exportedItems });
var exportedItems = new List<Activity>();
using var exporter = new InMemoryExporter<Activity>(exportedItems);
using var processor = new BatchExportProcessor<Activity>(
exporter,
maxQueueSize: 3,
Expand Down Expand Up @@ -119,8 +121,8 @@ public void CheckForceFlushExport(int timeout)
[InlineData(1)]
public void CheckShutdownExport(int timeout)
{
var exportedItems = new List<object>();
using var exporter = new InMemoryExporter<Activity>(new InMemoryExporterOptions { ExportedItems = exportedItems });
var exportedItems = new List<Activity>();
using var exporter = new InMemoryExporter<Activity>(exportedItems);
using var processor = new BatchExportProcessor<Activity>(
exporter,
maxQueueSize: 3,
Expand Down
24 changes: 12 additions & 12 deletions test/OpenTelemetry.Tests/Trace/SimpleExportActivityProcessorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public void CheckNullExporter()
[Fact]
public void CheckExportedOnEnd()
{
var exported = new List<object>();
using var exporter = new InMemoryExporter<Activity>(new InMemoryExporterOptions { ExportedItems = exported });
var exportedItems = new List<Activity>();
using var exporter = new InMemoryExporter<Activity>(exportedItems);
using var processor = new SimpleExportProcessor<Activity>(exporter);

processor.OnEnd(new Activity("start1"));
Assert.Single(exported);
Assert.Single(exportedItems);

processor.OnEnd(new Activity("start2"));
Assert.Equal(2, exported.Count);
Assert.Equal(2, exportedItems.Count);
}

[Theory]
Expand All @@ -51,19 +51,19 @@ public void CheckExportedOnEnd()
[InlineData(1)]
public void CheckForceFlushExport(int timeout)
{
var exported = new List<object>();
using var exporter = new InMemoryExporter<Activity>(new InMemoryExporterOptions { ExportedItems = exported });
var exportedItems = new List<Activity>();
using var exporter = new InMemoryExporter<Activity>(exportedItems);
using var processor = new SimpleExportProcessor<Activity>(exporter);

processor.OnEnd(new Activity("start1"));
processor.OnEnd(new Activity("start2"));

// checking before force flush
Assert.Equal(2, exported.Count);
Assert.Equal(2, exportedItems.Count);

// forcing flush
processor.ForceFlush(timeout);
Assert.Equal(2, exported.Count);
Assert.Equal(2, exportedItems.Count);
}

[Theory]
Expand All @@ -72,17 +72,17 @@ public void CheckForceFlushExport(int timeout)
[InlineData(1)]
public void CheckShutdownExport(int timeout)
{
var exported = new List<object>();
using var exporter = new InMemoryExporter<Activity>(new InMemoryExporterOptions { ExportedItems = exported });
var exportedItems = new List<Activity>();
using var exporter = new InMemoryExporter<Activity>(exportedItems);
using var processor = new SimpleExportProcessor<Activity>(exporter);

processor.OnEnd(new Activity("start"));

// checking before shutdown
Assert.Single(exported);
Assert.Single(exportedItems);

processor.Shutdown(timeout);
Assert.Single(exported);
Assert.Single(exportedItems);
}
}
}

0 comments on commit 745fccf

Please sign in to comment.