Skip to content

Commit 2c9ebbc

Browse files
authored
Merge pull request #152 from srogovtsev/configuration-delegate
Allow configuring OTEL_ values from any source
2 parents 02ec0a3 + a55d861 commit 2c9ebbc

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

Diff for: src/Serilog.Sinks.OpenTelemetry/OpenTelemetryLoggerConfigurationExtensions.cs

+23-2
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,35 @@ public static LoggerConfiguration OpenTelemetry(
5454
Action<BatchedOpenTelemetrySinkOptions> configure,
5555
bool ignoreEnvironment = false)
5656
{
57+
return loggerSinkConfiguration.OpenTelemetry(
58+
configure,
59+
ignoreEnvironment ? null : Environment.GetEnvironmentVariable
60+
);
61+
}
62+
63+
/// <summary>
64+
/// Send log events to an OTLP exporter.
65+
/// </summary>
66+
/// <param name="loggerSinkConfiguration">
67+
/// The `WriteTo` configuration object.
68+
/// </param>
69+
/// <param name="configure">The configuration callback.</param>
70+
/// <param name="getConfigurationVariable">Provides <see href="https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/">OTLP Exporter
71+
/// Configuration variables</see> that will override other options when present.</param>
72+
public static LoggerConfiguration OpenTelemetry(
73+
this LoggerSinkConfiguration loggerSinkConfiguration,
74+
Action<BatchedOpenTelemetrySinkOptions> configure,
75+
Func<string, string?>? getConfigurationVariable)
76+
{
77+
if (loggerSinkConfiguration == null) throw new ArgumentNullException(nameof(loggerSinkConfiguration));
5778
if (configure == null) throw new ArgumentNullException(nameof(configure));
5879

5980
var options = new BatchedOpenTelemetrySinkOptions();
6081
configure(options);
6182

62-
if (!ignoreEnvironment)
83+
if (getConfigurationVariable != null)
6384
{
64-
OpenTelemetryEnvironment.Configure(options, Environment.GetEnvironmentVariable);
85+
OpenTelemetryEnvironment.Configure(options, getConfigurationVariable);
6586
}
6687

6788
var exporter = Exporter.Create(

Diff for: src/Serilog.Sinks.OpenTelemetry/Sinks/OpenTelemetry/Configuration/OpenTelemetryEnvironment.cs

+6-6
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
}

Diff for: test/Serilog.Sinks.OpenTelemetry.Tests/PublicApiVisibilityTests.approved.txt

+2-1
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)