Skip to content

Commit 1d018e4

Browse files
committed
Allow configuring OTEL_ values from any source
1 parent ec43a5d commit 1d018e4

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

src/Serilog.Sinks.OpenTelemetry/OpenTelemetryLoggerConfigurationExtensions.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,37 @@ public static LoggerConfiguration OpenTelemetry(
5353
this LoggerSinkConfiguration loggerSinkConfiguration,
5454
Action<BatchedOpenTelemetrySinkOptions> configure,
5555
bool ignoreEnvironment = false)
56+
{
57+
if (loggerSinkConfiguration == null) throw new ArgumentNullException(nameof(loggerSinkConfiguration));
58+
59+
return loggerSinkConfiguration.OpenTelemetry(
60+
configure,
61+
ignoreEnvironment ? null : Environment.GetEnvironmentVariable
62+
);
63+
}
64+
65+
/// <summary>
66+
/// Send log events to an OTLP exporter.
67+
/// </summary>
68+
/// <param name="loggerSinkConfiguration">
69+
/// The `WriteTo` configuration object.
70+
/// </param>
71+
/// <param name="configure">The configuration callback.</param>
72+
/// <param name="getConfigurationVariable">Provides <see href="https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/">OTLP Exporter
73+
/// Configuration options</see> that will override values in configuration</param>
74+
public static LoggerConfiguration OpenTelemetry(
75+
this LoggerSinkConfiguration loggerSinkConfiguration,
76+
Action<BatchedOpenTelemetrySinkOptions> configure,
77+
Func<string, string?>? getConfigurationVariable)
5678
{
5779
if (configure == null) throw new ArgumentNullException(nameof(configure));
5880

5981
var options = new BatchedOpenTelemetrySinkOptions();
6082
configure(options);
6183

62-
if (!ignoreEnvironment)
84+
if (getConfigurationVariable != null)
6385
{
64-
OpenTelemetryEnvironment.Configure(options, Environment.GetEnvironmentVariable);
86+
OpenTelemetryEnvironment.Configure(options, getConfigurationVariable);
6587
}
6688

6789
var exporter = Exporter.Create(

src/Serilog.Sinks.OpenTelemetry/Sinks/OpenTelemetry/Configuration/OpenTelemetryEnvironment.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ static class OpenTelemetryEnvironment
2222
const string ResourceAttributesVarName = "OTEL_RESOURCE_ATTRIBUTES";
2323
const string ServiceNameVarName = "OTEL_SERVICE_NAME";
2424

25-
public static void Configure(BatchedOpenTelemetrySinkOptions options, Func<string, string?> getEnvironmentVariable)
25+
public static void Configure(BatchedOpenTelemetrySinkOptions options, Func<string, string?> getConfigurationVariable)
2626
{
27-
options.Protocol = getEnvironmentVariable(ProtocolVarName) switch
27+
options.Protocol = getConfigurationVariable(ProtocolVarName) switch
2828
{
2929
"http/protobuf" => OtlpProtocol.HttpProtobuf,
3030
"grpc" => OtlpProtocol.Grpc,
3131
_ => options.Protocol
3232
};
3333

34-
if (getEnvironmentVariable(EndpointVarName) is { Length: > 1 } endpoint)
34+
if (getConfigurationVariable(EndpointVarName) is { Length: > 1 } endpoint)
3535
options.Endpoint = endpoint;
3636

37-
FillHeadersIfPresent(getEnvironmentVariable(HeaderVarName), options.Headers);
37+
FillHeadersIfPresent(getConfigurationVariable(HeaderVarName), options.Headers);
3838

39-
FillHeadersResourceAttributesIfPresent(getEnvironmentVariable(ResourceAttributesVarName), options.ResourceAttributes);
39+
FillHeadersResourceAttributesIfPresent(getConfigurationVariable(ResourceAttributesVarName), options.ResourceAttributes);
4040

41-
if (getEnvironmentVariable(ServiceNameVarName) is { Length: > 1 } serviceName)
41+
if (getConfigurationVariable(ServiceNameVarName) is { Length: > 1 } serviceName)
4242
{
4343
options.ResourceAttributes[SemanticConventions.AttributeServiceName] = serviceName;
4444
}

test/Serilog.Sinks.OpenTelemetry.Tests/PublicApiVisibilityTests.approved.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
namespace Serilog
1+
namespace Serilog
22
{
33
public static class OpenTelemetryLoggerConfigurationExtensions
44
{
55
public static Serilog.LoggerConfiguration OpenTelemetry(this Serilog.Configuration.LoggerAuditSinkConfiguration loggerAuditSinkConfiguration, System.Action<Serilog.Sinks.OpenTelemetry.OpenTelemetrySinkOptions> configure) { }
66
public static Serilog.LoggerConfiguration OpenTelemetry(this Serilog.Configuration.LoggerSinkConfiguration loggerSinkConfiguration, System.Action<Serilog.Sinks.OpenTelemetry.BatchedOpenTelemetrySinkOptions> configure, bool ignoreEnvironment = false) { }
7+
public static Serilog.LoggerConfiguration OpenTelemetry(this Serilog.Configuration.LoggerSinkConfiguration loggerSinkConfiguration, System.Action<Serilog.Sinks.OpenTelemetry.BatchedOpenTelemetrySinkOptions> configure, System.Func<string, string?>? getConfigurationVariable) { }
78
public static Serilog.LoggerConfiguration OpenTelemetry(this Serilog.Configuration.LoggerAuditSinkConfiguration loggerAuditSinkConfiguration, string endpoint = "http://localhost:4317", Serilog.Sinks.OpenTelemetry.OtlpProtocol protocol = 0, System.Collections.Generic.IDictionary<string, string>? headers = null, System.Collections.Generic.IDictionary<string, object>? resourceAttributes = null, Serilog.Sinks.OpenTelemetry.IncludedData? includedData = default) { }
89
public static Serilog.LoggerConfiguration OpenTelemetry(this Serilog.Configuration.LoggerSinkConfiguration loggerSinkConfiguration, string endpoint = "http://localhost:4317", Serilog.Sinks.OpenTelemetry.OtlpProtocol protocol = 0, System.Collections.Generic.IDictionary<string, string>? headers = null, System.Collections.Generic.IDictionary<string, object>? resourceAttributes = null, Serilog.Sinks.OpenTelemetry.IncludedData? includedData = default, Serilog.Events.LogEventLevel restrictedToMinimumLevel = 0, Serilog.Core.LoggingLevelSwitch? levelSwitch = null) { }
910
}

0 commit comments

Comments
 (0)