Skip to content

Commit 34ea693

Browse files
authored
Use data capture config in instrumentations (#101)
* Polish config and use the body capture in instrumentations Signed-off-by: Pavol Loffay <[email protected]> * Not working smoketests Signed-off-by: Pavol Loffay <[email protected]> * Working Signed-off-by: Pavol Loffay <[email protected]> * remove Signed-off-by: Pavol Loffay <[email protected]> * remove Signed-off-by: Pavol Loffay <[email protected]> * working demo app Signed-off-by: Pavol Loffay <[email protected]> * working Signed-off-by: Pavol Loffay <[email protected]> * Working shading to OTEL Signed-off-by: Pavol Loffay <[email protected]> * exclude core from /inst Signed-off-by: Pavol Loffay <[email protected]> * Add comment Signed-off-by: Pavol Loffay <[email protected]> * revert Signed-off-by: Pavol Loffay <[email protected]>
1 parent e064caf commit 34ea693

File tree

26 files changed

+283
-160
lines changed

26 files changed

+283
-160
lines changed

instrumentation/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,19 @@ dependencies{
3838
tasks {
3939
// Keep in sync with https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/f893ca540b72a895fbf18c14d2df8d1cabaf2c7f/instrumentation/instrumentation.gradle#L51
4040
shadowJar {
41+
dependencies{
42+
// exclude core, it lives in the bootstrap classloader
43+
exclude(project(":javaagent-core"))
44+
}
45+
4146
mergeServiceFiles()
4247

48+
relocate("com.fasterxml.jackson", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.fasterxml.jackson")
49+
relocate("com.google", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.google")
50+
relocate("google.protobuf", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.google.protobuf")
51+
relocate("org.checkerframework", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.checkerframework")
52+
relocate("org.yaml", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.org.yaml")
53+
4354
exclude("**/module-info.class")
4455

4556
// Prevents conflict with other SLF4J instances. Important for premain.

instrumentation/grpc-1.5/src/main/java/io/opentelemetry/instrumentation/hypertrace/grpc/v1_5/client/GrpcClientBodyInstrumentation.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ public String[] helperClassNames() {
5858
"io.opentelemetry.instrumentation.grpc.v1_5.server.TracingServerInterceptor",
5959
"io.opentelemetry.instrumentation.grpc.v1_5.server.TracingServerInterceptor$TracingServerCall",
6060
"io.opentelemetry.instrumentation.grpc.v1_5.server.TracingServerInterceptor$TracingServerCallListener",
61-
"org.hypertrace.agent.core.HypertraceSemanticAttributes",
62-
"org.hypertrace.agent.core.DynamicConfig",
6361
"io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.GrpcTracer",
6462
"io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.GrpcSpanDecorator",
6563
"io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.InstrumentationName",

instrumentation/grpc-1.5/src/main/java/io/opentelemetry/instrumentation/hypertrace/grpc/v1_5/client/GrpcClientInterceptor.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.GrpcTracer;
3030
import io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.InstrumentationName;
3131
import io.opentelemetry.trace.Span;
32-
import org.hypertrace.agent.core.DynamicConfig;
32+
import org.hypertrace.agent.core.HypertraceConfig;
3333
import org.hypertrace.agent.core.HypertraceSemanticAttributes;
3434

3535
public class GrpcClientInterceptor implements ClientInterceptor {
@@ -39,40 +39,41 @@ public class GrpcClientInterceptor implements ClientInterceptor {
3939
@Override
4040
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
4141
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
42-
if (!DynamicConfig.isEnabled(InstrumentationName.INSTRUMENTATION_NAME)) {
42+
if (!HypertraceConfig.isInstrumentationEnabled(InstrumentationName.INSTRUMENTATION_NAME)) {
4343
return next.newCall(method, callOptions);
4444
}
4545

4646
Span currentSpan = TRACER.getCurrentSpan();
4747
ClientCall<ReqT, RespT> clientCall = next.newCall(method, callOptions);
48-
return new GrpcClientInterceptor.TracingClientCall<>(clientCall, currentSpan, TRACER);
48+
return new GrpcClientInterceptor.TracingClientCall<>(clientCall, currentSpan);
4949
}
5050

5151
static final class TracingClientCall<ReqT, RespT>
5252
extends ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT> {
5353

5454
private final Span span;
5555

56-
TracingClientCall(
57-
ClientCall<ReqT, RespT> delegate,
58-
Span span,
59-
io.opentelemetry.instrumentation.grpc.v1_5.client.GrpcClientTracer tracer) {
56+
TracingClientCall(ClientCall<ReqT, RespT> delegate, Span span) {
6057
super(delegate);
6158
this.span = span;
6259
}
6360

6461
@Override
6562
public void start(Listener<RespT> responseListener, Metadata headers) {
6663
super.start(new TracingClientCallListener<>(responseListener, span), headers);
67-
GrpcSpanDecorator.addMetadataAttributes(
68-
headers, span, HypertraceSemanticAttributes::rpcRequestMetadata);
64+
if (HypertraceConfig.get().getDataCapture().getRpcMetadata().getRequest().getValue()) {
65+
GrpcSpanDecorator.addMetadataAttributes(
66+
headers, span, HypertraceSemanticAttributes::rpcRequestMetadata);
67+
}
6968
}
7069

7170
@Override
7271
public void sendMessage(ReqT message) {
7372
super.sendMessage(message);
74-
GrpcSpanDecorator.addMessageAttribute(
75-
message, span, HypertraceSemanticAttributes.RPC_REQUEST_BODY);
73+
if (HypertraceConfig.get().getDataCapture().getRpcBody().getRequest().getValue()) {
74+
GrpcSpanDecorator.addMessageAttribute(
75+
message, span, HypertraceSemanticAttributes.RPC_REQUEST_BODY);
76+
}
7677
}
7778
}
7879

@@ -88,15 +89,19 @@ static final class TracingClientCallListener<RespT>
8889
@Override
8990
public void onMessage(RespT message) {
9091
delegate().onMessage(message);
91-
GrpcSpanDecorator.addMessageAttribute(
92-
message, span, HypertraceSemanticAttributes.RPC_RESPONSE_BODY);
92+
if (HypertraceConfig.get().getDataCapture().getRpcBody().getResponse().getValue()) {
93+
GrpcSpanDecorator.addMessageAttribute(
94+
message, span, HypertraceSemanticAttributes.RPC_RESPONSE_BODY);
95+
}
9396
}
9497

9598
@Override
9699
public void onHeaders(Metadata headers) {
97100
super.onHeaders(headers);
98-
GrpcSpanDecorator.addMetadataAttributes(
99-
headers, span, HypertraceSemanticAttributes::rpcResponseMetadata);
101+
if (HypertraceConfig.get().getDataCapture().getRpcMetadata().getResponse().getValue()) {
102+
GrpcSpanDecorator.addMetadataAttributes(
103+
headers, span, HypertraceSemanticAttributes::rpcResponseMetadata);
104+
}
100105
}
101106
}
102107
}

instrumentation/grpc-1.5/src/main/java/io/opentelemetry/instrumentation/hypertrace/grpc/v1_5/server/GrpcServerBodyInstrumentation.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ public String[] helperClassNames() {
6262
"org.hypertrace.agent.filter.ExecutionBlocked",
6363
"org.hypertrace.agent.filter.ExecutionNotBlocked",
6464
"org.hypertrace.agent.filter.MockFilterEvaluator",
65-
"org.hypertrace.agent.core.HypertraceSemanticAttributes",
66-
"org.hypertrace.agent.core.DynamicConfig",
6765
"io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.GrpcTracer",
6866
"io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.GrpcSpanDecorator",
6967
"io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.InstrumentationName",

instrumentation/grpc-1.5/src/main/java/io/opentelemetry/instrumentation/hypertrace/grpc/v1_5/server/GrpcServerInterceptor.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import io.opentelemetry.instrumentation.hypertrace.grpc.v1_5.server.GrpcServerInterceptor.TracingServerCall.TracingServerCallListener;
3131
import io.opentelemetry.trace.Span;
3232
import java.util.Map;
33-
import org.hypertrace.agent.core.DynamicConfig;
33+
import org.hypertrace.agent.core.HypertraceConfig;
3434
import org.hypertrace.agent.core.HypertraceSemanticAttributes;
3535
import org.hypertrace.agent.filter.FilterProvider;
3636
import org.hypertrace.agent.filter.FilterResult;
@@ -42,15 +42,18 @@ public class GrpcServerInterceptor implements ServerInterceptor {
4242
@Override
4343
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
4444
ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
45-
if (!DynamicConfig.isEnabled(InstrumentationName.INSTRUMENTATION_NAME)) {
45+
if (!HypertraceConfig.isInstrumentationEnabled(InstrumentationName.INSTRUMENTATION_NAME)) {
4646
return next.startCall(call, headers);
4747
}
4848

4949
Span currentSpan = TRACER.getCurrentSpan();
5050

5151
Map<String, String> mapHeaders = GrpcSpanDecorator.metadataToMap(headers);
52-
GrpcSpanDecorator.addMetadataAttributes(
53-
mapHeaders, currentSpan, HypertraceSemanticAttributes::rpcRequestMetadata);
52+
53+
if (HypertraceConfig.get().getDataCapture().getRpcMetadata().getRequest().getValue()) {
54+
GrpcSpanDecorator.addMetadataAttributes(
55+
mapHeaders, currentSpan, HypertraceSemanticAttributes::rpcRequestMetadata);
56+
}
5457

5558
FilterResult filterResult =
5659
FilterProvider.getFilterEvaluator().evaluateRequestHeaders(currentSpan, mapHeaders);
@@ -78,15 +81,19 @@ static final class TracingServerCall<ReqT, RespT>
7881
@Override
7982
public void sendMessage(RespT message) {
8083
super.sendMessage(message);
81-
GrpcSpanDecorator.addMessageAttribute(
82-
message, span, HypertraceSemanticAttributes.RPC_RESPONSE_BODY);
84+
if (HypertraceConfig.get().getDataCapture().getRpcBody().getResponse().getValue()) {
85+
GrpcSpanDecorator.addMessageAttribute(
86+
message, span, HypertraceSemanticAttributes.RPC_RESPONSE_BODY);
87+
}
8388
}
8489

8590
@Override
8691
public void sendHeaders(Metadata headers) {
8792
super.sendHeaders(headers);
88-
GrpcSpanDecorator.addMetadataAttributes(
89-
headers, span, HypertraceSemanticAttributes::rpcResponseMetadata);
93+
if (HypertraceConfig.get().getDataCapture().getRpcMetadata().getResponse().getValue()) {
94+
GrpcSpanDecorator.addMetadataAttributes(
95+
headers, span, HypertraceSemanticAttributes::rpcResponseMetadata);
96+
}
9097
}
9198

9299
static final class TracingServerCallListener<ReqT>
@@ -102,8 +109,10 @@ static final class TracingServerCallListener<ReqT>
102109
@Override
103110
public void onMessage(ReqT message) {
104111
delegate().onMessage(message);
105-
GrpcSpanDecorator.addMessageAttribute(
106-
message, span, HypertraceSemanticAttributes.RPC_REQUEST_BODY);
112+
if (HypertraceConfig.get().getDataCapture().getRpcBody().getRequest().getValue()) {
113+
GrpcSpanDecorator.addMessageAttribute(
114+
message, span, HypertraceSemanticAttributes.RPC_REQUEST_BODY);
115+
}
107116
}
108117
}
109118
}

instrumentation/grpc-1.5/src/test/java/io/opentelemetry/instrumentation/hypertrace/grpc/v1_5/GrpcInstrumentationTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import io.grpc.stub.MetadataUtils;
3333
import io.opentelemetry.sdk.trace.data.SpanData;
3434
import java.io.IOException;
35+
import java.net.URL;
3536
import java.util.List;
3637
import java.util.concurrent.TimeoutException;
3738
import org.hypertrace.agent.core.EnvironmentConfig;
@@ -48,7 +49,6 @@
4849
import org.junit.jupiter.api.Assertions;
4950
import org.junit.jupiter.api.BeforeAll;
5051
import org.junit.jupiter.api.Test;
51-
import org.junitpioneer.jupiter.ClearSystemProperty;
5252

5353
public class GrpcInstrumentationTest extends AbstractInstrumenterTest {
5454

@@ -168,10 +168,11 @@ public void serverRequestBlocking() throws TimeoutException, InterruptedExceptio
168168
}
169169

170170
@Test
171-
@ClearSystemProperty(key = EnvironmentConfig.CAPTURE_HTTP_HEADERS_PREFIX + "request")
172171
public void disabledInstrumentation_dynamicConfig()
173-
throws TimeoutException, InterruptedException {
174-
System.setProperty(EnvironmentConfig.CAPTURE_HTTP_BODY_PREFIX + "request", "false");
172+
throws TimeoutException, InterruptedException, IOException {
173+
URL configUrl = getClass().getClassLoader().getResource("ht-config-all-disabled.yaml");
174+
System.setProperty(EnvironmentConfig.CONFIG_FILE_PROPERTY, configUrl.getPath());
175+
HypertraceConfig.reset();
175176

176177
GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(CHANNEL);
177178
Response response = blockingStub.sayHello(REQUEST);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
serviceName: app_under_test
2+
reporting:
3+
address: http://localhost:9411
4+
secure: true
5+
dataCapture:
6+
httpHeaders:
7+
request: false
8+
response: false
9+
httpBody:
10+
request: false
11+
response: false
12+
rpcMetadata:
13+
request: false
14+
response: false
15+
rpcBody:
16+
request: false
17+
response: false

javaagent-core/src/main/java/org/hypertrace/agent/core/DynamicConfig.java renamed to instrumentation/okhttp/okhttp-3.0/src/main/java/io/opentelemetry/instrumentation/hypertrace/okhttp/v3_0/InstrumentationName.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.hypertrace.agent.core;
17+
package io.opentelemetry.instrumentation.hypertrace.okhttp.v3_0;
1818

19-
public class DynamicConfig {
20-
21-
private DynamicConfig() {}
22-
23-
public static boolean isEnabled(String[] instrumentationNames) {
24-
if (!HypertraceConfig.get().getDataCapture().getHttpBody().getRequest().getValue()) {
25-
return false;
26-
}
27-
28-
for (String name : instrumentationNames) {
29-
String integrationEnabled =
30-
EnvironmentConfig.getProperty("hypertrace.integration." + name + ".enabled");
31-
if (integrationEnabled != null && "false".equals(integrationEnabled.toLowerCase())) {
32-
return false;
33-
}
34-
}
35-
return true;
36-
}
19+
public class InstrumentationName {
20+
public static final String[] INSTRUMENTATION_NAME = {"okhttp", "okhttp-3"};
3721
}

instrumentation/okhttp/okhttp-3.0/src/main/java/io/opentelemetry/instrumentation/hypertrace/okhttp/v3_0/OkHttp3BodyInstrumentation.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
public class OkHttp3BodyInstrumentation extends Instrumenter.Default {
3636

3737
public OkHttp3BodyInstrumentation() {
38-
super("okhttp", "okhttp-3");
38+
super(InstrumentationName.INSTRUMENTATION_NAME[0], InstrumentationName.INSTRUMENTATION_NAME[1]);
3939
}
4040

4141
@Override
@@ -58,9 +58,7 @@ public Map<? extends ElementMatcher<? super MethodDescription>, String> transfor
5858
@Override
5959
public String[] helperClassNames() {
6060
return new String[] {
61-
"org.hypertrace.agent.core.ContentTypeUtils",
62-
"org.hypertrace.agent.core.HypertraceSemanticAttributes",
63-
packageName + ".OkHttpTracingInterceptor",
61+
packageName + ".InstrumentationName", packageName + ".OkHttpTracingInterceptor",
6462
};
6563
}
6664

instrumentation/okhttp/okhttp-3.0/src/main/java/io/opentelemetry/instrumentation/hypertrace/okhttp/v3_0/OkHttpTracingInterceptor.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import okhttp3.ResponseBody;
3131
import okio.Buffer;
3232
import org.hypertrace.agent.core.ContentTypeUtils;
33+
import org.hypertrace.agent.core.HypertraceConfig;
3334
import org.hypertrace.agent.core.HypertraceSemanticAttributes;
3435
import org.slf4j.Logger;
3536
import org.slf4j.LoggerFactory;
@@ -41,18 +42,29 @@ public class OkHttpTracingInterceptor implements Interceptor {
4142

4243
@Override
4344
public Response intercept(Chain chain) throws IOException {
45+
if (!HypertraceConfig.isInstrumentationEnabled(InstrumentationName.INSTRUMENTATION_NAME)) {
46+
return chain.proceed(chain.request());
47+
}
48+
4449
Span span = TRACER.getCurrentSpan();
4550

4651
Request request = chain.request();
47-
captureHeaders(span, request.headers(), HypertraceSemanticAttributes::httpRequestHeader);
52+
if (HypertraceConfig.get().getDataCapture().getHttpHeaders().getRequest().getValue()) {
53+
captureHeaders(span, request.headers(), HypertraceSemanticAttributes::httpRequestHeader);
54+
}
4855
captureRequestBody(span, request.body());
4956

5057
Response response = chain.proceed(request);
51-
captureHeaders(span, response.headers(), HypertraceSemanticAttributes::httpResponseHeader);
58+
if (HypertraceConfig.get().getDataCapture().getHttpHeaders().getResponse().getValue()) {
59+
captureHeaders(span, response.headers(), HypertraceSemanticAttributes::httpResponseHeader);
60+
}
5261
return captureResponseBody(span, response);
5362
}
5463

5564
private static void captureRequestBody(Span span, RequestBody requestBody) {
65+
if (!HypertraceConfig.get().getDataCapture().getHttpBody().getRequest().getValue()) {
66+
return;
67+
}
5668
if (requestBody == null) {
5769
return;
5870
}
@@ -70,6 +82,9 @@ private static void captureRequestBody(Span span, RequestBody requestBody) {
7082
}
7183

7284
private static Response captureResponseBody(Span span, final Response response) {
85+
if (!HypertraceConfig.get().getDataCapture().getHttpBody().getResponse().getValue()) {
86+
return response;
87+
}
7388
if (response.body() == null) {
7489
return response;
7590
}

0 commit comments

Comments
 (0)