Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
GlowrootWebSocketEndpointWrapper.java
Go to the documentation of this file.
1package dev.rafex.ether.glowroot.jetty12;
2
3import java.nio.ByteBuffer;
4import java.util.Set;
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.websocket.core.WebSocketCloseStatus;
35import dev.rafex.ether.websocket.core.WebSocketEndpoint;
36import dev.rafex.ether.websocket.core.WebSocketSession;
37
38/**
39 * Decorator that instruments a {@link WebSocketEndpoint} with Glowroot APM.
40 *
41 * <p>
42 * Wraps every WebSocket lifecycle event (open, text, binary, close, error) and
43 * records it as a Glowroot {@code "WebSocket"} transaction, tagging each one
44 * with the session id, path, and event-specific metadata.
45 * </p>
46 *
47 * <p>
48 * Usage — wrap the endpoint before registering it:
49 * </p>
50 *
51 * <pre>{@code
52 * routeRegistry.add(WebSocketRoute.of("/ws/chat", new GlowrootWebSocketEndpointWrapper(new ChatEndpoint())));
53 * }</pre>
54 */
56
57 private final WebSocketEndpoint delegate;
58
60 if (delegate == null) {
61 throw new IllegalArgumentException("delegate must not be null");
62 }
63 this.delegate = delegate;
64 }
65
66 @Override
67 public void onOpen(final WebSocketSession session) throws Exception {
68 tag(session, "OPEN");
69 try {
70 Glowroot.addTransactionAttribute("websocket.event", "open");
71 } catch (final Throwable ignore) {
72 }
73 delegate.onOpen(session);
74 }
75
76 @Override
77 public void onText(final WebSocketSession session, final String message) throws Exception {
78 tag(session, "TEXT");
79 try {
80 Glowroot.addTransactionAttribute("websocket.event", "text");
81 Glowroot.addTransactionAttribute("websocket.message_length",
82 String.valueOf(message == null ? 0 : message.length()));
83 } catch (final Throwable ignore) {
84 }
85 delegate.onText(session, message);
86 }
87
88 @Override
89 public void onBinary(final WebSocketSession session, final ByteBuffer message) throws Exception {
90 tag(session, "BINARY");
91 try {
92 Glowroot.addTransactionAttribute("websocket.event", "binary");
93 Glowroot.addTransactionAttribute("websocket.message_length",
94 String.valueOf(message == null ? 0 : message.remaining()));
95 } catch (final Throwable ignore) {
96 }
97 delegate.onBinary(session, message);
98 }
99
100 @Override
101 public void onClose(final WebSocketSession session, final WebSocketCloseStatus closeStatus) throws Exception {
102 tag(session, "CLOSE");
103 try {
104 Glowroot.addTransactionAttribute("websocket.event", "close");
105 if (closeStatus != null) {
106 Glowroot.addTransactionAttribute("websocket.close_code", String.valueOf(closeStatus.code()));
107 Glowroot.addTransactionAttribute("websocket.close_reason", closeStatus.reason());
108 }
109 } catch (final Throwable ignore) {
110 }
111 delegate.onClose(session, closeStatus);
112 }
113
114 @Override
115 public void onError(final WebSocketSession session, final Throwable error) {
116 tag(session, "ERROR");
117 try {
118 Glowroot.addTransactionAttribute("websocket.event", "error");
119 if (error != null) {
120 Glowroot.addTransactionAttribute("error", error.getClass().getName());
121 Glowroot.addTransactionAttribute("error.message", error.getMessage() == null ? "" : error.getMessage());
122 }
123 } catch (final Throwable ignore) {
124 }
125 delegate.onError(session, error);
126 }
127
128 @Override
129 public Set<String> subprotocols() {
130 return delegate.subprotocols();
131 }
132
133 private static void tag(final WebSocketSession session, final String event) {
134 try {
135 Glowroot.setTransactionType("WebSocket");
136 Glowroot.setTransactionName(event + " " + session.path());
137 Glowroot.addTransactionAttribute("websocket.session_id", session.id());
138 Glowroot.addTransactionAttribute("websocket.path", session.path());
139 } catch (final Throwable ignore) {
140 // Glowroot agent not present; do not affect WebSocket handling
141 }
142 }
143}
void onOpen(final WebSocketSession session)
Invocado cuando la conexión WebSocket se abre exitosamente.
void onBinary(final WebSocketSession session, final ByteBuffer message)
Invocado cuando se recibe un mensaje binario del cliente.
void onClose(final WebSocketSession session, final WebSocketCloseStatus closeStatus)
Invocado cuando el cliente solicita cerrar la conexión.
void onText(final WebSocketSession session, final String message)
Invocado cuando se recibe un mensaje de texto del cliente.
void onError(final WebSocketSession session, final Throwable error)
Invocado cuando ocurre un error en la conexión WebSocket.
Set< String > subprotocols()
Devuelve el conjunto de subprotocolos WebSocket que este endpoint acepta.
Define el contrato de un punto final WebSocket.
Representa una sesión WebSocket activa.
String path()
Devuelve el path de la petición WebSocket que originó esta sesión.
String id()
Devuelve el identificador único de esta sesión.