Align server contextual names with OTel conventions

This commit ensures that the matching path pattern for the request being
observed is used in the conytextual name, as advised in the OTel HTTP
server semantic conventions.

If the path pattern is not available, no additional value is provided
and the "http {method}" baseline is being used.

Fixes gh-29424
This commit is contained in:
Brian Clozel 2022-11-02 20:57:41 +01:00
parent db79d1d2b9
commit a94b0e51e2
4 changed files with 22 additions and 0 deletions

View File

@ -76,6 +76,10 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
@Override
public String getContextualName(ServerRequestObservationContext context) {
if (context.getPathPattern() != null) {
return String.format("http %s %s", context.getCarrier().getMethod().toLowerCase(),
context.getPathPattern());
}
return "http " + context.getCarrier().getMethod().toLowerCase();
}

View File

@ -79,6 +79,10 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
@Override
public String getContextualName(ServerRequestObservationContext context) {
if (context.getPathPattern() != null) {
return String.format("http %s %s", context.getCarrier().getMethod().name().toLowerCase(),
context.getPathPattern().toString());
}
return "http " + context.getCarrier().getMethod().name().toLowerCase();
}

View File

@ -50,6 +50,12 @@ class DefaultServerRequestObservationConventionTests {
assertThat(convention.getContextualName(this.context)).isEqualTo("http get");
}
@Test
void contextualNameShouldUsePathPatternWhenAvailable() {
this.context.setPathPattern("/test/{name}");
assertThat(convention.getContextualName(this.context)).isEqualTo("http get /test/{name}");
}
@Test
void supportsOnlyHttpRequestsObservationContext() {
assertThat(this.convention.supportsContext(this.context)).isTrue();

View File

@ -49,6 +49,14 @@ class DefaultServerRequestObservationConventionTests {
assertThat(convention.getContextualName(context)).isEqualTo("http get");
}
@Test
void contextualNameShouldUsePathPatternWhenAvailable() {
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/test/resource"));
ServerRequestObservationContext context = new ServerRequestObservationContext(exchange);
context.setPathPattern(PathPatternParser.defaultInstance.parse("/test/{name}"));
assertThat(convention.getContextualName(context)).isEqualTo("http get /test/{name}");
}
@Test
void supportsOnlyHttpRequestsObservationContext() {
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post("/test/resource"));