Skip to content

Add Servlet no wrapping instrumentation #235

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

Merged
merged 45 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
fdd1ef8
Add servlet no wrapping instrumentation
pavolloffay Jan 14, 2021
91bcde7
cleanup
pavolloffay Jan 14, 2021
a4a76ea
Servlet input stream instrumentation
pavolloffay Jan 15, 2021
9b48752
broken out stream
pavolloffay Jan 15, 2021
7d127a2
capture output, not working in spring
pavolloffay Jan 18, 2021
69a122c
Not capturing the response body
pavolloffay Jan 18, 2021
3fa7cdd
Working build
pavolloffay Jan 18, 2021
7258c01
Working
pavolloffay Jan 19, 2021
b2b9722
Working build
pavolloffay Jan 19, 2021
1850982
passing build
pavolloffay Jan 19, 2021
9d952b2
Passing nowrapping
pavolloffay Jan 19, 2021
f198140
working
pavolloffay Jan 19, 2021
af1dc25
rename passing
pavolloffay Jan 19, 2021
4802259
remove unused
pavolloffay Jan 19, 2021
774563c
remove unused
pavolloffay Jan 19, 2021
609af5c
Rename
pavolloffay Jan 19, 2021
e5ab2f5
Remove response buffer
pavolloffay Jan 20, 2021
1632dfe
Working
pavolloffay Jan 20, 2021
44e13ee
Split modules to pass muzzle
pavolloffay Jan 20, 2021
0d2d881
Some cleanup
pavolloffay Jan 20, 2021
151cdec
Refactor packages
pavolloffay Jan 20, 2021
3871322
Add more stream tests
pavolloffay Jan 20, 2021
537d374
More tests, writer
pavolloffay Jan 20, 2021
6b84b37
urlencoded tests
pavolloffay Jan 20, 2021
01afc89
Fix wrapping
pavolloffay Jan 20, 2021
d881cf3
Remove print
pavolloffay Jan 20, 2021
1dee020
Fixed wrapper of req/resp objects
pavolloffay Jan 21, 2021
2b30a03
Wrapping of input stream
pavolloffay Jan 21, 2021
cc3fb60
wrapping reader test
pavolloffay Jan 21, 2021
5376525
Wrap output stream
pavolloffay Jan 21, 2021
db1326b
Small corrections to the outstream
pavolloffay Jan 21, 2021
d2da196
reader and writer instr
pavolloffay Jan 21, 2021
ed4791d
Foo bar
pavolloffay Jan 21, 2021
9d6eb90
Delagating types
pavolloffay Jan 21, 2021
bdfe19f
Revert back
pavolloffay Jan 21, 2021
253988d
Align inputstream instrumentation with servlet
pavolloffay Jan 21, 2021
f5fc488
Adapt outputstream impl
pavolloffay Jan 21, 2021
d245b18
Use older jetty
pavolloffay Jan 21, 2021
05447d2
Add async test
pavolloffay Jan 21, 2021
5bcc7a0
remove prints
pavolloffay Jan 21, 2021
76b63e7
outputstream tests
pavolloffay Jan 21, 2021
eb30347
Rename metadata
pavolloffay Jan 21, 2021
defa35a
Remove mockito
pavolloffay Jan 21, 2021
28e2af9
Fix names
pavolloffay Jan 21, 2021
b5c19d4
test blocking
pavolloffay Jan 21, 2021
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
2 changes: 2 additions & 0 deletions instrumentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ dependencies{
implementation(project(":instrumentation:servlet:servlet-2.3"))
implementation(project(":instrumentation:servlet:servlet-3.0"))
implementation(project(":instrumentation:servlet:servlet-3.1"))
implementation(project(":instrumentation:servlet:servlet-rw"))
implementation(project(":instrumentation:servlet:servlet-3.0-no-wrapping"))
implementation(project(":instrumentation:spark-2.3"))
implementation(project(":instrumentation:grpc-1.5"))
implementation(project(":instrumentation:okhttp:okhttp-3.0"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package io.opentelemetry.javaagent.instrumentation.hypertrace.java.inputstream;

import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.extendsClass;
import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.safeHasSuperType;
import static net.bytebuddy.matcher.ElementMatchers.is;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap;
import io.opentelemetry.javaagent.tooling.InstrumentationModule;
import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
import java.io.IOException;
Expand Down Expand Up @@ -68,7 +71,8 @@ static class InputStreamInstrumentation implements TypeInstrumentation {

@Override
public ElementMatcher<? super TypeDescription> typeMatcher() {
return extendsClass(named(InputStream.class.getName()));
return extendsClass(named(InputStream.class.getName()))
.and(not(safeHasSuperType(named("javax.servlet.ServletInputStream"))));
}

@Override
Expand Down Expand Up @@ -116,14 +120,20 @@ public static SpanAndBuffer enter(@Advice.This InputStream thizz) {
return InputStreamUtils.check(thizz);
}

@Advice.OnMethodExit(suppress = Throwable.class)
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void exit(
@Advice.This InputStream thizz,
@Advice.Return int read,
@Advice.Enter SpanAndBuffer spanAndBuffer) {
if (spanAndBuffer != null) {
InputStreamUtils.read(thizz, spanAndBuffer, read);
if (spanAndBuffer == null) {
return;
}
int callDepth = CallDepthThreadLocalMap.decrementCallDepth(InputStream.class);
if (callDepth > 0) {
return;
}

InputStreamUtils.read(thizz, spanAndBuffer, read);
}
}

Expand All @@ -133,15 +143,21 @@ public static SpanAndBuffer enter(@Advice.This InputStream thizz) {
return InputStreamUtils.check(thizz);
}

@Advice.OnMethodExit(suppress = Throwable.class)
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void exit(
@Advice.This InputStream thizz,
@Advice.Return int read,
@Advice.Argument(0) byte b[],
@Advice.Enter SpanAndBuffer spanAndBuffer) {
if (spanAndBuffer != null) {
InputStreamUtils.read(thizz, spanAndBuffer, read, b);
if (spanAndBuffer == null) {
return;
}
int callDepth = CallDepthThreadLocalMap.decrementCallDepth(InputStream.class);
if (callDepth > 0) {
return;
}

InputStreamUtils.read(thizz, spanAndBuffer, read, b);
}
}

Expand All @@ -151,17 +167,23 @@ public static SpanAndBuffer enter(@Advice.This InputStream thizz) {
return InputStreamUtils.check(thizz);
}

@Advice.OnMethodExit(suppress = Throwable.class)
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void exit(
@Advice.This InputStream thizz,
@Advice.Return int read,
@Advice.Argument(0) byte b[],
@Advice.Argument(1) int off,
@Advice.Argument(2) int len,
@Advice.Enter SpanAndBuffer spanAndBuffer) {
if (spanAndBuffer != null) {
InputStreamUtils.read(thizz, spanAndBuffer, read, b, off, len);
if (spanAndBuffer == null) {
return;
}
int callDepth = CallDepthThreadLocalMap.decrementCallDepth(InputStream.class);
if (callDepth > 0) {
return;
}

InputStreamUtils.read(thizz, spanAndBuffer, read, b, off, len);
}
}

Expand All @@ -171,15 +193,21 @@ public static SpanAndBuffer enter(@Advice.This InputStream thizz) {
return InputStreamUtils.check(thizz);
}

@Advice.OnMethodExit(suppress = Throwable.class)
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void exit(
@Advice.This InputStream thizz,
@Advice.Return byte[] b,
@Advice.Enter SpanAndBuffer spanAndBuffer)
throws IOException {
if (spanAndBuffer != null) {
InputStreamUtils.readAll(thizz, spanAndBuffer, b);
if (spanAndBuffer == null) {
return;
}
int callDepth = CallDepthThreadLocalMap.decrementCallDepth(InputStream.class);
if (callDepth > 0) {
return;
}

InputStreamUtils.readAll(thizz, spanAndBuffer, b);
}
}

Expand All @@ -189,14 +217,21 @@ public static SpanAndBuffer enter(@Advice.This InputStream thizz) {
return InputStreamUtils.check(thizz);
}

@Advice.OnMethodExit(suppress = Throwable.class)
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void exit(
@Advice.This InputStream thizz,
@Advice.Return int read,
@Advice.Argument(0) byte[] b,
@Advice.Argument(1) int off,
@Advice.Argument(2) int len,
@Advice.Enter SpanAndBuffer spanAndBuffer) {
if (spanAndBuffer == null) {
return;
}
int callDepth = CallDepthThreadLocalMap.decrementCallDepth(InputStream.class);
if (callDepth > 0) {
return;
}
InputStreamUtils.readNBytes(thizz, spanAndBuffer, read, b, off, len);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ public static SpanAndBuffer check(InputStream inputStream) {
return null;
}

int callDepth = CallDepthThreadLocalMap.incrementCallDepth(InputStream.class);
if (callDepth > 0) {
return null;
}
CallDepthThreadLocalMap.incrementCallDepth(InputStream.class);
return spanAndBuffer;
}

Expand All @@ -94,7 +91,6 @@ public static void read(InputStream inputStream, SpanAndBuffer spanAndBuffer, in
spanAndBuffer.charset);
GlobalObjectRegistry.inputStreamToSpanAndBufferMap.remove(inputStream);
}
CallDepthThreadLocalMap.reset(InputStream.class);
}

public static void read(
Expand All @@ -109,7 +105,6 @@ public static void read(
spanAndBuffer.charset);
GlobalObjectRegistry.inputStreamToSpanAndBufferMap.remove(inputStream);
}
CallDepthThreadLocalMap.reset(InputStream.class);
}

public static void read(
Expand All @@ -124,14 +119,12 @@ public static void read(
spanAndBuffer.charset);
GlobalObjectRegistry.inputStreamToSpanAndBufferMap.remove(inputStream);
}
CallDepthThreadLocalMap.reset(InputStream.class);
}

public static void readAll(InputStream inputStream, SpanAndBuffer spanAndBuffer, byte[] b)
throws IOException {
spanAndBuffer.byteArrayBuffer.write(b);
GlobalObjectRegistry.inputStreamToSpanAndBufferMap.remove(inputStream);
CallDepthThreadLocalMap.reset(InputStream.class);
}

public static void readNBytes(
Expand All @@ -146,7 +139,6 @@ public static void readNBytes(
} else {
spanAndBuffer.byteArrayBuffer.write(b, off, read);
}
CallDepthThreadLocalMap.reset(InputStream.class);
}

public static void available(InputStream inputStream, int available) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap;
import io.opentelemetry.javaagent.tooling.InstrumentationModule;
import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
import java.io.IOException;
Expand All @@ -37,6 +38,7 @@
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.hypertrace.agent.core.instrumentation.GlobalObjectRegistry;
import org.hypertrace.agent.core.instrumentation.buffer.BoundedByteArrayOutputStream;

/**
* {@link OutputStream} instrumentation. The type matcher applies to all implementations. However
Expand Down Expand Up @@ -96,51 +98,79 @@ public Map<? extends ElementMatcher<? super MethodDescription>, String> transfor

static class OutputStream_WriteIntAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static boolean enter(@Advice.This OutputStream thizz) {
return OutputStreamUtils.check(thizz);
public static BoundedByteArrayOutputStream enter(
@Advice.This OutputStream thizz, @Advice.Argument(0) int b) {
BoundedByteArrayOutputStream buffer = GlobalObjectRegistry.outputStreamToBufferMap.get(thizz);
if (buffer == null) {
return null;
}
int callDepth = CallDepthThreadLocalMap.incrementCallDepth(OutputStream.class);
if (callDepth > 0) {
return buffer;
}

buffer.write(b);
return buffer;
}

@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void exit(
@Advice.This OutputStream thizz,
@Advice.Argument(0) int b,
@Advice.Enter boolean retEnterAdvice)
throws IOException {
OutputStreamUtils.write(thizz, retEnterAdvice, b);
public static void exit(@Advice.Enter BoundedByteArrayOutputStream buffer) {
if (buffer != null) {
CallDepthThreadLocalMap.decrementCallDepth(OutputStream.class);
}
}
}

static class OutputStream_WriteByteArrAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static boolean enter(@Advice.This OutputStream thizz) {
return OutputStreamUtils.check(thizz);
public static BoundedByteArrayOutputStream enter(
@Advice.This OutputStream thizz, @Advice.Argument(0) byte b[]) throws IOException {
BoundedByteArrayOutputStream buffer = GlobalObjectRegistry.outputStreamToBufferMap.get(thizz);
if (buffer == null) {
return null;
}
int callDepth = CallDepthThreadLocalMap.incrementCallDepth(OutputStream.class);
if (callDepth > 0) {
return buffer;
}

buffer.write(b);
return buffer;
}

@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void exit(
@Advice.This OutputStream thizz,
@Advice.Argument(0) byte b[],
@Advice.Enter boolean retEnterAdvice)
throws IOException {
OutputStreamUtils.write(thizz, retEnterAdvice, b);
public static void exit(@Advice.Enter BoundedByteArrayOutputStream buffer) {
if (buffer != null) {
CallDepthThreadLocalMap.decrementCallDepth(OutputStream.class);
}
}
}

static class OutputStream_WriteByteArrOffsetAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static boolean enter(@Advice.This OutputStream thizz) {
return OutputStreamUtils.check(thizz);
}

@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void exit(
public static BoundedByteArrayOutputStream enter(
@Advice.This OutputStream thizz,
@Advice.Argument(0) byte b[],
@Advice.Argument(1) int off,
@Advice.Argument(2) int len,
@Advice.Enter boolean retEnterAdvice)
throws IOException {
OutputStreamUtils.write(thizz, retEnterAdvice, b, off, len);
@Advice.Argument(2) int len) {
BoundedByteArrayOutputStream buffer = GlobalObjectRegistry.outputStreamToBufferMap.get(thizz);
if (buffer == null) {
return null;
}
int callDepth = CallDepthThreadLocalMap.incrementCallDepth(OutputStream.class);
if (callDepth > 0) {
return buffer;
}

buffer.write(b, off, len);
return buffer;
}

@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void exit(@Advice.Enter BoundedByteArrayOutputStream buffer) {
if (buffer != null) {
CallDepthThreadLocalMap.decrementCallDepth(OutputStream.class);
}
}
}
}
Loading