1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
1
6
package io .opentelemetry .javaagent .instrumentation .activejhttp ;
2
7
3
8
import static io .opentelemetry .javaagent .bootstrap .Java8BytecodeBridge .currentContext ;
25
30
import net .bytebuddy .matcher .ElementMatcher ;
26
31
27
32
/**
28
- * <p> This class provides instrumentation for ActiveJ HTTP server connections by applying advice to the
29
- * {@code serve} method of classes that extend {@code io.activej.http.AsyncServlet}. The instrumentation is
30
- * designed to integrate with OpenTelemetry for distributed tracing, capturing and propagating trace context
31
- * through HTTP requests and responses.</p>
33
+ * This class provides instrumentation for ActiveJ HTTP server connections by applying advice to the
34
+ * {@code serve} method of classes that extend {@code io.activej.http.AsyncServlet}. The
35
+ * instrumentation is designed to integrate with OpenTelemetry for distributed tracing, capturing
36
+ * and propagating trace context through HTTP requests and responses.
32
37
*
33
38
* @author Krishna Chaitanya Surapaneni
34
39
*/
@@ -41,37 +46,39 @@ public class ActiveJHttpServerConnectionInstrumentation implements TypeInstrumen
41
46
*/
42
47
@ Override
43
48
public ElementMatcher <TypeDescription > typeMatcher () {
44
- return hasSuperType (named ("io.activej.http.AsyncServlet" ))
45
- .and (not (isInterface ()));
49
+ return hasSuperType (named ("io.activej.http.AsyncServlet" )).and (not (isInterface ()));
46
50
}
47
51
48
52
/**
49
- * Applies advice to the {@code serve} method of the matched classes. The advice captures trace context
50
- * at the start of the method and propagates it through the response.
53
+ * Applies advice to the {@code serve} method of the matched classes. The advice captures trace
54
+ * context at the start of the method and propagates it through the response.
51
55
*
52
56
* @param transformer The {@code TypeTransformer} used to apply the advice.
53
57
*/
54
58
@ Override
55
59
public void transform (TypeTransformer transformer ) {
56
60
transformer .applyAdviceToMethod (
57
- isMethod ().and (named ("serve" )).and (takesArguments (1 )
58
- .and (takesArgument (0 , named ("io.activej.http.HttpRequest" )))),
61
+ isMethod ()
62
+ .and (named ("serve" ))
63
+ .and (takesArguments (1 ).and (takesArgument (0 , named ("io.activej.http.HttpRequest" )))),
59
64
this .getClass ().getName () + "$ServeAdvice" );
60
65
}
61
66
62
67
/**
63
- * <p>Inner class containing the advice logic for the {@code serve} method. This class defines two methods:</p>
68
+ * Inner class containing the advice logic for the {@code serve} method. This class defines two
69
+ * methods:
70
+ *
64
71
* <ul>
65
- * <li>{@code methodEnter}: Captures the trace context at the start of the method.</li>
66
- * <li>{@code methodExit}: Propagates the trace context to the response and ends the span.</li>
72
+ * <li>{@code methodEnter}: Captures the trace context at the start of the method.
73
+ * <li>{@code methodExit}: Propagates the trace context to the response and ends the span.
67
74
* </ul>
68
75
*/
69
76
@ SuppressWarnings ("unused" )
70
77
public static class ServeAdvice {
71
78
72
79
/**
73
- * Advice executed at the start of the {@code serve} method. Captures the current trace context and
74
- * starts a new span if tracing is enabled for the request.
80
+ * Advice executed at the start of the {@code serve} method. Captures the current trace context
81
+ * and starts a new span if tracing is enabled for the request.
75
82
*
76
83
* @param asyncServlet The {@code AsyncServlet} instance handling the request.
77
84
* @param request The incoming HTTP request.
@@ -96,8 +103,8 @@ public static void methodEnter(
96
103
}
97
104
98
105
/**
99
- * Advice executed at the end of the {@code serve} method. Propagates the trace context to the response,
100
- * handles exceptions, and ends the span.
106
+ * Advice executed at the end of the {@code serve} method. Propagates the trace context to the
107
+ * response, handles exceptions, and ends the span.
101
108
*
102
109
* @param asyncServlet The {@code AsyncServlet} instance handling the request.
103
110
* @param responsePromise The promise representing the HTTP response.
@@ -119,8 +126,8 @@ public static void methodExit(
119
126
}
120
127
String traceId = Span .fromContext (context ).getSpanContext ().getTraceId ();
121
128
String spanId = Span .fromContext (context ).getSpanContext ().getSpanId ();
122
- String traceFlags = Span . fromContext ( context ). getSpanContext (). getTraceFlags (). asHex ()
123
- .substring (0 , 2 );
129
+ String traceFlags =
130
+ Span . fromContext ( context ). getSpanContext (). getTraceFlags (). asHex () .substring (0 , 2 );
124
131
String traceparent = String .format ("00-%s-%s-%s" , traceId , spanId , traceFlags );
125
132
126
133
scope .close ();
@@ -135,24 +142,23 @@ public static void methodExit(
135
142
instrumenter ().end (context , httpRequest , httpResponse , error );
136
143
responsePromise = Promise .of (httpResponse );
137
144
} else if (throwable != null ) {
138
- HttpResponse httpResponse = HttpResponse . builder ()
139
- . withCode ( 500 )
140
- . withPlainText ( throwable . getMessage () )
141
- . withHeader ( HttpHeaders . of ( traceparentHeader ), traceparent )
142
- . build ();
143
- instrumenter (). end ( context , httpRequest , httpResponse ,
144
- throwable );
145
+ HttpResponse httpResponse =
146
+ HttpResponse . builder ( )
147
+ . withCode ( 500 )
148
+ . withPlainText ( throwable . getMessage () )
149
+ . withHeader ( HttpHeaders . of ( traceparentHeader ), traceparent )
150
+ . build ();
151
+ instrumenter (). end ( context , httpRequest , httpResponse , throwable );
145
152
responsePromise = Promise .of (httpResponse );
146
153
throwable = null ;
147
154
} else {
148
- HttpResponse httpResponse = HttpResponse . notFound404 ()
149
- . withHeader ( HttpHeaders . of ( traceparentHeader ), traceparent )
150
- . build ();
151
- instrumenter (). end ( context , httpRequest , httpResponse ,
152
- throwable );
155
+ HttpResponse httpResponse =
156
+ HttpResponse . notFound404 ( )
157
+ . withHeader ( HttpHeaders . of ( traceparentHeader ), traceparent )
158
+ . build ();
159
+ instrumenter (). end ( context , httpRequest , httpResponse , throwable );
153
160
responsePromise = Promise .of (httpResponse );
154
161
}
155
162
}
156
163
}
157
-
158
164
}
0 commit comments