[inmemory-exporter] Add extension on LoggerProviderBuilder (#4541)
This commit is contained in:
parent
e67d44e9f7
commit
b9a0138a61
|
|
@ -20,6 +20,7 @@ using System.Runtime.CompilerServices;
|
||||||
[assembly: InternalsVisibleTo("OpenTelemetry.Api.ProviderBuilderExtensions" + AssemblyInfo.PublicKey)]
|
[assembly: InternalsVisibleTo("OpenTelemetry.Api.ProviderBuilderExtensions" + AssemblyInfo.PublicKey)]
|
||||||
[assembly: InternalsVisibleTo("OpenTelemetry.Api.ProviderBuilderExtensions.Tests" + AssemblyInfo.PublicKey)]
|
[assembly: InternalsVisibleTo("OpenTelemetry.Api.ProviderBuilderExtensions.Tests" + AssemblyInfo.PublicKey)]
|
||||||
[assembly: InternalsVisibleTo("OpenTelemetry.Api.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" + AssemblyInfo.PublicKey)]
|
||||||
[assembly: InternalsVisibleTo("OpenTelemetry.Extensions.Hosting.Tests" + AssemblyInfo.PublicKey)]
|
[assembly: InternalsVisibleTo("OpenTelemetry.Extensions.Hosting.Tests" + AssemblyInfo.PublicKey)]
|
||||||
[assembly: InternalsVisibleTo("OpenTelemetry.Shims.OpenTracing.Tests" + AssemblyInfo.PublicKey)]
|
[assembly: InternalsVisibleTo("OpenTelemetry.Shims.OpenTracing.Tests" + AssemblyInfo.PublicKey)]
|
||||||
|
|
|
||||||
|
|
@ -26,25 +26,48 @@ namespace OpenTelemetry.Logs
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
|
/// <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>
|
/// <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>
|
/// <returns>The supplied instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
|
||||||
public static OpenTelemetryLoggerOptions AddInMemoryExporter(this OpenTelemetryLoggerOptions loggerOptions, ICollection<LogRecord> exportedItems)
|
/// 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(loggerOptions);
|
||||||
Guard.ThrowIfNull(exportedItems);
|
Guard.ThrowIfNull(exportedItems);
|
||||||
|
|
||||||
var logExporter = new InMemoryExporter<LogRecord>(
|
var logExporter = BuildExporter(exportedItems);
|
||||||
exportFunc: (in Batch<LogRecord> batch) => ExportLogRecord(in batch, exportedItems));
|
|
||||||
|
|
||||||
return loggerOptions.AddProcessor(new SimpleLogRecordExportProcessor(logExporter));
|
return loggerOptions.AddProcessor(
|
||||||
|
new SimpleLogRecordExportProcessor(logExporter));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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)
|
||||||
|
{
|
||||||
|
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)
|
private static ExportResult ExportLogRecord(in Batch<LogRecord> batch, ICollection<LogRecord> exportedItems)
|
||||||
{
|
{
|
||||||
if (exportedItems == null)
|
|
||||||
{
|
|
||||||
return ExportResult.Failure;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var log in batch)
|
foreach (var log in batch)
|
||||||
{
|
{
|
||||||
exportedItems.Add(log.Copy());
|
exportedItems.Add(log.Copy());
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,9 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<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" />
|
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
|
||||||
|
-->
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue