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

[logs-branch] Port circular reference fix for LoggerProvider + IConfiguration for ExportLogRecordProcessorOptions #3814

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using OpenTelemetry.Internal;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

Expand All @@ -31,6 +32,9 @@ public static IServiceCollection AddOpenTelemetryLoggerProviderBuilderServices(t
{
services.AddOpenTelemetryProviderBuilderServices();

services.TryAddSingleton<LoggerProviderBuilderState>();
services.RegisterOptionsFactory(configuration => new ExportLogRecordProcessorOptions(configuration));

return services;
}

Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry/Logs/Builder/LoggerProviderBuilderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public LoggerProviderBuilderSdk()
var services = new ServiceCollection();

services.AddOpenTelemetryLoggerProviderBuilderServices();
services.AddSingleton<LoggerProvider>(
sp => throw new NotSupportedException("External LoggerProvider created through Sdk.CreateLoggerProviderBuilder cannot be accessed using service provider."));

this.services = services;
this.ownsServices = true;
Expand Down
3 changes: 3 additions & 0 deletions src/OpenTelemetry/Logs/ExportLogRecordProcessorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

namespace OpenTelemetry.Logs;

/// <summary>
/// Options for configuring either a <see cref="SimpleLogRecordExportProcessor"/> or <see cref="BatchLogRecordExportProcessor"/>.
/// </summary>
public class ExportLogRecordProcessorOptions
{
private BatchExportLogRecordProcessorOptions? batchExportProcessorOptions;
Expand Down
7 changes: 4 additions & 3 deletions src/OpenTelemetry/Logs/LoggerProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Diagnostics;
using System.Text;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Internal;
using OpenTelemetry.Resources;

Expand Down Expand Up @@ -49,6 +50,9 @@ public LoggerProviderSdk(
{
Debug.Assert(serviceProvider != null, "serviceProvider was null");

var state = serviceProvider!.GetRequiredService<LoggerProviderBuilderState>();
state.RegisterProvider(nameof(LoggerProvider), this);

OpenTelemetrySdkEventSource.Log.LoggerProviderSdkEvent("Building LoggerProviderSdk.");

this.ServiceProvider = serviceProvider!;
Expand All @@ -60,9 +64,6 @@ public LoggerProviderSdk(
Debug.Assert(this.ownedServiceProvider != null, "ownedServiceProvider was null");
}

var state = new LoggerProviderBuilderState(serviceProvider!);
state.RegisterProvider(nameof(LoggerProvider), this);

CallbackHelper.InvokeRegisteredConfigureStateCallbacks(
serviceProvider!,
state);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// <copyright file="BatchExportLogRecordProcessorOptionsTest.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 System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Xunit;

namespace OpenTelemetry.Logs.Tests
{
public class BatchExportLogRecordProcessorOptionsTest : IDisposable
{
public BatchExportLogRecordProcessorOptionsTest()
{
ClearEnvVars();
}

public void Dispose()
{
ClearEnvVars();
GC.SuppressFinalize(this);
}

[Fact]
public void BatchExportProcessorOptions_Defaults()
{
var options = new BatchExportLogRecordProcessorOptions();

Assert.Equal(30000, options.ExporterTimeoutMilliseconds);
Assert.Equal(512, options.MaxExportBatchSize);
Assert.Equal(2048, options.MaxQueueSize);
Assert.Equal(5000, options.ScheduledDelayMilliseconds);
}

[Fact]
public void BatchExportProcessorOptions_EnvironmentVariableOverride()
{
Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.ExporterTimeoutEnvVarKey, "1");
Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.MaxExportBatchSizeEnvVarKey, "2");
Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.MaxQueueSizeEnvVarKey, "3");
Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.ScheduledDelayEnvVarKey, "4");

var options = new BatchExportLogRecordProcessorOptions();

Assert.Equal(1, options.ExporterTimeoutMilliseconds);
Assert.Equal(2, options.MaxExportBatchSize);
Assert.Equal(3, options.MaxQueueSize);
Assert.Equal(4, options.ScheduledDelayMilliseconds);
}

[Fact]
public void ExportLogRecordProcessorOptions_UsingIConfiguration()
{
var values = new Dictionary<string, string>()
{
[BatchExportLogRecordProcessorOptions.MaxQueueSizeEnvVarKey] = "1",
[BatchExportLogRecordProcessorOptions.MaxExportBatchSizeEnvVarKey] = "2",
[BatchExportLogRecordProcessorOptions.ExporterTimeoutEnvVarKey] = "3",
[BatchExportLogRecordProcessorOptions.ScheduledDelayEnvVarKey] = "4",
};

var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(values)
.Build();

var parentOptions = new ExportLogRecordProcessorOptions(configuration);

Assert.Equal(1, parentOptions.BatchExportProcessorOptions.MaxQueueSize);
Assert.Equal(2, parentOptions.BatchExportProcessorOptions.MaxExportBatchSize);
Assert.Equal(3, parentOptions.BatchExportProcessorOptions.ExporterTimeoutMilliseconds);
Assert.Equal(4, parentOptions.BatchExportProcessorOptions.ScheduledDelayMilliseconds);

var options = new BatchExportLogRecordProcessorOptions(configuration);

Assert.Equal(1, options.MaxQueueSize);
Assert.Equal(2, options.MaxExportBatchSize);
Assert.Equal(3, options.ExporterTimeoutMilliseconds);
Assert.Equal(4, options.ScheduledDelayMilliseconds);
}

[Fact]
public void BatchExportProcessorOptions_InvalidPortEnvironmentVariableOverride()
{
Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.ExporterTimeoutEnvVarKey, "invalid");

Assert.Throws<FormatException>(() => new BatchExportLogRecordProcessorOptions());
}

[Fact]
public void BatchExportProcessorOptions_SetterOverridesEnvironmentVariable()
{
Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.ExporterTimeoutEnvVarKey, "123");

var options = new BatchExportLogRecordProcessorOptions
{
ExporterTimeoutMilliseconds = 89000,
};

Assert.Equal(89000, options.ExporterTimeoutMilliseconds);
}

[Fact]
public void BatchExportProcessorOptions_EnvironmentVariableNames()
{
Assert.Equal("OTEL_BLRP_EXPORT_TIMEOUT", BatchExportLogRecordProcessorOptions.ExporterTimeoutEnvVarKey);
Assert.Equal("OTEL_BLRP_MAX_EXPORT_BATCH_SIZE", BatchExportLogRecordProcessorOptions.MaxExportBatchSizeEnvVarKey);
Assert.Equal("OTEL_BLRP_MAX_QUEUE_SIZE", BatchExportLogRecordProcessorOptions.MaxQueueSizeEnvVarKey);
Assert.Equal("OTEL_BLRP_SCHEDULE_DELAY", BatchExportLogRecordProcessorOptions.ScheduledDelayEnvVarKey);
}

private static void ClearEnvVars()
{
Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.ExporterTimeoutEnvVarKey, null);
Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.MaxExportBatchSizeEnvVarKey, null);
Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.MaxQueueSizeEnvVarKey, null);
Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.ScheduledDelayEnvVarKey, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void LoggerProviderBuilderAddInstrumentationTest()

using (var provider = Sdk.CreateLoggerProviderBuilder()
.AddInstrumentation<CustomInstrumentation>()
.AddInstrumentation((sp, provider) => new CustomInstrumentation(provider))
.AddInstrumentation((sp, provider) => new CustomInstrumentation() { Provider = provider })
.Build() as LoggerProviderSdk)
{
Assert.NotNull(provider);
Expand All @@ -160,6 +160,56 @@ public void LoggerProviderBuilderAddInstrumentationTest()
Assert.True(((CustomInstrumentation)instrumentation[1]).Disposed);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void LoggerProviderNestedResolutionUsingBuilderTest(bool callNestedConfigure)
{
bool innerTestExecuted = false;

using var provider = Sdk.CreateLoggerProviderBuilder()
.ConfigureServices(services =>
{
if (callNestedConfigure)
{
services.ConfigureOpenTelemetryLogging();
}
})
.ConfigureBuilder((sp, builder) =>
{
innerTestExecuted = true;
Assert.Throws<NotSupportedException>(() => sp.GetService<LoggerProvider>());
})
.Build();

Assert.True(innerTestExecuted);

Assert.Throws<NotSupportedException>(() => provider.GetServiceProvider()?.GetService<LoggerProvider>());
}

[Fact]
public void LoggerProviderNestedResolutionUsingConfigureTest()
{
bool innerTestExecuted = false;

var serviceCollection = new ServiceCollection();

serviceCollection.ConfigureOpenTelemetryLogging(builder =>
{
builder.ConfigureBuilder((sp, builder) =>
{
innerTestExecuted = true;
Assert.Throws<NotSupportedException>(() => sp.GetService<LoggerProvider>());
});
});

using var serviceProvider = serviceCollection.BuildServiceProvider();

var resolvedProvider = serviceProvider.GetRequiredService<LoggerProvider>();

Assert.True(innerTestExecuted);
}

private sealed class CustomInstrumentation : IDisposable
{
public bool Disposed;
Expand All @@ -169,11 +219,6 @@ public CustomInstrumentation()
{
}

public CustomInstrumentation(LoggerProvider provider)
{
this.Provider = provider;
}

public void Dispose()
{
this.Disposed = true;
Expand Down