Skip to content

Commit defed1f

Browse files
committed
Remove usage of grpc internal api
1 parent 3c14532 commit defed1f

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

instrumentation/armeria/armeria-grpc-1.14/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/armeria/grpc/v1_14/ArmeriaGrpcInstrumentationModule.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public ArmeriaGrpcInstrumentationModule() {
2222
public List<TypeInstrumentation> typeInstrumentations() {
2323
return asList(
2424
new ArmeriaGrpcClientBuilderInstrumentation(),
25-
new ArmeriaGrpcServiceBuilderInstrumentation());
25+
new ArmeriaGrpcServiceBuilderInstrumentation(),
26+
new ArmeriaServerCallInstrumentation());
2627
}
2728
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.armeria.grpc.v1_14;
7+
8+
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
9+
import static net.bytebuddy.matcher.ElementMatchers.named;
10+
11+
import com.linecorp.armeria.server.ServiceRequestContext;
12+
import io.grpc.ServerCall;
13+
import io.opentelemetry.instrumentation.api.util.VirtualField;
14+
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
15+
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
16+
import net.bytebuddy.asm.Advice;
17+
import net.bytebuddy.description.type.TypeDescription;
18+
import net.bytebuddy.matcher.ElementMatcher;
19+
20+
public class ArmeriaServerCallInstrumentation implements TypeInstrumentation {
21+
@Override
22+
public ElementMatcher<TypeDescription> typeMatcher() {
23+
return named("com.linecorp.armeria.server.grpc.ArmeriaServerCall");
24+
}
25+
26+
@Override
27+
public void transform(TypeTransformer transformer) {
28+
transformer.applyAdviceToMethod(
29+
isConstructor(), this.getClass().getName() + "$ConstructorAdvice");
30+
}
31+
32+
@SuppressWarnings("unused")
33+
public static class ConstructorAdvice {
34+
35+
@Advice.OnMethodExit(suppress = Throwable.class)
36+
public static void onExit(
37+
@Advice.This ServerCall<?, ?> serverCall,
38+
@Advice.FieldValue("ctx") ServiceRequestContext ctx) {
39+
String authority = ctx.request().headers().get(":authority");
40+
if (authority != null) {
41+
// ArmeriaServerCall does not implement getAuthority. We will store the value for authority
42+
// header as virtual field, this field is read in grpc instrumentation in
43+
// TracingServerInterceptor
44+
VirtualField.find(ServerCall.class, String.class).set(serverCall, authority);
45+
}
46+
}
47+
}
48+
}

instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingServerInterceptor.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import io.grpc.ForwardingServerCall;
1010
import io.grpc.ForwardingServerCallListener;
1111
import io.grpc.Grpc;
12-
import io.grpc.InternalMetadata;
1312
import io.grpc.Metadata;
1413
import io.grpc.ServerCall;
1514
import io.grpc.ServerCallHandler;
@@ -21,6 +20,7 @@
2120
import io.opentelemetry.context.Context;
2221
import io.opentelemetry.context.Scope;
2322
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
23+
import io.opentelemetry.instrumentation.api.util.VirtualField;
2424
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
2525

2626
final class TracingServerInterceptor implements ServerInterceptor {
@@ -36,8 +36,8 @@ final class TracingServerInterceptor implements ServerInterceptor {
3636
private static final AtomicLongFieldUpdater<TracingServerCall> MESSAGE_ID_UPDATER =
3737
AtomicLongFieldUpdater.newUpdater(TracingServerCall.class, "messageId");
3838

39-
private static final Metadata.Key<String> AUTHORITY_KEY =
40-
InternalMetadata.keyOf(":authority", Metadata.ASCII_STRING_MARSHALLER);
39+
private static final VirtualField<ServerCall<?, ?>, String> AUTHORITY_FIELD =
40+
VirtualField.find(ServerCall.class, String.class);
4141

4242
private final Instrumenter<GrpcRequest, Status> instrumenter;
4343
private final boolean captureExperimentalSpanAttributes;
@@ -54,9 +54,11 @@ public <REQUEST, RESPONSE> ServerCall.Listener<REQUEST> interceptCall(
5454
Metadata headers,
5555
ServerCallHandler<REQUEST, RESPONSE> next) {
5656
String authority = call.getAuthority();
57-
if (authority == null && headers != null) {
58-
// armeria grpc client exposes authority in a header
59-
authority = headers.get(AUTHORITY_KEY);
57+
if (authority == null) {
58+
// Armeria grpc server call does not implement getAuthority(). In
59+
// ArmeriaServerCallInstrumentation we store the value for the authority header in a virtual
60+
// field.
61+
authority = AUTHORITY_FIELD.get(call);
6062
}
6163
GrpcRequest request =
6264
new GrpcRequest(

0 commit comments

Comments
 (0)