|
| 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.javaagent.instrumentation.hypertrace.undertow.v1_4; |
| 18 | + |
| 19 | +import static net.bytebuddy.matcher.ElementMatchers.isPublic; |
| 20 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 21 | +import static net.bytebuddy.matcher.ElementMatchers.returns; |
| 22 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 23 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 24 | + |
| 25 | +import io.opentelemetry.javaagent.instrumentation.api.ContextStore; |
| 26 | +import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext; |
| 27 | +import io.opentelemetry.javaagent.instrumentation.hypertrace.undertow.v1_4.utils.Utils; |
| 28 | +import io.opentelemetry.javaagent.tooling.TypeInstrumentation; |
| 29 | +import io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers; |
| 30 | +import io.undertow.server.HttpServerExchange; |
| 31 | +import java.nio.ByteBuffer; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.Map; |
| 34 | +import net.bytebuddy.asm.Advice; |
| 35 | +import net.bytebuddy.description.method.MethodDescription; |
| 36 | +import net.bytebuddy.description.type.TypeDescription; |
| 37 | +import net.bytebuddy.matcher.ElementMatcher; |
| 38 | +import org.hypertrace.agent.core.instrumentation.HypertraceCallDepthThreadLocalMap; |
| 39 | +import org.hypertrace.agent.core.instrumentation.SpanAndBuffer; |
| 40 | +import org.xnio.channels.StreamSourceChannel; |
| 41 | + |
| 42 | +/** Instrumentation for {@link StreamSourceChannel} implementations */ |
| 43 | +public final class StreamSourceChannelInstrumentation implements TypeInstrumentation { |
| 44 | + |
| 45 | + @Override |
| 46 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 47 | + return AgentElementMatchers.safeHasSuperType(named("org.xnio.channels.StreamSourceChannel")); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { |
| 52 | + return Collections.singletonMap( |
| 53 | + named("read") |
| 54 | + .and(takesArguments(1)) |
| 55 | + .and(takesArgument(0, ByteBuffer.class)) |
| 56 | + .and(returns(int.class)) |
| 57 | + .and(isPublic()), |
| 58 | + StreamSourceChannelInstrumentation.class.getName() + "$Read_advice"); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Decorates the {@link StreamSourceChannel#read(ByteBuffer)} implementations with logic to |
| 63 | + * capture data read into the request body {@link ByteBuffer} and report it. This instrumentation |
| 64 | + * short-circuits if: |
| 65 | + * |
| 66 | + * <ul> |
| 67 | + * <li>We're in a nested {@link StreamSourceChannel#read(ByteBuffer)} call |
| 68 | + * <li>A {@link Throwable} was thrown in the context of the {@link |
| 69 | + * StreamSourceChannel#read(ByteBuffer)}, causing the method to exit |
| 70 | + * <li>The instrumented {@link StreamSourceChannel} was never put in the {@link |
| 71 | + * InstrumentationContext} by {@link |
| 72 | + * UndertowHttpServerExchangeInstrumentation.GetRequestChannel_advice#exit(HttpServerExchange, |
| 73 | + * StreamSourceChannel)} |
| 74 | + * </ul> |
| 75 | + */ |
| 76 | + static final class Read_advice { |
| 77 | + |
| 78 | + @Advice.OnMethodEnter |
| 79 | + public static void trackCallDepth() { |
| 80 | + HypertraceCallDepthThreadLocalMap.incrementCallDepth(StreamSourceChannel.class); |
| 81 | + } |
| 82 | + |
| 83 | + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) |
| 84 | + public static void exit( |
| 85 | + @Advice.Return final int numBytesRead, |
| 86 | + @Advice.This final StreamSourceChannel streamSourceChannel, |
| 87 | + @Advice.Thrown final Throwable thrown, |
| 88 | + @Advice.Argument(0) final ByteBuffer byteBuffer) { |
| 89 | + if (HypertraceCallDepthThreadLocalMap.decrementCallDepth(StreamSourceChannel.class) > 0 |
| 90 | + || thrown != null) { |
| 91 | + return; |
| 92 | + } |
| 93 | + final ContextStore<StreamSourceChannel, SpanAndBuffer> contextStore = |
| 94 | + InstrumentationContext.get(StreamSourceChannel.class, SpanAndBuffer.class); |
| 95 | + final SpanAndBuffer spanAndBuffer = contextStore.get(streamSourceChannel); |
| 96 | + if (spanAndBuffer != null) { |
| 97 | + Utils.handleRead(byteBuffer.asReadOnlyBuffer(), numBytesRead, spanAndBuffer); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments