-
Notifications
You must be signed in to change notification settings - Fork 1k
Add http client url template customizer #15217
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
Open
laurit
wants to merge
6
commits into
open-telemetry:main
Choose a base branch
from
laurit:url-template-customizer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6dbb413
Add http client url template customizer
laurit 3bcf70c
extract url template in onStart
laurit 0247279
address review comment
laurit 831bb4d
add declarative config
laurit 54077d3
add config file
laurit 30b2b6f
remove unused code
laurit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
instrumentation-api-incubator/javaagent-testing/build.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| plugins { | ||
| id("otel.javaagent-testing") | ||
| } | ||
|
|
||
| dependencies { | ||
| testInstrumentation(project(":instrumentation:http-url-connection:javaagent")) | ||
|
|
||
| testImplementation(project(":testing-common")) | ||
| } | ||
|
|
||
| tasks { | ||
| test { | ||
| jvmArgs("-Dotel.instrumentation.http.client.emit-experimental-telemetry=true") | ||
| jvmArgs("-Dotel.instrumentation.http.client.url-template-rules=http://localhost:.*/hello/.*,/hello/*") | ||
| } | ||
|
|
||
| val declarativeConfigTest by registering(Test::class) { | ||
| testClassesDirs = sourceSets.test.get().output.classesDirs | ||
| classpath = sourceSets.test.get().runtimeClasspath | ||
|
|
||
| jvmArgs( | ||
| "-Dotel.experimental.config.file=$projectDir/src/test/resources/declarative-config.yaml" | ||
| ) | ||
| } | ||
|
|
||
| check { | ||
| dependsOn(declarativeConfigTest) | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
...metry/instrumentation/api/incubator/semconv/http/HttpClientUrlTemplateCustomizerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.api.incubator.semconv.http; | ||
|
|
||
| import static io.opentelemetry.semconv.incubating.UrlIncubatingAttributes.URL_TEMPLATE; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import io.opentelemetry.api.trace.SpanKind; | ||
| import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
| import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
| import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestServer; | ||
| import io.opentelemetry.sdk.trace.data.StatusData; | ||
| import java.net.HttpURLConnection; | ||
| import java.net.URI; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| class HttpClientUrlTemplateCustomizerTest { | ||
| private static HttpClientTestServer server; | ||
|
|
||
| @RegisterExtension | ||
| static InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
|
||
| @BeforeAll | ||
| static void setUp() { | ||
| server = new HttpClientTestServer(testing.getOpenTelemetry()); | ||
| server.start(); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void tearDown() { | ||
| server.stop(); | ||
| } | ||
|
|
||
| @Test | ||
| void test() throws Exception { | ||
| URI uri = URI.create("http://localhost:" + server.httpPort() + "/hello/world"); | ||
| HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection(); | ||
| connection.getInputStream().close(); | ||
| int responseCode = connection.getResponseCode(); | ||
| connection.disconnect(); | ||
|
|
||
| assertThat(responseCode).isEqualTo(200); | ||
|
|
||
| testing.waitAndAssertTraces( | ||
| trace -> | ||
| trace.hasSpansSatisfyingExactly( | ||
| span -> | ||
| span.hasName("GET /hello/*") | ||
| .hasNoParent() | ||
| .hasKind(SpanKind.CLIENT) | ||
| .hasAttribute(URL_TEMPLATE, "/hello/*") | ||
| .hasStatus(StatusData.unset()), | ||
| span -> | ||
| span.hasName("test-http-server") | ||
| .hasParent(trace.getSpan(0)) | ||
| .hasKind(SpanKind.SERVER))); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
instrumentation-api-incubator/javaagent-testing/src/test/resources/declarative-config.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| file_format: "1.0-rc.1" | ||
| propagator: | ||
| composite_list: "tracecontext" | ||
| instrumentation/development: | ||
| java: | ||
| http: | ||
| client: | ||
| emit_experimental_telemetry: true | ||
| url_template_rules: | ||
| - pattern: "http://localhost:.*/hello/.*" | ||
| template: "/hello/*" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...telemetry/instrumentation/api/incubator/semconv/http/HttpClientUrlTemplateCustomizer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.api.incubator.semconv.http; | ||
|
|
||
| import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** A service provider interface (SPI) for customizing http client url template. */ | ||
| public interface HttpClientUrlTemplateCustomizer { | ||
|
|
||
| /** | ||
| * Customize url template for given request. Typically, the customizer will extract full url from | ||
| * the request and apply some logic (e.g. regex matching) to generate url template. The customizer | ||
| * can choose to override existing url template or skip customization when a url template is | ||
| * already set. | ||
| * | ||
| * @param urlTemplate existing url template, can be null | ||
| * @param request current request | ||
| * @param getter request attributes getter | ||
| * @return customized url template, or null | ||
| */ | ||
| @Nullable | ||
| <REQUEST> String customize( | ||
| @Nullable String urlTemplate, REQUEST request, HttpClientAttributesGetter<REQUEST, ?> getter); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...emetry/instrumentation/api/incubator/semconv/http/internal/HttpClientUrlTemplateUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.api.incubator.semconv.http.internal; | ||
|
|
||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpClientExperimentalAttributesGetter; | ||
| import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpClientUrlTemplate; | ||
| import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpClientUrlTemplateCustomizer; | ||
| import io.opentelemetry.instrumentation.api.internal.InstrumenterContext; | ||
| import io.opentelemetry.instrumentation.api.internal.ServiceLoaderUtil; | ||
| import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
| * any time. | ||
| */ | ||
| public final class HttpClientUrlTemplateUtil { | ||
|
|
||
| private static final List<HttpClientUrlTemplateCustomizer> customizers = new ArrayList<>(); | ||
|
|
||
| static { | ||
| for (HttpClientUrlTemplateCustomizer customizer : | ||
| ServiceLoaderUtil.load(HttpClientUrlTemplateCustomizer.class)) { | ||
| customizers.add(customizer); | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| public static <REQUEST> String getUrlTemplate( | ||
| Context context, REQUEST request, HttpClientAttributesGetter<REQUEST, ?> getter) { | ||
| // first, try to get url template from context | ||
| String urlTemplate = HttpClientUrlTemplate.get(context); | ||
| if (urlTemplate == null && getter instanceof HttpClientExperimentalAttributesGetter) { | ||
| HttpClientExperimentalAttributesGetter<REQUEST, ?> experimentalGetter = | ||
| (HttpClientExperimentalAttributesGetter<REQUEST, ?>) getter; | ||
| // next, try to get url template from getter | ||
| urlTemplate = experimentalGetter.getUrlTemplate(request); | ||
| } | ||
|
|
||
| return customizeUrlTemplate(urlTemplate, request, getter); | ||
| } | ||
|
|
||
| @Nullable | ||
| private static <REQUEST> String customizeUrlTemplate( | ||
| @Nullable String urlTemplate, | ||
| REQUEST request, | ||
| HttpClientAttributesGetter<REQUEST, ?> getter) { | ||
| if (customizers.isEmpty()) { | ||
| return urlTemplate; | ||
| } | ||
|
|
||
| // we cache the computation in InstrumenterContext because url template is used by both | ||
| // HttpSpanNameExtractor and HttpExperimentalAttributesExtractor | ||
| return InstrumenterContext.computeIfAbsent( | ||
| "url.template", | ||
| unused -> { | ||
| for (HttpClientUrlTemplateCustomizer customizer : customizers) { | ||
| String result = customizer.customize(urlTemplate, request, getter); | ||
| if (result != null) { | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| return urlTemplate; | ||
| }); | ||
| } | ||
|
|
||
| private HttpClientUrlTemplateUtil() {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...a/io/opentelemetry/javaagent/tooling/instrumentation/http/RegexUrlTemplateCustomizer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.tooling.instrumentation.http; | ||
|
|
||
| import static io.opentelemetry.javaagent.tooling.instrumentation.http.UrlTemplateRules.getRules; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpClientUrlTemplateCustomizer; | ||
| import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter; | ||
| import io.opentelemetry.javaagent.tooling.instrumentation.http.UrlTemplateRules.Rule; | ||
| import java.util.regex.Pattern; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| @AutoService(HttpClientUrlTemplateCustomizer.class) | ||
| public final class RegexUrlTemplateCustomizer implements HttpClientUrlTemplateCustomizer { | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public <REQUEST> String customize( | ||
| @Nullable String urlTemplate, | ||
| REQUEST request, | ||
| HttpClientAttributesGetter<REQUEST, ?> getter) { | ||
| String url = getter.getUrlFull(request); | ||
| if (url == null) { | ||
| return null; | ||
| } | ||
|
|
||
| for (Rule rule : getRules()) { | ||
| if (urlTemplate != null && !rule.getOverride()) { | ||
| continue; | ||
| } | ||
|
|
||
| Pattern pattern = rule.getPattern(); | ||
| // to generate the url template, we apply the regex replacement on the full url | ||
| String result = pattern.matcher(url).replaceFirst(rule.getReplacement()); | ||
| if (!url.equals(result)) { | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.