Skip to content

Commit 4caabae

Browse files
authored
Add support for gRPC 1.33.0 (#175)
Signed-off-by: Pavol Loffay <[email protected]>
1 parent 05d0fd0 commit 4caabae

File tree

5 files changed

+72
-26
lines changed

5 files changed

+72
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ List of supported frameworks with additional capabilities:
1515
| Library/Framework | Versions |
1616
|--------------------------------------------------------------------------------------------------------|-----------------|
1717
| [Apache HttpClient](https://hc.apache.org/index.html) | 4.0+ |
18-
| [gRPC](https://github.com/grpc/grpc-java) | 1.5+ to [1.32.0](https://github.com/hypertrace/javaagent/issues/70) |
18+
| [gRPC](https://github.com/grpc/grpc-java) | 1.5+ |
1919
| [JAX-RS Client](https://javaee.github.io/javaee-spec/javadocs/javax/ws/rs/client/package-summary.html) | 2.0+ |
2020
| [OkHttp](https://github.com/square/okhttp/) | 3.0+ |
2121
| [Servlet](https://javaee.github.io/javaee-spec/javadocs/javax/servlet/package-summary.html) | 2.3+ |

instrumentation/grpc-1.5/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ muzzle {
1313
pass {
1414
group = "io.grpc"
1515
module = "grpc-core"
16-
versions = "[1.5.0, 1.33.0)" // see https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/1453
16+
versions = "[1.5.0,)"
1717
// for body capture via com.google.protobuf.util.JsonFormat
1818
extraDependency("io.grpc:grpc-protobuf:1.5.0")
1919
extraDependency("io.grpc:grpc-netty:1.5.0")
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.opentelemetry.instrumentation.hypertrace.grpc.v1_5;
17+
package io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.client;
1818

1919
import com.google.auto.service.AutoService;
20-
import io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.client.GrpcClientBodyInstrumentation;
21-
import io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.server.GrpcServerBodyInstrumentation;
20+
import io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.GrpcInstrumentationName;
2221
import io.opentelemetry.javaagent.tooling.InstrumentationModule;
2322
import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
24-
import java.util.Arrays;
23+
import java.util.Collections;
2524
import java.util.List;
2625

2726
@AutoService(InstrumentationModule.class)
28-
public class GrpcBodyInstrumentationModule extends InstrumentationModule {
27+
public class GrpcClientBodyInstrumentationModule extends InstrumentationModule {
2928

30-
public GrpcBodyInstrumentationModule() {
29+
public GrpcClientBodyInstrumentationModule() {
3130
super(GrpcInstrumentationName.PRIMARY, GrpcInstrumentationName.OTHER);
3231
}
3332

@@ -38,6 +37,6 @@ public int getOrder() {
3837

3938
@Override
4039
public List<TypeInstrumentation> typeInstrumentations() {
41-
return Arrays.asList(new GrpcClientBodyInstrumentation(), new GrpcServerBodyInstrumentation());
40+
return Collections.singletonList(new GrpcClientBodyInstrumentation());
4241
}
4342
}

instrumentation/grpc-1.5/src/main/java/io/opentelemetry/instrumentation/hypertrace/grpc/v1_5/server/GrpcServerBodyInstrumentation.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616

1717
package io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.server;
1818

19+
import static io.opentelemetry.javaagent.tooling.ClassLoaderMatcher.hasClassesNamed;
20+
import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.safeHasSuperType;
1921
import static java.util.Collections.singletonMap;
2022
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
23+
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
2124
import static net.bytebuddy.matcher.ElementMatchers.named;
25+
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
2226

23-
import io.grpc.ServerInterceptor;
27+
import io.grpc.ServerBuilder;
28+
import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap;
2429
import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
25-
import java.util.List;
2630
import java.util.Map;
2731
import net.bytebuddy.asm.Advice;
2832
import net.bytebuddy.description.method.MethodDescription;
@@ -31,34 +35,35 @@
3135

3236
public class GrpcServerBodyInstrumentation implements TypeInstrumentation {
3337

38+
@Override
39+
public ElementMatcher<ClassLoader> classLoaderOptimization() {
40+
return hasClassesNamed("io.grpc.ServerBuilder");
41+
}
42+
3443
@Override
3544
public ElementMatcher<TypeDescription> typeMatcher() {
36-
return named("io.grpc.internal.AbstractServerImplBuilder");
45+
return safeHasSuperType(named("io.grpc.ServerBuilder"));
3746
}
3847

3948
@Override
4049
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
4150
return singletonMap(
42-
isMethod().and(named("build")),
51+
isMethod().and(isPublic()).and(named("build")).and(takesArguments(0)),
4352
GrpcServerBodyInstrumentation.class.getName() + "$AddInterceptorAdvice");
4453
}
4554

4655
public static class AddInterceptorAdvice {
4756
@Advice.OnMethodEnter(suppress = Throwable.class)
48-
public static void addInterceptor(
49-
@Advice.FieldValue("interceptors") List<ServerInterceptor> interceptors) {
50-
boolean shouldRegister = true;
51-
for (ServerInterceptor interceptor : interceptors) {
52-
if (interceptor instanceof GrpcServerInterceptor) {
53-
shouldRegister = false;
54-
break;
55-
}
56-
}
57-
if (shouldRegister) {
58-
// Interceptors run in the reverse order in which they are added
59-
// OTEL interceptor is last
60-
interceptors.add(interceptors.size() - 1, new GrpcServerInterceptor());
57+
public static void onEnter(@Advice.This ServerBuilder<?> serverBuilder) {
58+
int callDepth = CallDepthThreadLocalMap.incrementCallDepth(GrpcServerInterceptor.class);
59+
if (callDepth == 0) {
60+
serverBuilder.intercept(new GrpcServerInterceptor());
6161
}
6262
}
63+
64+
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
65+
public static void onExit() {
66+
CallDepthThreadLocalMap.decrementCallDepth(GrpcServerInterceptor.class);
67+
}
6368
}
6469
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright The Hypertrace Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.server;
18+
19+
import com.google.auto.service.AutoService;
20+
import io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.GrpcInstrumentationName;
21+
import io.opentelemetry.javaagent.tooling.InstrumentationModule;
22+
import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
23+
import java.util.Collections;
24+
import java.util.List;
25+
26+
@AutoService(InstrumentationModule.class)
27+
public class GrpcServerBodyInstrumentationModule extends InstrumentationModule {
28+
29+
public GrpcServerBodyInstrumentationModule() {
30+
super(GrpcInstrumentationName.PRIMARY, GrpcInstrumentationName.OTHER);
31+
}
32+
33+
@Override
34+
public int getOrder() {
35+
return -1;
36+
}
37+
38+
@Override
39+
public List<TypeInstrumentation> typeInstrumentations() {
40+
return Collections.singletonList(new GrpcServerBodyInstrumentation());
41+
}
42+
}

0 commit comments

Comments
 (0)