Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,35 @@ public static LoggerConfiguration OpenTelemetry(
Action<BatchedOpenTelemetrySinkOptions> configure,
bool ignoreEnvironment = false)
{
return loggerSinkConfiguration.OpenTelemetry(
configure,
ignoreEnvironment ? null : Environment.GetEnvironmentVariable
);
}

/// <summary>
/// Send log events to an OTLP exporter.
/// </summary>
/// <param name="loggerSinkConfiguration">
/// The `WriteTo` configuration object.
/// </param>
/// <param name="configure">The configuration callback.</param>
/// <param name="getConfigurationVariable">Provides <see href="https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/">OTLP Exporter
/// Configuration variables</see> that will override other options when present.</param>
public static LoggerConfiguration OpenTelemetry(
this LoggerSinkConfiguration loggerSinkConfiguration,
Action<BatchedOpenTelemetrySinkOptions> configure,
Func<string, string?>? getConfigurationVariable)
{
if (loggerSinkConfiguration == null) throw new ArgumentNullException(nameof(loggerSinkConfiguration));
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string?> getEnvironmentVariable)
public static void Configure(BatchedOpenTelemetrySinkOptions options, Func<string, string?> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
namespace Serilog
namespace Serilog
{
public static class OpenTelemetryLoggerConfigurationExtensions
{
public static Serilog.LoggerConfiguration OpenTelemetry(this Serilog.Configuration.LoggerAuditSinkConfiguration loggerAuditSinkConfiguration, System.Action<Serilog.Sinks.OpenTelemetry.OpenTelemetrySinkOptions> configure) { }
public static Serilog.LoggerConfiguration OpenTelemetry(this Serilog.Configuration.LoggerSinkConfiguration loggerSinkConfiguration, System.Action<Serilog.Sinks.OpenTelemetry.BatchedOpenTelemetrySinkOptions> configure, bool ignoreEnvironment = false) { }
public static Serilog.LoggerConfiguration OpenTelemetry(this Serilog.Configuration.LoggerSinkConfiguration loggerSinkConfiguration, System.Action<Serilog.Sinks.OpenTelemetry.BatchedOpenTelemetrySinkOptions> configure, System.Func<string, string?>? 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<string, string>? headers = null, System.Collections.Generic.IDictionary<string, object>? 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<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) { }
}
Expand Down