Skip to content

Commit 37a2943

Browse files
authored
Move agent config and its dependencies into agent classloader (#322)
* Move agent config and its deps into agent classloader Signed-off-by: Pavol Loffay <[email protected]> * fix grpc Signed-off-by: Pavol Loffay <[email protected]> * Fix issue with loading the filter Signed-off-by: Pavol Loffay <[email protected]>
1 parent 0dc7b00 commit 37a2943

File tree

41 files changed

+439
-236
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+439
-236
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "javaagent-core/src/main/proto"]
2-
path = javaagent-core/src/main/proto
2+
path = otel-extensions/src/main/proto
33
url = https://github.com/hypertrace/agent-config.git

filter-api/src/main/java/org/hypertrace/agent/filter/FilterRegistry.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616

1717
package org.hypertrace.agent.filter;
1818

19-
import com.google.protobuf.StringValue;
2019
import java.net.MalformedURLException;
2120
import java.net.URL;
2221
import java.net.URLClassLoader;
2322
import java.util.ArrayList;
23+
import java.util.Collections;
2424
import java.util.List;
2525
import java.util.ServiceLoader;
26-
import org.hypertrace.agent.core.config.EnvironmentConfig;
27-
import org.hypertrace.agent.core.config.HypertraceConfig;
2826
import org.hypertrace.agent.filter.api.Filter;
2927
import org.hypertrace.agent.filter.spi.FilterProvider;
3028
import org.slf4j.Logger;
@@ -55,7 +53,7 @@ public static Filter getFilter() {
5553
synchronized (FilterRegistry.class) {
5654
if (filter == null) {
5755
try {
58-
filter = load();
56+
filter = load(Collections.emptyList());
5957
} catch (Throwable t) {
6058
logger.error("Throwable thrown while loading filter jars", t);
6159
}
@@ -65,13 +63,26 @@ public static Filter getFilter() {
6563
return filter;
6664
}
6765

68-
private static Filter load() {
69-
ClassLoader cl = loadJars();
66+
/**
67+
* Initializes the registry by loading the filters. This method should be called only once at
68+
* javaagent startup.
69+
*
70+
* @param jarPaths paths to filter jar files.
71+
*/
72+
public static void initialize(List<String> jarPaths) {
73+
try {
74+
filter = load(jarPaths);
75+
} catch (Throwable t) {
76+
logger.error("Throwable thrown while loading filter jars", t);
77+
}
78+
}
79+
80+
private static Filter load(List<String> jarPaths) {
81+
ClassLoader cl = loadJars(jarPaths);
7082
ServiceLoader<FilterProvider> providers = ServiceLoader.load(FilterProvider.class, cl);
7183
List<Filter> filters = new ArrayList<>();
7284
for (FilterProvider provider : providers) {
73-
String disabled =
74-
EnvironmentConfig.getProperty(getProviderDisabledPropertyName(provider.getClass()));
85+
String disabled = getProperty(getProviderDisabledPropertyName(provider.getClass()));
7586
if ("true".equalsIgnoreCase(disabled)) {
7687
continue;
7788
}
@@ -81,18 +92,16 @@ private static Filter load() {
8192
return new MultiFilter(filters);
8293
}
8394

84-
private static ClassLoader loadJars() {
85-
List<StringValue> jarPaths = HypertraceConfig.get().getJavaagent().getFilterJarPathsList();
95+
private static ClassLoader loadJars(List<String> jarPaths) {
8696
URL[] urls = new URL[jarPaths.size()];
8797
int i = 0;
88-
for (StringValue jarPath : jarPaths) {
98+
for (String jarPath : jarPaths) {
8999
try {
90-
URL url = new URL("file", "", -1, jarPath.getValue());
100+
URL url = new URL("file", "", -1, jarPath);
91101
urls[i] = url;
92102
i++;
93103
} catch (MalformedURLException e) {
94-
logger.warn(
95-
String.format("Malformed URL exception for jar on path: %s", jarPath.getValue()), e);
104+
logger.warn(String.format("Malformed URL exception for jar on path: %s", jarPath), e);
96105
}
97106
}
98107
return new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
@@ -101,4 +110,8 @@ private static ClassLoader loadJars() {
101110
public static String getProviderDisabledPropertyName(Class<?> clazz) {
102111
return String.format("ht.filter.provider.%s.disabled", clazz.getSimpleName());
103112
}
113+
114+
public static String getProperty(String name) {
115+
return System.getProperty(name, System.getenv(name.replaceAll("\\.", "_").toUpperCase()));
116+
}
104117
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import org.apache.http.HttpEntityEnclosingRequest;
3030
import org.apache.http.HttpMessage;
3131
import org.apache.http.HttpResponse;
32-
import org.hypertrace.agent.config.Config.AgentConfig;
33-
import org.hypertrace.agent.core.config.HypertraceConfig;
32+
import org.hypertrace.agent.core.config.InstrumentationConfig;
33+
import org.hypertrace.agent.core.config.InstrumentationConfig.ConfigProvider;
3434
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
3535
import org.hypertrace.agent.core.instrumentation.buffer.BoundedBuffersFactory;
3636
import org.hypertrace.agent.core.instrumentation.buffer.BoundedByteArrayOutputStream;
@@ -42,6 +42,8 @@
4242
public class ApacheHttpClientUtils {
4343
private ApacheHttpClientUtils() {}
4444

45+
private static InstrumentationConfig instrumentationConfig = ConfigProvider.get();
46+
4547
private static final Logger log = LoggerFactory.getLogger(ApacheHttpClientUtils.class);
4648

4749
public static void addResponseHeaders(Span span, HeaderIterator headerIterator) {
@@ -63,12 +65,11 @@ private static void addHeaders(
6365
}
6466

6567
public static void traceRequest(Span span, HttpMessage request) {
66-
AgentConfig agentConfig = HypertraceConfig.get();
67-
if (agentConfig.getDataCapture().getHttpHeaders().getRequest().getValue()) {
68+
if (instrumentationConfig.httpHeaders().request()) {
6869
ApacheHttpClientUtils.addRequestHeaders(span, request.headerIterator());
6970
}
7071

71-
if (agentConfig.getDataCapture().getHttpBody().getRequest().getValue()
72+
if (instrumentationConfig.httpBody().request()
7273
&& request instanceof HttpEntityEnclosingRequest) {
7374
HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
7475
HttpEntity entity = entityRequest.getEntity();
@@ -78,12 +79,11 @@ public static void traceRequest(Span span, HttpMessage request) {
7879
}
7980

8081
public static void traceResponse(Span span, HttpResponse response) {
81-
AgentConfig agentConfig = HypertraceConfig.get();
82-
if (agentConfig.getDataCapture().getHttpHeaders().getResponse().getValue()) {
82+
if (instrumentationConfig.httpHeaders().response()) {
8383
ApacheHttpClientUtils.addResponseHeaders(span, response.headerIterator());
8484
}
8585

86-
if (agentConfig.getDataCapture().getHttpBody().getResponse().getValue()) {
86+
if (instrumentationConfig.httpBody().response()) {
8787
HttpEntity entity = response.getEntity();
8888
ApacheHttpClientUtils.traceEntity(
8989
span, HypertraceSemanticAttributes.HTTP_RESPONSE_BODY, entity);

instrumentation/build.gradle.kts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ tasks {
5353

5454
mergeServiceFiles()
5555

56-
relocate("com.fasterxml.jackson", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.fasterxml.jackson")
57-
relocate("com.google", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.google")
58-
relocate("google.protobuf", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.google.protobuf")
59-
relocate("org.checkerframework", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.checkerframework")
60-
relocate("org.yaml", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.org.yaml")
61-
6256
relocate("com.blogspot.mydailyjava.weaklockfree", "io.opentelemetry.instrumentation.api.internal.shaded.weaklockfree")
6357

6458
exclude("**/module-info.class")

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@
2828
import io.opentelemetry.api.trace.Span;
2929
import io.opentelemetry.javaagent.instrumentation.hypertrace.grpc.v1_5.GrpcInstrumentationName;
3030
import io.opentelemetry.javaagent.instrumentation.hypertrace.grpc.v1_5.GrpcSpanDecorator;
31-
import org.hypertrace.agent.core.config.HypertraceConfig;
31+
import org.hypertrace.agent.core.config.InstrumentationConfig;
3232
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
3333

3434
public class GrpcClientInterceptor implements ClientInterceptor {
3535

3636
@Override
3737
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
3838
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
39-
if (!HypertraceConfig.isInstrumentationEnabled(
39+
40+
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
41+
if (!instrumentationConfig.isInstrumentationEnabled(
4042
GrpcInstrumentationName.PRIMARY, GrpcInstrumentationName.OTHER)) {
4143
return next.newCall(method, callOptions);
4244
}
@@ -59,7 +61,9 @@ static final class TracingClientCall<ReqT, RespT>
5961
@Override
6062
public void start(Listener<RespT> responseListener, Metadata headers) {
6163
super.start(new TracingClientCallListener<>(responseListener, span), headers);
62-
if (HypertraceConfig.get().getDataCapture().getRpcMetadata().getRequest().getValue()) {
64+
65+
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
66+
if (instrumentationConfig.rpcMetadata().request()) {
6367
GrpcSpanDecorator.addMetadataAttributes(
6468
headers, span, HypertraceSemanticAttributes::rpcRequestMetadata);
6569
}
@@ -68,7 +72,9 @@ public void start(Listener<RespT> responseListener, Metadata headers) {
6872
@Override
6973
public void sendMessage(ReqT message) {
7074
super.sendMessage(message);
71-
if (HypertraceConfig.get().getDataCapture().getRpcBody().getRequest().getValue()) {
75+
76+
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
77+
if (instrumentationConfig.rpcBody().request()) {
7278
GrpcSpanDecorator.addMessageAttribute(
7379
message, span, HypertraceSemanticAttributes.RPC_REQUEST_BODY);
7480
}
@@ -87,7 +93,9 @@ static final class TracingClientCallListener<RespT>
8793
@Override
8894
public void onMessage(RespT message) {
8995
delegate().onMessage(message);
90-
if (HypertraceConfig.get().getDataCapture().getRpcBody().getResponse().getValue()) {
96+
97+
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
98+
if (instrumentationConfig.rpcBody().response()) {
9199
GrpcSpanDecorator.addMessageAttribute(
92100
message, span, HypertraceSemanticAttributes.RPC_RESPONSE_BODY);
93101
}
@@ -96,7 +104,9 @@ public void onMessage(RespT message) {
96104
@Override
97105
public void onHeaders(Metadata headers) {
98106
super.onHeaders(headers);
99-
if (HypertraceConfig.get().getDataCapture().getRpcMetadata().getResponse().getValue()) {
107+
108+
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
109+
if (instrumentationConfig.rpcMetadata().response()) {
100110
GrpcSpanDecorator.addMetadataAttributes(
101111
headers, span, HypertraceSemanticAttributes::rpcResponseMetadata);
102112
}

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import io.opentelemetry.javaagent.instrumentation.hypertrace.grpc.v1_5.GrpcInstrumentationName;
2929
import io.opentelemetry.javaagent.instrumentation.hypertrace.grpc.v1_5.GrpcSpanDecorator;
3030
import java.util.Map;
31-
import org.hypertrace.agent.core.config.HypertraceConfig;
31+
import org.hypertrace.agent.core.config.InstrumentationConfig;
3232
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
3333
import org.hypertrace.agent.filter.FilterRegistry;
3434

@@ -37,7 +37,9 @@ public class GrpcServerInterceptor implements ServerInterceptor {
3737
@Override
3838
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
3939
ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
40-
if (!HypertraceConfig.isInstrumentationEnabled(
40+
41+
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
42+
if (!instrumentationConfig.isInstrumentationEnabled(
4143
GrpcInstrumentationName.PRIMARY, GrpcInstrumentationName.OTHER)) {
4244
return next.startCall(call, headers);
4345
}
@@ -46,7 +48,7 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
4648

4749
Map<String, String> mapHeaders = GrpcSpanDecorator.metadataToMap(headers);
4850

49-
if (HypertraceConfig.get().getDataCapture().getRpcMetadata().getRequest().getValue()) {
51+
if (instrumentationConfig.rpcMetadata().request()) {
5052
GrpcSpanDecorator.addMetadataAttributes(mapHeaders, currentSpan);
5153
}
5254

@@ -75,7 +77,9 @@ static final class TracingServerCall<ReqT, RespT>
7577
@Override
7678
public void sendMessage(RespT message) {
7779
super.sendMessage(message);
78-
if (HypertraceConfig.get().getDataCapture().getRpcBody().getResponse().getValue()) {
80+
81+
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
82+
if (instrumentationConfig.rpcBody().response()) {
7983
GrpcSpanDecorator.addMessageAttribute(
8084
message, span, HypertraceSemanticAttributes.RPC_RESPONSE_BODY);
8185
}
@@ -84,7 +88,9 @@ public void sendMessage(RespT message) {
8488
@Override
8589
public void sendHeaders(Metadata headers) {
8690
super.sendHeaders(headers);
87-
if (HypertraceConfig.get().getDataCapture().getRpcMetadata().getResponse().getValue()) {
91+
92+
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
93+
if (instrumentationConfig.rpcMetadata().response()) {
8894
GrpcSpanDecorator.addMetadataAttributes(
8995
headers, span, HypertraceSemanticAttributes::rpcResponseMetadata);
9096
}
@@ -104,7 +110,9 @@ static final class TracingServerCallListener<ReqT>
104110
@Override
105111
public void onMessage(ReqT message) {
106112
delegate().onMessage(message);
107-
if (HypertraceConfig.get().getDataCapture().getRpcBody().getRequest().getValue()) {
113+
114+
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
115+
if (instrumentationConfig.rpcBody().request()) {
108116
GrpcSpanDecorator.addMessageAttribute(
109117
message, span, HypertraceSemanticAttributes.RPC_REQUEST_BODY);
110118
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@
3535
import java.net.URL;
3636
import java.util.List;
3737
import java.util.concurrent.TimeoutException;
38-
import org.hypertrace.agent.core.config.EnvironmentConfig;
39-
import org.hypertrace.agent.core.config.HypertraceConfig;
38+
import org.hypertrace.agent.core.config.InstrumentationConfig;
4039
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
40+
import org.hypertrace.agent.otel.extensions.config.EnvironmentConfig;
41+
import org.hypertrace.agent.otel.extensions.config.HypertraceConfig;
4142
import org.hypertrace.agent.testing.AbstractInstrumenterTest;
4243
import org.hypertrace.example.GreeterGrpc;
4344
import org.hypertrace.example.GreeterGrpc.GreeterBlockingStub;
@@ -190,6 +191,7 @@ public void disabledInstrumentation_dynamicConfig()
190191
URL configUrl = getClass().getClassLoader().getResource("ht-config-all-disabled.yaml");
191192
System.setProperty(EnvironmentConfig.CONFIG_FILE_PROPERTY, configUrl.getPath());
192193
HypertraceConfig.reset();
194+
InstrumentationConfig.ConfigProvider.reset();
193195

194196
GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(CHANNEL);
195197
Response response = blockingStub.sayHello(REQUEST);

instrumentation/jaxrs-client-2.0/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/jaxrs/v2_0/JaxrsClientBodyCaptureFilter.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@
2828
import javax.ws.rs.client.ClientResponseContext;
2929
import javax.ws.rs.client.ClientResponseFilter;
3030
import javax.ws.rs.core.MultivaluedMap;
31-
import org.hypertrace.agent.config.Config.AgentConfig;
32-
import org.hypertrace.agent.core.config.HypertraceConfig;
31+
import org.hypertrace.agent.core.config.InstrumentationConfig;
3332
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
3433
import org.slf4j.Logger;
3534
import org.slf4j.LoggerFactory;
3635

3736
public class JaxrsClientBodyCaptureFilter implements ClientRequestFilter, ClientResponseFilter {
3837

3938
private static final Logger log = LoggerFactory.getLogger(JaxrsClientBodyCaptureFilter.class);
39+
private static final InstrumentationConfig instrumentationConfig =
40+
InstrumentationConfig.ConfigProvider.get();
4041

4142
@Override
4243
public void filter(ClientRequestContext requestContext) {
@@ -47,10 +48,9 @@ public void filter(ClientRequestContext requestContext) {
4748

4849
Context currentContext = (Context) contextObj;
4950
Span currentSpan = Span.fromContext(currentContext);
50-
AgentConfig agentConfig = HypertraceConfig.get();
5151

5252
try {
53-
if (agentConfig.getDataCapture().getHttpHeaders().getRequest().getValue()) {
53+
if (instrumentationConfig.httpHeaders().request()) {
5454
captureHeaders(
5555
currentSpan,
5656
HypertraceSemanticAttributes::httpRequestHeader,
@@ -70,10 +70,9 @@ public void filter(ClientRequestContext requestContext, ClientResponseContext re
7070

7171
Context currentContext = (Context) contextObj;
7272
Span currentSpan = Span.fromContext(currentContext);
73-
AgentConfig agentConfig = HypertraceConfig.get();
7473

7574
try {
76-
if (agentConfig.getDataCapture().getHttpHeaders().getResponse().getValue()) {
75+
if (instrumentationConfig.httpHeaders().response()) {
7776
captureHeaders(
7877
currentSpan,
7978
HypertraceSemanticAttributes::httpResponseHeader,

instrumentation/jaxrs-client-2.0/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/jaxrs/v2_0/JaxrsClientEntityInterceptor.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
import javax.ws.rs.ext.ReaderInterceptorContext;
3232
import javax.ws.rs.ext.WriterInterceptor;
3333
import javax.ws.rs.ext.WriterInterceptorContext;
34-
import org.hypertrace.agent.config.Config.AgentConfig;
35-
import org.hypertrace.agent.core.config.HypertraceConfig;
34+
import org.hypertrace.agent.core.config.InstrumentationConfig;
3635
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
3736
import org.hypertrace.agent.core.instrumentation.SpanAndBuffer;
3837
import org.hypertrace.agent.core.instrumentation.buffer.BoundedBuffersFactory;
@@ -46,6 +45,8 @@
4645
public class JaxrsClientEntityInterceptor implements ReaderInterceptor, WriterInterceptor {
4746

4847
private static final Logger log = LoggerFactory.getLogger(JaxrsClientEntityInterceptor.class);
48+
private static final InstrumentationConfig instrumentationConfig =
49+
InstrumentationConfig.ConfigProvider.get();
4950

5051
private final ContextStore<InputStream, SpanAndBuffer> inputStreamContextStore;
5152
private final ContextStore<OutputStream, BoundedByteArrayOutputStream> outputStreamContextStore;
@@ -63,10 +64,9 @@ public Object aroundReadFrom(ReaderInterceptorContext responseContext)
6364
throws IOException, WebApplicationException {
6465

6566
MediaType mediaType = responseContext.getMediaType();
66-
AgentConfig agentConfig = HypertraceConfig.get();
6767
if (mediaType == null
6868
|| !ContentTypeUtils.shouldCapture(mediaType.toString())
69-
|| !agentConfig.getDataCapture().getHttpBody().getResponse().getValue()) {
69+
|| !instrumentationConfig.httpBody().response()) {
7070
return responseContext.proceed();
7171
}
7272

@@ -125,8 +125,7 @@ public void aroundWriteTo(WriterInterceptorContext requestContext)
125125
Context context = (Context) contextObj;
126126
Span currentSpan = Span.fromContext(context);
127127

128-
AgentConfig agentConfig = HypertraceConfig.get();
129-
if (agentConfig.getDataCapture().getHttpBody().getRequest().getValue()) {
128+
if (instrumentationConfig.httpBody().request()) {
130129
MediaType mediaType = requestContext.getMediaType();
131130
if (mediaType == null || !ContentTypeUtils.shouldCapture(mediaType.toString())) {
132131
requestContext.proceed();

0 commit comments

Comments
 (0)