Skip to content

Commit

Permalink
[inmemory-exporter] Add extension on LoggerProviderBuilder (#4541)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Jun 3, 2023
1 parent e67d44e commit b9a0138
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/OpenTelemetry.Api/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
[assembly: InternalsVisibleTo("OpenTelemetry.Api.ProviderBuilderExtensions" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("OpenTelemetry.Api.ProviderBuilderExtensions.Tests" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("OpenTelemetry.Api.Tests" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("OpenTelemetry.Exporter.InMemory" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("OpenTelemetry.Extensions.Hosting" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("OpenTelemetry.Extensions.Hosting.Tests" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("OpenTelemetry.Shims.OpenTracing.Tests" + AssemblyInfo.PublicKey)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,48 @@ public static class InMemoryExporterLoggingExtensions
/// </summary>
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
/// <param name="exportedItems">Collection which will be populated with the exported <see cref="LogRecord"/>.</param>
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
public static OpenTelemetryLoggerOptions AddInMemoryExporter(this OpenTelemetryLoggerOptions loggerOptions, ICollection<LogRecord> exportedItems)
/// <returns>The supplied instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
/// todo: [Obsolete("Call LoggerProviderBuilder.AddInMemoryExporter instead this method will be removed in a future version.")]
public static OpenTelemetryLoggerOptions AddInMemoryExporter(
this OpenTelemetryLoggerOptions loggerOptions,
ICollection<LogRecord> exportedItems)
{
Guard.ThrowIfNull(loggerOptions);
Guard.ThrowIfNull(exportedItems);

var logExporter = new InMemoryExporter<LogRecord>(
exportFunc: (in Batch<LogRecord> batch) => ExportLogRecord(in batch, exportedItems));
var logExporter = BuildExporter(exportedItems);

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

private static ExportResult ExportLogRecord(in Batch<LogRecord> batch, ICollection<LogRecord> exportedItems)
/// <summary>
/// Adds InMemory exporter to the LoggerProviderBuilder.
/// </summary>
/// <param name="loggerProviderBuilder"><see cref="LoggerProviderBuilder"/>.</param>
/// <param name="exportedItems">Collection which will be populated with the exported <see cref="LogRecord"/>.</param>
/// <returns>The supplied instance of <see cref="LoggerProviderBuilder"/> to chain the calls.</returns>
internal static LoggerProviderBuilder AddInMemoryExporter(
this LoggerProviderBuilder loggerProviderBuilder,
ICollection<LogRecord> exportedItems)
{
if (exportedItems == null)
{
return ExportResult.Failure;
}
Guard.ThrowIfNull(loggerProviderBuilder);
Guard.ThrowIfNull(exportedItems);

var logExporter = BuildExporter(exportedItems);

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

private static InMemoryExporter<LogRecord> BuildExporter(ICollection<LogRecord> exportedItems)
{
return new InMemoryExporter<LogRecord>(
exportFunc: (in Batch<LogRecord> batch) => ExportLogRecord(in batch, exportedItems));
}

private static ExportResult ExportLogRecord(in Batch<LogRecord> batch, ICollection<LogRecord> exportedItems)
{
foreach (var log in batch)
{
exportedItems.Add(log.Copy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
</ItemGroup>

<ItemGroup>
<!-- Note: OpenTelemetry.Exporter.InMemory temporarily sees OpenTelemetry.Api internals for LoggerProviderBuilder
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
-->
</ItemGroup>

</Project>

0 comments on commit b9a0138

Please sign in to comment.