Skip to content

Commit dbd4510

Browse files
Upgrade Hypertrace Java agent to 1.13.1 level of Open Telemetry agent (#368)
* Upgrade Hypertrace Java agent to 1.13.1 level * Fixed source formatting errors * Fix smoke tests * Fix SpringBootSmokeTest * Attempt to fix muzzle issue * Attempt to circumvent SNYK errors * Attempt to circumvent SNYK error * Create .snyk to ignore SNYK-JAVA-ORGJETBRAINSKOTLIN-2393744 * Circumvention for SNYK error SNYK-JAVA-ORGYAML-2806360 * Fix metric names in SpringBootSmokeTest * Remove missing metric from verification in SpringBootSmokeTest
1 parent 9972f3e commit dbd4510

File tree

29 files changed

+402
-135
lines changed

29 files changed

+402
-135
lines changed

build.gradle.kts

+12-5
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@ subprojects {
3939
extra.set("versions", mapOf(
4040
// when updating these values, some values must also be updated in buildSrc as this map
4141
// cannot be accessed there
42-
"opentelemetry" to "1.11.0",
42+
// 7/23/22 version 1.11.1-alpha is specified for opentelemetry_java_agent_jaxrs
43+
// gradle can't seem to load a more current version than that
44+
"opentelemetry" to "1.13.0",
45+
"opentelemetry_semconv" to "1.13.0-alpha",
4346
"opentelemetry_proto" to "0.11.0-alpha",
44-
"opentelemetry_java_agent" to "1.11.1-alpha",
45-
"opentelemetry_java_agent_all" to "1.11.1",
46-
"opentelemetry_gradle_plugin" to "1.11.1-alpha",
47-
"byte_buddy" to "1.12.6",
47+
"opentelemetry_java_agent" to "1.13.1-alpha",
48+
"opentelemetry_java_agent_all" to "1.13.1",
49+
"opentelemetry_java_agent_jaxrs" to "1.11.1-alpha",
50+
"opentelemetry_java_agent_netty" to "1.13.1-alpha",
51+
"opentelemetry_java_agent-tooling" to "1.13.1-alpha",
52+
53+
"opentelemetry_gradle_plugin" to "1.16.0-alpha",
54+
"byte_buddy" to "1.12.10",
4855
"slf4j" to "1.7.32"
4956
))
5057

buildSrc/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ repositories {
2727
dependencies {
2828
implementation(gradleApi())
2929
implementation(localGroovy())
30-
val otelInstrumentationVersion = "1.11.0-alpha"
30+
val otelInstrumentationVersion = "1.13.0-alpha"
3131
implementation("io.opentelemetry.javaagent:opentelemetry-muzzle:$otelInstrumentationVersion")
3232
implementation("io.opentelemetry.instrumentation.muzzle-generation:io.opentelemetry.instrumentation.muzzle-generation.gradle.plugin:$otelInstrumentationVersion")
3333
implementation("io.opentelemetry.instrumentation.muzzle-check:io.opentelemetry.instrumentation.muzzle-check.gradle.plugin:$otelInstrumentationVersion")
@@ -43,7 +43,7 @@ dependencies {
4343
implementation("net.bytebuddy:byte-buddy-gradle-plugin:1.12.6") {
4444
exclude(group = "net.bytebuddy", module = "byte-buddy")
4545
}
46-
implementation("net.bytebuddy:byte-buddy-dep:1.12.6")
46+
implementation("net.bytebuddy:byte-buddy-dep:1.12.13")
4747

4848
testImplementation("org.spockframework", "spock-core", "1.3-groovy-2.5")
4949
testImplementation("org.codehaus.groovy", "groovy-all", "2.5.8")

instrumentation/apache-httpasyncclient-4.1/build.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ dependencies {
3131
api(project(":instrumentation:apache-httpclient-4.0"))
3232

3333
implementation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-apache-httpasyncclient-4.1:${versions["opentelemetry_java_agent"]}")
34+
testImplementation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-apache-httpclient-4.0:${versions["opentelemetry_java_agent"]}")
35+
testImplementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-semconv:${versions["opentelemetry_semconv"]}")
3436
library("org.apache.httpcomponents:httpasyncclient:4.1")
3537
testImplementation(testFixtures(project(":testing-common")))
3638
}

instrumentation/apache-httpasyncclient-4.1/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/apachehttpasyncclient/ApacheAsyncClientInstrumentationModule.java

-115
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,20 @@
2525
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
2626

2727
import com.google.auto.service.AutoService;
28-
import io.opentelemetry.api.trace.Span;
2928
import io.opentelemetry.context.Context;
3029
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
3130
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
3231
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
3332
import io.opentelemetry.javaagent.instrumentation.apachehttpasyncclient.ApacheHttpAsyncClientInstrumentation.DelegatingRequestProducer;
3433
import io.opentelemetry.javaagent.instrumentation.apachehttpasyncclient.ApacheHttpAsyncClientInstrumentation.WrappedFutureCallback;
35-
import io.opentelemetry.javaagent.instrumentation.hypertrace.apachehttpclient.v4_0.ApacheHttpClientUtils;
36-
import java.io.IOException;
37-
import java.lang.invoke.MethodHandle;
38-
import java.lang.invoke.MethodHandles;
39-
import java.lang.invoke.MethodHandles.Lookup;
40-
import java.lang.invoke.MethodType;
41-
import java.lang.reflect.Field;
4234
import java.util.Collections;
4335
import java.util.List;
4436
import net.bytebuddy.asm.Advice;
4537
import net.bytebuddy.description.type.TypeDescription;
4638
import net.bytebuddy.matcher.ElementMatcher;
47-
import org.apache.http.HttpException;
48-
import org.apache.http.HttpRequest;
49-
import org.apache.http.HttpResponse;
5039
import org.apache.http.concurrent.FutureCallback;
5140
import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
5241
import org.apache.http.protocol.HttpContext;
53-
import org.apache.http.protocol.HttpCoreContext;
54-
import org.slf4j.Logger;
55-
import org.slf4j.LoggerFactory;
5642

5743
@AutoService(InstrumentationModule.class)
5844
public class ApacheAsyncClientInstrumentationModule extends InstrumentationModule {
@@ -121,105 +107,4 @@ public static void enter(
121107
}
122108
}
123109
}
124-
125-
public static class DelegatingCaptureBodyRequestProducer extends DelegatingRequestProducer {
126-
127-
private static final Logger log =
128-
LoggerFactory.getLogger(DelegatingCaptureBodyRequestProducer.class);
129-
130-
private static final Lookup lookup = MethodHandles.lookup();
131-
private static final MethodHandle getContext;
132-
133-
static {
134-
MethodHandle localGetContext;
135-
try {
136-
Field contextField = WrappedFutureCallback.class.getDeclaredField("context");
137-
contextField.setAccessible(true);
138-
MethodHandle unReflectedField = lookup.unreflectGetter(contextField);
139-
localGetContext =
140-
unReflectedField.asType(
141-
MethodType.methodType(Context.class, WrappedFutureCallback.class));
142-
} catch (NoSuchFieldException | IllegalAccessException e) {
143-
log.debug("Could not find context field on super class", e);
144-
localGetContext = null;
145-
}
146-
getContext = localGetContext;
147-
}
148-
149-
private final WrappedFutureCallback<?> wrappedFutureCallback;
150-
private final BodyCaptureDelegatingCallback<?> bodyCaptureDelegatingCallback;
151-
152-
public DelegatingCaptureBodyRequestProducer(
153-
Context parentContext,
154-
HttpAsyncRequestProducer delegate,
155-
WrappedFutureCallback<?> wrappedFutureCallback,
156-
BodyCaptureDelegatingCallback<?> bodyCaptureDelegatingCallback) {
157-
super(parentContext, delegate, wrappedFutureCallback);
158-
this.wrappedFutureCallback = wrappedFutureCallback;
159-
this.bodyCaptureDelegatingCallback = bodyCaptureDelegatingCallback;
160-
}
161-
162-
@Override
163-
public HttpRequest generateRequest() throws IOException, HttpException {
164-
HttpRequest request = super.generateRequest();
165-
try {
166-
/*
167-
* Ideally, we should not rely on the getContext MethodHandle here. The client context isn't
168-
* generated until super.generateRequest is called so ideally we should architect our code
169-
* such that we can access the client span more idiomatically after it is created, like via
170-
* the Java8BytecodeBridge.currentSpan call.
171-
*/
172-
Object getContextResult = getContext.invoke(wrappedFutureCallback);
173-
if (getContextResult instanceof Context) {
174-
Context context = (Context) getContextResult;
175-
Span clientSpan = Span.fromContextOrNull(context);
176-
bodyCaptureDelegatingCallback.clientContext = context;
177-
ApacheHttpClientUtils.traceRequest(clientSpan, request);
178-
}
179-
} catch (Throwable t) {
180-
log.debug("Could not access context field on super class", t);
181-
}
182-
return request;
183-
}
184-
}
185-
186-
public static class BodyCaptureDelegatingCallback<T> implements FutureCallback<T> {
187-
188-
final Context context;
189-
final HttpContext httpContext;
190-
private final FutureCallback<T> delegate;
191-
private volatile Context clientContext;
192-
193-
public BodyCaptureDelegatingCallback(
194-
Context context, HttpContext httpContext, FutureCallback<T> delegate) {
195-
this.context = context;
196-
this.httpContext = httpContext;
197-
this.delegate = delegate;
198-
}
199-
200-
@Override
201-
public void completed(T result) {
202-
HttpResponse httpResponse = getResponse(httpContext);
203-
ApacheHttpClientUtils.traceResponse(Span.fromContext(clientContext), httpResponse);
204-
delegate.completed(result);
205-
}
206-
207-
@Override
208-
public void failed(Exception ex) {
209-
HttpResponse httpResponse = getResponse(httpContext);
210-
ApacheHttpClientUtils.traceResponse(Span.fromContext(clientContext), httpResponse);
211-
delegate.failed(ex);
212-
}
213-
214-
@Override
215-
public void cancelled() {
216-
HttpResponse httpResponse = getResponse(httpContext);
217-
ApacheHttpClientUtils.traceResponse(Span.fromContext(clientContext), httpResponse);
218-
delegate.cancelled();
219-
}
220-
221-
private static HttpResponse getResponse(HttpContext context) {
222-
return (HttpResponse) context.getAttribute(HttpCoreContext.HTTP_RESPONSE);
223-
}
224-
}
225110
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright The Hypertrace Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opentelemetry.javaagent.instrumentation.hypertrace.apachehttpasyncclient;
18+
19+
import io.opentelemetry.api.trace.Span;
20+
import io.opentelemetry.context.Context;
21+
import io.opentelemetry.javaagent.instrumentation.hypertrace.apachehttpclient.v4_0.ApacheHttpClientUtils;
22+
import org.apache.http.HttpResponse;
23+
import org.apache.http.concurrent.FutureCallback;
24+
import org.apache.http.protocol.HttpContext;
25+
import org.apache.http.protocol.HttpCoreContext;
26+
27+
public class BodyCaptureDelegatingCallback<T> implements FutureCallback<T> {
28+
29+
final Context context;
30+
final HttpContext httpContext;
31+
private final FutureCallback<T> delegate;
32+
private volatile Context clientContext;
33+
34+
public BodyCaptureDelegatingCallback(
35+
Context context, HttpContext httpContext, FutureCallback<T> delegate) {
36+
this.context = context;
37+
this.httpContext = httpContext;
38+
this.delegate = delegate;
39+
}
40+
41+
@Override
42+
public void completed(T result) {
43+
HttpResponse httpResponse = getResponse(httpContext);
44+
ApacheHttpClientUtils.traceResponse(Span.fromContext(clientContext), httpResponse);
45+
delegate.completed(result);
46+
}
47+
48+
@Override
49+
public void failed(Exception ex) {
50+
HttpResponse httpResponse = getResponse(httpContext);
51+
ApacheHttpClientUtils.traceResponse(Span.fromContext(clientContext), httpResponse);
52+
delegate.failed(ex);
53+
}
54+
55+
@Override
56+
public void cancelled() {
57+
HttpResponse httpResponse = getResponse(httpContext);
58+
ApacheHttpClientUtils.traceResponse(Span.fromContext(clientContext), httpResponse);
59+
delegate.cancelled();
60+
}
61+
62+
private static HttpResponse getResponse(HttpContext context) {
63+
return (HttpResponse) context.getAttribute(HttpCoreContext.HTTP_RESPONSE);
64+
}
65+
66+
public void setClientContext(Context clientContext) {
67+
this.clientContext = clientContext;
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright The Hypertrace Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opentelemetry.javaagent.instrumentation.hypertrace.apachehttpasyncclient;
18+
19+
import io.opentelemetry.api.trace.Span;
20+
import io.opentelemetry.context.Context;
21+
import io.opentelemetry.javaagent.instrumentation.apachehttpasyncclient.ApacheHttpAsyncClientInstrumentation;
22+
import io.opentelemetry.javaagent.instrumentation.hypertrace.apachehttpclient.v4_0.ApacheHttpClientUtils;
23+
import java.io.IOException;
24+
import java.lang.invoke.MethodHandle;
25+
import java.lang.invoke.MethodHandles;
26+
import java.lang.invoke.MethodType;
27+
import java.lang.reflect.Field;
28+
import org.apache.http.HttpException;
29+
import org.apache.http.HttpRequest;
30+
import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
33+
34+
public class DelegatingCaptureBodyRequestProducer
35+
extends ApacheHttpAsyncClientInstrumentation.DelegatingRequestProducer {
36+
37+
private static final Logger log =
38+
LoggerFactory.getLogger(DelegatingCaptureBodyRequestProducer.class);
39+
40+
private static final MethodHandles.Lookup lookup = MethodHandles.lookup();
41+
private static final MethodHandle getContext;
42+
43+
static {
44+
MethodHandle localGetContext;
45+
try {
46+
Field contextField =
47+
ApacheHttpAsyncClientInstrumentation.WrappedFutureCallback.class.getDeclaredField(
48+
"context");
49+
contextField.setAccessible(true);
50+
MethodHandle unReflectedField = lookup.unreflectGetter(contextField);
51+
localGetContext =
52+
unReflectedField.asType(
53+
MethodType.methodType(
54+
Context.class, ApacheHttpAsyncClientInstrumentation.WrappedFutureCallback.class));
55+
} catch (NoSuchFieldException | IllegalAccessException e) {
56+
log.debug("Could not find context field on super class", e);
57+
localGetContext = null;
58+
}
59+
getContext = localGetContext;
60+
}
61+
62+
private final ApacheHttpAsyncClientInstrumentation.WrappedFutureCallback<?> wrappedFutureCallback;
63+
private final BodyCaptureDelegatingCallback<?> bodyCaptureDelegatingCallback;
64+
65+
public DelegatingCaptureBodyRequestProducer(
66+
Context parentContext,
67+
HttpAsyncRequestProducer delegate,
68+
ApacheHttpAsyncClientInstrumentation.WrappedFutureCallback<?> wrappedFutureCallback,
69+
BodyCaptureDelegatingCallback<?> bodyCaptureDelegatingCallback) {
70+
super(parentContext, delegate, wrappedFutureCallback);
71+
this.wrappedFutureCallback = wrappedFutureCallback;
72+
this.bodyCaptureDelegatingCallback = bodyCaptureDelegatingCallback;
73+
}
74+
75+
@Override
76+
public HttpRequest generateRequest() throws IOException, HttpException {
77+
HttpRequest request = super.generateRequest();
78+
try {
79+
/*
80+
* Ideally, we should not rely on the getContext MethodHandle here. The client context isn't
81+
* generated until super.generateRequest is called so ideally we should architect our code
82+
* such that we can access the client span more idiomatically after it is created, like via
83+
* the Java8BytecodeBridge.currentSpan call.
84+
*/
85+
Object getContextResult = getContext.invoke(wrappedFutureCallback);
86+
if (getContextResult instanceof Context) {
87+
Context context = (Context) getContextResult;
88+
Span clientSpan = Span.fromContextOrNull(context);
89+
bodyCaptureDelegatingCallback.setClientContext(context);
90+
ApacheHttpClientUtils.traceRequest(clientSpan, request);
91+
}
92+
} catch (Throwable t) {
93+
log.debug("Could not access context field on super class", t);
94+
}
95+
return request;
96+
}
97+
}

instrumentation/apache-httpclient-4.0/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ dependencies {
4444
library("org.apache.httpcomponents:httpclient:4.0")
4545

4646
testImplementation(testFixtures(project(":testing-common")))
47+
testImplementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-semconv:${versions["opentelemetry_semconv"]}")
4748
}

instrumentation/apache-httpclient-4.0/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/apachehttpclient/v4_0/ApacheHttpClientObjectRegistry.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import io.opentelemetry.api.common.AttributeKey;
2020
import io.opentelemetry.api.trace.Span;
21-
import io.opentelemetry.instrumentation.api.cache.Cache;
21+
import io.opentelemetry.instrumentation.api.internal.cache.Cache;
2222
import org.apache.http.HttpEntity;
2323

2424
public class ApacheHttpClientObjectRegistry {

instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/NettyHttp2HeadersInstrumentationModule.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,18 @@
4242
public class NettyHttp2HeadersInstrumentationModule extends InstrumentationModule {
4343

4444
private static final List<String> INSTRUMENTATION_NAMES = new ArrayList<>();
45+
private static final String[] INSTRUMENTATION_NAME_ARRAY;
4546

4647
static {
4748
INSTRUMENTATION_NAMES.add(GrpcInstrumentationName.PRIMARY);
4849
INSTRUMENTATION_NAMES.addAll(Arrays.asList(GrpcInstrumentationName.OTHER));
4950
INSTRUMENTATION_NAMES.add("grpc-netty-ht");
51+
INSTRUMENTATION_NAME_ARRAY =
52+
INSTRUMENTATION_NAMES.toArray(new String[INSTRUMENTATION_NAMES.size()]);
5053
}
5154

5255
public NettyHttp2HeadersInstrumentationModule() {
53-
super(INSTRUMENTATION_NAMES);
56+
super(GrpcInstrumentationName.PRIMARY, INSTRUMENTATION_NAME_ARRAY);
5457
}
5558

5659
@Override

0 commit comments

Comments
 (0)