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 Armeria #8274

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,18 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.armeria.v1_3;

import com.linecorp.armeria.common.ResponseHeadersBuilder;
import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseMutator;

enum ArmeriaHttpResponseMutator implements HttpServerResponseMutator<ResponseHeadersBuilder> {
INSTANCE;

@Override
public void appendHeader(ResponseHeadersBuilder response, String name, String value) {
response.add(name, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ public final class ArmeriaSingletons {
.build();

CLIENT_DECORATOR = telemetry.newClientDecorator();
SERVER_DECORATOR = telemetry.newServiceDecorator();
SERVER_DECORATOR = wrapResponseCustomizer(telemetry.newServiceDecorator());
}

private static Function<? super HttpService, ? extends HttpService> wrapResponseCustomizer(
Function<? super HttpService, ? extends HttpService> function) {
return function.compose(service -> new ResponseCustomizerDecorator(service));
breedx-splk marked this conversation as resolved.
Show resolved Hide resolved
}

private ArmeriaSingletons() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.armeria.v1_3;

import com.linecorp.armeria.common.FilteredHttpResponse;
import com.linecorp.armeria.common.HttpObject;
import com.linecorp.armeria.common.HttpRequest;
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.ResponseHeaders;
import com.linecorp.armeria.common.ResponseHeadersBuilder;
import com.linecorp.armeria.server.HttpService;
import com.linecorp.armeria.server.ServiceRequestContext;
import com.linecorp.armeria.server.SimpleDecoratingHttpService;
import io.opentelemetry.context.Context;
import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder;

class ResponseCustomizerDecorator extends SimpleDecoratingHttpService {
breedx-splk marked this conversation as resolved.
Show resolved Hide resolved

ResponseCustomizerDecorator(HttpService delegate) {
super(delegate);
}

@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
HttpResponse response = unwrap().serve(ctx, req);
Context context = Context.current();
return new FilteredHttpResponse(response) {
@Override
public HttpObject filter(HttpObject obj) {
// Ignore other objects like HttpData.
if (!(obj instanceof ResponseHeaders)) {
return obj;
}

ResponseHeaders headers = (ResponseHeaders) obj;
breedx-splk marked this conversation as resolved.
Show resolved Hide resolved
ResponseHeadersBuilder headersBuilder = headers.toBuilder();
HttpServerResponseCustomizerHolder.getCustomizer()
.customize(context, headersBuilder, ArmeriaHttpResponseMutator.INSTANCE);

return headersBuilder.build();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import io.opentelemetry.instrumentation.armeria.v1_3.AbstractArmeriaHttpServerTest;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpServerInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions;
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint;
import org.junit.jupiter.api.extension.RegisterExtension;

class ArmeriaHttpServerTest extends AbstractArmeriaHttpServerTest {
Expand All @@ -20,4 +22,11 @@ class ArmeriaHttpServerTest extends AbstractArmeriaHttpServerTest {
protected ServerBuilder configureServer(ServerBuilder sb) {
return sb;
}

@Override
protected void configure(HttpServerTestOptions options) {
super.configure(options);
options.setHasResponseCustomizer(
endpoint -> ServerEndpoint.NOT_FOUND != endpoint && ServerEndpoint.EXCEPTION != endpoint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected void stopServer(Server server) {
}

@Override
protected final void configure(HttpServerTestOptions options) {
protected void configure(HttpServerTestOptions options) {
options.setExpectedHttpRoute(
endpoint -> {
if (endpoint == ServerEndpoint.NOT_FOUND) {
Expand Down