diff --git a/src/Serilog.Sinks.OpenTelemetry/OpenTelemetryLoggerConfigurationExtensions.cs b/src/Serilog.Sinks.OpenTelemetry/OpenTelemetryLoggerConfigurationExtensions.cs index 6fbe1dd..147d081 100644 --- a/src/Serilog.Sinks.OpenTelemetry/OpenTelemetryLoggerConfigurationExtensions.cs +++ b/src/Serilog.Sinks.OpenTelemetry/OpenTelemetryLoggerConfigurationExtensions.cs @@ -53,15 +53,37 @@ public static LoggerConfiguration OpenTelemetry( this LoggerSinkConfiguration loggerSinkConfiguration, Action configure, bool ignoreEnvironment = false) + { + if (loggerSinkConfiguration == null) throw new ArgumentNullException(nameof(loggerSinkConfiguration)); + + return loggerSinkConfiguration.OpenTelemetry( + configure, + ignoreEnvironment ? null : Environment.GetEnvironmentVariable + ); + } + + /// + /// Send log events to an OTLP exporter. + /// + /// + /// The `WriteTo` configuration object. + /// + /// The configuration callback. + /// Provides OTLP Exporter + /// Configuration options that will override values in configuration + public static LoggerConfiguration OpenTelemetry( + this LoggerSinkConfiguration loggerSinkConfiguration, + Action configure, + Func? getConfigurationVariable) { if (configure == null) throw new ArgumentNullException(nameof(configure)); var options = new BatchedOpenTelemetrySinkOptions(); configure(options); - if (!ignoreEnvironment) + if (getConfigurationVariable != null) { - OpenTelemetryEnvironment.Configure(options, Environment.GetEnvironmentVariable); + OpenTelemetryEnvironment.Configure(options, getConfigurationVariable); } var exporter = Exporter.Create( diff --git a/src/Serilog.Sinks.OpenTelemetry/Sinks/OpenTelemetry/Configuration/OpenTelemetryEnvironment.cs b/src/Serilog.Sinks.OpenTelemetry/Sinks/OpenTelemetry/Configuration/OpenTelemetryEnvironment.cs index c659bbc..21c773c 100644 --- a/src/Serilog.Sinks.OpenTelemetry/Sinks/OpenTelemetry/Configuration/OpenTelemetryEnvironment.cs +++ b/src/Serilog.Sinks.OpenTelemetry/Sinks/OpenTelemetry/Configuration/OpenTelemetryEnvironment.cs @@ -22,23 +22,23 @@ static class OpenTelemetryEnvironment const string ResourceAttributesVarName = "OTEL_RESOURCE_ATTRIBUTES"; const string ServiceNameVarName = "OTEL_SERVICE_NAME"; - public static void Configure(BatchedOpenTelemetrySinkOptions options, Func getEnvironmentVariable) + public static void Configure(BatchedOpenTelemetrySinkOptions options, Func getConfigurationVariable) { - options.Protocol = getEnvironmentVariable(ProtocolVarName) switch + options.Protocol = getConfigurationVariable(ProtocolVarName) switch { "http/protobuf" => OtlpProtocol.HttpProtobuf, "grpc" => OtlpProtocol.Grpc, _ => options.Protocol }; - if (getEnvironmentVariable(EndpointVarName) is { Length: > 1 } endpoint) + if (getConfigurationVariable(EndpointVarName) is { Length: > 1 } endpoint) options.Endpoint = endpoint; - FillHeadersIfPresent(getEnvironmentVariable(HeaderVarName), options.Headers); + FillHeadersIfPresent(getConfigurationVariable(HeaderVarName), options.Headers); - FillHeadersResourceAttributesIfPresent(getEnvironmentVariable(ResourceAttributesVarName), options.ResourceAttributes); + FillHeadersResourceAttributesIfPresent(getConfigurationVariable(ResourceAttributesVarName), options.ResourceAttributes); - if (getEnvironmentVariable(ServiceNameVarName) is { Length: > 1 } serviceName) + if (getConfigurationVariable(ServiceNameVarName) is { Length: > 1 } serviceName) { options.ResourceAttributes[SemanticConventions.AttributeServiceName] = serviceName; } diff --git a/test/Serilog.Sinks.OpenTelemetry.Tests/PublicApiVisibilityTests.approved.txt b/test/Serilog.Sinks.OpenTelemetry.Tests/PublicApiVisibilityTests.approved.txt index add64b2..29bc753 100644 --- a/test/Serilog.Sinks.OpenTelemetry.Tests/PublicApiVisibilityTests.approved.txt +++ b/test/Serilog.Sinks.OpenTelemetry.Tests/PublicApiVisibilityTests.approved.txt @@ -1,9 +1,10 @@ -namespace Serilog +namespace Serilog { public static class OpenTelemetryLoggerConfigurationExtensions { public static Serilog.LoggerConfiguration OpenTelemetry(this Serilog.Configuration.LoggerAuditSinkConfiguration loggerAuditSinkConfiguration, System.Action configure) { } public static Serilog.LoggerConfiguration OpenTelemetry(this Serilog.Configuration.LoggerSinkConfiguration loggerSinkConfiguration, System.Action configure, bool ignoreEnvironment = false) { } + public static Serilog.LoggerConfiguration OpenTelemetry(this Serilog.Configuration.LoggerSinkConfiguration loggerSinkConfiguration, System.Action configure, System.Func? getConfigurationVariable) { } 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? headers = null, System.Collections.Generic.IDictionary? resourceAttributes = null, Serilog.Sinks.OpenTelemetry.IncludedData? includedData = default) { } 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? headers = null, System.Collections.Generic.IDictionary? resourceAttributes = null, Serilog.Sinks.OpenTelemetry.IncludedData? includedData = default, Serilog.Events.LogEventLevel restrictedToMinimumLevel = 0, Serilog.Core.LoggingLevelSwitch? levelSwitch = null) { } }