|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.opentelemetry; |
| 18 | + |
| 19 | +import java.io.ByteArrayOutputStream; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.LinkedHashMap; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.function.Function; |
| 25 | + |
| 26 | +import org.springframework.util.StringUtils; |
| 27 | + |
| 28 | +/** |
| 29 | + * OpenTelemetryResourceAttributes retrieves information from the |
| 30 | + * {@code OTEL_RESOURCE_ATTRIBUTES} and {@code OTEL_SERVICE_NAME} environment variables |
| 31 | + * and merges it with the resource attributes provided by the user. |
| 32 | + * <p> |
| 33 | + * <b>User-provided resource attributes take precedence.</b> |
| 34 | + * <p> |
| 35 | + * <a href= "https://opentelemetry.io/docs/specs/otel/resource/sdk/">OpenTelemetry |
| 36 | + * Resource Specification</a> |
| 37 | + * |
| 38 | + * @author Dmytro Nosan |
| 39 | + * @since 3.5.0 |
| 40 | + */ |
| 41 | +public final class OpenTelemetryResourceAttributes { |
| 42 | + |
| 43 | + private final Map<String, String> resourceAttributes; |
| 44 | + |
| 45 | + private final Function<String, String> getEnv; |
| 46 | + |
| 47 | + /** |
| 48 | + * Creates a new instance of {@link OpenTelemetryResourceAttributes}. |
| 49 | + * @param resourceAttributes user provided resource attributes to be used |
| 50 | + */ |
| 51 | + public OpenTelemetryResourceAttributes(Map<String, String> resourceAttributes) { |
| 52 | + this(resourceAttributes, null); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates a new {@link OpenTelemetryResourceAttributes} instance. |
| 57 | + * @param resourceAttributes user provided resource attributes to be used |
| 58 | + * @param getEnv a function to retrieve environment variables by name |
| 59 | + */ |
| 60 | + OpenTelemetryResourceAttributes(Map<String, String> resourceAttributes, Function<String, String> getEnv) { |
| 61 | + this.resourceAttributes = (resourceAttributes != null) ? resourceAttributes : Collections.emptyMap(); |
| 62 | + this.getEnv = (getEnv != null) ? getEnv : System::getenv; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns resource attributes by combining attributes from environment variables and |
| 67 | + * user-defined resource attributes. The final resource contains all attributes from |
| 68 | + * both sources. |
| 69 | + * <p> |
| 70 | + * If a key exists in both environment variables and user-defined resources, the value |
| 71 | + * from the user-defined resource takes precedence, even if it is empty. |
| 72 | + * <p> |
| 73 | + * <b>Null keys and values are ignored.</b> |
| 74 | + * @return the resource attributes |
| 75 | + */ |
| 76 | + public Map<String, String> asMap() { |
| 77 | + Map<String, String> attributes = getResourceAttributesFromEnv(); |
| 78 | + this.resourceAttributes.forEach((name, value) -> { |
| 79 | + if (name != null && value != null) { |
| 80 | + attributes.put(name, value); |
| 81 | + } |
| 82 | + }); |
| 83 | + return attributes; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Parses resource attributes from the {@link System#getenv()}. This method fetches |
| 88 | + * attributes defined in the {@code OTEL_RESOURCE_ATTRIBUTES} and |
| 89 | + * {@code OTEL_SERVICE_NAME} environment variables and provides them as key-value |
| 90 | + * pairs. |
| 91 | + * <p> |
| 92 | + * If {@code service.name} is also provided in {@code OTEL_RESOURCE_ATTRIBUTES}, then |
| 93 | + * {@code OTEL_SERVICE_NAME} takes precedence. |
| 94 | + * @return resource attributes |
| 95 | + */ |
| 96 | + private Map<String, String> getResourceAttributesFromEnv() { |
| 97 | + Map<String, String> attributes = new LinkedHashMap<>(); |
| 98 | + for (String attribute : StringUtils.tokenizeToStringArray(getEnv("OTEL_RESOURCE_ATTRIBUTES"), ",")) { |
| 99 | + int index = attribute.indexOf('='); |
| 100 | + if (index > 0) { |
| 101 | + String key = attribute.substring(0, index); |
| 102 | + String value = attribute.substring(index + 1); |
| 103 | + attributes.put(key.trim(), decode(value.trim())); |
| 104 | + } |
| 105 | + } |
| 106 | + String otelServiceName = getEnv("OTEL_SERVICE_NAME"); |
| 107 | + if (otelServiceName != null) { |
| 108 | + attributes.put("service.name", otelServiceName); |
| 109 | + } |
| 110 | + return attributes; |
| 111 | + } |
| 112 | + |
| 113 | + private String getEnv(String name) { |
| 114 | + return this.getEnv.apply(name); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Decodes a percent-encoded string. Converts sequences like '%HH' (where HH |
| 119 | + * represents hexadecimal digits) back into their literal representations. |
| 120 | + * @param value value to decode |
| 121 | + * @return the decoded string |
| 122 | + */ |
| 123 | + public static String decode(String value) { |
| 124 | + if (value.indexOf('%') < 0) { |
| 125 | + return value; |
| 126 | + } |
| 127 | + byte[] bytes = value.getBytes(StandardCharsets.UTF_8); |
| 128 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length); |
| 129 | + for (int i = 0; i < bytes.length; i++) { |
| 130 | + byte b = bytes[i]; |
| 131 | + if (b == '%') { |
| 132 | + int u = digit(bytes, ++i); |
| 133 | + int l = digit(bytes, ++i); |
| 134 | + if (u >= 0 && l >= 0) { |
| 135 | + bos.write((u << 4) + l); |
| 136 | + } |
| 137 | + else { |
| 138 | + bos.write(0xFF); |
| 139 | + } |
| 140 | + } |
| 141 | + else { |
| 142 | + bos.write(b); |
| 143 | + } |
| 144 | + } |
| 145 | + return bos.toString(StandardCharsets.UTF_8); |
| 146 | + } |
| 147 | + |
| 148 | + private static int digit(byte[] bytes, int index) { |
| 149 | + return (index < bytes.length) ? Character.digit(bytes[index], 16) : -1; |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments