Allow ServerHttpObservationFilter to be extended

This commit allows to extend  the `ServerHttpObservationFilter` when the
observation scope is opened. This typically allows to add the current
traceId as a response header.

Closes gh-30632
This commit is contained in:
Brian Clozel
2024-05-21 20:01:17 +02:00
parent 559fec052f
commit 1bd6b30ddd
2 changed files with 43 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -106,6 +106,7 @@ public class ServerHttpObservationFilter extends OncePerRequestFilter {
Observation observation = createOrFetchObservation(request, response);
try (Observation.Scope scope = observation.openScope()) {
onScopeOpened(scope, request, response);
filterChain.doFilter(request, response);
}
catch (Exception ex) {
@@ -125,6 +126,17 @@ public class ServerHttpObservationFilter extends OncePerRequestFilter {
}
}
/**
* Notifies this filter that a new {@link Observation.Scope} is opened for the observation that was just created.
* @param scope the newly opened observation scope
* @param request the HTTP client request
* @param response the filter's response@
* @since 6.2.0
**/
protected void onScopeOpened(Observation.Scope scope, HttpServletRequest request, HttpServletResponse response) {
}
private Observation createOrFetchObservation(HttpServletRequest request, HttpServletResponse response) {
Observation observation = (Observation) request.getAttribute(CURRENT_OBSERVATION_ATTRIBUTE);
if (observation == null) {