Skip to content
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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ and adds following capabilities:
List of supported frameworks with additional capabilities:
| Library/Framework | Versions |
|--------------------------------------------------------------------------------------------------------|-----------------|
| [Apache HttpClient](https://hc.apache.org/index.html) | 4.0+ |
| [gRPC](https://github.com/grpc/grpc-java) | 1.5+ to [1.32.0](https://github.com/hypertrace/javaagent/issues/70) |
| [JAX-RS Client](https://javaee.github.io/javaee-spec/javadocs/javax/ws/rs/client/package-summary.html) | 2.0+ |
| [OkHttp](https://github.com/square/okhttp/) | 3.0+ |
| [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+ |
| [gRPC](https://github.com/grpc/grpc-java) | 1.5+ |
| [OkHttp](https://github.com/square/okhttp/) | 3.0+ |

### Adding custom filter implementation

Expand Down Expand Up @@ -57,7 +59,7 @@ The following instrumentation names disable only Hypertrace instrumentations, no
* `ht` - all Hypertrace instrumentations
* `servlet-ht` - Servlet, Spark Web
* `okhttp-ht` - Okhttp
* `grpc-ht` - Okhttp
* `grpc-ht` - gRPC

The Hypertrace instrumentations use also the core OpenTelemetry instrumentation names so for example
`-Dotel.instrumentation.servlet.enabled=false` disables all servlet instrumentations including core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.nio.charset.Charset;
import org.hypertrace.agent.core.GlobalObjectRegistry;
import org.hypertrace.agent.core.GlobalObjectRegistry.SpanAndBuffer;
import org.hypertrace.agent.core.HypertraceSemanticAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -50,7 +51,7 @@ public static void addAttribute(Span span, AttributeKey<String> attributeKey, St
span.setAttribute(attributeKey, value);
} else {
TRACER
.spanBuilder("additional-data")
.spanBuilder(HypertraceSemanticAttributes.ADDITIONAL_DATA_SPAN_NAME)
.setParent(Context.root().with(span))
.setAttribute(attributeKey, value)
.startSpan()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public void getJson() throws IOException, TimeoutException, InterruptedException
@Test
public void postUrlEncoded() throws IOException, TimeoutException, InterruptedException {
List<NameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair("code", "22"));
nvps.add(new BasicNameValuePair("key1", "value1"));
nvps.add(new BasicNameValuePair("key2", "value2"));

HttpPost postRequest = new HttpPost();
postRequest.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
Expand All @@ -126,7 +127,8 @@ public void postUrlEncoded() throws IOException, TimeoutException, InterruptedEx
.getAttributes()
.get(HypertraceSemanticAttributes.httpResponseHeader("test-response-header")));
Assertions.assertEquals(
"code=22", clientSpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY));
"key1=value1&key2=value2",
clientSpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY));
Assertions.assertNull(
clientSpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY));
}
Expand Down
1 change: 1 addition & 0 deletions instrumentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies{
implementation(project(":instrumentation:grpc-1.5"))
implementation(project(":instrumentation:okhttp:okhttp-3.0"))
implementation(project(":instrumentation:apache-httpclient-4.0"))
implementation(project(":instrumentation:jaxrs-client-2.0"))
implementation(project(":otel-extensions"))
}

Expand Down
41 changes: 41 additions & 0 deletions instrumentation/jaxrs-client-2.0/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
plugins {
`java-library`
id("net.bytebuddy.byte-buddy")
id("io.opentelemetry.instrumentation.auto-instrumentation")
muzzle
}

muzzle {
pass {
group = "javax.ws.rs"
module = "javax.ws.rs-api"
versions = "[2.0,)"
}
pass {
// We want to support the dropwizard clients too.
group = "io.dropwizard"
module = "dropwizard-client"
versions = "[0.8.0,)"
assertInverse = true
}
}

afterEvaluate{
io.opentelemetry.instrumentation.gradle.bytebuddy.ByteBuddyPluginConfigurator(project,
sourceSets.main.get(),
"io.opentelemetry.javaagent.tooling.muzzle.collector.MuzzleCodeGenerationPlugin",
project(":javaagent-tooling").configurations["instrumentationMuzzle"] + configurations.runtimeClasspath
).configure()
}

val versions: Map<String, String> by extra

dependencies {
api("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-jaxrs-client-2.0-common:${versions["opentelemetry_java_agent"]}")

compileOnly("javax.ws.rs:javax.ws.rs-api:2.0.1")

testImplementation(project(":testing-common"))
testImplementation("org.glassfish.jersey.core:jersey-client:2.27")
testImplementation("org.glassfish.jersey.inject:jersey-hk2:2.27")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* 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.instrumentation.hypertrace.jaxrs.v2_0;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.javaagent.instrumentation.jaxrsclient.v2_0.ClientTracingFilter;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.client.ClientResponseContext;
import javax.ws.rs.client.ClientResponseFilter;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import org.hypertrace.agent.config.Config.AgentConfig;
import org.hypertrace.agent.core.ContentTypeUtils;
import org.hypertrace.agent.core.HypertraceConfig;
import org.hypertrace.agent.core.HypertraceSemanticAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JaxrsClientBodyCaptureFilter implements ClientRequestFilter, ClientResponseFilter {

private static final Logger log = LoggerFactory.getLogger(JaxrsClientBodyCaptureFilter.class);

@Override
public void filter(ClientRequestContext requestContext) {
Object spanObj = requestContext.getProperty(ClientTracingFilter.SPAN_PROPERTY_NAME);
if (!(spanObj instanceof Span)) {
return;
}

Span currentSpan = (Span) spanObj;
AgentConfig agentConfig = HypertraceConfig.get();

try {
if (agentConfig.getDataCapture().getHttpHeaders().getRequest().getValue()) {
captureHeaders(
currentSpan,
HypertraceSemanticAttributes::httpRequestHeader,
requestContext.getStringHeaders());
}
if (requestContext.hasEntity()
&& agentConfig.getDataCapture().getHttpBody().getRequest().getValue()) {
MediaType mediaType = requestContext.getMediaType();
if (mediaType == null || !ContentTypeUtils.shouldCapture(mediaType.toString())) {
return;
}

Object entity = requestContext.getEntity();
if (entity != null) {
if (entity instanceof Form) {
Form form = (Form) entity;
String content = getUrlEncodedContent(form);
currentSpan.setAttribute(HypertraceSemanticAttributes.HTTP_REQUEST_BODY, content);
} else {
currentSpan.setAttribute(
HypertraceSemanticAttributes.HTTP_REQUEST_BODY, entity.toString());
}
}
}
requestContext.getEntity();
} catch (Exception ex) {
log.error("Exception while getting request entity or headers", ex);
}
}

@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) {
Object spanObj = requestContext.getProperty(ClientTracingFilter.SPAN_PROPERTY_NAME);
if (!(spanObj instanceof Span)) {
return;
}

Span currentSpan = (Span) spanObj;
AgentConfig agentConfig = HypertraceConfig.get();

try {
if (agentConfig.getDataCapture().getHttpHeaders().getResponse().getValue()) {
captureHeaders(
currentSpan,
HypertraceSemanticAttributes::httpResponseHeader,
responseContext.getHeaders());
}
} catch (Exception ex) {
log.error("Exception while getting response entity or headers", ex);
}
}

private static String getUrlEncodedContent(Form form) {
MultivaluedMap<String, String> formMap = form.asMap();
StringBuilder sb = new StringBuilder();
if (formMap != null) {
for (Map.Entry<String, List<String>> entry : formMap.entrySet()) {
if (sb.length() > 0) {
sb.append("&");
}
for (String value : entry.getValue()) {
sb.append(entry.getKey());
sb.append("=");
sb.append(value);
}
}
}
return sb.toString();
}

private static void captureHeaders(
Span span,
Function<String, AttributeKey<String>> keySupplier,
MultivaluedMap<String, String> headers) {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
for (Object value : entry.getValue()) {
span.setAttribute(keySupplier.apply(entry.getKey()), value.toString());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.instrumentation.hypertrace.jaxrs.v2_0;

import static io.opentelemetry.javaagent.tooling.ClassLoaderMatcher.hasClassesNamed;
import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.extendsClass;
import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.hasInterface;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.returns;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.tooling.InstrumentationModule;
import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.ws.rs.client.Client;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.bytecode.assign.Assigner;
import net.bytebuddy.matcher.ElementMatcher;

@AutoService(InstrumentationModule.class)
public class JaxrsClientBodyInstrumentationModule extends InstrumentationModule {

@Override
public int getOrder() {
return 1;
}

public JaxrsClientBodyInstrumentationModule() {
super(JaxrsClientBodyInstrumentationName.PRIMARY, JaxrsClientBodyInstrumentationName.OTHER);
}

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return Collections.singletonList(new JaxrsClientBuilderInstrumentation());
}

class JaxrsClientBuilderInstrumentation implements TypeInstrumentation {

@Override
public ElementMatcher<ClassLoader> classLoaderOptimization() {
return hasClassesNamed("javax.ws.rs.client.ClientBuilder");
}

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return extendsClass(named("javax.ws.rs.client.ClientBuilder"));
}

@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(
named("build").and(returns(hasInterface(named("javax.ws.rs.client.Client")))),
ClientBuilder_build_Advice.class.getName());
}
}

static class ClientBuilder_build_Advice {
@Advice.OnMethodExit
public static void registerFeature(
@Advice.Return(typing = Assigner.Typing.DYNAMIC) Client client) {
// Register on the generated client instead of the builder
// The build() can be called multiple times and is not thread safe
// A client is only created once
// Use lowest priority to run after OTEL filter that controls lifecycle of span
client.register(JaxrsClientBodyCaptureFilter.class, Integer.MIN_VALUE);
client.register(JaxrsClientEntityInterceptor.class);
}
}
}
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.instrumentation.hypertrace.jaxrs.v2_0;

public class JaxrsClientBodyInstrumentationName {
public static final String PRIMARY = "jaxrs-client";
public static final String[] OTHER = {
"jaxrs-client-2.0", "ht", "jaxrs-client-ht", "jaxrs-client-2.0-ht"
};
}
Loading