Additional extension method for otlp log exporter to configure processor options (#4733)
This commit is contained in:
parent
8b5d725022
commit
a869e61b7c
|
|
@ -1,3 +1,4 @@
|
|||
OpenTelemetry.Logs.OtlpLogExporterHelperExtensions
|
||||
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
|
||||
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Action<OpenTelemetry.Exporter.OtlpExporterOptions, OpenTelemetry.Logs.LogRecordExportProcessorOptions> configureExporterAndProcessor) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
|
||||
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Action<OpenTelemetry.Exporter.OtlpExporterOptions> configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
OpenTelemetry.Logs.OtlpLogExporterHelperExtensions
|
||||
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
|
||||
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Action<OpenTelemetry.Exporter.OtlpExporterOptions, OpenTelemetry.Logs.LogRecordExportProcessorOptions> configureExporterAndProcessor) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
|
||||
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Action<OpenTelemetry.Exporter.OtlpExporterOptions> configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
OpenTelemetry.Logs.OtlpLogExporterHelperExtensions
|
||||
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
|
||||
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Action<OpenTelemetry.Exporter.OtlpExporterOptions, OpenTelemetry.Logs.LogRecordExportProcessorOptions> configureExporterAndProcessor) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
|
||||
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Action<OpenTelemetry.Exporter.OtlpExporterOptions> configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
OpenTelemetry.Logs.OtlpLogExporterHelperExtensions
|
||||
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
|
||||
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Action<OpenTelemetry.Exporter.OtlpExporterOptions, OpenTelemetry.Logs.LogRecordExportProcessorOptions> configureExporterAndProcessor) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
|
||||
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Action<OpenTelemetry.Exporter.OtlpExporterOptions> configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions
|
||||
|
|
|
|||
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
## Unreleased
|
||||
|
||||
* Added extension method for configuring export processor options for otlp log
|
||||
exporter.
|
||||
([#4733](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4733))
|
||||
|
||||
* Added support for configuring the metric exporter's temporality using the
|
||||
environment variable `OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE` as
|
||||
defined in the
|
||||
|
|
|
|||
|
|
@ -150,11 +150,13 @@ public class OtlpExporterOptions
|
|||
/// <summary>
|
||||
/// Gets or sets the export processor type to be used with the OpenTelemetry Protocol Exporter. The default value is <see cref="ExportProcessorType.Batch"/>.
|
||||
/// </summary>
|
||||
/// <remarks>Note: This only applies when exporting traces.</remarks>
|
||||
public ExportProcessorType ExportProcessorType { get; set; } = ExportProcessorType.Batch;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the BatchExportProcessor options. Ignored unless ExportProcessorType is Batch.
|
||||
/// </summary>
|
||||
/// <remarks>Note: This only applies when exporting traces.</remarks>
|
||||
public BatchExportProcessorOptions<Activity> BatchExportProcessorOptions { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@
|
|||
// limitations under the License.
|
||||
// </copyright>
|
||||
|
||||
using System.Diagnostics;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenTelemetry.Exporter;
|
||||
|
||||
namespace OpenTelemetry.Logs;
|
||||
|
|
@ -25,7 +25,7 @@ namespace OpenTelemetry.Logs;
|
|||
public static class OtlpLogExporterHelperExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds OTLP Exporter as a configuration to the OpenTelemetry ILoggingBuilder.
|
||||
/// Adds an OTLP Exporter to the OpenTelemetry <see cref="ILoggerProvider"/>.
|
||||
/// </summary>
|
||||
/// <remarks><inheritdoc cref="AddOtlpExporter(OpenTelemetryLoggerOptions, Action{OtlpExporterOptions})" path="/remarks"/></remarks>
|
||||
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
|
||||
|
|
@ -34,7 +34,7 @@ public static class OtlpLogExporterHelperExtensions
|
|||
=> AddOtlpExporterInternal(loggerOptions, configure: null);
|
||||
|
||||
/// <summary>
|
||||
/// Adds OTLP Exporter as a configuration to the OpenTelemetry ILoggingBuilder.
|
||||
/// Adds an OTLP Exporter to the OpenTelemetry <see cref="ILoggerProvider"/>.
|
||||
/// </summary>
|
||||
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
|
||||
/// <param name="configure">Callback action for configuring <see cref="OtlpExporterOptions"/>.</param>
|
||||
|
|
@ -44,29 +44,55 @@ public static class OtlpLogExporterHelperExtensions
|
|||
Action<OtlpExporterOptions> configure)
|
||||
=> AddOtlpExporterInternal(loggerOptions, configure);
|
||||
|
||||
/// <summary>
|
||||
/// Adds an OTLP Exporter to the OpenTelemetry <see cref="ILoggerProvider"/>.
|
||||
/// </summary>
|
||||
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
|
||||
/// <param name="configureExporterAndProcessor">Callback action for configuring <see cref="OtlpExporterOptions"/> and <see cref="LogRecordExportProcessorOptions"/>.</param>
|
||||
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
|
||||
public static OpenTelemetryLoggerOptions AddOtlpExporter(
|
||||
this OpenTelemetryLoggerOptions loggerOptions,
|
||||
Action<OtlpExporterOptions, LogRecordExportProcessorOptions> configureExporterAndProcessor)
|
||||
{
|
||||
var exporterOptions = new OtlpExporterOptions();
|
||||
var processorOptions = new LogRecordExportProcessorOptions();
|
||||
|
||||
configureExporterAndProcessor?.Invoke(exporterOptions, processorOptions);
|
||||
|
||||
return AddOtlpLogExporterInternal(
|
||||
loggerOptions,
|
||||
exporterOptions: exporterOptions,
|
||||
processorOptions: processorOptions);
|
||||
}
|
||||
|
||||
private static OpenTelemetryLoggerOptions AddOtlpExporterInternal(
|
||||
OpenTelemetryLoggerOptions loggerOptions,
|
||||
Action<OtlpExporterOptions> configure)
|
||||
{
|
||||
var exporterOptions = new OtlpExporterOptions();
|
||||
|
||||
// TODO: We are using span/activity batch environment variable keys
|
||||
// here when we should be using the ones for logs.
|
||||
var defaultBatchOptions = exporterOptions.BatchExportProcessorOptions;
|
||||
|
||||
Debug.Assert(defaultBatchOptions != null, "defaultBatchOptions was null");
|
||||
|
||||
configure?.Invoke(exporterOptions);
|
||||
|
||||
return AddOtlpLogExporterInternal(
|
||||
loggerOptions,
|
||||
exporterOptions: exporterOptions,
|
||||
processorOptions: new());
|
||||
}
|
||||
|
||||
private static OpenTelemetryLoggerOptions AddOtlpLogExporterInternal(
|
||||
OpenTelemetryLoggerOptions loggerOptions,
|
||||
OtlpExporterOptions exporterOptions,
|
||||
LogRecordExportProcessorOptions processorOptions)
|
||||
{
|
||||
var otlpExporter = new OtlpLogExporter(exporterOptions);
|
||||
|
||||
if (exporterOptions.ExportProcessorType == ExportProcessorType.Simple)
|
||||
if (processorOptions.ExportProcessorType == ExportProcessorType.Simple)
|
||||
{
|
||||
loggerOptions.AddProcessor(new SimpleLogRecordExportProcessor(otlpExporter));
|
||||
}
|
||||
else
|
||||
{
|
||||
var batchOptions = exporterOptions.BatchExportProcessorOptions ?? defaultBatchOptions;
|
||||
var batchOptions = processorOptions.BatchExportProcessorOptions;
|
||||
|
||||
loggerOptions.AddProcessor(new BatchLogRecordExportProcessor(
|
||||
otlpExporter,
|
||||
|
|
|
|||
|
|
@ -38,9 +38,15 @@ public static class OpenTelemetryLoggingExtensions
|
|||
/// Adds an OpenTelemetry logger named 'OpenTelemetry' to the <see cref="ILoggerFactory"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Note: This is safe to be called multiple times and by library
|
||||
/// authors. Only a single <see cref="OpenTelemetryLoggerProvider"/>
|
||||
/// will be created for a given <see cref="IServiceCollection"/>.
|
||||
/// Notes:
|
||||
/// <list type="bullet">
|
||||
/// <item>This is safe to be called multiple times and by library authors.
|
||||
/// Only a single <see cref="OpenTelemetryLoggerProvider"/> will be created
|
||||
/// for a given <see cref="IServiceCollection"/>.</item>
|
||||
/// <item><see cref="IServiceCollection"/> / <see cref="IServiceProvider"/>
|
||||
/// features (DI, Options, IConfiguration, etc.) are not available when
|
||||
/// using <see cref="ILoggingBuilder"/>.</item>
|
||||
/// </list>
|
||||
/// </remarks>
|
||||
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
|
||||
/// <returns>The supplied <see cref="ILoggingBuilder"/> for call chaining.</returns>
|
||||
|
|
|
|||
|
|
@ -14,8 +14,10 @@
|
|||
// limitations under the License.
|
||||
// </copyright>
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
|
@ -1128,6 +1130,61 @@ public class OtlpLogExporterTests : Http2UnencryptedSupportTests
|
|||
Assert.Contains(scopeValue2, allScopeValues);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddOtlpLogExporterDefaultOptionsTest()
|
||||
{
|
||||
var options = new OpenTelemetryLoggerOptions();
|
||||
|
||||
options.AddOtlpExporter();
|
||||
|
||||
var processors = (List<BaseProcessor<LogRecord>>)typeof(OpenTelemetryLoggerOptions).GetField("Processors", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(options);
|
||||
|
||||
Assert.Single(processors);
|
||||
|
||||
var batchProcesor = processors[0] as BatchLogRecordExportProcessor;
|
||||
|
||||
Assert.NotNull(batchProcesor);
|
||||
|
||||
var batchProcessorType = typeof(BatchExportProcessor<LogRecord>);
|
||||
|
||||
Assert.Equal(5000, batchProcessorType.GetField("scheduledDelayMilliseconds", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(batchProcesor));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(ExportProcessorType.Simple)]
|
||||
[InlineData(ExportProcessorType.Batch)]
|
||||
public void AddOtlpLogExporterLogRecordProcessorOptionsTest(ExportProcessorType processorType)
|
||||
{
|
||||
var options = new OpenTelemetryLoggerOptions();
|
||||
|
||||
options.AddOtlpExporter((o, l) =>
|
||||
{
|
||||
l.ExportProcessorType = processorType;
|
||||
l.BatchExportProcessorOptions = new BatchExportLogRecordProcessorOptions() { ScheduledDelayMilliseconds = 1000 };
|
||||
});
|
||||
|
||||
var processors = (List<BaseProcessor<LogRecord>>)typeof(OpenTelemetryLoggerOptions).GetField("Processors", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(options);
|
||||
|
||||
Assert.Single(processors);
|
||||
|
||||
if (processorType == ExportProcessorType.Batch)
|
||||
{
|
||||
var batchProcesor = processors[0] as BatchLogRecordExportProcessor;
|
||||
|
||||
Assert.NotNull(batchProcesor);
|
||||
|
||||
var batchProcessorType = typeof(BatchExportProcessor<LogRecord>);
|
||||
|
||||
Assert.Equal(1000, batchProcessorType.GetField("scheduledDelayMilliseconds", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(batchProcesor));
|
||||
}
|
||||
else
|
||||
{
|
||||
var simpleProcesor = processors[0] as SimpleLogRecordExportProcessor;
|
||||
|
||||
Assert.NotNull(simpleProcesor);
|
||||
}
|
||||
}
|
||||
|
||||
private static OtlpCommon.KeyValue TryGetAttribute(OtlpLogs.LogRecord record, string key)
|
||||
{
|
||||
return record.Attributes.FirstOrDefault(att => att.Key == key);
|
||||
|
|
|
|||
Loading…
Reference in New Issue