Skip to content

Instrument vertx client directly #311

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 8 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ List of supported frameworks with additional capabilities:
| [Servlet](https://javaee.github.io/javaee-spec/javadocs/javax/servlet/package-summary.html) | 2.3+ |
| [Spark Web Framework](https://github.com/perwendel/spark) | 2.3+ |
| [Spring Webflux](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/reactive/package-summary.html) | 5.0+ |
| [Vert.x](https://vertx.io) | 3.0+ |
| [Vert.x](https://vertx.io) | 3.0+ (4 not supported yet) |
| [Struts](https://struts.apache.org/) | 2.3+ |

### Adding custom filter implementation
Expand Down
15 changes: 12 additions & 3 deletions instrumentation/vertx-web-3.0/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
`java-library`
id("net.bytebuddy.byte-buddy")
id("io.opentelemetry.instrumentation.auto-instrumentation")
muzzle
}

afterEvaluate{
Expand All @@ -12,17 +13,25 @@ afterEvaluate{
).configure()
}

muzzle {
pass {
group = "io.vertx"
module = "vertx-web"
versions = "[3.0.0,4.0.0)"
}
}

val versions: Map<String, String> by extra
// version used by io.vertx:vertx-web:3.0.0
val nettyVersion = "4.0.28.Final"

dependencies {
implementation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-vertx-web-3.0:${versions["opentelemetry_java_agent"]}")
implementation("io.vertx:vertx-web:3.0.0")

testImplementation(project(":testing-common"))
testImplementation(project(":instrumentation:netty:netty-4.0"))
testImplementation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-netty-4.0:${versions["opentelemetry_java_agent"]}")
testImplementation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-vertx-web-3.0:${versions["opentelemetry_java_agent"]}")
testImplementation("io.vertx:vertx-web:3.0.0")


testImplementation("io.netty:netty-codec-http:${nettyVersion}") {
version {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.vertx;

import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.ClassLoaderMatcher.hasClassesNamed;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext;
import io.opentelemetry.javaagent.instrumentation.vertx.client.Contexts;
import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import java.util.HashMap;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.hypertrace.agent.core.config.HypertraceConfig;
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
import org.hypertrace.agent.core.instrumentation.utils.ContentTypeUtils;

public class HttpRequestHandleInstrumentation implements TypeInstrumentation {

@Override
public ElementMatcher<ClassLoader> classLoaderOptimization() {
return hasClassesNamed("io.vertx.core.http.HttpClientRequest");
}

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return implementsInterface(named("io.vertx.core.http.HttpClientRequest"));
}

@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
Map<ElementMatcher<? super MethodDescription>, String> transformers = new HashMap<>();

// capture response data
transformers.put(
isMethod().and(named("handleResponse")),
HttpRequestHandleInstrumentation.class.getName() + "$HandleResponseAdvice");
return transformers;
}

public static class HandleResponseAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void handleResponseEnter(
@Advice.This HttpClientRequest request, @Advice.Argument(0) HttpClientResponse response) {

Contexts contexts =
InstrumentationContext.get(HttpClientRequest.class, Contexts.class).get(request);
if (contexts == null) {
return;
}
Span span = Span.fromContext(contexts.context);

if (HypertraceConfig.get().getDataCapture().getHttpHeaders().getRequest().getValue()) {
for (Map.Entry<String, String> entry : request.headers()) {
span.setAttribute(
HypertraceSemanticAttributes.httpRequestHeader(entry.getKey()), entry.getValue());
}
}

if (HypertraceConfig.get().getDataCapture().getHttpHeaders().getResponse().getValue()) {
for (Map.Entry<String, String> entry : response.headers()) {
span.setAttribute(
HypertraceSemanticAttributes.httpResponseHeader(entry.getKey()), entry.getValue());
}
}

String contentType = response.getHeader("Content-Type");
if (HypertraceConfig.get().getDataCapture().getHttpBody().getResponse().getValue()
&& ContentTypeUtils.shouldCapture(contentType)) {
InstrumentationContext.get(HttpClientResponse.class, Span.class).put(response, span);
}
}
}
}
Loading