|
21 | 21 | import java.util.Collections;
|
22 | 22 | import java.util.LinkedHashMap;
|
23 | 23 | import java.util.Map;
|
| 24 | +import java.util.function.BiConsumer; |
24 | 25 | import java.util.function.Function;
|
25 | 26 |
|
| 27 | +import org.springframework.core.env.Environment; |
| 28 | +import org.springframework.util.Assert; |
26 | 29 | import org.springframework.util.StringUtils;
|
27 | 30 |
|
28 | 31 | /**
|
29 |
| - * OpenTelemetryResourceAttributes retrieves information from the |
| 32 | + * {@link OpenTelemetryResourceAttributes} retrieves information from the |
30 | 33 | * {@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 | + * and merges it with the resource attributes provided by the user. User-provided resource |
| 35 | + * attributes take precedence. Additionally, {@code spring.application.*} related |
| 36 | + * properties can be applied as defaults. |
34 | 37 | * <p>
|
35 | 38 | * <a href= "https://opentelemetry.io/docs/specs/otel/resource/sdk/">OpenTelemetry
|
36 | 39 | * Resource Specification</a>
|
|
40 | 43 | */
|
41 | 44 | public final class OpenTelemetryResourceAttributes {
|
42 | 45 |
|
| 46 | + /** |
| 47 | + * Default value for service name if {@code service.name} is not set. |
| 48 | + */ |
| 49 | + private static final String DEFAULT_SERVICE_NAME = "unknown_service"; |
| 50 | + |
| 51 | + private final Environment environment; |
| 52 | + |
43 | 53 | private final Map<String, String> resourceAttributes;
|
44 | 54 |
|
45 | 55 | private final Function<String, String> getEnv;
|
46 | 56 |
|
47 | 57 | /**
|
48 | 58 | * Creates a new instance of {@link OpenTelemetryResourceAttributes}.
|
| 59 | + * @param environment the environment |
49 | 60 | * @param resourceAttributes user provided resource attributes to be used
|
50 | 61 | */
|
51 |
| - public OpenTelemetryResourceAttributes(Map<String, String> resourceAttributes) { |
52 |
| - this(resourceAttributes, null); |
| 62 | + public OpenTelemetryResourceAttributes(Environment environment, Map<String, String> resourceAttributes) { |
| 63 | + this(environment, resourceAttributes, null); |
53 | 64 | }
|
54 | 65 |
|
55 | 66 | /**
|
56 | 67 | * Creates a new {@link OpenTelemetryResourceAttributes} instance.
|
| 68 | + * @param environment the environment |
57 | 69 | * @param resourceAttributes user provided resource attributes to be used
|
58 | 70 | * @param getEnv a function to retrieve environment variables by name
|
59 | 71 | */
|
60 |
| - OpenTelemetryResourceAttributes(Map<String, String> resourceAttributes, Function<String, String> getEnv) { |
| 72 | + OpenTelemetryResourceAttributes(Environment environment, Map<String, String> resourceAttributes, |
| 73 | + Function<String, String> getEnv) { |
| 74 | + Assert.notNull(environment, "'environment' must not be null"); |
| 75 | + this.environment = environment; |
61 | 76 | this.resourceAttributes = (resourceAttributes != null) ? resourceAttributes : Collections.emptyMap();
|
62 | 77 | this.getEnv = (getEnv != null) ? getEnv : System::getenv;
|
63 | 78 | }
|
64 | 79 |
|
65 | 80 | /**
|
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. |
| 81 | + * Applies resource attributes to the provided BiConsumer after being combined from |
| 82 | + * environment variables and user-defined resource attributes. |
69 | 83 | * <p>
|
70 | 84 | * If a key exists in both environment variables and user-defined resources, the value
|
71 | 85 | * from the user-defined resource takes precedence, even if it is empty.
|
72 | 86 | * <p>
|
73 |
| - * <b>Null keys and values are ignored.</b> |
74 |
| - * @return the resource attributes |
| 87 | + * Additionally, {@code spring.application.name} or {@code unknown_service} will be |
| 88 | + * used as the default for {@code service.name}, and {@code spring.application.group} |
| 89 | + * will serve as the default for {@code service.group}. |
| 90 | + * @param consumer the {@link BiConsumer} to apply |
75 | 91 | */
|
76 |
| - public Map<String, String> asMap() { |
| 92 | + public void applyTo(BiConsumer<String, String> consumer) { |
| 93 | + Assert.notNull(consumer, "'consumer' must not be null"); |
77 | 94 | Map<String, String> attributes = getResourceAttributesFromEnv();
|
78 | 95 | this.resourceAttributes.forEach((name, value) -> {
|
79 |
| - if (name != null && value != null) { |
| 96 | + if (StringUtils.hasLength(name) && value != null) { |
80 | 97 | attributes.put(name, value);
|
81 | 98 | }
|
82 | 99 | });
|
83 |
| - return attributes; |
| 100 | + attributes.computeIfAbsent("service.name", (k) -> getApplicationName()); |
| 101 | + attributes.computeIfAbsent("service.group", (k) -> getApplicationGroup()); |
| 102 | + attributes.forEach(consumer); |
| 103 | + } |
| 104 | + |
| 105 | + private String getApplicationName() { |
| 106 | + return this.environment.getProperty("spring.application.name", DEFAULT_SERVICE_NAME); |
| 107 | + } |
| 108 | + |
| 109 | + private String getApplicationGroup() { |
| 110 | + String applicationGroup = this.environment.getProperty("spring.application.group"); |
| 111 | + return (StringUtils.hasLength(applicationGroup)) ? applicationGroup : null; |
84 | 112 | }
|
85 | 113 |
|
86 | 114 | /**
|
@@ -122,7 +150,7 @@ private String getEnv(String name) {
|
122 | 150 | * @param value value to decode
|
123 | 151 | * @return the decoded string
|
124 | 152 | */
|
125 |
| - public static String decode(String value) { |
| 153 | + private static String decode(String value) { |
126 | 154 | if (value.indexOf('%') < 0) {
|
127 | 155 | return value;
|
128 | 156 | }
|
|
0 commit comments