Skip to content

Support OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES environment variables #44394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,8 +16,6 @@

package org.springframework.boot.actuate.autoconfigure.metrics.export.otlp;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

Expand All @@ -27,9 +25,8 @@

import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesConfigAdapter;
import org.springframework.boot.actuate.autoconfigure.opentelemetry.OpenTelemetryProperties;
import org.springframework.boot.actuate.autoconfigure.opentelemetry.OpenTelemetryResourceAttributes;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
* Adapter to convert {@link OtlpMetricsProperties} to an {@link OtlpConfig}.
Expand Down Expand Up @@ -78,21 +75,20 @@ public AggregationTemporality aggregationTemporality() {

@Override
public Map<String, String> resourceAttributes() {
Map<String, String> resourceAttributes = this.openTelemetryProperties.getResourceAttributes();
Map<String, String> result = new HashMap<>((!CollectionUtils.isEmpty(resourceAttributes)) ? resourceAttributes
: OtlpConfig.super.resourceAttributes());
result.computeIfAbsent("service.name", (key) -> getApplicationName());
result.computeIfAbsent("service.group", (key) -> getApplicationGroup());
return Collections.unmodifiableMap(result);
Map<String, String> attributes = new OpenTelemetryResourceAttributes(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exact copy of config from OpenTelemetryAutoConfiguration. Consider reuse

this.openTelemetryProperties.getResourceAttributes())
.asMap();
attributes.computeIfAbsent("service.name", (key) -> getApplicationName());
attributes.computeIfAbsent("service.group", (key) -> getApplicationGroup());
return attributes;
}

private String getApplicationName() {
return this.environment.getProperty("spring.application.name", DEFAULT_APPLICATION_NAME);
}

private String getApplicationGroup() {
String applicationGroup = this.environment.getProperty("spring.application.group");
return (StringUtils.hasLength(applicationGroup)) ? applicationGroup : null;
return this.environment.getProperty("spring.application.group");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,9 +16,9 @@

package org.springframework.boot.actuate.autoconfigure.opentelemetry;

import java.util.Map;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.OpenTelemetrySdkBuilder;
Expand All @@ -36,7 +36,6 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

/**
* {@link EnableAutoConfiguration Auto-configuration} for OpenTelemetry.
Expand All @@ -54,10 +53,6 @@ public class OpenTelemetryAutoConfiguration {
*/
private static final String DEFAULT_APPLICATION_NAME = "unknown_service";

private static final AttributeKey<String> ATTRIBUTE_KEY_SERVICE_NAME = AttributeKey.stringKey("service.name");

private static final AttributeKey<String> ATTRIBUTE_KEY_SERVICE_GROUP = AttributeKey.stringKey("service.group");

@Bean
@ConditionalOnMissingBean(OpenTelemetry.class)
OpenTelemetrySdk openTelemetry(ObjectProvider<SdkTracerProvider> tracerProvider,
Expand All @@ -74,20 +69,26 @@ OpenTelemetrySdk openTelemetry(ObjectProvider<SdkTracerProvider> tracerProvider,
@Bean
@ConditionalOnMissingBean
Resource openTelemetryResource(Environment environment, OpenTelemetryProperties properties) {
String applicationName = environment.getProperty("spring.application.name", DEFAULT_APPLICATION_NAME);
String applicationGroup = environment.getProperty("spring.application.group");
Resource resource = Resource.getDefault()
.merge(Resource.create(Attributes.of(ATTRIBUTE_KEY_SERVICE_NAME, applicationName)));
if (StringUtils.hasLength(applicationGroup)) {
resource = resource.merge(Resource.create(Attributes.of(ATTRIBUTE_KEY_SERVICE_GROUP, applicationGroup)));
}
return resource.merge(toResource(properties));
Resource resource = Resource.getDefault();
return resource.merge(toResource(environment, properties));
}

private static Resource toResource(OpenTelemetryProperties properties) {
private Resource toResource(Environment environment, OpenTelemetryProperties properties) {
ResourceBuilder builder = Resource.builder();
properties.getResourceAttributes().forEach(builder::put);
Map<String, String> attributes = new OpenTelemetryResourceAttributes(properties.getResourceAttributes())
.asMap();
attributes.computeIfAbsent("service.name", (key) -> getApplicationName(environment));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"service.name" is declared in OpenTelemetryResourceAttributes too.
Better to reuse a public constant from OpenTelemetryResourceAttributes.
Even better to reuse a constant from opentelemetry SDK.

For "service.group" the same note

attributes.computeIfAbsent("service.group", (key) -> getApplicationGroup(environment));
attributes.forEach(builder::put);
return builder.build();
}

private String getApplicationName(Environment environment) {
return environment.getProperty("spring.application.name", DEFAULT_APPLICATION_NAME);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a @value(${}) variable.
For "spring.application.group" too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of @Value(${..}) is uncommon in the Spring Boot codebase. Since we already have access to the Environment here, it is better to use it.

}

private String getApplicationGroup(Environment environment) {
return environment.getProperty("spring.application.group");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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.
*/

package org.springframework.boot.actuate.autoconfigure.opentelemetry;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;

import org.springframework.util.StringUtils;

/**
* OpenTelemetryResourceAttributes retrieves information from the
* {@code OTEL_RESOURCE_ATTRIBUTES} and {@code OTEL_SERVICE_NAME} environment variables
* and merges it with the resource attributes provided by the user.
* <p>
* <b>User-provided resource attributes take precedence.</b>
* <p>
* <a href= "https://opentelemetry.io/docs/specs/otel/resource/sdk/">OpenTelemetry
* Resource Specification</a>
*
* @author Dmytro Nosan
* @since 3.5.0
*/
public final class OpenTelemetryResourceAttributes {

private final Map<String, String> resourceAttributes;

private final Function<String, String> getEnv;

/**
* Creates a new instance of {@link OpenTelemetryResourceAttributes}.
* @param resourceAttributes user provided resource attributes to be used
*/
public OpenTelemetryResourceAttributes(Map<String, String> resourceAttributes) {
this(resourceAttributes, null);
}

/**
* Creates a new {@link OpenTelemetryResourceAttributes} instance.
* @param resourceAttributes user provided resource attributes to be used
* @param getEnv a function to retrieve environment variables by name
*/
OpenTelemetryResourceAttributes(Map<String, String> resourceAttributes, Function<String, String> getEnv) {
this.resourceAttributes = (resourceAttributes != null) ? resourceAttributes : Collections.emptyMap();
this.getEnv = (getEnv != null) ? getEnv : System::getenv;
}

/**
* Returns resource attributes by combining attributes from environment variables and
* user-defined resource attributes. The final resource contains all attributes from
* both sources.
* <p>
* If a key exists in both environment variables and user-defined resources, the value
* from the user-defined resource takes precedence, even if it is empty.
* <p>
* <b>Null keys and values are ignored.</b>
* @return the resource attributes
*/
public Map<String, String> asMap() {
Map<String, String> attributes = getResourceAttributesFromEnv();
this.resourceAttributes.forEach((name, value) -> {
if (name != null && value != null) {
attributes.put(name, value);
}
});
return attributes;
}

/**
* Parses resource attributes from the {@link System#getenv()}. This method fetches
* attributes defined in the {@code OTEL_RESOURCE_ATTRIBUTES} and
* {@code OTEL_SERVICE_NAME} environment variables and provides them as key-value
* pairs.
* <p>
* If {@code service.name} is also provided in {@code OTEL_RESOURCE_ATTRIBUTES}, then
* {@code OTEL_SERVICE_NAME} takes precedence.
* @return resource attributes
*/
private Map<String, String> getResourceAttributesFromEnv() {
Map<String, String> attributes = new LinkedHashMap<>();
for (String attribute : StringUtils.tokenizeToStringArray(getEnv("OTEL_RESOURCE_ATTRIBUTES"), ",")) {
int index = attribute.indexOf('=');
if (index > 0) {
String key = attribute.substring(0, index);
String value = attribute.substring(index + 1);
attributes.put(key.trim(), decode(value.trim()));
}
}
String otelServiceName = getEnv("OTEL_SERVICE_NAME");
if (otelServiceName != null) {
attributes.put("service.name", otelServiceName);
}
return attributes;
}

private String getEnv(String name) {
return this.getEnv.apply(name);
}

/**
* Decodes a percent-encoded string. Converts sequences like '%HH' (where HH
* represents hexadecimal digits) back into their literal representations.
* <p>
* Inspired by {@code org.apache.commons.codec.net.PercentCodec}.
* @param value value to decode
* @return the decoded string
*/
public static String decode(String value) {
Copy link
Contributor Author

@nosan nosan Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed various SDKs, and each has a different approach to parsing OTEL_RESOURCE_ATTRIBUTES environment variable:

I've not found information, that '+' should be decoded as a space (' ').

I was trying to implement parsing OTEL_RESOURCE_ATTRIBUTES environment variable based on the following:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the decoder looks dubious there.

  • as I know "implementation was borrowed from {@code org.apache.commons.codec.net.PercentCodec}". Reusing is always better then "borrowing".
  • stick to existing approach if it's not compromised yet. Replacing '+' to spaces seems to be simler then decoding. standardization makes life better. Might be java.net.URLDecoder

if (value.indexOf('%') < 0) {
return value;
}
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
if (b == '%') {
int u = decodeHex(bytes, ++i);
int l = decodeHex(bytes, ++i);
if (u >= 0 && l >= 0) {
bos.write((u << 4) + l);
}
else {
throw new IllegalArgumentException(
"Failed to decode percent-encoded characters at index %d in the value: '%s'"
.formatted(i - 2, value));
}
}
else {
bos.write(b);
}
}
return bos.toString(StandardCharsets.UTF_8);
}

private static int decodeHex(byte[] bytes, int index) {
return (index < bytes.length) ? Character.digit(bytes[index], 16) : -1;
}

}
Loading