|
| 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; |
| 18 | + |
| 19 | +import static net.bytebuddy.matcher.ElementMatchers.failSafe; |
| 20 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 21 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 22 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 23 | + |
| 24 | +import com.google.auto.service.AutoService; |
| 25 | +import io.grpc.Metadata; |
| 26 | +import io.netty.handler.codec.http2.Http2Headers; |
| 27 | +import io.opentelemetry.api.trace.Span; |
| 28 | +import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; |
| 29 | +import io.opentelemetry.javaagent.tooling.InstrumentationModule; |
| 30 | +import io.opentelemetry.javaagent.tooling.TypeInstrumentation; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | +import net.bytebuddy.asm.Advice; |
| 37 | +import net.bytebuddy.description.method.MethodDescription; |
| 38 | +import net.bytebuddy.description.type.TypeDescription; |
| 39 | +import net.bytebuddy.matcher.ElementMatcher; |
| 40 | +import org.hypertrace.agent.core.HypertraceSemanticAttributes; |
| 41 | + |
| 42 | +@AutoService(InstrumentationModule.class) |
| 43 | +public class NettyHttp2HeadersInstrumentationModule extends InstrumentationModule { |
| 44 | + |
| 45 | + private static final List<String> INSTRUMENTATION_NAMES = new ArrayList<>(); |
| 46 | + |
| 47 | + static { |
| 48 | + INSTRUMENTATION_NAMES.add(GrpcInstrumentationName.PRIMARY); |
| 49 | + INSTRUMENTATION_NAMES.addAll(Arrays.asList(GrpcInstrumentationName.OTHER)); |
| 50 | + INSTRUMENTATION_NAMES.add("grpc-netty-ht"); |
| 51 | + } |
| 52 | + |
| 53 | + public NettyHttp2HeadersInstrumentationModule() { |
| 54 | + super(INSTRUMENTATION_NAMES); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public List<TypeInstrumentation> typeInstrumentations() { |
| 59 | + return Arrays.asList(new NettyUtilsInstrumentation()); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * The server side HTTP2 headers are added in tracing gRPC interceptor. The headers are added to |
| 64 | + * metadata in {@link GrpcUtils_convertHeaders_Advice}. |
| 65 | + * |
| 66 | + * <p>The client side HTTP2 headers are added directly to span in {@link |
| 67 | + * Utils_convertClientHeaders_Advice}. TODO However it does not work for the first request |
| 68 | + * https://github.com/hypertrace/javaagent/issues/109#issuecomment-740918018. |
| 69 | + */ |
| 70 | + class NettyUtilsInstrumentation implements TypeInstrumentation { |
| 71 | + @Override |
| 72 | + public ElementMatcher<? super TypeDescription> typeMatcher() { |
| 73 | + return failSafe(named("io.grpc.netty.Utils")); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { |
| 78 | + Map<ElementMatcher<? super MethodDescription>, String> transformers = new HashMap<>(); |
| 79 | + transformers.put( |
| 80 | + isMethod().and(named("convertClientHeaders")).and(takesArguments(6)), |
| 81 | + Utils_convertClientHeaders_Advice.class.getName()); |
| 82 | + transformers.put( |
| 83 | + isMethod().and(named("convertHeaders")).and(takesArguments(1)), |
| 84 | + GrpcUtils_convertHeaders_Advice.class.getName()); |
| 85 | + return transformers; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + static class Utils_convertClientHeaders_Advice { |
| 90 | + @Advice.OnMethodExit(suppress = Throwable.class) |
| 91 | + public static void exit( |
| 92 | + @Advice.Argument(1) Object scheme, |
| 93 | + @Advice.Argument(2) Object defaultPath, |
| 94 | + @Advice.Argument(3) Object authority, |
| 95 | + @Advice.Argument(4) Object method) { |
| 96 | + |
| 97 | + Span currentSpan = Java8BytecodeBridge.currentSpan(); |
| 98 | + if (scheme != null) { |
| 99 | + currentSpan.setAttribute( |
| 100 | + HypertraceSemanticAttributes.rpcRequestMetadata(GrpcSemanticAttributes.SCHEME), |
| 101 | + scheme.toString()); |
| 102 | + } |
| 103 | + if (defaultPath != null) { |
| 104 | + currentSpan.setAttribute( |
| 105 | + HypertraceSemanticAttributes.rpcRequestMetadata(GrpcSemanticAttributes.PATH), |
| 106 | + defaultPath.toString()); |
| 107 | + } |
| 108 | + if (authority != null) { |
| 109 | + currentSpan.setAttribute( |
| 110 | + HypertraceSemanticAttributes.rpcRequestMetadata(GrpcSemanticAttributes.AUTHORITY), |
| 111 | + authority.toString()); |
| 112 | + } |
| 113 | + if (method != null) { |
| 114 | + currentSpan.setAttribute( |
| 115 | + HypertraceSemanticAttributes.rpcRequestMetadata(GrpcSemanticAttributes.METHOD), |
| 116 | + method.toString()); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * There are multiple implementations of {@link Http2Headers}. Only some of them support getting |
| 123 | + * authority, path etc. For instance {@code GrpcHttp2ResponseHeaders} throws unsupported exception |
| 124 | + * when accessing authority etc. This header is used client response. |
| 125 | + * |
| 126 | + * @see {@link io.grpc.netty.GrpcHttp2HeadersUtils} |
| 127 | + */ |
| 128 | + static class GrpcUtils_convertHeaders_Advice { |
| 129 | + @Advice.OnMethodExit(suppress = Throwable.class) |
| 130 | + public static void exit( |
| 131 | + @Advice.Argument(0) Http2Headers http2Headers, @Advice.Return Metadata metadata) { |
| 132 | + |
| 133 | + if (http2Headers.authority() != null) { |
| 134 | + metadata.put( |
| 135 | + GrpcSemanticAttributes.AUTHORITY_METADATA_KEY, http2Headers.authority().toString()); |
| 136 | + } |
| 137 | + if (http2Headers.path() != null) { |
| 138 | + metadata.put(GrpcSemanticAttributes.PATH_METADATA_KEY, http2Headers.path().toString()); |
| 139 | + } |
| 140 | + if (http2Headers.method() != null) { |
| 141 | + metadata.put(GrpcSemanticAttributes.METHOD_METADATA_KEY, http2Headers.method().toString()); |
| 142 | + } |
| 143 | + if (http2Headers.scheme() != null) { |
| 144 | + metadata.put(GrpcSemanticAttributes.SCHEME_METADATA_KEY, http2Headers.scheme().toString()); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments