Register per-repository pattern for observability purposes.

The standard Spring MVC observability integration registers the plain request pattern for observations. For our repository controllers that would result in one pattern registered for all individual repository resources (e.g. /{repository}/{id} etc.). However, the insights users would like to gain rather follows the individual repositories exposed. That's why we have so far exposed repository specific path pattern (/myrepo/{id}) via a custom request attribute. To adhere to the new observability integration of Spring Framework 6, we need to expose that particular pattern on the ServerRequestObservationContext, too.

Fixes #2212.
This commit is contained in:
Oliver Drotbohm
2023-01-09 15:31:57 +01:00
parent 96f02a7365
commit aa1685d95d
3 changed files with 51 additions and 4 deletions

View File

@@ -42,6 +42,7 @@ import org.springframework.util.StringValueResolver;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.filter.ServerHttpObservationFilter;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PathPatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
@@ -242,8 +243,12 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping {
PathPatternParser parser = getPatternParser();
parser = parser != null ? parser : PARSER;
request.setAttribute(EFFECTIVE_LOOKUP_PATH_ATTRIBUTE,
parser.parse(pattern.replace("/{repository}", repositoryBasePath)));
var repositorySpecificPattern = pattern.replace("/{repository}", repositoryBasePath);
ServerHttpObservationFilter.findObservationContext(request)
.ifPresent(context -> context.setPathPattern(repositorySpecificPattern));
request.setAttribute(EFFECTIVE_LOOKUP_PATH_ATTRIBUTE, parser.parse(repositorySpecificPattern));
}
private static String getPattern(RequestMappingInfo info, HttpServletRequest request) {