Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional extension method for otlp log exporter to configure processor options #4733

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,13 @@ public Uri Endpoint
/// <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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.
// </copyright>

using System.Diagnostics;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Exporter;

namespace OpenTelemetry.Logs;
Expand All @@ -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>
Expand All @@ -34,7 +34,7 @@ public static OpenTelemetryLoggerOptions AddOtlpExporter(this OpenTelemetryLogge
=> 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>
Expand All @@ -44,29 +44,55 @@ public static OpenTelemetryLoggerOptions AddOtlpExporter(this OpenTelemetryLogge
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)
CodeBlanch marked this conversation as resolved.
Show resolved Hide resolved
{
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());
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
}

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)
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
{
loggerOptions.AddProcessor(new SimpleLogRecordExportProcessor(otlpExporter));
}
else
{
var batchOptions = exporterOptions.BatchExportProcessorOptions ?? defaultBatchOptions;
var batchOptions = processorOptions.BatchExportProcessorOptions;

loggerOptions.AddProcessor(new BatchLogRecordExportProcessor(
otlpExporter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1128,6 +1130,61 @@ public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_AndScopeIsUsedInLogMethod_C
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) =>
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
{
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);
Expand Down