Skip to content

Commit a94b0e5

Browse files
committed
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
1 parent db79d1d commit a94b0e5

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public String getName() {
7676

7777
@Override
7878
public String getContextualName(ServerRequestObservationContext context) {
79+
if (context.getPathPattern() != null) {
80+
return String.format("http %s %s", context.getCarrier().getMethod().toLowerCase(),
81+
context.getPathPattern());
82+
}
7983
return "http " + context.getCarrier().getMethod().toLowerCase();
8084
}
8185

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public String getName() {
7979

8080
@Override
8181
public String getContextualName(ServerRequestObservationContext context) {
82+
if (context.getPathPattern() != null) {
83+
return String.format("http %s %s", context.getCarrier().getMethod().name().toLowerCase(),
84+
context.getPathPattern().toString());
85+
}
8286
return "http " + context.getCarrier().getMethod().name().toLowerCase();
8387
}
8488

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ void shouldHaveContextualName() {
5050
assertThat(convention.getContextualName(this.context)).isEqualTo("http get");
5151
}
5252

53+
@Test
54+
void contextualNameShouldUsePathPatternWhenAvailable() {
55+
this.context.setPathPattern("/test/{name}");
56+
assertThat(convention.getContextualName(this.context)).isEqualTo("http get /test/{name}");
57+
}
58+
5359
@Test
5460
void supportsOnlyHttpRequestsObservationContext() {
5561
assertThat(this.convention.supportsContext(this.context)).isTrue();

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ void shouldHaveContextualName() {
4949
assertThat(convention.getContextualName(context)).isEqualTo("http get");
5050
}
5151

52+
@Test
53+
void contextualNameShouldUsePathPatternWhenAvailable() {
54+
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/test/resource"));
55+
ServerRequestObservationContext context = new ServerRequestObservationContext(exchange);
56+
context.setPathPattern(PathPatternParser.defaultInstance.parse("/test/{name}"));
57+
assertThat(convention.getContextualName(context)).isEqualTo("http get /test/{name}");
58+
}
59+
5260
@Test
5361
void supportsOnlyHttpRequestsObservationContext() {
5462
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post("/test/resource"));

0 commit comments

Comments
 (0)