Skip to content

Commit 831bb4d

Browse files
committed
add declarative config
1 parent 0247279 commit 831bb4d

File tree

2 files changed

+70
-30
lines changed

2 files changed

+70
-30
lines changed

instrumentation-api-incubator/javaagent-testing/build.gradle.kts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,22 @@ dependencies {
88
testImplementation(project(":testing-common"))
99
}
1010

11-
tasks.withType<Test>().configureEach {
12-
jvmArgs("-Dotel.instrumentation.http.client.emit-experimental-telemetry=true")
13-
jvmArgs("-Dotel.instrumentation.http.client.url-template-rules=http://localhost:.*/hello/.*,/hello/*")
11+
tasks {
12+
test {
13+
jvmArgs("-Dotel.instrumentation.http.client.emit-experimental-telemetry=true")
14+
jvmArgs("-Dotel.instrumentation.http.client.url-template-rules=http://localhost:.*/hello/.*,/hello/*")
15+
}
16+
17+
val declarativeConfigTest by registering(Test::class) {
18+
testClassesDirs = sourceSets.test.get().output.classesDirs
19+
classpath = sourceSets.test.get().runtimeClasspath
20+
21+
jvmArgs(
22+
"-Dotel.experimental.config.file=$projectDir/src/test/resources/declarative-config.yaml"
23+
)
24+
}
25+
26+
check {
27+
dependsOn(declarativeConfigTest)
28+
}
1429
}

javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/instrumentation/http/RegexUrlTemplateCustomizerInitializer.java

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55

66
package io.opentelemetry.javaagent.tooling.instrumentation.http;
77

8+
import static io.opentelemetry.api.incubator.config.DeclarativeConfigProperties.empty;
9+
import static java.util.Collections.emptyList;
810
import static java.util.logging.Level.WARNING;
911

1012
import com.google.auto.service.AutoService;
13+
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
14+
import io.opentelemetry.instrumentation.api.incubator.config.internal.InstrumentationConfig;
15+
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
1116
import io.opentelemetry.javaagent.tooling.BeforeAgentListener;
1217
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
13-
import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil;
14-
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
1518
import java.util.logging.Logger;
1619
import java.util.regex.Pattern;
1720
import java.util.regex.PatternSyntaxException;
@@ -23,19 +26,36 @@ public final class RegexUrlTemplateCustomizerInitializer implements BeforeAgentL
2326

2427
@Override
2528
public void beforeAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) {
26-
ConfigProperties config = AutoConfigureUtil.getConfig(autoConfiguredOpenTelemetrySdk);
27-
if (config == null) {
28-
return;
29-
}
29+
InstrumentationConfig config = AgentInstrumentationConfig.get();
3030
// url template is emitted only when http client experimental telemetry is enabled
3131
boolean urlTemplateEnabled =
3232
config.getBoolean("otel.instrumentation.http.client.emit-experimental-telemetry", false);
3333
if (!urlTemplateEnabled) {
3434
return;
3535
}
36-
String rules = config.getString("otel.instrumentation.http.client.url-template-rules");
37-
if (rules != null && !rules.isEmpty()) {
38-
parse(rules);
36+
if (config.isDeclarative()) {
37+
DeclarativeConfigProperties configuration =
38+
config.getDeclarativeConfig("http").getStructured("client", empty());
39+
configuration
40+
.getStructuredList("url_template_rules", emptyList())
41+
.forEach(
42+
rule -> {
43+
String patternString = rule.getString("pattern", "");
44+
String template = rule.getString("template", "");
45+
if (patternString.isEmpty() || template.isEmpty()) {
46+
return;
47+
}
48+
boolean override = rule.getBoolean("override", false);
49+
Pattern pattern = toPattern(patternString);
50+
if (pattern != null) {
51+
UrlTemplateRules.addRule(pattern, template, override);
52+
}
53+
});
54+
} else {
55+
String rules = config.getString("otel.instrumentation.http.client.url-template-rules");
56+
if (rules != null && !rules.isEmpty()) {
57+
parse(rules);
58+
}
3959
}
4060
}
4161

@@ -54,28 +74,33 @@ static void parse(String rules) {
5474
continue;
5575
}
5676

57-
Pattern pattern;
58-
try {
59-
String patternString = parts[0].trim();
60-
// ensure that pattern matches the whole url
61-
if (!patternString.startsWith("^")) {
62-
patternString = "^" + patternString;
63-
}
64-
if (!patternString.endsWith("$")) {
65-
patternString = patternString + "$";
66-
}
67-
pattern = Pattern.compile(patternString);
68-
} catch (PatternSyntaxException exception) {
69-
logger.log(
70-
WARNING,
71-
"Invalid pattern in http client url template customization rule \""
72-
+ parts[0].trim()
73-
+ "\".",
74-
exception);
77+
Pattern pattern = toPattern(parts[0].trim());
78+
if (pattern == null) {
7579
continue;
7680
}
7781
UrlTemplateRules.addRule(
7882
pattern, parts[1].trim(), parts.length == 3 && Boolean.parseBoolean(parts[2].trim()));
7983
}
8084
}
85+
86+
private static Pattern toPattern(String patternString) {
87+
try {
88+
// ensure that pattern matches the whole url
89+
if (!patternString.startsWith("^")) {
90+
patternString = "^" + patternString;
91+
}
92+
if (!patternString.endsWith("$")) {
93+
patternString = patternString + "$";
94+
}
95+
return Pattern.compile(patternString);
96+
} catch (PatternSyntaxException exception) {
97+
logger.log(
98+
WARNING,
99+
"Invalid pattern in http client url template customization rule \""
100+
+ patternString
101+
+ "\".",
102+
exception);
103+
return null;
104+
}
105+
}
81106
}

0 commit comments

Comments
 (0)