Skip to content

Commit 96e8f4c

Browse files
committed
improve handling of general properties, signal if declarative config is available at all
1 parent 103044b commit 96e8f4c

File tree

6 files changed

+91
-65
lines changed

6 files changed

+91
-65
lines changed

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/config/internal/CommonConfig.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77

88
import static java.util.Collections.emptyMap;
99

10+
import io.opentelemetry.api.incubator.config.ConfigProvider;
11+
import io.opentelemetry.api.incubator.config.InstrumentationConfigUtil;
1012
import io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants;
1113
import io.opentelemetry.instrumentation.api.incubator.semconv.net.PeerServiceResolver;
1214
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
1315
import java.util.ArrayList;
1416
import java.util.HashSet;
1517
import java.util.List;
1618
import java.util.Set;
19+
import java.util.function.Function;
20+
import java.util.function.Supplier;
1721

1822
/**
1923
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
@@ -39,16 +43,32 @@ public final class CommonConfig {
3943
public CommonConfig(InstrumentationConfig config) {
4044
peerServiceResolver =
4145
PeerServiceResolver.create(
42-
config.getMap("otel.instrumentation.common.peer-service-mapping", emptyMap()));
46+
getFromConfigProviderOrFallback(
47+
config,
48+
InstrumentationConfigUtil::peerServiceMapping,
49+
() ->
50+
config.getMap("otel.instrumentation.common.peer-service-mapping", emptyMap())));
4351

4452
clientRequestHeaders =
45-
config.getList("otel.instrumentation.http.client.capture-request-headers");
53+
getFromConfigProviderOrFallback(
54+
config,
55+
InstrumentationConfigUtil::httpClientRequestCapturedHeaders,
56+
() -> config.getList("otel.instrumentation.http.client.capture-request-headers"));
4657
clientResponseHeaders =
47-
config.getList("otel.instrumentation.http.client.capture-response-headers");
58+
getFromConfigProviderOrFallback(
59+
config,
60+
InstrumentationConfigUtil::httpClientResponseCapturedHeaders,
61+
() -> config.getList("otel.instrumentation.http.client.capture-response-headers"));
4862
serverRequestHeaders =
49-
config.getList("otel.instrumentation.http.server.capture-request-headers");
63+
getFromConfigProviderOrFallback(
64+
config,
65+
InstrumentationConfigUtil::httpServerRequestCapturedHeaders,
66+
() -> config.getList("otel.instrumentation.http.server.capture-request-headers"));
5067
serverResponseHeaders =
51-
config.getList("otel.instrumentation.http.server.capture-response-headers");
68+
getFromConfigProviderOrFallback(
69+
config,
70+
InstrumentationConfigUtil::httpSeverResponseCapturedHeaders,
71+
() -> config.getList("otel.instrumentation.http.server.capture-response-headers"));
5272
knownHttpRequestMethods =
5373
new HashSet<>(
5474
config.getList(
@@ -130,4 +150,15 @@ public String getSpanIdKey() {
130150
public String getTraceFlagsKey() {
131151
return loggingTraceFlagsKey;
132152
}
153+
154+
private static <T> T getFromConfigProviderOrFallback(
155+
InstrumentationConfig config,
156+
Function<ConfigProvider, T> getFromConfigProvider,
157+
Supplier<T> fallback) {
158+
ConfigProvider configProvider = config.getConfigProvider();
159+
if (configProvider != null) {
160+
return getFromConfigProvider.apply(configProvider);
161+
}
162+
return fallback.get();
163+
}
133164
}

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/config/internal/InstrumentationConfig.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import static java.util.Collections.emptyList;
99

10+
import io.opentelemetry.api.incubator.config.ConfigProvider;
1011
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
1112
import java.time.Duration;
1213
import java.util.List;
@@ -111,15 +112,23 @@ default List<String> getList(String name) {
111112

112113
/**
113114
* Returns a {@link DeclarativeConfigProperties} for the given instrumentation name, or {@code
114-
* null} if no declarative configuration is available for that instrumentation.
115+
* null} if no declarative configuration <b>not used at all</b>.
115116
*
116117
* <p>Declarative configuration is used to configure instrumentation properties in a declarative
117118
* way, such as through YAML or JSON files.
118119
*
119-
* @param instrumentationName the name of the instrumentation
120-
* @return the declarative configuration properties for the given instrumentation name, or {@code
121-
* null} if not available
120+
* @param node the name of the instrumentation (e.g. "log4j"), the vendor name (e.g. "google"), or
121+
* "common" for common Java settings that don't apply to other languages
122+
* @return the declarative configuration properties for the given node name, or {@code * null} if
123+
* no declarative configuration <b>not used at all</b>.
124+
*/
125+
DeclarativeConfigProperties getDeclarativeConfig(String node);
126+
127+
/**
128+
* Returns the {@link ConfigProvider} if declarative configuration is available,
129+
*
130+
* @return the {@link ConfigProvider} or {@code null} if no provider is available
122131
*/
123132
@Nullable
124-
DeclarativeConfigProperties getDeclarativeConfig(String instrumentationName);
133+
ConfigProvider getConfigProvider();
125134
}

instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/properties/ConfigPropertiesBridge.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties;
77

8+
import io.opentelemetry.api.incubator.config.ConfigProvider;
89
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
910
import io.opentelemetry.instrumentation.api.incubator.config.internal.InstrumentationConfig;
1011
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
@@ -112,7 +113,14 @@ public Map<String, String> getMap(String name, Map<String, String> defaultValue)
112113

113114
@Nullable
114115
@Override
115-
public DeclarativeConfigProperties getDeclarativeConfig(String instrumentationName) {
116+
public DeclarativeConfigProperties getDeclarativeConfig(String node) {
117+
// create a spring boot bridge for DeclarativeConfigProperties
118+
return null;
119+
}
120+
121+
@Nullable
122+
@Override
123+
public ConfigProvider getConfigProvider() {
116124
// create a spring boot bridge for DeclarativeConfigProperties
117125
return null;
118126
}

javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/EmptyInstrumentationConfig.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.javaagent.bootstrap.internal;
77

8+
import io.opentelemetry.api.incubator.config.ConfigProvider;
89
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
910
import io.opentelemetry.instrumentation.api.incubator.config.internal.InstrumentationConfig;
1011
import java.time.Duration;
@@ -62,7 +63,13 @@ public Map<String, String> getMap(String name, Map<String, String> defaultValue)
6263

6364
@Nullable
6465
@Override
65-
public DeclarativeConfigProperties getDeclarativeConfig(String instrumentationName) {
66+
public DeclarativeConfigProperties getDeclarativeConfig(String node) {
67+
return null;
68+
}
69+
70+
@Nullable
71+
@Override
72+
public ConfigProvider getConfigProvider() {
6673
return null;
6774
}
6875
}

javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/extension/internal/DeclarativeConfigPropertiesBridge.java

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
import java.util.HashMap;
1616
import java.util.List;
1717
import java.util.Map;
18-
import java.util.Objects;
1918
import java.util.function.BiFunction;
20-
import java.util.stream.Collectors;
2119
import javax.annotation.Nullable;
2220

2321
/**
@@ -58,52 +56,19 @@ public final class DeclarativeConfigPropertiesBridge implements ConfigProperties
5856
private static final String OTEL_JAVA_AGENT_PREFIX = "otel.javaagent.";
5957

6058
private static final Map<String, String> JAVA_MAPPING_RULES = new HashMap<>();
61-
private static final Map<String, String> GENERAL_MAPPING_RULES = new HashMap<>();
6259

6360
// The node at .instrumentation.java
6461
private final DeclarativeConfigProperties instrumentationJavaNode;
6562

66-
private final DeclarativeConfigProperties instrumentationGeneralNode;
67-
6863
static {
6964
JAVA_MAPPING_RULES.put("otel.instrumentation.common.default-enabled", "common.default.enabled");
7065
JAVA_MAPPING_RULES.put(
7166
"otel.javaagent.logging.application.logs-buffer-max-records",
7267
"agent.logging.output.application.logs_buffer_max_records");
73-
74-
// todo not supported in SDK yet (this is strictly typed)
75-
// GENERAL_MAPPING_RULES.put("otel.instrumentation.http.known-methods",
76-
// "http.known_methods");
77-
GENERAL_MAPPING_RULES.put(
78-
"otel.instrumentation.http.client.capture-request-headers",
79-
"http.client.request_captured_headers");
80-
GENERAL_MAPPING_RULES.put(
81-
"otel.instrumentation.http.client.capture-response-headers",
82-
"http.client.response_captured_headers");
83-
GENERAL_MAPPING_RULES.put(
84-
"otel.instrumentation.http.server.capture-request-headers",
85-
"http.server.request_captured_headers");
86-
GENERAL_MAPPING_RULES.put(
87-
"otel.instrumentation.http.server.capture-response-headers",
88-
"http.server.response_captured_headers");
8968
}
9069

9170
private final Map<String, Object> earlyInitProperties;
9271

93-
private static Map<String, String> getPeerServiceMapping(
94-
DeclarativeConfigPropertiesBridge bridge) {
95-
List<DeclarativeConfigProperties> configProperties =
96-
bridge
97-
.instrumentationGeneralNode
98-
.getStructured("peer", empty())
99-
.getStructuredList("service_mapping", Collections.emptyList());
100-
return configProperties.stream()
101-
.collect(
102-
Collectors.toMap(
103-
e -> Objects.requireNonNull(e.getString("peer"), "peer must not be null"),
104-
e -> Objects.requireNonNull(e.getString("service"), "service must not be null")));
105-
}
106-
10772
public DeclarativeConfigPropertiesBridge(
10873
ConfigProvider configProvider, Map<String, Object> earlyInitProperties) {
10974
this.earlyInitProperties = earlyInitProperties;
@@ -112,7 +77,6 @@ public DeclarativeConfigPropertiesBridge(
11277
inst = DeclarativeConfigProperties.empty();
11378
}
11479
instrumentationJavaNode = inst.getStructured("java", empty());
115-
instrumentationGeneralNode = inst.getStructured("general", empty());
11680
}
11781

11882
@Nullable
@@ -174,10 +138,6 @@ public List<String> getList(String propertyName) {
174138

175139
@Override
176140
public Map<String, String> getMap(String propertyName) {
177-
if ("otel.instrumentation.common.peer-service-mapping".equals(propertyName)) {
178-
return getPeerServiceMapping(this);
179-
}
180-
181141
DeclarativeConfigProperties propertyValue =
182142
getPropertyValue(propertyName, DeclarativeConfigProperties::getStructured);
183143
if (propertyValue == null) {
@@ -200,15 +160,7 @@ public Map<String, String> getMap(String propertyName) {
200160
@Nullable
201161
private <T> T getPropertyValue(
202162
String property, BiFunction<DeclarativeConfigProperties, String, T> extractor) {
203-
String generalPath = GENERAL_MAPPING_RULES.get(property);
204-
if (generalPath != null) {
205-
return splitOnDot(generalPath, instrumentationGeneralNode, extractor);
206-
}
207-
String javaPath = getJavaPath(property);
208-
if (javaPath != null) {
209-
return splitOnDot(javaPath, instrumentationJavaNode, extractor);
210-
}
211-
return null;
163+
return splitOnDot(getJavaPath(property), instrumentationJavaNode, extractor);
212164
}
213165

214166
private static <T> T splitOnDot(

javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/config/ConfigPropertiesBridge.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.opentelemetry.javaagent.tooling.config;
77

8+
import static io.opentelemetry.api.incubator.config.DeclarativeConfigProperties.empty;
9+
810
import io.opentelemetry.api.incubator.config.ConfigProvider;
911
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
1012
import io.opentelemetry.api.incubator.config.InstrumentationConfigUtil;
@@ -111,9 +113,26 @@ public Map<String, String> getMap(String name, Map<String, String> defaultValue)
111113

112114
@Nullable
113115
@Override
114-
public DeclarativeConfigProperties getDeclarativeConfig(String instrumentationName) {
115-
return configProvider != null
116-
? InstrumentationConfigUtil.javaInstrumentationConfig(configProvider, instrumentationName)
117-
: null;
116+
public DeclarativeConfigProperties getDeclarativeConfig(String node) {
117+
if (configProvider == null) {
118+
// declarative config is not supported at all
119+
return null;
120+
}
121+
122+
DeclarativeConfigProperties config =
123+
InstrumentationConfigUtil.javaInstrumentationConfig(configProvider, node);
124+
if (config == null) {
125+
// there is no declarative config for this node
126+
// this needs to be a different value than null to avoid confusion with
127+
// the case when declarative config is not supported at all
128+
return empty();
129+
}
130+
return config;
131+
}
132+
133+
@Nullable
134+
@Override
135+
public ConfigProvider getConfigProvider() {
136+
return configProvider;
118137
}
119138
}

0 commit comments

Comments
 (0)