From f037dd2b76e35f1f9d7a8ad5df58f6eb99d6ef57 Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Mon, 10 Apr 2023 20:27:37 +0300 Subject: [PATCH 1/2] Implement HttpServerResponseCustomizer support for Grizzly --- .../grizzly/GrizzlyHttpResponseMutator.java | 31 +++++++++++++++++++ .../HttpServerFilterInstrumentation.java | 10 ++++++ .../src/test/groovy/GrizzlyTest.groovy | 5 +++ 3 files changed, 46 insertions(+) create mode 100644 instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/GrizzlyHttpResponseMutator.java diff --git a/instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/GrizzlyHttpResponseMutator.java b/instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/GrizzlyHttpResponseMutator.java new file mode 100644 index 000000000000..6880c816bc5e --- /dev/null +++ b/instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/GrizzlyHttpResponseMutator.java @@ -0,0 +1,31 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.grizzly; + +import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseMutator; +import org.glassfish.grizzly.http.HttpResponsePacket; +import org.glassfish.grizzly.http.util.DataChunk; +import org.glassfish.grizzly.http.util.MimeHeaders; + +public enum GrizzlyHttpResponseMutator implements HttpServerResponseMutator { + INSTANCE; + + GrizzlyHttpResponseMutator() {} + + @Override + public void appendHeader(HttpResponsePacket response, String name, String value) { + MimeHeaders headers = response.getHeaders(); + DataChunk data = headers.getValue(name); + if (data == null) { + data = headers.addValue(name); + } + if (data.getLength() > 0) { + data.setString(data.toString() + "," + value); + } else { + data.setString(value); + } + } +} diff --git a/instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/HttpServerFilterInstrumentation.java b/instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/HttpServerFilterInstrumentation.java index fcc5d0c9e1da..7b515a73ecca 100644 --- a/instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/HttpServerFilterInstrumentation.java +++ b/instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/HttpServerFilterInstrumentation.java @@ -11,6 +11,7 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import io.opentelemetry.context.Context; +import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder; import io.opentelemetry.javaagent.bootstrap.servlet.AppServerBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; @@ -43,6 +44,15 @@ public void transform(TypeTransformer transformer) { @SuppressWarnings("unused") public static class PrepareResponseAdvice { + @Advice.OnMethodEnter(suppress = Throwable.class) + public static void onEnter( + @Advice.Argument(0) FilterChainContext ctx, + @Advice.Argument(2) HttpResponsePacket response) { + Context context = GrizzlyStateStorage.getContext(ctx); + HttpServerResponseCustomizerHolder.getCustomizer() + .customize(context, response, GrizzlyHttpResponseMutator.INSTANCE); + } + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void onExit( @Advice.Argument(0) FilterChainContext ctx, diff --git a/instrumentation/grizzly-2.0/javaagent/src/test/groovy/GrizzlyTest.groovy b/instrumentation/grizzly-2.0/javaagent/src/test/groovy/GrizzlyTest.groovy index e638be4e0de4..6b9d885f1cb7 100644 --- a/instrumentation/grizzly-2.0/javaagent/src/test/groovy/GrizzlyTest.groovy +++ b/instrumentation/grizzly-2.0/javaagent/src/test/groovy/GrizzlyTest.groovy @@ -58,6 +58,11 @@ class GrizzlyTest extends HttpServerTest implements AgentTestTrait { server.stop() } + @Override + boolean hasResponseCustomizer(ServerEndpoint endpoint) { + true + } + @Override boolean testCapturedHttpHeaders() { false From 600ef791f39ee5215ff59e862521b21a9b78883d Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Wed, 12 Apr 2023 09:29:08 +0300 Subject: [PATCH 2/2] Remove unneeded constructor --- .../instrumentation/grizzly/GrizzlyHttpResponseMutator.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/GrizzlyHttpResponseMutator.java b/instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/GrizzlyHttpResponseMutator.java index 6880c816bc5e..4e808c5de94a 100644 --- a/instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/GrizzlyHttpResponseMutator.java +++ b/instrumentation/grizzly-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grizzly/GrizzlyHttpResponseMutator.java @@ -13,8 +13,6 @@ public enum GrizzlyHttpResponseMutator implements HttpServerResponseMutator { INSTANCE; - GrizzlyHttpResponseMutator() {} - @Override public void appendHeader(HttpResponsePacket response, String name, String value) { MimeHeaders headers = response.getHeaders();