Browse Source

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
pull/29167/head
Brian Clozel 3 years ago
parent
commit
a94b0e51e2
  1. 4
      spring-web/src/main/java/org/springframework/http/observation/DefaultServerRequestObservationConvention.java
  2. 4
      spring-web/src/main/java/org/springframework/http/observation/reactive/DefaultServerRequestObservationConvention.java
  3. 6
      spring-web/src/test/java/org/springframework/http/observation/DefaultServerRequestObservationConventionTests.java
  4. 8
      spring-web/src/test/java/org/springframework/http/observation/reactive/DefaultServerRequestObservationConventionTests.java

4
spring-web/src/main/java/org/springframework/http/observation/DefaultServerRequestObservationConvention.java

@ -76,6 +76,10 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
@Override @Override
public String getContextualName(ServerRequestObservationContext context) { 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(); return "http " + context.getCarrier().getMethod().toLowerCase();
} }

4
spring-web/src/main/java/org/springframework/http/observation/reactive/DefaultServerRequestObservationConvention.java

@ -79,6 +79,10 @@ public class DefaultServerRequestObservationConvention implements ServerRequestO
@Override @Override
public String getContextualName(ServerRequestObservationContext context) { 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(); return "http " + context.getCarrier().getMethod().name().toLowerCase();
} }

6
spring-web/src/test/java/org/springframework/http/observation/DefaultServerRequestObservationConventionTests.java

@ -50,6 +50,12 @@ class DefaultServerRequestObservationConventionTests {
assertThat(convention.getContextualName(this.context)).isEqualTo("http get"); 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 @Test
void supportsOnlyHttpRequestsObservationContext() { void supportsOnlyHttpRequestsObservationContext() {
assertThat(this.convention.supportsContext(this.context)).isTrue(); assertThat(this.convention.supportsContext(this.context)).isTrue();

8
spring-web/src/test/java/org/springframework/http/observation/reactive/DefaultServerRequestObservationConventionTests.java

@ -49,6 +49,14 @@ class DefaultServerRequestObservationConventionTests {
assertThat(convention.getContextualName(context)).isEqualTo("http get"); 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 @Test
void supportsOnlyHttpRequestsObservationContext() { void supportsOnlyHttpRequestsObservationContext() {
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post("/test/resource")); ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post("/test/resource"));

Loading…
Cancel
Save