1+ /*
2+ * Copyright The OpenTelemetry Authors
3+ * SPDX-License-Identifier: Apache-2.0
4+ */
5+
16package io .opentelemetry .javaagent .instrumentation .activejhttp ;
27
38import static io .opentelemetry .javaagent .bootstrap .Java8BytecodeBridge .currentContext ;
2530import net .bytebuddy .matcher .ElementMatcher ;
2631
2732/**
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.
3237 *
3338 * @author Krishna Chaitanya Surapaneni
3439 */
@@ -41,37 +46,39 @@ public class ActiveJHttpServerConnectionInstrumentation implements TypeInstrumen
4146 */
4247 @ Override
4348 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 ()));
4650 }
4751
4852 /**
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.
5155 *
5256 * @param transformer The {@code TypeTransformer} used to apply the advice.
5357 */
5458 @ Override
5559 public void transform (TypeTransformer transformer ) {
5660 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" )))),
5964 this .getClass ().getName () + "$ServeAdvice" );
6065 }
6166
6267 /**
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+ *
6471 * <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.
6774 * </ul>
6875 */
6976 @ SuppressWarnings ("unused" )
7077 public static class ServeAdvice {
7178
7279 /**
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.
7582 *
7683 * @param asyncServlet The {@code AsyncServlet} instance handling the request.
7784 * @param request The incoming HTTP request.
@@ -96,8 +103,8 @@ public static void methodEnter(
96103 }
97104
98105 /**
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.
101108 *
102109 * @param asyncServlet The {@code AsyncServlet} instance handling the request.
103110 * @param responsePromise The promise representing the HTTP response.
@@ -119,8 +126,8 @@ public static void methodExit(
119126 }
120127 String traceId = Span .fromContext (context ).getSpanContext ().getTraceId ();
121128 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 );
124131 String traceparent = String .format ("00-%s-%s-%s" , traceId , spanId , traceFlags );
125132
126133 scope .close ();
@@ -135,24 +142,23 @@ public static void methodExit(
135142 instrumenter ().end (context , httpRequest , httpResponse , error );
136143 responsePromise = Promise .of (httpResponse );
137144 } 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 );
145152 responsePromise = Promise .of (httpResponse );
146153 throwable = null ;
147154 } 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 );
153160 responsePromise = Promise .of (httpResponse );
154161 }
155162 }
156163 }
157-
158164}
0 commit comments