Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement HttpServerResponseCustomizer support for Grizzly #8263

Merged
merged 2 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<HttpResponsePacket> {
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class GrizzlyTest extends HttpServerTest<HttpServer> implements AgentTestTrait {
server.stop()
}

@Override
boolean hasResponseCustomizer(ServerEndpoint endpoint) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like there's a naming conflict here... perhaps this should be renamed to hasResponseMutator at some point?

Copy link
Member

@mateuszrzeszutek mateuszrzeszutek Apr 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should rename the HttpServerResponseCustomizerHolder as well nvm, I did not look at the code 🙈

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tylerbenson I didn't follow, what's the naming conflict? thx

true
}

@Override
boolean testCapturedHttpHeaders() {
false
Expand Down