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 @@ -12,14 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// ReSharper disable once RedundantUsingDirective
using System.Net.Http;

using Serilog.Collections;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
using Serilog.Sinks.OpenTelemetry;
using Serilog.Sinks.OpenTelemetry.Configuration;
using Serilog.Sinks.OpenTelemetry.Exporters;
using System.Net.Http;
// ReSharper disable MemberCanBePrivate.Global

namespace Serilog;

Expand All @@ -28,6 +31,7 @@ namespace Serilog;
/// </summary>
public static class OpenTelemetryLoggerConfigurationExtensions
{
// ReSharper disable once ReturnTypeCanBeNotNullable
static HttpMessageHandler? CreateDefaultHttpMessageHandler() =>
#if FEATURE_SOCKETS_HTTP_HANDLER
new SocketsHttpHandler { ActivityHeadersPropagator = null };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<Description>This Serilog sink transforms Serilog events into OpenTelemetry
logs and sends them to an OTLP (gRPC or HTTP) endpoint.</Description>
<VersionPrefix>3.0.1</VersionPrefix>
<VersionPrefix>4.0.0</VersionPrefix>
<Authors>Serilog Contributors</Authors>
<!-- .NET Framework version targeting is frozen at these two TFMs. -->
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT'">net471;net462</TargetFrameworks>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,65 @@
namespace Serilog.Sinks.OpenTelemetry.Configuration;
// Copyright © Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Serilog.Sinks.OpenTelemetry.Configuration;

static class OpenTelemetryEnvironment
{
private const string PROTOCOL = "OTEL_EXPORTER_OTLP_PROTOCOL";
private const string ENDPOINT = "OTEL_EXPORTER_OTLP_ENDPOINT";
private const string HEADERS = "OTEL_EXPORTER_OTLP_HEADERS";
private const string RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES";
const string ProtocolVarName = "OTEL_EXPORTER_OTLP_PROTOCOL";
const string EndpointVarName = "OTEL_EXPORTER_OTLP_ENDPOINT";
const string HeaderVarName = "OTEL_EXPORTER_OTLP_HEADERS";
const string ResourceAttributesVarName = "OTEL_RESOURCE_ATTRIBUTES";

public static void Configure(BatchedOpenTelemetrySinkOptions options, Func<string, string?> getEnvironmentVariable)
{
options.Protocol = getEnvironmentVariable(PROTOCOL) switch
options.Protocol = getEnvironmentVariable(ProtocolVarName) switch
{
"http/protobuf" => OtlpProtocol.HttpProtobuf,
"grpc" => OtlpProtocol.Grpc,
_ => options.Protocol
};

if (getEnvironmentVariable(ENDPOINT) is { Length: > 1 } endpoint)
if (getEnvironmentVariable(EndpointVarName) is { Length: > 1 } endpoint)
options.Endpoint = endpoint;

if (options.Protocol == OtlpProtocol.HttpProtobuf && !string.IsNullOrEmpty(options.Endpoint) && !options.Endpoint.EndsWith("/v1/logs"))
options.Endpoint = $"{options.Endpoint}/v1/logs";
options.Endpoint = $"{options.Endpoint.TrimEnd('/')}/v1/logs";

FillHeadersIfPresent(getEnvironmentVariable(HEADERS), options.Headers);
FillHeadersIfPresent(getEnvironmentVariable(HeaderVarName), options.Headers);

FillHeadersResourceAttributesIfPresent(getEnvironmentVariable(RESOURCE_ATTRIBUTES), options.ResourceAttributes);
FillHeadersResourceAttributesIfPresent(getEnvironmentVariable(ResourceAttributesVarName), options.ResourceAttributes);
}

private static void FillHeadersIfPresent(string? config, IDictionary<string, string> headers)
static void FillHeadersIfPresent(string? config, IDictionary<string, string> headers)
{
foreach (var part in config?.Split(',') ?? [])
{
if (part.Split('=') is { Length: 2 } parts)
headers.Add(parts[0], parts[1]);
headers[parts[0]] = parts[1];
else
throw new InvalidOperationException($"Invalid header format: {part} in {HEADERS} environment variable.");
throw new InvalidOperationException($"Invalid header format `{part}` in {HeaderVarName} environment variable.");
}
}

private static void FillHeadersResourceAttributesIfPresent(string? config, IDictionary<string, object> resourceAttributes)
static void FillHeadersResourceAttributesIfPresent(string? config, IDictionary<string, object> resourceAttributes)
{
foreach (var part in config?.Split(',') ?? [])
{
if (part.Split('=') is { Length: 2 } parts)
resourceAttributes.Add(parts[0], parts[1]);
resourceAttributes[parts[0]] = parts[1];
else
throw new InvalidOperationException($"Invalid resourceAttributes format: {part} in {RESOURCE_ATTRIBUTES} environment variable.");
throw new InvalidOperationException($"Invalid resource attributes format `{part}` in {ResourceAttributesVarName} environment variable.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void ConfigureThrowsIfHeaderEnvIsInvalidFormat()

var exception = Assert.Throws<InvalidOperationException>(() => OpenTelemetryEnvironment.Configure(options, GetEnvVar));

Assert.Equal("Invalid header format: header1 in OTEL_EXPORTER_OTLP_HEADERS environment variable.", exception.Message);
Assert.Equal("Invalid header format `header1` in OTEL_EXPORTER_OTLP_HEADERS environment variable.", exception.Message);

string? GetEnvVar(string name)
=> name switch
Expand All @@ -83,7 +83,7 @@ public void ConfigureThrowsIfResourceAttributesEnvIsInvalidFormat()

var exception = Assert.Throws<InvalidOperationException>(() => OpenTelemetryEnvironment.Configure(options, GetEnvVar));

Assert.Equal("Invalid resourceAttributes format: resource1 in OTEL_RESOURCE_ATTRIBUTES environment variable.", exception.Message);
Assert.Equal("Invalid resource attributes format `resource1` in OTEL_RESOURCE_ATTRIBUTES environment variable.", exception.Message);

string? GetEnvVar(string name)
=> name switch
Expand Down