Skip to content

Commit

Permalink
add test paths for optional name
Browse files Browse the repository at this point in the history
  • Loading branch information
Yun-Ting committed Nov 15, 2023
1 parent 9f46d13 commit fcd2dff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ private static OtlpExporterOptions GetOtlpExporterOptions(IServiceProvider sp, s
{
// Note: If OtlpExporter has been registered for tracing and/or metrics
// then IOptionsFactory will be set by a call to
// OtlpExporterOptions.RegisterOtlpExporterOptionsFactory. However if we
// are only using logging we don't have an opportunity to do that
// OtlpExporterOptions.RegisterOtlpExporterOptionsFactory. However, if we
// are only using logging, we don't have an opportunity to do that
// registration so we manually create a factory.

var optionsFactory = sp.GetRequiredService<IOptionsFactory<OtlpExporterOptions>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1289,8 +1289,12 @@ public void ValidateInstrumentationScope()
Assert.Empty(OtlpLogRecordTransformer.LogListPool);
}

[Fact]
public void VerifyEnvironmentVariablesTakenFromIConfigurationWhenUsingLoggerFactoryCreate()
[Theory]
[InlineData("logging", true)]
[InlineData("logging", false)]
[InlineData("tracing", true)]
[InlineData("tracing", false)]
public void VerifyEnvironmentVariablesTakenFromIConfigurationWhenUsingLoggerFactoryCreate(string name, bool optionalNameMatch)
{
var values = new Dictionary<string, string>()
{
Expand All @@ -1307,20 +1311,31 @@ public void VerifyEnvironmentVariablesTakenFromIConfigurationWhenUsingLoggerFact
{
logging.Services.AddSingleton<IConfiguration>(configuration);

logging.Services.Configure<OtlpExporterOptions>(o =>
logging.Services.Configure<OtlpExporterOptions>(name, o =>
{
configureDelegateCalled = true;
Assert.Equal(new Uri("http://test:8888"), o.Endpoint);
});

logging.AddOpenTelemetry(o => o.AddOtlpExporter());
logging.AddOpenTelemetry(o => o.AddOtlpExporter(optionalNameMatch ? name : "otherSignalName", configure: null));
});

Assert.True(configureDelegateCalled);
if (optionalNameMatch)
{
Assert.True(configureDelegateCalled);
}
else
{
Assert.False(configureDelegateCalled);
}
}

[Fact]
public void VerifyEnvironmentVariablesTakenFromIConfigurationWhenUsingLoggingBuilder()
[Theory]
[InlineData("logging", true)]
[InlineData("logging", false)]
[InlineData("tracing", true)]
[InlineData("tracing", false)]
public void VerifyEnvironmentVariablesTakenFromIConfigurationWhenUsingLoggingBuilder(string name, bool optionalNameMatch)
{
var values = new Dictionary<string, string>()
{
Expand All @@ -1337,22 +1352,30 @@ public void VerifyEnvironmentVariablesTakenFromIConfigurationWhenUsingLoggingBui

services.AddSingleton<IConfiguration>(configuration);

services.Configure<OtlpExporterOptions>(o =>
services.Configure<OtlpExporterOptions>(name, o =>
{
configureDelegateCalled = true;
Assert.Equal(new Uri("http://test:8888"), o.Endpoint);
});

services.AddLogging(
logging => logging.AddOpenTelemetry(o => o.AddOtlpExporter()));
logging => logging.AddOpenTelemetry(o =>
o.AddOtlpExporter(optionalNameMatch ? name : "otherSignalName", configure: null)));

using var sp = services.BuildServiceProvider();

var factory = sp.GetRequiredService<ILoggerFactory>();

Assert.NotNull(factory);

Assert.True(configureDelegateCalled);
if (optionalNameMatch)
{
Assert.True(configureDelegateCalled);
}
else
{
Assert.False(configureDelegateCalled);
}
}

private static OtlpCommon.KeyValue TryGetAttribute(OtlpLogs.LogRecord record, string key)
Expand Down

0 comments on commit fcd2dff

Please sign in to comment.