Skip to content

Commit 7341a47

Browse files
authored
Update OpenTelemetry.Exporter.InMemory to use file scoped namespaces (#4683)
1 parent d016bb1 commit 7341a47

File tree

5 files changed

+287
-292
lines changed

5 files changed

+287
-292
lines changed

src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,67 +14,66 @@
1414
// limitations under the License.
1515
// </copyright>
1616

17-
namespace OpenTelemetry.Exporter
17+
namespace OpenTelemetry.Exporter;
18+
19+
public class InMemoryExporter<T> : BaseExporter<T>
20+
where T : class
1821
{
19-
public class InMemoryExporter<T> : BaseExporter<T>
20-
where T : class
22+
private readonly ICollection<T> exportedItems;
23+
private readonly ExportFunc onExport;
24+
private bool disposed;
25+
private string disposedStackTrace;
26+
27+
public InMemoryExporter(ICollection<T> exportedItems)
2128
{
22-
private readonly ICollection<T> exportedItems;
23-
private readonly ExportFunc onExport;
24-
private bool disposed;
25-
private string disposedStackTrace;
29+
this.exportedItems = exportedItems;
30+
this.onExport = this.DefaultExport;
31+
}
2632

27-
public InMemoryExporter(ICollection<T> exportedItems)
28-
{
29-
this.exportedItems = exportedItems;
30-
this.onExport = this.DefaultExport;
31-
}
33+
internal InMemoryExporter(ExportFunc exportFunc)
34+
{
35+
this.onExport = exportFunc;
36+
}
37+
38+
internal delegate ExportResult ExportFunc(in Batch<T> batch);
3239

33-
internal InMemoryExporter(ExportFunc exportFunc)
40+
public override ExportResult Export(in Batch<T> batch)
41+
{
42+
if (this.disposed)
3443
{
35-
this.onExport = exportFunc;
44+
// Note: In-memory exporter is designed for testing purposes so this error is strategic to surface lifecycle management bugs during development.
45+
throw new ObjectDisposedException(
46+
this.GetType().Name,
47+
$"The in-memory exporter is still being invoked after it has been disposed. This could be the result of invalid lifecycle management of the OpenTelemetry .NET SDK. Dispose was called on the following stack trace:{Environment.NewLine}{this.disposedStackTrace}");
3648
}
3749

38-
internal delegate ExportResult ExportFunc(in Batch<T> batch);
50+
return this.onExport(batch);
51+
}
3952

40-
public override ExportResult Export(in Batch<T> batch)
53+
/// <inheritdoc/>
54+
protected override void Dispose(bool disposing)
55+
{
56+
if (!this.disposed)
4157
{
42-
if (this.disposed)
43-
{
44-
// Note: In-memory exporter is designed for testing purposes so this error is strategic to surface lifecycle management bugs during development.
45-
throw new ObjectDisposedException(
46-
this.GetType().Name,
47-
$"The in-memory exporter is still being invoked after it has been disposed. This could be the result of invalid lifecycle management of the OpenTelemetry .NET SDK. Dispose was called on the following stack trace:{Environment.NewLine}{this.disposedStackTrace}");
48-
}
49-
50-
return this.onExport(batch);
58+
this.disposedStackTrace = Environment.StackTrace;
59+
this.disposed = true;
5160
}
5261

53-
/// <inheritdoc/>
54-
protected override void Dispose(bool disposing)
55-
{
56-
if (!this.disposed)
57-
{
58-
this.disposedStackTrace = Environment.StackTrace;
59-
this.disposed = true;
60-
}
62+
base.Dispose(disposing);
63+
}
6164

62-
base.Dispose(disposing);
65+
private ExportResult DefaultExport(in Batch<T> batch)
66+
{
67+
if (this.exportedItems == null)
68+
{
69+
return ExportResult.Failure;
6370
}
6471

65-
private ExportResult DefaultExport(in Batch<T> batch)
72+
foreach (var data in batch)
6673
{
67-
if (this.exportedItems == null)
68-
{
69-
return ExportResult.Failure;
70-
}
71-
72-
foreach (var data in batch)
73-
{
74-
this.exportedItems.Add(data);
75-
}
76-
77-
return ExportResult.Success;
74+
this.exportedItems.Add(data);
7875
}
76+
77+
return ExportResult.Success;
7978
}
8079
}

src/OpenTelemetry.Exporter.InMemory/InMemoryExporterHelperExtensions.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,21 @@
1818
using OpenTelemetry.Exporter;
1919
using OpenTelemetry.Internal;
2020

21-
namespace OpenTelemetry.Trace
21+
namespace OpenTelemetry.Trace;
22+
23+
public static class InMemoryExporterHelperExtensions
2224
{
23-
public static class InMemoryExporterHelperExtensions
25+
/// <summary>
26+
/// Adds InMemory exporter to the TracerProvider.
27+
/// </summary>
28+
/// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param>
29+
/// <param name="exportedItems">Collection which will be populated with the exported <see cref="Activity"/>.</param>
30+
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
31+
public static TracerProviderBuilder AddInMemoryExporter(this TracerProviderBuilder builder, ICollection<Activity> exportedItems)
2432
{
25-
/// <summary>
26-
/// Adds InMemory exporter to the TracerProvider.
27-
/// </summary>
28-
/// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param>
29-
/// <param name="exportedItems">Collection which will be populated with the exported <see cref="Activity"/>.</param>
30-
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
31-
public static TracerProviderBuilder AddInMemoryExporter(this TracerProviderBuilder builder, ICollection<Activity> exportedItems)
32-
{
33-
Guard.ThrowIfNull(builder);
34-
Guard.ThrowIfNull(exportedItems);
33+
Guard.ThrowIfNull(builder);
34+
Guard.ThrowIfNull(exportedItems);
3535

36-
return builder.AddProcessor(new SimpleActivityExportProcessor(new InMemoryExporter<Activity>(exportedItems)));
37-
}
36+
return builder.AddProcessor(new SimpleActivityExportProcessor(new InMemoryExporter<Activity>(exportedItems)));
3837
}
3938
}

src/OpenTelemetry.Exporter.InMemory/InMemoryExporterLoggingExtensions.cs

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,63 +17,62 @@
1717
using OpenTelemetry.Exporter;
1818
using OpenTelemetry.Internal;
1919

20-
namespace OpenTelemetry.Logs
20+
namespace OpenTelemetry.Logs;
21+
22+
public static class InMemoryExporterLoggingExtensions
2123
{
22-
public static class InMemoryExporterLoggingExtensions
24+
/// <summary>
25+
/// Adds InMemory exporter to the OpenTelemetryLoggerOptions.
26+
/// </summary>
27+
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
28+
/// <param name="exportedItems">Collection which will be populated with the exported <see cref="LogRecord"/>.</param>
29+
/// <returns>The supplied instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
30+
/// todo: [Obsolete("Call LoggerProviderBuilder.AddInMemoryExporter instead this method will be removed in a future version.")]
31+
public static OpenTelemetryLoggerOptions AddInMemoryExporter(
32+
this OpenTelemetryLoggerOptions loggerOptions,
33+
ICollection<LogRecord> exportedItems)
2334
{
24-
/// <summary>
25-
/// Adds InMemory exporter to the OpenTelemetryLoggerOptions.
26-
/// </summary>
27-
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
28-
/// <param name="exportedItems">Collection which will be populated with the exported <see cref="LogRecord"/>.</param>
29-
/// <returns>The supplied instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
30-
/// todo: [Obsolete("Call LoggerProviderBuilder.AddInMemoryExporter instead this method will be removed in a future version.")]
31-
public static OpenTelemetryLoggerOptions AddInMemoryExporter(
32-
this OpenTelemetryLoggerOptions loggerOptions,
33-
ICollection<LogRecord> exportedItems)
34-
{
35-
Guard.ThrowIfNull(loggerOptions);
36-
Guard.ThrowIfNull(exportedItems);
35+
Guard.ThrowIfNull(loggerOptions);
36+
Guard.ThrowIfNull(exportedItems);
3737

38-
var logExporter = BuildExporter(exportedItems);
38+
var logExporter = BuildExporter(exportedItems);
3939

40-
return loggerOptions.AddProcessor(
41-
new SimpleLogRecordExportProcessor(logExporter));
42-
}
40+
return loggerOptions.AddProcessor(
41+
new SimpleLogRecordExportProcessor(logExporter));
42+
}
4343

44-
/// <summary>
45-
/// Adds InMemory exporter to the LoggerProviderBuilder.
46-
/// </summary>
47-
/// <param name="loggerProviderBuilder"><see cref="LoggerProviderBuilder"/>.</param>
48-
/// <param name="exportedItems">Collection which will be populated with the exported <see cref="LogRecord"/>.</param>
49-
/// <returns>The supplied instance of <see cref="LoggerProviderBuilder"/> to chain the calls.</returns>
50-
public static LoggerProviderBuilder AddInMemoryExporter(
51-
this LoggerProviderBuilder loggerProviderBuilder,
52-
ICollection<LogRecord> exportedItems)
53-
{
54-
Guard.ThrowIfNull(loggerProviderBuilder);
55-
Guard.ThrowIfNull(exportedItems);
44+
/// <summary>
45+
/// Adds InMemory exporter to the LoggerProviderBuilder.
46+
/// </summary>
47+
/// <param name="loggerProviderBuilder"><see cref="LoggerProviderBuilder"/>.</param>
48+
/// <param name="exportedItems">Collection which will be populated with the exported <see cref="LogRecord"/>.</param>
49+
/// <returns>The supplied instance of <see cref="LoggerProviderBuilder"/> to chain the calls.</returns>
50+
public static LoggerProviderBuilder AddInMemoryExporter(
51+
this LoggerProviderBuilder loggerProviderBuilder,
52+
ICollection<LogRecord> exportedItems)
53+
{
54+
Guard.ThrowIfNull(loggerProviderBuilder);
55+
Guard.ThrowIfNull(exportedItems);
5656

57-
var logExporter = BuildExporter(exportedItems);
57+
var logExporter = BuildExporter(exportedItems);
5858

59-
return loggerProviderBuilder.AddProcessor(
60-
new SimpleLogRecordExportProcessor(logExporter));
61-
}
59+
return loggerProviderBuilder.AddProcessor(
60+
new SimpleLogRecordExportProcessor(logExporter));
61+
}
6262

63-
private static InMemoryExporter<LogRecord> BuildExporter(ICollection<LogRecord> exportedItems)
64-
{
65-
return new InMemoryExporter<LogRecord>(
66-
exportFunc: (in Batch<LogRecord> batch) => ExportLogRecord(in batch, exportedItems));
67-
}
63+
private static InMemoryExporter<LogRecord> BuildExporter(ICollection<LogRecord> exportedItems)
64+
{
65+
return new InMemoryExporter<LogRecord>(
66+
exportFunc: (in Batch<LogRecord> batch) => ExportLogRecord(in batch, exportedItems));
67+
}
6868

69-
private static ExportResult ExportLogRecord(in Batch<LogRecord> batch, ICollection<LogRecord> exportedItems)
69+
private static ExportResult ExportLogRecord(in Batch<LogRecord> batch, ICollection<LogRecord> exportedItems)
70+
{
71+
foreach (var log in batch)
7072
{
71-
foreach (var log in batch)
72-
{
73-
exportedItems.Add(log.Copy());
74-
}
75-
76-
return ExportResult.Success;
73+
exportedItems.Add(log.Copy());
7774
}
75+
76+
return ExportResult.Success;
7877
}
7978
}

0 commit comments

Comments
 (0)