Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
GlowrootHealthExclusionMiddleware.java
Go to the documentation of this file.
1package dev.rafex.ether.glowroot.jetty12;
2
3import java.util.Set;
4import java.util.concurrent.TimeUnit;
5
6import org.glowroot.agent.api.Glowroot;
7
8/*-
9 * #%L
10 * ether-glowroot-jetty12
11 * %%
12 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
13 * %%
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 * THE SOFTWARE.
31 * #L%
32 */
33
34import dev.rafex.ether.http.core.HttpHandler;
35import dev.rafex.ether.http.core.Middleware;
36
37/**
38 * Middleware that suppresses Glowroot slow-transaction alerts for
39 * infrastructure paths such as health checks, readiness probes, and metrics.
40 *
41 * <p>
42 * For matching paths the slow-transaction threshold is raised to
43 * {@link Long#MAX_VALUE} milliseconds, so they never appear in Glowroot's
44 * slow-trace list. They will still be recorded normally; only the <em>slow</em>
45 * flag is suppressed.
46 * </p>
47 *
48 * <p>
49 * Use {@link #defaults()} for the most common Kubernetes/Docker health
50 * endpoints, or {@link #of(String...)} to specify your own set:
51 * </p>
52 *
53 * <pre>{@code
54 * // Default paths: /health, /ready, /live, /metrics
55 * middlewareRegistry.add(GlowrootHealthExclusionMiddleware.defaults());
56 *
57 * // Custom paths
58 * middlewareRegistry.add(GlowrootHealthExclusionMiddleware.of("/ping", "/status"));
59 * }</pre>
60 */
61public final class GlowrootHealthExclusionMiddleware implements Middleware {
62
63 /** Paths used by {@link #defaults()}. */
64 public static final Set<String> DEFAULT_PATHS = Set.of("/health", "/ready", "/live", "/metrics");
65
66 private final Set<String> excludedPaths;
67
68 private GlowrootHealthExclusionMiddleware(final Set<String> excludedPaths) {
69 this.excludedPaths = Set.copyOf(excludedPaths);
70 }
71
72 /**
73 * Creates an instance with the given exact paths.
74 *
75 * @param paths paths to suppress (e.g. {@code "/ping"}, {@code "/status"})
76 * @return a new {@link GlowrootHealthExclusionMiddleware} for those paths
77 */
78 public static GlowrootHealthExclusionMiddleware of(final String... paths) {
79 return new GlowrootHealthExclusionMiddleware(Set.of(paths));
80 }
81
82 /** Creates an instance excluding {@link #DEFAULT_PATHS}. */
83 public static GlowrootHealthExclusionMiddleware defaults() {
84 return new GlowrootHealthExclusionMiddleware(DEFAULT_PATHS);
85 }
86
87 @Override
88 public HttpHandler wrap(final HttpHandler next) {
89 return exchange -> {
90 final var path = exchange.path();
91 if (path != null && excludedPaths.contains(path)) {
92 try {
93 Glowroot.setTransactionSlowThreshold(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
94 } catch (final Throwable ignore) {
95 // Glowroot agent not present; do not affect request
96 }
97 }
98 return next.handle(exchange);
99 };
100 }
101}
static GlowrootHealthExclusionMiddleware of(final String... paths)
Creates an instance with the given exact paths.
static GlowrootHealthExclusionMiddleware defaults()
Creates an instance excluding DEFAULT_PATHS.
boolean handle(HttpExchange exchange)