Skip to content

Commit

Permalink
Make otel spans for RR clients more spec compliant
Browse files Browse the repository at this point in the history
The OpenTelementry [Spec](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.10.0/specification/trace/semantic_conventions/http.md#name) is pretty clear about not using the URI as the default span name due to its high cardinality.

> Instrumentation MUST NOT default to using URI path as span name…

This PR creates span names that always include `HTTP` and then if available the method (e.g. `GET`) and if the route template is available then that is appended too.
  • Loading branch information
kdubb committed Jul 13, 2022
1 parent f547d57 commit bcdfc3f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void client() {

SpanData client = spans.get(1);
assertEquals(CLIENT, client.getKind());
assertEquals("/hello", client.getName());
assertEquals("HTTP GET /hello", client.getName());
assertEquals(HTTP_OK, client.getAttributes().get(HTTP_STATUS_CODE));
assertEquals(HttpMethod.GET, client.getAttributes().get(HTTP_METHOD));
assertEquals(uri.toString() + "hello", client.getAttributes().get(HTTP_URL));
Expand All @@ -89,7 +89,7 @@ void spanNameWithoutQueryString() {

SpanData client = spans.get(1);
assertEquals(CLIENT, client.getKind());
assertEquals("/hello", client.getName());
assertEquals("HTTP GET /hello", client.getName());
assertEquals(HTTP_OK, client.getAttributes().get(HTTP_STATUS_CODE));
assertEquals(HttpMethod.GET, client.getAttributes().get(HTTP_METHOD));
assertEquals(uri.toString() + "hello?query=1", client.getAttributes().get(HTTP_URL));
Expand All @@ -109,7 +109,7 @@ void urlWithoutAuthentication() {

SpanData client = spans.get(1);
assertEquals(CLIENT, client.getKind());
assertEquals("/hello", client.getName());
assertEquals("HTTP GET", client.getName());
assertEquals(HTTP_OK, client.getAttributes().get(HTTP_STATUS_CODE));
assertEquals(HttpMethod.GET, client.getAttributes().get(HTTP_METHOD));
assertEquals(uri.toString() + "hello?query=1", client.getAttributes().get(HTTP_URL));
Expand All @@ -136,7 +136,7 @@ void path() {

SpanData client = spans.get(1);
assertEquals(CLIENT, client.getKind());
assertEquals("/hello/{path}", client.getName());
assertEquals("HTTP GET /hello/{path}", client.getName());
assertEquals(HTTP_OK, client.getAttributes().get(HTTP_STATUS_CODE));
assertEquals(HttpMethod.GET, client.getAttributes().get(HTTP_METHOD));
assertEquals(uri.toString() + "hello/another", client.getAttributes().get(HTTP_URL));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,19 @@ public void set(final ClientRequestContext carrier, final String key, final Stri
private static class ClientSpanNameExtractor implements SpanNameExtractor<ClientRequestContext> {
@Override
public String extract(final ClientRequestContext request) {
String pathTemplate = (String) request.getProperty("UrlPathTemplate");
if (pathTemplate != null && pathTemplate.length() > 1) {
return pathTemplate;
StringBuilder name = new StringBuilder("HTTP");

String method = request.getMethod();
if (method != null && method.length() > 1) {
name.append(" ").append(method);
}

String uriPath = request.getUri().getPath();
if (uriPath != null && uriPath.length() > 1) {
return uriPath;
String pathTemplate = (String) request.getProperty("UrlPathTemplate");
if (pathTemplate != null && pathTemplate.length() > 1) {
name.append(" ").append(pathTemplate);
}

return "HTTP " + request.getMethod();
return name.toString();
}
}

Expand Down

0 comments on commit bcdfc3f

Please sign in to comment.