Skip to content

Commit c15f431

Browse files
committed
Use static INSTANCE singleton for stateless HttpServerResponseMutators
Adds 'static final INSTANCE' field and private constructor to the stateless final-class HttpServerResponseMutator implementations (Grizzly, Helidon, Jetty 8/11/12, Servlet 3) and updates their call sites to use INSTANCE instead of 'new XyzMutator()'. These mutators are invoked from per-response advice / filters / handlers, matching the hot-path exception in general-rules.md ('Prefer Instance Creation Over Singletons'). This also brings them in line with the existing enum-singleton mutators (Armeria, JavaHttpServer, Netty 3.8/4.0/4.1, Tomcat 10, Restlet 1/2, Undertow) and the static-INSTANCE mutators (Tomcat 7, Servlet 2.2). Akka and Pekko mutators are intentionally left unchanged because they hold per-request header state.
1 parent 327c496 commit c15f431

13 files changed

Lines changed: 35 additions & 7 deletions

File tree

instrumentation/grizzly-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/v2_3/GrizzlyHttpResponseMutator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
public class GrizzlyHttpResponseMutator implements HttpServerResponseMutator<HttpResponsePacket> {
1414

15+
public static final GrizzlyHttpResponseMutator INSTANCE = new GrizzlyHttpResponseMutator();
16+
17+
private GrizzlyHttpResponseMutator() {}
18+
1519
@Override
1620
public void appendHeader(HttpResponsePacket response, String name, String value) {
1721
MimeHeaders headers = response.getHeaders();

instrumentation/grizzly-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/v2_3/HttpServerFilterInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static void onEnter(
5050
@Advice.Argument(2) HttpResponsePacket response) {
5151
Context context = GrizzlyStateStorage.getContext(ctx);
5252
HttpServerResponseCustomizerHolder.getCustomizer()
53-
.customize(context, response, new GrizzlyHttpResponseMutator());
53+
.customize(context, response, GrizzlyHttpResponseMutator.INSTANCE);
5454
}
5555

5656
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class, inline = false)

instrumentation/helidon-4.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/helidon/v4_3/HelidonServerResponseMutator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
final class HelidonServerResponseMutator implements HttpServerResponseMutator<ServerResponse> {
1212

13+
static final HelidonServerResponseMutator INSTANCE = new HelidonServerResponseMutator();
14+
15+
private HelidonServerResponseMutator() {}
16+
1317
@Override
1418
public void appendHeader(ServerResponse res, String name, String value) {
1519
res.header(name, value);

instrumentation/helidon-4.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/helidon/v4_3/ResponseCustomizingFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void filter(FilterChain chain, RoutingRequest req, RoutingResponse res) {
2121

2222
var context = Context.current();
2323
HttpServerResponseCustomizerHolder.getCustomizer()
24-
.customize(context, res, new HelidonServerResponseMutator());
24+
.customize(context, res, HelidonServerResponseMutator.INSTANCE);
2525
chain.proceed();
2626
}
2727
}

instrumentation/jetty/jetty-11.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/v11_0/Jetty11InstrumentationModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static AdviceScope start(HttpServletRequest request, HttpServletResponse
7777
// Must be set here since Jetty handlers can use startAsync outside of servlet scope.
7878
helper().setAsyncListenerResponse(context, response);
7979
HttpServerResponseCustomizerHolder.getCustomizer()
80-
.customize(context, response, new Jetty11ResponseMutator());
80+
.customize(context, response, Jetty11ResponseMutator.INSTANCE);
8181
return new AdviceScope(requestContext, context, scope);
8282
}
8383

instrumentation/jetty/jetty-11.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/v11_0/Jetty11ResponseMutator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
final class Jetty11ResponseMutator implements HttpServerResponseMutator<HttpServletResponse> {
1212

13+
static final Jetty11ResponseMutator INSTANCE = new Jetty11ResponseMutator();
14+
15+
private Jetty11ResponseMutator() {}
16+
1317
@Override
1418
public void appendHeader(HttpServletResponse response, String name, String value) {
1519
response.addHeader(name, value);

instrumentation/jetty/jetty-12.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/v12_0/Jetty12ResponseMutator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
public class Jetty12ResponseMutator implements HttpServerResponseMutator<Response> {
1212

13+
public static final Jetty12ResponseMutator INSTANCE = new Jetty12ResponseMutator();
14+
15+
private Jetty12ResponseMutator() {}
16+
1317
@Override
1418
public void appendHeader(Response response, String name, String value) {
1519
response.getHeaders().add(name, value);

instrumentation/jetty/jetty-12.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/v12_0/Jetty12ServerInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static AdviceScope start(Request request, Response response) {
6161
Context context = helper().start(parentContext, request, response);
6262
Scope scope = context.makeCurrent();
6363
HttpServerResponseCustomizerHolder.getCustomizer()
64-
.customize(context, response, new Jetty12ResponseMutator());
64+
.customize(context, response, Jetty12ResponseMutator.INSTANCE);
6565
return new AdviceScope(context, scope);
6666
}
6767

instrumentation/jetty/jetty-8.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/v8_0/Jetty8InstrumentationModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static AdviceScope start(HttpServletRequest request, HttpServletResponse
7878
// Must be set here since Jetty handlers can use startAsync outside of servlet scope.
7979
helper().setAsyncListenerResponse(context, response);
8080
HttpServerResponseCustomizerHolder.getCustomizer()
81-
.customize(context, response, new Jetty8ResponseMutator());
81+
.customize(context, response, Jetty8ResponseMutator.INSTANCE);
8282
return new AdviceScope(requestContext, context, scope);
8383
}
8484

instrumentation/jetty/jetty-8.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/v8_0/Jetty8ResponseMutator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
import javax.servlet.http.HttpServletResponse;
1010

1111
final class Jetty8ResponseMutator implements HttpServerResponseMutator<HttpServletResponse> {
12+
13+
static final Jetty8ResponseMutator INSTANCE = new Jetty8ResponseMutator();
14+
15+
private Jetty8ResponseMutator() {}
16+
1217
@Override
1318
public void appendHeader(HttpServletResponse response, String name, String value) {
1419
response.addHeader(name, value);

0 commit comments

Comments
 (0)