|
| 1 | +package org.opentripplanner.standalone.server; |
| 2 | + |
| 3 | +import io.micrometer.core.instrument.MeterRegistry; |
| 4 | +import io.micrometer.core.instrument.Metrics; |
| 5 | +import io.micrometer.core.instrument.Timer; |
| 6 | +import jakarta.ws.rs.container.ContainerRequestContext; |
| 7 | +import jakarta.ws.rs.container.ContainerRequestFilter; |
| 8 | +import jakarta.ws.rs.container.ContainerResponseContext; |
| 9 | +import jakarta.ws.rs.container.ContainerResponseFilter; |
| 10 | +import java.time.Duration; |
| 11 | +import java.util.Locale; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.concurrent.ConcurrentHashMap; |
| 14 | +import java.util.concurrent.TimeUnit; |
| 15 | +import java.util.stream.Collectors; |
| 16 | + |
| 17 | +/** |
| 18 | + * A Jersey filter that records HTTP request response times with client identification. |
| 19 | + * <p> |
| 20 | + * The client is identified by a configurable HTTP header. Only known clients |
| 21 | + * (configured via {@code server.clientMetrics.knownClients}) are tracked individually; |
| 22 | + * unknown or missing client names are grouped under the "other" tag to prevent cardinality explosion. |
| 23 | + * <p> |
| 24 | + * The metric {@code http.client.requests} is recorded as a Timer with percentile histograms, |
| 25 | + * allowing analysis of response time distribution per client. |
| 26 | + */ |
| 27 | +public class ClientRequestMetricsFilter implements ContainerRequestFilter, ContainerResponseFilter { |
| 28 | + |
| 29 | + static final String METRIC_NAME = "http_server_requests"; |
| 30 | + private static final String START_TIME_PROPERTY = "metrics.startTime"; |
| 31 | + private static final String OTHER_CLIENT = "other"; |
| 32 | + |
| 33 | + private static final ClientRequestMetricsFilter DISABLED = new ClientRequestMetricsFilter(); |
| 34 | + |
| 35 | + private final String clientHeader; |
| 36 | + private final Set<String> knownClients; |
| 37 | + private final MeterRegistry registry; |
| 38 | + private final boolean enabled; |
| 39 | + private final ConcurrentHashMap<TimerKey, Timer> timerCache; |
| 40 | + |
| 41 | + private record TimerKey(String client, String uri) {} |
| 42 | + |
| 43 | + /** |
| 44 | + * Creates a filter for recording client request metrics. |
| 45 | + * |
| 46 | + * @param clientHeader the HTTP header name used to identify the client |
| 47 | + * @param knownClients the set of known client names to track individually (case-insensitive) |
| 48 | + * @param registry the meter registry to record metrics to |
| 49 | + */ |
| 50 | + public ClientRequestMetricsFilter( |
| 51 | + String clientHeader, |
| 52 | + Set<String> knownClients, |
| 53 | + MeterRegistry registry |
| 54 | + ) { |
| 55 | + this.clientHeader = clientHeader; |
| 56 | + this.knownClients = knownClients |
| 57 | + .stream() |
| 58 | + .map(s -> s.toLowerCase(Locale.ROOT)) |
| 59 | + .collect(Collectors.toUnmodifiableSet()); |
| 60 | + this.registry = registry; |
| 61 | + this.enabled = true; |
| 62 | + this.timerCache = new ConcurrentHashMap<>(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates a filter using the global meter registry. |
| 67 | + * |
| 68 | + * @param clientHeader the HTTP header name used to identify the client |
| 69 | + * @param knownClients the set of known client names to track individually |
| 70 | + */ |
| 71 | + public ClientRequestMetricsFilter(String clientHeader, Set<String> knownClients) { |
| 72 | + this(clientHeader, knownClients, Metrics.globalRegistry); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Private constructor for disabled filter. |
| 77 | + */ |
| 78 | + private ClientRequestMetricsFilter() { |
| 79 | + this.clientHeader = null; |
| 80 | + this.knownClients = Set.of(); |
| 81 | + this.registry = null; |
| 82 | + this.enabled = false; |
| 83 | + this.timerCache = null; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns a disabled filter that does nothing. |
| 88 | + */ |
| 89 | + public static ClientRequestMetricsFilter disabled() { |
| 90 | + return DISABLED; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void filter(ContainerRequestContext requestContext) { |
| 95 | + if (enabled) { |
| 96 | + requestContext.setProperty(START_TIME_PROPERTY, System.nanoTime()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void filter( |
| 102 | + ContainerRequestContext requestContext, |
| 103 | + ContainerResponseContext responseContext |
| 104 | + ) { |
| 105 | + if (!enabled) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + Long startTime = (Long) requestContext.getProperty(START_TIME_PROPERTY); |
| 110 | + if (startTime == null) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + String clientName = requestContext.getHeaderString(clientHeader); |
| 115 | + String clientTag = resolveClientTag(clientName); |
| 116 | + String uri = requestContext.getUriInfo().getRequestUri().getPath(); |
| 117 | + |
| 118 | + long duration = System.nanoTime() - startTime; |
| 119 | + |
| 120 | + Timer timer = getTimer(clientTag, uri); |
| 121 | + timer.record(duration, TimeUnit.NANOSECONDS); |
| 122 | + } |
| 123 | + |
| 124 | + private Timer getTimer(String clientTag, String uri) { |
| 125 | + return timerCache.computeIfAbsent(new TimerKey(clientTag, uri), key -> |
| 126 | + Timer.builder(METRIC_NAME) |
| 127 | + .description("HTTP request response time by client") |
| 128 | + .tag("client", key.client()) |
| 129 | + .tag("uri", key.uri()) |
| 130 | + .publishPercentileHistogram() |
| 131 | + .minimumExpectedValue(Duration.ofMillis(100)) |
| 132 | + .maximumExpectedValue(Duration.ofMillis(1000)) |
| 133 | + .register(registry) |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + private String resolveClientTag(String clientName) { |
| 138 | + if (clientName != null) { |
| 139 | + String lowercaseName = clientName.toLowerCase(Locale.ROOT); |
| 140 | + if (knownClients.contains(lowercaseName)) { |
| 141 | + return lowercaseName; |
| 142 | + } |
| 143 | + } |
| 144 | + return OTHER_CLIENT; |
| 145 | + } |
| 146 | +} |
0 commit comments