Skip to content

Commit

Permalink
Code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch committed Dec 19, 2023
1 parent c19868d commit e208fbc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
10 changes: 5 additions & 5 deletions src/OpenTelemetry/Logs/ILogger/OpenTelemetryLoggingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ private static ILoggingBuilder AddOpenTelemetryInternal(
RegisterLoggerProviderOptions(services);

/* Note: This ensures IConfiguration is available when using
* IServiceCollections NOT attached to a host. For example when
* performing:
*
* new ServiceCollection().AddLogging(b => b.AddOpenTelemetry())
*/
* IServiceCollections NOT attached to a host. For example when
* performing:
*
* new ServiceCollection().AddLogging(b => b.AddOpenTelemetry())
*/
services.AddOpenTelemetrySharedProviderBuilderServices();

if (configureOptions != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,6 @@ public class OtlpLogExporterTests : Http2UnencryptedSupportTests
{
private static readonly SdkLimitOptions DefaultSdkLimitOptions = new();

[Theory]
[InlineData(true)]
[InlineData(false)]
public void ResolutionOrderTest(bool requestLoggerProviderDirectly)
{
IServiceCollection services = new ServiceCollection();

services.AddLogging(builder => builder.AddOpenTelemetry());

using var serviceProvider = services.BuildServiceProvider();

if (requestLoggerProviderDirectly)
{
var provider = serviceProvider.GetRequiredService<LoggerProvider>();
Assert.NotNull(provider);
}
else
{
var factory = serviceProvider.GetRequiredService<ILoggerFactory>();
Assert.NotNull(factory);
}
}

[Fact]
public void AddOtlpExporterWithNamedOptions()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,54 @@ public void VerifyExceptionIsThrownWhenImplementationFactoryIsNull()
Assert.Throws<ArgumentNullException>(() => sp.GetRequiredService<LoggerProvider>() as LoggerProviderSdk);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void CircularReferenceTest(bool requestLoggerProviderDirectly)
{
var services = new ServiceCollection();

services.AddLogging(logging => logging.AddOpenTelemetry());

services.ConfigureOpenTelemetryLoggerProvider(builder => builder.AddProcessor<TestLogProcessorWithILoggerFactoryDependency>());

using var sp = services.BuildServiceProvider();

if (requestLoggerProviderDirectly)
{
var provider = sp.GetRequiredService<LoggerProvider>();
Assert.NotNull(provider);
}
else
{
var factory = sp.GetRequiredService<ILoggerFactory>();
Assert.NotNull(factory);
}
}

private class TestLogProcessor : BaseProcessor<LogRecord>
{
}

private class TestLogProcessorWithILoggerFactoryDependency : BaseProcessor<LogRecord>
{
private readonly ILogger logger;

public TestLogProcessorWithILoggerFactoryDependency(ILoggerFactory loggerFactory)
{
// Note: It is NOT recommended to log from inside a processor. This
// test is meant to mirror someone injecting IHttpClientFactory
// (which itself uses ILoggerFactory) as part of an exporter. That
// is a more realistic scenario but needs a dependency to do that so
// here we approximate the graph.
this.logger = loggerFactory.CreateLogger("MyLogger");
}

protected override void Dispose(bool disposing)
{
this.logger.LogInformation("Dispose called");

base.Dispose(disposing);
}
}
}

0 comments on commit e208fbc

Please sign in to comment.