Skip to content

Conversation

@MarcusDunn
Copy link

@MarcusDunn MarcusDunn commented Mar 21, 2025

Added support for url.template (plus KTOR integration)

if ktorio/ktor#4747 gets merged in. Fixes #13570.
If it does not get merged, allows applications to set the attribute.

url.template is currently hard coded as it's not in the semantics package yet.

I imagine this is due to stability stuff. I'm unsure if this makes this PR unmergable.

@MarcusDunn MarcusDunn requested a review from a team as a code owner March 21, 2025 22:00
@MarcusDunn MarcusDunn changed the title url.template add url.template to client spans Mar 21, 2025
internalSet(attributes, UrlAttributes.URL_SCHEME, urlScheme);
internalSet(attributes, UrlAttributes.URL_PATH, urlPath);
internalSet(attributes, UrlAttributes.URL_QUERY, urlQuery);
internalSet(attributes, AttributeKey.stringKey("url.template"), urlTemplate);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constant is in the UrlIncubatingAttributes class. You will have to take a dependency on opentelemetry-semconv-incubating to reference that, and I suspect that the maintainers might frown on that. 😶

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could not find any policies on stability, but I suspect you're right. (perhaps there is an expectation that things like this will be put behind a flag?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current guidance is that incubating attribute classes such as UrlIncubatingAttributes should only be used in tests. Implementation needs to copy the constant because incubating attributes can be removed or rnamed in subsequent versions of the semantic conventions. You will have to move this code to incubating module, perhaps to https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/http/HttpExperimentalAttributesExtractor.java

override fun getServerPort(request: HttpRequestData) = request.url.port

private val urlTemplateAttributeKey = AttributeKey<String>("URL_TEMPLATE")
override fun getUrlTemplate(request: HttpRequestData): String? = request.attributes.getOrNull(urlTemplateAttributeKey)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest you to remove the ktor changes until there is a version of ktor that would support this. Once there is a version where this works you could add it back along with a test.

NetworkAttributesGetter<REQUEST, RESPONSE>,
ServerAttributesGetter<REQUEST> {
ServerAttributesGetter<REQUEST>,
UrlAttributesGetter<REQUEST> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UrlAttributesGetter is for server instrumentations. Http client semantic conventions https://github.com/open-telemetry/semantic-conventions/blob/5816d4bb099b69eba1cb45084d7cceb51ad6bbfe/docs/http/http-spans.md#http-client don't list all of the attributes that UrlAttributesGetter provides.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so can I add getUrlTemplate to the HttpClientAttributesGetter interface directly?

does this cause issues with incubating vs stable?

I don't see a way to add it similar to http.request.body.size because the template would not reside in the headers and would instead be pretty instrumentation specific

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so can I add getUrlTemplate to the HttpClientAttributesGetter interface directly?

As far as I understand, no you can't. You could if that attribute was declared stable in the semantic conventions. For now you'll have to approach it in a roundabout way. HttpClientAttributesGetter is part of the stable api. You could introduce an interface to the instrumentation-api-incubator module that either extends the HttpClientAttributesGetter or just provides the getUrlTemplate method. In the extractor you could test the getter with instanceof. Probably the most tricky one will be handling the span name in HttpSpanNameExtractor. For that you could register a callback in https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/Experimental.java so that instrumentation-api could call into instrumentation-api-incubator.

* <p>Examples: {@code /users/{id}}; {@code /users?q={query}}
*/
@Nullable
default String getUrlTemplate(REQUEST request) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since instrumentation-api module is marked as stable we can not introduce unstable features here. You will have to add this to instrumentation-api-incubator module.

@MarcusDunn MarcusDunn marked this pull request as draft March 24, 2025 16:20
@MarcusDunn
Copy link
Author

I'll close for now as I've got a janky workaround on my application and it looks like it would be an uphill battle getting the incubating attribute working.

I may come back to this.

@MarcusDunn MarcusDunn closed this Mar 25, 2025
@laurit
Copy link
Contributor

laurit commented Apr 2, 2025

Support for url.template attribute was added in #13581

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ktor Client Span names should include url.template when available

3 participants