-
Notifications
You must be signed in to change notification settings - Fork 14
Add servlet 2.2 instrumentation #376
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
Open
kaushal02
wants to merge
7
commits into
main
Choose a base branch
from
kaushal02/instrumentation-servlet-2.2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
554d90f
Add servlet 2.2 instrumentation
kaushal02 4a71a2d
remove old warnings and add httpServletResponseInstrumentation
kaushal02 d7eecf6
merge instrumentation files
kaushal02 20edca1
remove comments and move from draft to ready
kaushal02 d0bd73e
PR comments
kaushal02 16a5ca3
fixed muzzle error, added tests WIP
kaushal02 4aaada5
fix jetty version
kaushal02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
plugins { | ||
`java-library` | ||
id("net.bytebuddy.byte-buddy") | ||
id("io.opentelemetry.instrumentation.auto-instrumentation") | ||
muzzle | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group = "javax.servlet" | ||
module = "servlet-api" | ||
versions = "[2.2,3.0)" | ||
} | ||
fail { | ||
group = "javax.servlet" | ||
module = "javax.servlet-api" | ||
versions = "(,)" | ||
} | ||
} | ||
|
||
afterEvaluate{ | ||
io.opentelemetry.instrumentation.gradle.bytebuddy.ByteBuddyPluginConfigurator( | ||
project, | ||
sourceSets.main.get(), | ||
io.opentelemetry.javaagent.tooling.muzzle.generation.MuzzleCodeGenerationPlugin::class.java.name, | ||
project(":javaagent-tooling").configurations["instrumentationMuzzle"] + configurations.runtimeClasspath | ||
).configure() | ||
} | ||
|
||
val versions: Map<String, String> by extra | ||
|
||
dependencies { | ||
implementation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-servlet-common:${versions["opentelemetry_java_agent"]}") | ||
implementation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-servlet-2.2:${versions["opentelemetry_java_agent"]}") // Servlet2Accessor | ||
testImplementation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-servlet-2.2:${versions["opentelemetry_java_agent"]}") | ||
testImplementation("io.opentelemetry.javaagent:opentelemetry-muzzle:${versions["opentelemetry_java_agent"]}") | ||
compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-bootstrap:${versions["opentelemetry_java_agent"]}") | ||
compileOnly("javax.servlet:servlet-api:2.2") | ||
testRuntimeOnly("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-servlet-common-bootstrap:${versions["opentelemetry_java_agent"]}") | ||
muzzleBootstrap("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-servlet-common-bootstrap:${versions["opentelemetry_java_agent"]}") | ||
|
||
testImplementation(project(":instrumentation:servlet:servlet-rw")) | ||
testImplementation(testFixtures(project(":testing-common")) as ProjectDependency) { | ||
exclude(group = "org.eclipse.jetty", module = "jetty-server") | ||
} | ||
testImplementation("org.eclipse.jetty:jetty-server:7.0.0.v20091005") | ||
testImplementation("org.eclipse.jetty:jetty-servlet:7.0.0.v20091005") | ||
testImplementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-semconv:${versions["opentelemetry_semconv"]}") | ||
} |
216 changes: 216 additions & 0 deletions
216
.../instrumentation/hypertrace/servlet/v2_2/nowrapping/Servlet2AndFilterInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
/* | ||
* Copyright The Hypertrace Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.hypertrace.servlet.v2_2.nowrapping; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasSuperType; | ||
import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.instrumentation.api.field.VirtualField; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; | ||
import java.io.BufferedReader; | ||
import java.io.PrintWriter; | ||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.servlet.ServletInputStream; | ||
import javax.servlet.ServletOutputStream; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.hypertrace.agent.core.config.InstrumentationConfig; | ||
import org.hypertrace.agent.core.instrumentation.HypertraceCallDepthThreadLocalMap; | ||
import org.hypertrace.agent.core.instrumentation.HypertraceEvaluationException; | ||
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes; | ||
import org.hypertrace.agent.core.instrumentation.SpanAndObjectPair; | ||
import org.hypertrace.agent.core.instrumentation.buffer.*; | ||
import org.hypertrace.agent.core.instrumentation.utils.ContentTypeUtils; | ||
import org.hypertrace.agent.filter.FilterRegistry; | ||
|
||
public class Servlet2AndFilterInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<ClassLoader> classLoaderOptimization() { | ||
return hasClassesNamed("javax.servlet.Servlet"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return hasSuperType(namedOneOf("javax.servlet.Filter", "javax.servlet.Servlet")); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
namedOneOf("doFilter", "service") | ||
.and(takesArgument(0, named("javax.servlet.ServletRequest"))) | ||
.and(takesArgument(1, named("javax.servlet.ServletResponse"))) | ||
.and(isPublic()), | ||
Servlet2AndFilterInstrumentation.class.getName() + "$ServletAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ServletAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class, skipOn = Advice.OnNonDefaultValue.class) | ||
public static void start( | ||
@Advice.Argument(value = 0) ServletRequest request, | ||
@Advice.Argument(value = 1) ServletResponse response, | ||
@Advice.Local("currentSpan") Span currentSpan) { | ||
int callDepth = | ||
HypertraceCallDepthThreadLocalMap.incrementCallDepth(Servlet2InstrumentationName.class); | ||
if (callDepth > 0) { | ||
return; | ||
} | ||
if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) { | ||
return; | ||
} | ||
|
||
HttpServletRequest httpRequest = (HttpServletRequest) request; | ||
HttpServletResponse httpResponse = (HttpServletResponse) response; | ||
currentSpan = Java8BytecodeBridge.currentSpan(); | ||
|
||
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); | ||
|
||
Utils.addSessionId(currentSpan, httpRequest); | ||
|
||
// set request headers | ||
Enumeration<?> headerNames = httpRequest.getHeaderNames(); | ||
Map<String, String> headers = new HashMap<>(); | ||
while (headerNames.hasMoreElements()) { | ||
String headerName; | ||
try { | ||
headerName = (String) headerNames.nextElement(); | ||
} catch (Exception e) { | ||
continue; | ||
} | ||
String headerValue = httpRequest.getHeader(headerName); | ||
AttributeKey<String> attributeKey = | ||
HypertraceSemanticAttributes.httpRequestHeader(headerName); | ||
|
||
if (instrumentationConfig.httpHeaders().request()) { | ||
currentSpan.setAttribute(attributeKey, headerValue); | ||
} | ||
headers.put(attributeKey.getKey(), headerValue); | ||
} | ||
|
||
if (FilterRegistry.getFilter().evaluateRequestHeaders(currentSpan, headers)) { | ||
httpResponse.setStatus(403); | ||
// skip execution of the user code | ||
return; | ||
} | ||
|
||
if (instrumentationConfig.httpBody().request() | ||
&& ContentTypeUtils.shouldCapture(httpRequest.getContentType())) { | ||
// The HttpServletRequest instrumentation uses this to | ||
// enable the instrumentation | ||
VirtualField.find(HttpServletRequest.class, SpanAndObjectPair.class) | ||
.set( | ||
httpRequest, | ||
new SpanAndObjectPair(currentSpan, Collections.unmodifiableMap(headers))); | ||
} | ||
return; | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void exit( | ||
@Advice.Argument(0) ServletRequest request, | ||
@Advice.Argument(1) ServletResponse response, | ||
@Advice.Thrown(readOnly = false) Throwable throwable, | ||
@Advice.Local("currentSpan") Span currentSpan) { | ||
int callDepth = | ||
HypertraceCallDepthThreadLocalMap.decrementCallDepth(Servlet2InstrumentationName.class); | ||
if (callDepth > 0) { | ||
return; | ||
} | ||
// we are in the most outermost level of Servlet instrumentation | ||
|
||
if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) { | ||
return; | ||
} | ||
|
||
HttpServletResponse httpResponse = (HttpServletResponse) response; | ||
HttpServletRequest httpRequest = (HttpServletRequest) request; | ||
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); | ||
|
||
try { | ||
// response context to capture body and clear the context | ||
VirtualField<HttpServletResponse, SpanAndObjectPair> responseContextStore = | ||
VirtualField.find(HttpServletResponse.class, SpanAndObjectPair.class); | ||
VirtualField<ServletOutputStream, BoundedByteArrayOutputStream> outputStreamContextStore = | ||
VirtualField.find(ServletOutputStream.class, BoundedByteArrayOutputStream.class); | ||
VirtualField<PrintWriter, BoundedCharArrayWriter> writerContextStore = | ||
VirtualField.find(PrintWriter.class, BoundedCharArrayWriter.class); | ||
|
||
// request context to clear body buffer | ||
VirtualField<HttpServletRequest, SpanAndObjectPair> requestContextStore = | ||
VirtualField.find(HttpServletRequest.class, SpanAndObjectPair.class); | ||
VirtualField<ServletInputStream, ByteBufferSpanPair> inputStreamContextStore = | ||
VirtualField.find(ServletInputStream.class, ByteBufferSpanPair.class); | ||
VirtualField<BufferedReader, CharBufferSpanPair> readerContextStore = | ||
VirtualField.find(BufferedReader.class, CharBufferSpanPair.class); | ||
VirtualField<HttpServletRequest, StringMapSpanPair> urlEncodedMapContextStore = | ||
VirtualField.find(HttpServletRequest.class, StringMapSpanPair.class); | ||
|
||
// capture response body | ||
// FIXME: capture body based on response content type | ||
if (instrumentationConfig.httpBody().response()) { | ||
Utils.captureResponseBody( | ||
currentSpan, | ||
httpResponse, | ||
responseContextStore, | ||
outputStreamContextStore, | ||
writerContextStore); | ||
} | ||
|
||
// remove request body buffers from context stores, otherwise they might get reused | ||
if (instrumentationConfig.httpBody().request() | ||
&& ContentTypeUtils.shouldCapture(httpRequest.getContentType())) { | ||
Utils.resetRequestBodyBuffers( | ||
httpRequest, | ||
requestContextStore, | ||
inputStreamContextStore, | ||
readerContextStore, | ||
urlEncodedMapContextStore); | ||
} | ||
} finally { | ||
Throwable tmp = throwable; | ||
while (tmp != null) { // loop in case our exception is nested (eg. springframework) | ||
if (tmp instanceof HypertraceEvaluationException) { | ||
httpResponse.setStatus(403); | ||
// bytebuddy treats the reassignment of this variable to null as an instruction to | ||
// suppress this exception, which is what we want | ||
throwable = null; | ||
break; | ||
} | ||
tmp = tmp.getCause(); | ||
} | ||
} | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...ent/instrumentation/hypertrace/servlet/v2_2/nowrapping/Servlet2InstrumentationModule.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright The Hypertrace Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.hypertrace.servlet.v2_2.nowrapping; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
import static net.bytebuddy.matcher.ElementMatchers.not; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.instrumentation.hypertrace.servlet.v2_2.nowrapping.request.ServletInputStreamInstrumentation; | ||
import io.opentelemetry.javaagent.instrumentation.hypertrace.servlet.v2_2.nowrapping.request.ServletRequestInstrumentation; | ||
import io.opentelemetry.javaagent.instrumentation.hypertrace.servlet.v2_2.nowrapping.response.ServletOutputStreamInstrumentation; | ||
import io.opentelemetry.javaagent.instrumentation.hypertrace.servlet.v2_2.nowrapping.response.ServletResponseInstrumentation; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
@SuppressWarnings("unused") | ||
@AutoService(InstrumentationModule.class) | ||
public class Servlet2InstrumentationModule extends InstrumentationModule { | ||
|
||
public Servlet2InstrumentationModule() { | ||
super(Servlet2InstrumentationName.PRIMARY, Servlet2InstrumentationName.OTHER); | ||
} | ||
|
||
@Override | ||
public int order() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { | ||
return not(hasClassesNamed("javax.servlet.HttpConstraintElement")); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return Arrays.asList( | ||
new Servlet2AndFilterInstrumentation(), | ||
new ServletRequestInstrumentation(), | ||
new ServletInputStreamInstrumentation(), | ||
new ServletResponseInstrumentation(), | ||
new ServletOutputStreamInstrumentation()); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...agent/instrumentation/hypertrace/servlet/v2_2/nowrapping/Servlet2InstrumentationName.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright The Hypertrace Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.hypertrace.servlet.v2_2.nowrapping; | ||
|
||
public class Servlet2InstrumentationName { | ||
public static final String PRIMARY = "servlet"; | ||
public static final String[] OTHER = { | ||
"servlet-2", "ht", "servlet-ht", "servlet-2-ht", "servlet-2-no-wrapping", "servlet-no-wrapping" | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.