Skip to content

Commit 4cd098a

Browse files
committed
replace OncePerRequestFilter subclass by a custom micrometer ObservationFilter implementation
following the examples from the spring-boot docs https://docs.spring.io/spring-framework/reference/integration/observability.html#observability.config.conventions
1 parent 021ddfe commit 4cd098a

2 files changed

Lines changed: 57 additions & 76 deletions

File tree

src/main/java/nl/dtls/fairdatapoint/api/filter/RequestMetricsFilter.java

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* The MIT License
3+
* Copyright © 2017 DTL
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package nl.dtls.fairdatapoint.api.filter;
25+
26+
import io.micrometer.common.KeyValue;
27+
import io.micrometer.observation.Observation;
28+
import io.micrometer.observation.ObservationFilter;
29+
import lombok.extern.slf4j.Slf4j;
30+
import org.springframework.http.server.observation.ServerRequestObservationContext;
31+
import org.springframework.stereotype.Component;
32+
33+
import java.util.Optional;
34+
35+
@Slf4j
36+
@Component
37+
public class RequestObservationFilter implements ObservationFilter {
38+
39+
/**
40+
* Replaces the actuator metrics http.server.requests default `uri` path pattern values with full paths.
41+
* @param context Micrometer Observation.Context
42+
* @return modified context
43+
*/
44+
@Override
45+
public Observation.Context map(Observation.Context context) {
46+
if (context instanceof ServerRequestObservationContext serverContext) {
47+
log.debug("Using customized micrometer observation context");
48+
final Optional<String> requestURI = Optional.ofNullable(serverContext.getCarrier().getRequestURI());
49+
// Replace the default URI path pattern (e.g. `/catalog/{id}`) with the full URI path (e.g. `/catalog/123`).
50+
// This is high-cardinality data, but we add it as low-cardinality to make sure it shows up in the metrics.
51+
// Beware, this could lead to memory issues in case of excessive number of URIs.
52+
// See micrometer docs for more info.
53+
context.addLowCardinalityKeyValue(KeyValue.of("uri", requestURI.orElse("")));
54+
}
55+
return context;
56+
}
57+
}

0 commit comments

Comments
 (0)