Skip to content

Commit 6d9c9f3

Browse files
committed
Use shadowJar to pull in dependencies instead of copy
Separated unit tests to different project to enable unit testing against the shadowJar.
1 parent dce25bd commit 6d9c9f3

File tree

65 files changed

+115
-2549
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+115
-2549
lines changed

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

Lines changed: 3 additions & 71 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;
@@ -30,67 +23,6 @@
3023
@SuppressWarnings("unused")
3124
public class Servlet3Advice {
3225

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-
9426
@AssignReturned.ToArguments({
9527
@ToArgument(value = 0, index = 1),
9628
@ToArgument(value = 1, index = 2)
@@ -114,8 +46,8 @@ public static Object[] onEnter(
11446
response =
11547
new Servlet3SnippetInjectingResponseWrapper((HttpServletResponse) response, snippet);
11648
}
117-
AdviceScope adviceScope =
118-
new AdviceScope(
49+
Servlet3RequestAdviceScope adviceScope =
50+
new Servlet3RequestAdviceScope(
11951
CallDepth.forClass(AppServerBridge.getCallDepthKey()),
12052
(HttpServletRequest) request,
12153
(HttpServletResponse) response,
@@ -129,7 +61,7 @@ public static void stopSpan(
12961
@Advice.Argument(1) ServletResponse response,
13062
@Advice.Thrown @Nullable Throwable throwable,
13163
@Advice.Enter Object[] enterResult) {
132-
AdviceScope adviceScope = (AdviceScope) enterResult[0];
64+
Servlet3RequestAdviceScope adviceScope = (Servlet3RequestAdviceScope) enterResult[0];
13365
if (adviceScope == null
13466
|| !(request instanceof HttpServletRequest)
13567
|| !(response instanceof HttpServletResponse)) {
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.instrumentation.servlet.v3_0.copied;
6+
package io.opentelemetry.javaagent.instrumentation.servlet.v3_0;
77

8-
import static io.opentelemetry.instrumentation.servlet.v3_0.copied.Servlet3Singletons.helper;
8+
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.helper;
99

1010
import io.opentelemetry.context.Context;
1111
import io.opentelemetry.context.Scope;
12+
import io.opentelemetry.javaagent.bootstrap.CallDepth;
13+
import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder;
14+
import io.opentelemetry.javaagent.bootstrap.servlet.MappingResolver;
15+
import io.opentelemetry.javaagent.instrumentation.servlet.ServletRequestContext;
1216
import javax.annotation.Nullable;
1317
import javax.servlet.Servlet;
1418
import javax.servlet.http.HttpServletRequest;
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.instrumentation.servlet.v3_0.copied;
6+
package io.opentelemetry.javaagent.instrumentation.servlet.v3_0;
77

8-
import static io.opentelemetry.instrumentation.servlet.v3_0.copied.Servlet3Singletons.responseInstrumenter;
8+
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.responseInstrumenter;
99

1010
import io.opentelemetry.context.Context;
1111
import io.opentelemetry.context.Scope;
1212
import io.opentelemetry.instrumentation.api.incubator.semconv.util.ClassAndMethod;
13+
import io.opentelemetry.javaagent.bootstrap.CallDepth;
14+
import io.opentelemetry.javaagent.instrumentation.servlet.common.response.HttpServletResponseAdviceHelper;
1315
import javax.annotation.Nullable;
1416

1517
public class Servlet3ResponseAdviceScope {

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
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
plugins {
2+
id("otel.library-instrumentation")
3+
}
4+
5+
// These tests are split out so we can unit test against the actual shadowed jar.
6+
7+
dependencies {
8+
testImplementation(project(":instrumentation:servlet:servlet-3.0:library", configuration = "shadow"))
9+
testImplementation(project(":instrumentation:servlet:servlet-3.0:testing"))
10+
11+
testLibrary("org.eclipse.jetty:jetty-server:8.0.0.v20110901")
12+
testLibrary("org.eclipse.jetty:jetty-servlet:8.0.0.v20110901")
13+
testLibrary("org.apache.tomcat.embed:tomcat-embed-core:8.0.41")
14+
testLibrary("org.apache.tomcat.embed:tomcat-embed-jasper:8.0.41")
15+
16+
latestDepTestLibrary("org.eclipse.jetty:jetty-server:10.+") // see servlet-5.0 module
17+
latestDepTestLibrary("org.eclipse.jetty:jetty-servlet:10.+") // see servlet-5.0 module
18+
19+
latestDepTestLibrary("org.apache.tomcat.embed:tomcat-embed-core:9.+") // see servlet-5.0 module
20+
latestDepTestLibrary("org.apache.tomcat.embed:tomcat-embed-jasper:9.+") // see servlet-5.0 module
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+
}
29+
}
30+
31+
// Servlet 3.0 in latest Jetty versions requires Java 11
32+
// However, projects that depend on this module are still be using Java 8 in testLatestDeps mode
33+
// Therefore, we need a separate project for servlet 3.0 tests
34+
val latestDepTest = findProperty("testLatestDeps") as Boolean
35+
36+
if (latestDepTest) {
37+
otelJava {
38+
minJavaVersionSupported.set(JavaVersion.VERSION_11)
39+
}
40+
}

0 commit comments

Comments
 (0)