Skip to content

Commit

Permalink
[Logs] Sync up logger provider build-up scenarios with tracer provider (
Browse files Browse the repository at this point in the history
#3596)

* Since up logger provider build-up scenarios with tracer provider.

* CHANGELOG update.

* Code review.
  • Loading branch information
CodeBlanch authored Sep 7, 2022
1 parent 9c3d1b1 commit 5208ec0
Show file tree
Hide file tree
Showing 14 changed files with 458 additions and 165 deletions.
12 changes: 12 additions & 0 deletions src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
Microsoft.Extensions.DependencyInjection.TracerProviderBuilderServiceCollectionExtensions
OpenTelemetry.Logs.BatchExportLogRecordProcessorOptions
OpenTelemetry.Logs.BatchExportLogRecordProcessorOptions.BatchExportLogRecordProcessorOptions() -> void
OpenTelemetry.Logs.ExportLogRecordProcessorOptions
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.BatchExportProcessorOptions.get -> OpenTelemetry.Logs.BatchExportLogRecordProcessorOptions!
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.BatchExportProcessorOptions.set -> void
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.ExportLogRecordProcessorOptions() -> void
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.ExportProcessorType.get -> OpenTelemetry.ExportProcessorType
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.ExportProcessorType.set -> void
OpenTelemetry.Logs.LogRecord.CategoryName.set -> void
OpenTelemetry.Logs.LogRecord.EventId.set -> void
OpenTelemetry.Logs.LogRecord.Exception.set -> void
Expand All @@ -8,6 +16,10 @@ OpenTelemetry.Logs.LogRecord.Timestamp.set -> void
OpenTelemetry.Logs.LogRecord.TraceFlags.set -> void
OpenTelemetry.Logs.LogRecord.TraceId.set -> void
OpenTelemetry.Logs.LogRecord.TraceState.set -> void
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter(OpenTelemetry.ExportProcessorType exportProcessorType, OpenTelemetry.BaseExporter<OpenTelemetry.Logs.LogRecord!>! exporter) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter(OpenTelemetry.ExportProcessorType exportProcessorType, OpenTelemetry.BaseExporter<OpenTelemetry.Logs.LogRecord!>! exporter, System.Action<OpenTelemetry.Logs.ExportLogRecordProcessorOptions!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter<T>(OpenTelemetry.ExportProcessorType exportProcessorType) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter<T>(OpenTelemetry.ExportProcessorType exportProcessorType, System.Action<OpenTelemetry.Logs.ExportLogRecordProcessorOptions!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddProcessor<T>() -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ConfigureProvider(System.Action<System.IServiceProvider!, OpenTelemetry.Logs.OpenTelemetryLoggerProvider!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ConfigureResource(System.Action<OpenTelemetry.Resources.ResourceBuilder!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
Expand Down
12 changes: 12 additions & 0 deletions src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
Microsoft.Extensions.DependencyInjection.TracerProviderBuilderServiceCollectionExtensions
OpenTelemetry.Logs.BatchExportLogRecordProcessorOptions
OpenTelemetry.Logs.BatchExportLogRecordProcessorOptions.BatchExportLogRecordProcessorOptions() -> void
OpenTelemetry.Logs.ExportLogRecordProcessorOptions
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.BatchExportProcessorOptions.get -> OpenTelemetry.Logs.BatchExportLogRecordProcessorOptions!
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.BatchExportProcessorOptions.set -> void
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.ExportLogRecordProcessorOptions() -> void
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.ExportProcessorType.get -> OpenTelemetry.ExportProcessorType
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.ExportProcessorType.set -> void
OpenTelemetry.Logs.LogRecord.CategoryName.set -> void
OpenTelemetry.Logs.LogRecord.EventId.set -> void
OpenTelemetry.Logs.LogRecord.Exception.set -> void
Expand All @@ -8,6 +16,10 @@ OpenTelemetry.Logs.LogRecord.Timestamp.set -> void
OpenTelemetry.Logs.LogRecord.TraceFlags.set -> void
OpenTelemetry.Logs.LogRecord.TraceId.set -> void
OpenTelemetry.Logs.LogRecord.TraceState.set -> void
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter(OpenTelemetry.ExportProcessorType exportProcessorType, OpenTelemetry.BaseExporter<OpenTelemetry.Logs.LogRecord!>! exporter) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter(OpenTelemetry.ExportProcessorType exportProcessorType, OpenTelemetry.BaseExporter<OpenTelemetry.Logs.LogRecord!>! exporter, System.Action<OpenTelemetry.Logs.ExportLogRecordProcessorOptions!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter<T>(OpenTelemetry.ExportProcessorType exportProcessorType) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter<T>(OpenTelemetry.ExportProcessorType exportProcessorType, System.Action<OpenTelemetry.Logs.ExportLogRecordProcessorOptions!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddProcessor<T>() -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ConfigureProvider(System.Action<System.IServiceProvider!, OpenTelemetry.Logs.OpenTelemetryLoggerProvider!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ConfigureResource(System.Action<OpenTelemetry.Resources.ResourceBuilder!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
Microsoft.Extensions.DependencyInjection.TracerProviderBuilderServiceCollectionExtensions
OpenTelemetry.Logs.BatchExportLogRecordProcessorOptions
OpenTelemetry.Logs.BatchExportLogRecordProcessorOptions.BatchExportLogRecordProcessorOptions() -> void
OpenTelemetry.Logs.ExportLogRecordProcessorOptions
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.BatchExportProcessorOptions.get -> OpenTelemetry.Logs.BatchExportLogRecordProcessorOptions!
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.BatchExportProcessorOptions.set -> void
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.ExportLogRecordProcessorOptions() -> void
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.ExportProcessorType.get -> OpenTelemetry.ExportProcessorType
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.ExportProcessorType.set -> void
OpenTelemetry.Logs.LogRecord.CategoryName.set -> void
OpenTelemetry.Logs.LogRecord.EventId.set -> void
OpenTelemetry.Logs.LogRecord.Exception.set -> void
Expand All @@ -8,6 +16,10 @@ OpenTelemetry.Logs.LogRecord.Timestamp.set -> void
OpenTelemetry.Logs.LogRecord.TraceFlags.set -> void
OpenTelemetry.Logs.LogRecord.TraceId.set -> void
OpenTelemetry.Logs.LogRecord.TraceState.set -> void
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter(OpenTelemetry.ExportProcessorType exportProcessorType, OpenTelemetry.BaseExporter<OpenTelemetry.Logs.LogRecord!>! exporter) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter(OpenTelemetry.ExportProcessorType exportProcessorType, OpenTelemetry.BaseExporter<OpenTelemetry.Logs.LogRecord!>! exporter, System.Action<OpenTelemetry.Logs.ExportLogRecordProcessorOptions!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter<T>(OpenTelemetry.ExportProcessorType exportProcessorType) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter<T>(OpenTelemetry.ExportProcessorType exportProcessorType, System.Action<OpenTelemetry.Logs.ExportLogRecordProcessorOptions!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddProcessor<T>() -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ConfigureProvider(System.Action<System.IServiceProvider!, OpenTelemetry.Logs.OpenTelemetryLoggerProvider!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ConfigureServices(System.Action<Microsoft.Extensions.DependencyInjection.IServiceCollection!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
Microsoft.Extensions.DependencyInjection.TracerProviderBuilderServiceCollectionExtensions
OpenTelemetry.Logs.BatchExportLogRecordProcessorOptions
OpenTelemetry.Logs.BatchExportLogRecordProcessorOptions.BatchExportLogRecordProcessorOptions() -> void
OpenTelemetry.Logs.ExportLogRecordProcessorOptions
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.BatchExportProcessorOptions.get -> OpenTelemetry.Logs.BatchExportLogRecordProcessorOptions!
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.BatchExportProcessorOptions.set -> void
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.ExportLogRecordProcessorOptions() -> void
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.ExportProcessorType.get -> OpenTelemetry.ExportProcessorType
OpenTelemetry.Logs.ExportLogRecordProcessorOptions.ExportProcessorType.set -> void
OpenTelemetry.Logs.LogRecord.CategoryName.set -> void
OpenTelemetry.Logs.LogRecord.EventId.set -> void
OpenTelemetry.Logs.LogRecord.Exception.set -> void
Expand All @@ -8,6 +16,10 @@ OpenTelemetry.Logs.LogRecord.Timestamp.set -> void
OpenTelemetry.Logs.LogRecord.TraceFlags.set -> void
OpenTelemetry.Logs.LogRecord.TraceId.set -> void
OpenTelemetry.Logs.LogRecord.TraceState.set -> void
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter(OpenTelemetry.ExportProcessorType exportProcessorType, OpenTelemetry.BaseExporter<OpenTelemetry.Logs.LogRecord!>! exporter) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter(OpenTelemetry.ExportProcessorType exportProcessorType, OpenTelemetry.BaseExporter<OpenTelemetry.Logs.LogRecord!>! exporter, System.Action<OpenTelemetry.Logs.ExportLogRecordProcessorOptions!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter<T>(OpenTelemetry.ExportProcessorType exportProcessorType) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddExporter<T>(OpenTelemetry.ExportProcessorType exportProcessorType, System.Action<OpenTelemetry.Logs.ExportLogRecordProcessorOptions!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.AddProcessor<T>() -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ConfigureProvider(System.Action<System.IServiceProvider!, OpenTelemetry.Logs.OpenTelemetryLoggerProvider!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ConfigureResource(System.Action<OpenTelemetry.Resources.ResourceBuilder!>! configure) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions!
Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
* Added support for `UpDownCounter` and `ObservableUpDownCounter` instruments.
([#3606](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3606))

* Added `AddExporter` `OpenTelemetryLoggerOptions` methods and further refined
the `OpenTelemetryLoggerProvider` dependency injection scenarios
([#3596](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3596))

## 1.4.0-alpha.2

Released 2022-Aug-18
Expand Down
75 changes: 75 additions & 0 deletions src/OpenTelemetry/Logs/BatchExportLogRecordProcessorOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// <copyright file="BatchExportLogRecordProcessorOptions.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Logs
{
/// <summary>
/// Batch log processor options. OTEL_DOTNET_BLP_MAX_QUEUE_SIZE,
/// OTEL_DOTNET_BLP_MAX_EXPORT_BATCH_SIZE, OTEL_DOTNET_BLP_EXPORT_TIMEOUT,
/// OTEL_DOTNET_BLP_SCHEDULE_DELAY environment variables are parsed during
/// object construction.
/// </summary>
/// <remarks>
/// Notes:
/// <list type="bullet">
/// <item>The constructor throws <see cref="FormatException"/> if it fails
/// to parse any of the supported environment variables.</item>
/// <item>The environment variable keys are currently experimental and
/// subject to change. See: <see
/// href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md#opentelemetry-environment-variable-specification">OpenTelemetry
/// Environment Variable Specification</see>.
/// </item>
/// </list>
/// </remarks>
public class BatchExportLogRecordProcessorOptions : BatchExportProcessorOptions<LogRecord>
{
internal const string MaxQueueSizeEnvVarKey = "OTEL_DOTNET_BLP_MAX_QUEUE_SIZE";

internal const string MaxExportBatchSizeEnvVarKey = "OTEL_DOTNET_BLP_MAX_EXPORT_BATCH_SIZE";

internal const string ExporterTimeoutEnvVarKey = "OTEL_DOTNET_BLP_EXPORT_TIMEOUT";

internal const string ScheduledDelayEnvVarKey = "OTEL_DOTNET_BLP_SCHEDULE_DELAY";

public BatchExportLogRecordProcessorOptions()
{
int value;

if (EnvironmentVariableHelper.LoadNumeric(ExporterTimeoutEnvVarKey, out value))
{
this.ExporterTimeoutMilliseconds = value;
}

if (EnvironmentVariableHelper.LoadNumeric(MaxExportBatchSizeEnvVarKey, out value))
{
this.MaxExportBatchSize = value;
}

if (EnvironmentVariableHelper.LoadNumeric(MaxQueueSizeEnvVarKey, out value))
{
this.MaxQueueSize = value;
}

if (EnvironmentVariableHelper.LoadNumeric(ScheduledDelayEnvVarKey, out value))
{
this.ScheduledDelayMilliseconds = value;
}
}
}
}
45 changes: 45 additions & 0 deletions src/OpenTelemetry/Logs/ExportLogRecordProcessorOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// <copyright file="ExportLogRecordProcessorOptions.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

#nullable enable

using OpenTelemetry.Internal;

namespace OpenTelemetry.Logs;

public class ExportLogRecordProcessorOptions
{
private BatchExportLogRecordProcessorOptions? batchExportProcessorOptions;

/// <summary>
/// Gets or sets the export processor type to be used. The default value is <see cref="ExportProcessorType.Batch"/>.
/// </summary>
public ExportProcessorType ExportProcessorType { get; set; }

/// <summary>
/// Gets or sets the batch export options. Ignored unless <see cref="ExportProcessorType"/> is <see cref="ExportProcessorType.Batch"/>.
/// </summary>
public BatchExportLogRecordProcessorOptions BatchExportProcessorOptions
{
get => this.batchExportProcessorOptions ??= new();
set
{
Guard.ThrowIfNull(value);

this.batchExportProcessorOptions = value;
}
}
}
41 changes: 1 addition & 40 deletions src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ public OpenTelemetryLoggerProvider()
{
}

// Note: This is only for tests. Options will be missing ServiceCollection & ServiceProvider features will be unavailable.
internal OpenTelemetryLoggerProvider(OpenTelemetryLoggerOptions options)
: this(options, serviceProvider: null, ownsServiceProvider: false)
{
}

internal OpenTelemetryLoggerProvider(OpenTelemetryLoggerOptions options, IServiceProvider? serviceProvider, bool ownsServiceProvider)
{
Guard.ThrowIfNull(options);
Expand All @@ -91,33 +85,11 @@ internal OpenTelemetryLoggerProvider(OpenTelemetryLoggerOptions options, IServic
Debug.Assert(this.ownedServiceProvider != null, "ownedServiceProvider was null");
}

// Step 1: Add any processors added to options.

foreach (var processor in options.Processors)
{
this.AddProcessor(processor);
}

this.ResourceBuilder = options.ResourceBuilder ?? ResourceBuilder.CreateDefault();

if (serviceProvider != null)
{
// Step 2: Look for any Action<IServiceProvider,
// OpenTelemetryLoggerProvider> configuration actions registered and
// execute them.

var registeredConfigurations = serviceProvider.GetServices<Action<IServiceProvider, OpenTelemetryLoggerProvider>>();
foreach (var registeredConfiguration in registeredConfigurations)
{
registeredConfiguration?.Invoke(serviceProvider, this);
}
}
this.ResourceBuilder ??= ResourceBuilder.CreateDefault();

var configurationActions = options.ConfigurationActions;
if (configurationActions?.Count > 0)
{
// Step 3: Execute any configuration actions.

if (serviceProvider == null)
{
throw new InvalidOperationException("Configuration actions were registered on options but no service provider was supplied.");
Expand All @@ -133,17 +105,6 @@ internal OpenTelemetryLoggerProvider(OpenTelemetryLoggerOptions options, IServic
options.ConfigurationActions = null;
}

if (serviceProvider != null)
{
// Step 4: Look for any processors registered directly with the service provider.

var registeredProcessors = serviceProvider.GetServices<BaseProcessor<LogRecord>>();
foreach (BaseProcessor<LogRecord> processor in registeredProcessors)
{
this.AddProcessor(processor);
}
}

this.Resource = this.ResourceBuilder.Build();
this.ResourceBuilder = null;
}
Expand Down
Loading

0 comments on commit 5208ec0

Please sign in to comment.