Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
GlowrootHttpMiddleware.java
Go to the documentation of this file.
1package dev.rafex.ether.glowroot.jetty12;
2
3import org.glowroot.agent.api.Glowroot;
4
5/*-
6 * #%L
7 * ether-glowroot-jetty12
8 * %%
9 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
10 * %%
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 * #L%
29 */
30
31import dev.rafex.ether.http.core.HttpHandler;
32import dev.rafex.ether.http.core.Middleware;
33
34/**
35 * Middleware that instruments HTTP requests with Glowroot APM.
36 *
37 * <p>
38 * Sets the Glowroot transaction type to {@code "Web"} and names each
39 * transaction as {@code "METHOD /normalized/path"}, replacing dynamic path
40 * segments (UUIDs, ObjectIds, numeric IDs) with canonical placeholders so that
41 * Glowroot can aggregate similar endpoints.
42 * </p>
43 *
44 * <p>
45 * Usage — register once when building the Jetty server:
46 * </p>
47 *
48 * <pre>{@code
49 * middlewareRegistry.add(new GlowrootHttpMiddleware());
50 * }</pre>
51 */
52public final class GlowrootHttpMiddleware implements Middleware {
53
54 @Override
55 public HttpHandler wrap(final HttpHandler next) {
56 return exchange -> {
57 final var method = exchange.method();
58 final var path = exchange.path();
59 final var normalized = PathNormalizer.normalize(path);
60
61 try {
62 Glowroot.setTransactionType("Web");
63 Glowroot.setTransactionName(method + " " + normalized);
64 Glowroot.addTransactionAttribute("http.method", method);
65 Glowroot.addTransactionAttribute("http.path", path == null ? "unknown" : path);
66 Glowroot.addTransactionAttribute("http.normalized_path", normalized);
67 } catch (final Throwable ignore) {
68 // Glowroot agent not present; do not affect the request
69 }
70
71 try {
72 return next.handle(exchange);
73 } catch (final Throwable t) {
74 try {
75 Glowroot.addTransactionAttribute("error", t.getClass().getName());
76 Glowroot.addTransactionAttribute("error.message", t.getMessage() == null ? "" : t.getMessage());
77 } catch (final Throwable ignore) {
78 // Glowroot agent not present; do not suppress original exception
79 }
80 throw t;
81 }
82 };
83 }
84}
Middleware that instruments HTTP requests with Glowroot APM.
boolean handle(HttpExchange exchange)