Skip to content

Commit 58773b5

Browse files
committed
passing unit tests
Copied over from :servlet3.0:javaagent. Had to disable/remove some test cases/scenarios that didn't make sense or just weren't working. Still not using proper gradle project structure though.
1 parent cd80806 commit 58773b5

36 files changed

+2004
-160
lines changed

instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Advice.java

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,11 @@
66
package io.opentelemetry.javaagent.instrumentation.servlet.v3_0;
77

88
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.getSnippetInjectionHelper;
9-
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.helper;
109

11-
import io.opentelemetry.context.Context;
12-
import io.opentelemetry.context.Scope;
1310
import io.opentelemetry.javaagent.bootstrap.CallDepth;
14-
import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder;
1511
import io.opentelemetry.javaagent.bootstrap.servlet.AppServerBridge;
16-
import io.opentelemetry.javaagent.bootstrap.servlet.MappingResolver;
17-
import io.opentelemetry.javaagent.instrumentation.servlet.ServletRequestContext;
1812
import io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.Servlet3SnippetInjectingResponseWrapper;
1913
import javax.annotation.Nullable;
20-
import javax.servlet.Servlet;
2114
import javax.servlet.ServletRequest;
2215
import javax.servlet.ServletResponse;
2316
import javax.servlet.http.HttpServletRequest;
@@ -27,70 +20,8 @@
2720
import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument;
2821
import net.bytebuddy.implementation.bytecode.assign.Assigner;
2922

30-
@SuppressWarnings("unused")
3123
public class Servlet3Advice {
3224

33-
public static class AdviceScope {
34-
private final CallDepth callDepth;
35-
private final ServletRequestContext<HttpServletRequest> requestContext;
36-
private final Context context;
37-
private final Scope scope;
38-
39-
public AdviceScope(
40-
CallDepth callDepth,
41-
HttpServletRequest request,
42-
HttpServletResponse response,
43-
Object servletOrFilter) {
44-
this.callDepth = callDepth;
45-
this.callDepth.getAndIncrement();
46-
47-
Context currentContext = Context.current();
48-
Context attachedContext = helper().getServerContext(request);
49-
Context contextToUpdate;
50-
51-
requestContext = new ServletRequestContext<>(request, servletOrFilter);
52-
if (attachedContext == null && helper().shouldStart(currentContext, requestContext)) {
53-
context = helper().start(currentContext, requestContext);
54-
helper().setAsyncListenerResponse(context, response);
55-
56-
contextToUpdate = context;
57-
} else if (attachedContext != null
58-
&& helper().needsRescoping(currentContext, attachedContext)) {
59-
// Given request already has a context associated with it.
60-
// see the needsRescoping() javadoc for more explanation
61-
contextToUpdate = attachedContext;
62-
context = null;
63-
} else {
64-
// We are inside nested servlet/filter/app-server span, don't create new span
65-
contextToUpdate = currentContext;
66-
context = null;
67-
}
68-
69-
// Update context with info from current request to ensure that server span gets the best
70-
// possible name.
71-
// In case server span was created by app server instrumentations calling updateContext
72-
// returns a new context that contains servlet context path that is used in other
73-
// instrumentations for naming server span.
74-
MappingResolver mappingResolver = Servlet3Singletons.getMappingResolver(servletOrFilter);
75-
boolean servlet = servletOrFilter instanceof Servlet;
76-
contextToUpdate = helper().updateContext(contextToUpdate, request, mappingResolver, servlet);
77-
scope = contextToUpdate.makeCurrent();
78-
79-
if (context != null) {
80-
// Only trigger response customizer once, so only if server span was created here
81-
HttpServerResponseCustomizerHolder.getCustomizer()
82-
.customize(contextToUpdate, response, Servlet3Accessor.INSTANCE);
83-
}
84-
}
85-
86-
public void exit(
87-
@Nullable Throwable throwable, HttpServletRequest request, HttpServletResponse response) {
88-
89-
boolean topLevel = callDepth.decrementAndGet() == 0;
90-
helper().end(requestContext, request, response, throwable, topLevel, context, scope);
91-
}
92-
}
93-
9425
@AssignReturned.ToArguments({
9526
@ToArgument(value = 0, index = 1),
9627
@ToArgument(value = 1, index = 2)
@@ -114,8 +45,8 @@ public static Object[] onEnter(
11445
response =
11546
new Servlet3SnippetInjectingResponseWrapper((HttpServletResponse) response, snippet);
11647
}
117-
AdviceScope adviceScope =
118-
new AdviceScope(
48+
Servlet3RequestAdviceScope adviceScope =
49+
new Servlet3RequestAdviceScope(
11950
CallDepth.forClass(AppServerBridge.getCallDepthKey()),
12051
(HttpServletRequest) request,
12152
(HttpServletResponse) response,
@@ -129,7 +60,7 @@ public static void stopSpan(
12960
@Advice.Argument(1) ServletResponse response,
13061
@Advice.Thrown @Nullable Throwable throwable,
13162
@Advice.Enter Object[] enterResult) {
132-
AdviceScope adviceScope = (AdviceScope) enterResult[0];
63+
Servlet3RequestAdviceScope adviceScope = (Servlet3RequestAdviceScope) enterResult[0];
13364
if (adviceScope == null
13465
|| !(request instanceof HttpServletRequest)
13566
|| !(response instanceof HttpServletResponse)) {

instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3FilterInitAdvice.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package io.opentelemetry.javaagent.instrumentation.servlet.v3_0;
77

8-
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.FILTER_MAPPING_RESOLVER_FACTORY;
8+
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.FILTER_MAPPING_RESOLVER;
99

1010
import javax.servlet.Filter;
1111
import javax.servlet.FilterConfig;
@@ -20,7 +20,7 @@ public static void filterInit(
2020
if (filterConfig == null) {
2121
return;
2222
}
23-
FILTER_MAPPING_RESOLVER_FACTORY.set(
23+
FILTER_MAPPING_RESOLVER.set(
2424
filter, new Servlet3FilterMappingResolverFactory(filterConfig));
2525
}
2626
}

instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3InitAdvice.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package io.opentelemetry.javaagent.instrumentation.servlet.v3_0;
77

8-
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.SERVLET_MAPPING_RESOLVER_FACTORY;
8+
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.SERVLET_MAPPING_RESOLVER;
99

1010
import javax.servlet.Servlet;
1111
import javax.servlet.ServletConfig;
@@ -20,7 +20,7 @@ public static void servletInit(
2020
if (servletConfig == null) {
2121
return;
2222
}
23-
SERVLET_MAPPING_RESOLVER_FACTORY.set(
23+
SERVLET_MAPPING_RESOLVER.set(
2424
servlet, new Servlet3MappingResolverFactory(servletConfig));
2525
}
2626
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package io.opentelemetry.javaagent.instrumentation.servlet.v3_0;
2+
3+
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.helper;
4+
5+
import io.opentelemetry.context.Context;
6+
import io.opentelemetry.context.Scope;
7+
import io.opentelemetry.javaagent.bootstrap.CallDepth;
8+
import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder;
9+
import io.opentelemetry.javaagent.bootstrap.servlet.MappingResolver;
10+
import io.opentelemetry.javaagent.instrumentation.servlet.ServletRequestContext;
11+
import javax.annotation.Nullable;
12+
import javax.servlet.Servlet;
13+
import javax.servlet.http.HttpServletRequest;
14+
import javax.servlet.http.HttpServletResponse;
15+
16+
public class Servlet3RequestAdviceScope {
17+
private final CallDepth callDepth;
18+
private final ServletRequestContext<HttpServletRequest> requestContext;
19+
private final Context context;
20+
private final Scope scope;
21+
22+
public Servlet3RequestAdviceScope(
23+
CallDepth callDepth,
24+
HttpServletRequest request,
25+
HttpServletResponse response,
26+
Object servletOrFilter) {
27+
this.callDepth = callDepth;
28+
this.callDepth.getAndIncrement();
29+
30+
Context currentContext = Context.current();
31+
Context attachedContext = helper().getServerContext(request);
32+
Context contextToUpdate;
33+
34+
requestContext = new ServletRequestContext<>(request, servletOrFilter);
35+
if (attachedContext == null && helper().shouldStart(currentContext, requestContext)) {
36+
context = helper().start(currentContext, requestContext);
37+
helper().setAsyncListenerResponse(context, response);
38+
39+
contextToUpdate = context;
40+
} else if (attachedContext != null
41+
&& helper().needsRescoping(currentContext, attachedContext)) {
42+
// Given request already has a context associated with it.
43+
// see the needsRescoping() javadoc for more explanation
44+
contextToUpdate = attachedContext;
45+
context = null;
46+
} else {
47+
// We are inside nested servlet/filter/app-server span, don't create new span
48+
contextToUpdate = currentContext;
49+
context = null;
50+
}
51+
52+
// Update context with info from current request to ensure that server span gets the best
53+
// possible name.
54+
// In case server span was created by app server instrumentations calling updateContext
55+
// returns a new context that contains servlet context path that is used in other
56+
// instrumentations for naming server span.
57+
MappingResolver mappingResolver = Servlet3Singletons.getMappingResolver(servletOrFilter);
58+
boolean servlet = servletOrFilter instanceof Servlet;
59+
contextToUpdate = helper().updateContext(contextToUpdate, request, mappingResolver, servlet);
60+
scope = contextToUpdate.makeCurrent();
61+
62+
if (context != null) {
63+
// Only trigger response customizer once, so only if server span was created here
64+
HttpServerResponseCustomizerHolder.getCustomizer()
65+
.customize(contextToUpdate, response, Servlet3Accessor.INSTANCE);
66+
}
67+
}
68+
69+
public void exit(
70+
@Nullable Throwable throwable, HttpServletRequest request, HttpServletResponse response) {
71+
72+
boolean topLevel = callDepth.decrementAndGet() == 0;
73+
helper().end(requestContext, request, response, throwable, topLevel, context, scope);
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.opentelemetry.javaagent.instrumentation.servlet.v3_0;
2+
3+
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.responseInstrumenter;
4+
5+
import io.opentelemetry.context.Context;
6+
import io.opentelemetry.context.Scope;
7+
import io.opentelemetry.instrumentation.api.incubator.semconv.util.ClassAndMethod;
8+
import io.opentelemetry.javaagent.bootstrap.CallDepth;
9+
import io.opentelemetry.javaagent.instrumentation.servlet.common.response.HttpServletResponseAdviceHelper;
10+
import javax.annotation.Nullable;
11+
12+
public class Servlet3ResponseAdviceScope {
13+
private final CallDepth callDepth;
14+
private final ClassAndMethod classAndMethod;
15+
private final Context context;
16+
private final Scope scope;
17+
18+
public Servlet3ResponseAdviceScope(
19+
CallDepth callDepth, Class<?> declaringClass, String methodName) {
20+
this.callDepth = callDepth;
21+
if (callDepth.getAndIncrement() > 0) {
22+
this.classAndMethod = null;
23+
this.context = null;
24+
this.scope = null;
25+
return;
26+
}
27+
HttpServletResponseAdviceHelper.StartResult result =
28+
HttpServletResponseAdviceHelper.startSpan(
29+
responseInstrumenter(), declaringClass, methodName);
30+
if (result != null) {
31+
classAndMethod = result.getClassAndMethod();
32+
context = result.getContext();
33+
scope = result.getScope();
34+
} else {
35+
classAndMethod = null;
36+
context = null;
37+
scope = null;
38+
}
39+
}
40+
41+
public void exit(@Nullable Throwable throwable) {
42+
if (callDepth.decrementAndGet() > 0) {
43+
return;
44+
}
45+
HttpServletResponseAdviceHelper.stopSpan(
46+
responseInstrumenter(), throwable, context, scope, classAndMethod);
47+
}
48+
}

instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3ResponseSendAdvice.java

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,23 @@
55

66
package io.opentelemetry.javaagent.instrumentation.servlet.v3_0;
77

8-
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.responseInstrumenter;
9-
10-
import io.opentelemetry.context.Context;
11-
import io.opentelemetry.context.Scope;
12-
import io.opentelemetry.instrumentation.api.incubator.semconv.util.ClassAndMethod;
138
import io.opentelemetry.javaagent.bootstrap.CallDepth;
14-
import io.opentelemetry.javaagent.instrumentation.servlet.common.response.HttpServletResponseAdviceHelper;
15-
import javax.annotation.Nullable;
169
import javax.servlet.http.HttpServletResponse;
1710
import net.bytebuddy.asm.Advice;
1811

1912
@SuppressWarnings("unused")
2013
public class Servlet3ResponseSendAdvice {
2114

22-
public static class AdviceScope {
23-
private final CallDepth callDepth;
24-
private final ClassAndMethod classAndMethod;
25-
private final Context context;
26-
private final Scope scope;
27-
28-
public AdviceScope(CallDepth callDepth, Class<?> declaringClass, String methodName) {
29-
this.callDepth = callDepth;
30-
if (callDepth.getAndIncrement() > 0) {
31-
this.classAndMethod = null;
32-
this.context = null;
33-
this.scope = null;
34-
return;
35-
}
36-
HttpServletResponseAdviceHelper.StartResult result =
37-
HttpServletResponseAdviceHelper.startSpan(
38-
responseInstrumenter(), declaringClass, methodName);
39-
if (result != null) {
40-
classAndMethod = result.getClassAndMethod();
41-
context = result.getContext();
42-
scope = result.getScope();
43-
} else {
44-
classAndMethod = null;
45-
context = null;
46-
scope = null;
47-
}
48-
}
49-
50-
public void exit(@Nullable Throwable throwable) {
51-
if (callDepth.decrementAndGet() > 0) {
52-
return;
53-
}
54-
HttpServletResponseAdviceHelper.stopSpan(
55-
responseInstrumenter(), throwable, context, scope, classAndMethod);
56-
}
57-
}
58-
5915
@Advice.OnMethodEnter(suppress = Throwable.class)
60-
public static AdviceScope start(
16+
public static Servlet3ResponseAdviceScope start(
6117
@Advice.Origin("#t") Class<?> declaringClass, @Advice.Origin("#m") String methodName) {
62-
return new AdviceScope(
18+
return new Servlet3ResponseAdviceScope(
6319
CallDepth.forClass(HttpServletResponse.class), declaringClass, methodName);
6420
}
6521

6622
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6723
public static void stopSpan(
68-
@Advice.Thrown Throwable throwable, @Advice.Enter AdviceScope adviceScope) {
24+
@Advice.Thrown Throwable throwable, @Advice.Enter Servlet3ResponseAdviceScope adviceScope) {
6925
adviceScope.exit(throwable);
7026
}
7127
}

instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Singletons.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@
2424
public final class Servlet3Singletons {
2525
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.servlet-3.0";
2626

27-
public static final VirtualField<Servlet, MappingResolver.Factory>
28-
SERVLET_MAPPING_RESOLVER_FACTORY =
29-
VirtualField.find(Servlet.class, MappingResolver.Factory.class);
30-
31-
public static final VirtualField<Filter, MappingResolver.Factory>
32-
FILTER_MAPPING_RESOLVER_FACTORY =
33-
VirtualField.find(Filter.class, MappingResolver.Factory.class);
34-
3527
private static final Instrumenter<
3628
ServletRequestContext<HttpServletRequest>, ServletResponseContext<HttpServletResponse>>
3729
INSTRUMENTER =
@@ -41,9 +33,9 @@ public final class Servlet3Singletons {
4133
private static final ServletHelper<HttpServletRequest, HttpServletResponse> HELPER =
4234
new ServletHelper<>(INSTRUMENTER, Servlet3Accessor.INSTANCE);
4335

44-
private static final VirtualField<Servlet, MappingResolver.Factory> SERVLET_MAPPING_RESOLVER =
36+
public static final VirtualField<Servlet, MappingResolver.Factory> SERVLET_MAPPING_RESOLVER =
4537
VirtualField.find(Servlet.class, MappingResolver.Factory.class);
46-
private static final VirtualField<Filter, MappingResolver.Factory> FILTER_MAPPING_RESOLVER =
38+
public static final VirtualField<Filter, MappingResolver.Factory> FILTER_MAPPING_RESOLVER =
4739
VirtualField.find(Filter.class, MappingResolver.Factory.class);
4840

4941
private static final Instrumenter<ClassAndMethod, Void> RESPONSE_INSTRUMENTER =

instrumentation/servlet/servlet-3.0/library/build.gradle.kts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,22 @@ dependencies {
88
// FIXME: These dependencies need to be shadowed into the library.
99
library(project(":instrumentation:servlet:servlet-3.0:javaagent"))
1010
library(project(":instrumentation:servlet:servlet-common:javaagent"))
11+
library(project(":instrumentation:servlet:servlet-common:bootstrap"))
12+
library(project(":javaagent-extension-api"))
13+
14+
// testImplementation(project(":testing:agent-exporter"))
15+
16+
testLibrary("org.eclipse.jetty:jetty-server:8.0.0.v20110901")
17+
testLibrary("org.eclipse.jetty:jetty-servlet:8.0.0.v20110901")
18+
testLibrary("org.apache.tomcat.embed:tomcat-embed-core:8.0.41")
19+
testLibrary("org.apache.tomcat.embed:tomcat-embed-jasper:8.0.41")
20+
}
21+
22+
23+
tasks {
24+
withType<Test>().configureEach {
25+
// required on jdk17 to allow tomcat to shutdown properly.
26+
jvmArgs("--add-opens=java.base/java.util=ALL-UNNAMED")
27+
jvmArgs("-XX:+IgnoreUnrecognizedVMOptions")
28+
}
1129
}

0 commit comments

Comments
 (0)