Skip to content

Commit 2110215

Browse files
committed
Provide more detailed uri tag for calls to Data REST repositories
Closes gh-14872
1 parent d838787 commit 2110215

File tree

2 files changed

+20
-0
lines changed
  • spring-boot-project/spring-boot-actuator/src

2 files changed

+20
-0
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.http.HttpStatus;
2525
import org.springframework.util.StringUtils;
2626
import org.springframework.web.servlet.HandlerMapping;
27+
import org.springframework.web.util.pattern.PathPattern;
2728

2829
/**
2930
* Factory methods for {@link Tag Tags} associated with a request-response exchange that
@@ -36,6 +37,8 @@
3637
*/
3738
public final class WebMvcTags {
3839

40+
private static final String DATA_REST_PATH_PATTERN_ATTRIBUTE = "org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping.EFFECTIVE_REPOSITORY_RESOURCE_LOOKUP_PATH";
41+
3942
private static final Tag URI_NOT_FOUND = Tag.of("uri", "NOT_FOUND");
4043

4144
private static final Tag URI_REDIRECTION = Tag.of("uri", "REDIRECTION");
@@ -119,6 +122,11 @@ private static HttpStatus extractStatus(HttpServletResponse response) {
119122
}
120123

121124
private static String getMatchingPattern(HttpServletRequest request) {
125+
PathPattern dataRestPathPattern = (PathPattern) request
126+
.getAttribute(DATA_REST_PATH_PATTERN_ATTRIBUTE);
127+
if (dataRestPathPattern != null) {
128+
return dataRestPathPattern.getPatternString();
129+
}
122130
return (String) request
123131
.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
124132
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/servlet/WebMvcTagsTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.mock.web.MockHttpServletRequest;
2424
import org.springframework.mock.web.MockHttpServletResponse;
2525
import org.springframework.web.servlet.HandlerMapping;
26+
import org.springframework.web.util.pattern.PathPatternParser;
2627

2728
import static org.assertj.core.api.Assertions.assertThat;
2829

@@ -38,6 +39,17 @@ public class WebMvcTagsTests {
3839

3940
private final MockHttpServletResponse response = new MockHttpServletResponse();
4041

42+
@Test
43+
public void uriTagIsDataRestsEffectiveRepositoryLookupPathWhenAvailable() {
44+
this.request.setAttribute(
45+
"org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping.EFFECTIVE_REPOSITORY_RESOURCE_LOOKUP_PATH",
46+
new PathPatternParser().parse("/api/cities"));
47+
this.request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE,
48+
"/api/{repository}");
49+
Tag tag = WebMvcTags.uri(this.request, this.response);
50+
assertThat(tag.getValue()).isEqualTo("/api/cities");
51+
}
52+
4153
@Test
4254
public void uriTagValueIsBestMatchingPatternWhenAvailable() {
4355
this.request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE,

0 commit comments

Comments
 (0)