Skip to content

Commit

Permalink
Merge pull request #146 from nblumhardt/env-service-name
Browse files Browse the repository at this point in the history
Use the `OTEL_SERVICE_NAME` variable when present
  • Loading branch information
nblumhardt committed Jul 16, 2024
2 parents b61bbb3 + 4c12f8d commit 8c5bcfc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ static class OpenTelemetryEnvironment
const string EndpointVarName = "OTEL_EXPORTER_OTLP_ENDPOINT";
const string HeaderVarName = "OTEL_EXPORTER_OTLP_HEADERS";
const string ResourceAttributesVarName = "OTEL_RESOURCE_ATTRIBUTES";
const string ServiceNameVarName = "OTEL_SERVICE_NAME";

public static void Configure(BatchedOpenTelemetrySinkOptions options, Func<string, string?> getEnvironmentVariable)
{
Expand All @@ -39,6 +40,11 @@ public static void Configure(BatchedOpenTelemetrySinkOptions options, Func<strin
FillHeadersIfPresent(getEnvironmentVariable(HeaderVarName), options.Headers);

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

if (getEnvironmentVariable(ServiceNameVarName) is { Length: > 1 } serviceName)
{
options.ResourceAttributes[SemanticConventions.AttributeServiceName] = serviceName;
}
}

static void FillHeadersIfPresent(string? config, IDictionary<string, string> headers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ namespace Serilog.Sinks.OpenTelemetry.Tests;
public class OpenTelemetryEnvironmentTests
{
[Fact]
public void ConfigureFillOptionsWithEnvironmentVariablesValues()
public void ConfigureFillsOptionsWithEnvironmentVariableValues()
{
BatchedOpenTelemetrySinkOptions options = new();
var endpoint = "http://localhost";
var protocol = OtlpProtocol.Grpc;
var headers = "header1=1,header2=2";
var resourceAttributes = "name1=1,name2=2";
var serviceName = "my-service";

OpenTelemetryEnvironment.Configure(options, GetEnvVar);

Expand All @@ -23,17 +24,40 @@ public void ConfigureFillOptionsWithEnvironmentVariablesValues()
e => Assert.Equal(("header2", "2"), (e.Key, e.Value)));
Assert.Collection(options.ResourceAttributes,
e => Assert.Equal(("name1", "1"), (e.Key, e.Value)),
e => Assert.Equal(("name2", "2"), (e.Key, e.Value)));
e => Assert.Equal(("name2", "2"), (e.Key, e.Value)),
e => Assert.Equal(("service.name", serviceName), (e.Key, e.Value)));
return;

string? GetEnvVar(string name) => name switch
{
"OTEL_EXPORTER_OTLP_ENDPOINT" => endpoint,
"OTEL_EXPORTER_OTLP_HEADERS" => headers,
"OTEL_RESOURCE_ATTRIBUTES" => resourceAttributes,
"OTEL_EXPORTER_OTLP_PROTOCOL" => "grpc",
"OTEL_SERVICE_NAME" => serviceName,
_ => null
};
}

string? GetEnvVar(string name)
=> name switch
{
"OTEL_EXPORTER_OTLP_ENDPOINT" => endpoint,
"OTEL_EXPORTER_OTLP_HEADERS" => headers,
"OTEL_RESOURCE_ATTRIBUTES" => resourceAttributes,
"OTEL_EXPORTER_OTLP_PROTOCOL" => "grpc",
_ => null
};
[Fact]
public void ExplicitServiceNameOverridesResourceAttribute()
{
BatchedOpenTelemetrySinkOptions options = new();
var resourceAttributes = "service.name=other-service";
var serviceName = "my-service";

OpenTelemetryEnvironment.Configure(options, GetEnvVar);

Assert.Collection(options.ResourceAttributes,
e => Assert.Equal(("service.name", serviceName), (e.Key, e.Value)));
return;

string? GetEnvVar(string name) => name switch
{
"OTEL_RESOURCE_ATTRIBUTES" => resourceAttributes,
"OTEL_SERVICE_NAME" => serviceName,
_ => null
};
}

[Fact]
Expand All @@ -47,6 +71,7 @@ public void ConfigureAppendPathToEndpointIfProtocolIsHttpProtobufAndEndpointDoes

Assert.Equal($"{endpoint}/v1/logs", options.Endpoint);
Assert.Equal(protocol, options.Protocol);
return;

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

0 comments on commit 8c5bcfc

Please sign in to comment.