Skip to content

Move agent config and its dependencies into agent classloader #322

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 3 commits into from
Jun 21, 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 .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "javaagent-core/src/main/proto"]
path = javaagent-core/src/main/proto
path = otel-extensions/src/main/proto
url = https://github.com/hypertrace/agent-config.git
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@

package org.hypertrace.agent.filter;

import com.google.protobuf.StringValue;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ServiceLoader;
import org.hypertrace.agent.core.config.EnvironmentConfig;
import org.hypertrace.agent.core.config.HypertraceConfig;
import org.hypertrace.agent.filter.api.Filter;
import org.hypertrace.agent.filter.spi.FilterProvider;
import org.slf4j.Logger;
Expand Down Expand Up @@ -55,7 +53,7 @@ public static Filter getFilter() {
synchronized (FilterRegistry.class) {
if (filter == null) {
try {
filter = load();
filter = load(Collections.emptyList());
} catch (Throwable t) {
logger.error("Throwable thrown while loading filter jars", t);
}
Expand All @@ -65,13 +63,26 @@ public static Filter getFilter() {
return filter;
}

private static Filter load() {
ClassLoader cl = loadJars();
/**
* Initializes the registry by loading the filters. This method should be called only once at
* javaagent startup.
*
* @param jarPaths paths to filter jar files.
*/
public static void initialize(List<String> jarPaths) {
try {
filter = load(jarPaths);
} catch (Throwable t) {
logger.error("Throwable thrown while loading filter jars", t);
}
}

private static Filter load(List<String> jarPaths) {
ClassLoader cl = loadJars(jarPaths);
ServiceLoader<FilterProvider> providers = ServiceLoader.load(FilterProvider.class, cl);
List<Filter> filters = new ArrayList<>();
for (FilterProvider provider : providers) {
String disabled =
EnvironmentConfig.getProperty(getProviderDisabledPropertyName(provider.getClass()));
String disabled = getProperty(getProviderDisabledPropertyName(provider.getClass()));
if ("true".equalsIgnoreCase(disabled)) {
continue;
}
Expand All @@ -81,18 +92,16 @@ private static Filter load() {
return new MultiFilter(filters);
}

private static ClassLoader loadJars() {
List<StringValue> jarPaths = HypertraceConfig.get().getJavaagent().getFilterJarPathsList();
private static ClassLoader loadJars(List<String> jarPaths) {
URL[] urls = new URL[jarPaths.size()];
int i = 0;
for (StringValue jarPath : jarPaths) {
for (String jarPath : jarPaths) {
try {
URL url = new URL("file", "", -1, jarPath.getValue());
URL url = new URL("file", "", -1, jarPath);
urls[i] = url;
i++;
} catch (MalformedURLException e) {
logger.warn(
String.format("Malformed URL exception for jar on path: %s", jarPath.getValue()), e);
logger.warn(String.format("Malformed URL exception for jar on path: %s", jarPath), e);
}
}
return new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
Expand All @@ -101,4 +110,8 @@ private static ClassLoader loadJars() {
public static String getProviderDisabledPropertyName(Class<?> clazz) {
return String.format("ht.filter.provider.%s.disabled", clazz.getSimpleName());
}

public static String getProperty(String name) {
return System.getProperty(name, System.getenv(name.replaceAll("\\.", "_").toUpperCase()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpMessage;
import org.apache.http.HttpResponse;
import org.hypertrace.agent.config.Config.AgentConfig;
import org.hypertrace.agent.core.config.HypertraceConfig;
import org.hypertrace.agent.core.config.InstrumentationConfig;
import org.hypertrace.agent.core.config.InstrumentationConfig.ConfigProvider;
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
import org.hypertrace.agent.core.instrumentation.buffer.BoundedBuffersFactory;
import org.hypertrace.agent.core.instrumentation.buffer.BoundedByteArrayOutputStream;
Expand All @@ -42,6 +42,8 @@
public class ApacheHttpClientUtils {
private ApacheHttpClientUtils() {}

private static InstrumentationConfig instrumentationConfig = ConfigProvider.get();

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

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

public static void traceRequest(Span span, HttpMessage request) {
AgentConfig agentConfig = HypertraceConfig.get();
if (agentConfig.getDataCapture().getHttpHeaders().getRequest().getValue()) {
if (instrumentationConfig.httpHeaders().request()) {
ApacheHttpClientUtils.addRequestHeaders(span, request.headerIterator());
}

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

public static void traceResponse(Span span, HttpResponse response) {
AgentConfig agentConfig = HypertraceConfig.get();
if (agentConfig.getDataCapture().getHttpHeaders().getResponse().getValue()) {
if (instrumentationConfig.httpHeaders().response()) {
ApacheHttpClientUtils.addResponseHeaders(span, response.headerIterator());
}

if (agentConfig.getDataCapture().getHttpBody().getResponse().getValue()) {
if (instrumentationConfig.httpBody().response()) {
HttpEntity entity = response.getEntity();
ApacheHttpClientUtils.traceEntity(
span, HypertraceSemanticAttributes.HTTP_RESPONSE_BODY, entity);
Expand Down
6 changes: 0 additions & 6 deletions instrumentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ tasks {

mergeServiceFiles()

relocate("com.fasterxml.jackson", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.fasterxml.jackson")
relocate("com.google", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.google")
relocate("google.protobuf", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.google.protobuf")
relocate("org.checkerframework", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.com.checkerframework")
relocate("org.yaml", "io.opentelemetry.javaagent.shaded.org.hypertrace.shaded.org.yaml")

relocate("com.blogspot.mydailyjava.weaklockfree", "io.opentelemetry.instrumentation.api.internal.shaded.weaklockfree")

exclude("**/module-info.class")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.javaagent.instrumentation.hypertrace.grpc.v1_5.GrpcInstrumentationName;
import io.opentelemetry.javaagent.instrumentation.hypertrace.grpc.v1_5.GrpcSpanDecorator;
import org.hypertrace.agent.core.config.HypertraceConfig;
import org.hypertrace.agent.core.config.InstrumentationConfig;
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;

public class GrpcClientInterceptor implements ClientInterceptor {

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
if (!HypertraceConfig.isInstrumentationEnabled(

InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
if (!instrumentationConfig.isInstrumentationEnabled(
GrpcInstrumentationName.PRIMARY, GrpcInstrumentationName.OTHER)) {
return next.newCall(method, callOptions);
}
Expand All @@ -59,7 +61,9 @@ static final class TracingClientCall<ReqT, RespT>
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
super.start(new TracingClientCallListener<>(responseListener, span), headers);
if (HypertraceConfig.get().getDataCapture().getRpcMetadata().getRequest().getValue()) {

InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
if (instrumentationConfig.rpcMetadata().request()) {
GrpcSpanDecorator.addMetadataAttributes(
headers, span, HypertraceSemanticAttributes::rpcRequestMetadata);
}
Expand All @@ -68,7 +72,9 @@ public void start(Listener<RespT> responseListener, Metadata headers) {
@Override
public void sendMessage(ReqT message) {
super.sendMessage(message);
if (HypertraceConfig.get().getDataCapture().getRpcBody().getRequest().getValue()) {

InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
if (instrumentationConfig.rpcBody().request()) {
GrpcSpanDecorator.addMessageAttribute(
message, span, HypertraceSemanticAttributes.RPC_REQUEST_BODY);
}
Expand All @@ -87,7 +93,9 @@ static final class TracingClientCallListener<RespT>
@Override
public void onMessage(RespT message) {
delegate().onMessage(message);
if (HypertraceConfig.get().getDataCapture().getRpcBody().getResponse().getValue()) {

InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
if (instrumentationConfig.rpcBody().response()) {
GrpcSpanDecorator.addMessageAttribute(
message, span, HypertraceSemanticAttributes.RPC_RESPONSE_BODY);
}
Expand All @@ -96,7 +104,9 @@ public void onMessage(RespT message) {
@Override
public void onHeaders(Metadata headers) {
super.onHeaders(headers);
if (HypertraceConfig.get().getDataCapture().getRpcMetadata().getResponse().getValue()) {

InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
if (instrumentationConfig.rpcMetadata().response()) {
GrpcSpanDecorator.addMetadataAttributes(
headers, span, HypertraceSemanticAttributes::rpcResponseMetadata);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import io.opentelemetry.javaagent.instrumentation.hypertrace.grpc.v1_5.GrpcInstrumentationName;
import io.opentelemetry.javaagent.instrumentation.hypertrace.grpc.v1_5.GrpcSpanDecorator;
import java.util.Map;
import org.hypertrace.agent.core.config.HypertraceConfig;
import org.hypertrace.agent.core.config.InstrumentationConfig;
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
import org.hypertrace.agent.filter.FilterRegistry;

Expand All @@ -37,7 +37,9 @@ public class GrpcServerInterceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
if (!HypertraceConfig.isInstrumentationEnabled(

InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
if (!instrumentationConfig.isInstrumentationEnabled(
GrpcInstrumentationName.PRIMARY, GrpcInstrumentationName.OTHER)) {
return next.startCall(call, headers);
}
Expand All @@ -46,7 +48,7 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(

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

if (HypertraceConfig.get().getDataCapture().getRpcMetadata().getRequest().getValue()) {
if (instrumentationConfig.rpcMetadata().request()) {
GrpcSpanDecorator.addMetadataAttributes(mapHeaders, currentSpan);
}

Expand Down Expand Up @@ -75,7 +77,9 @@ static final class TracingServerCall<ReqT, RespT>
@Override
public void sendMessage(RespT message) {
super.sendMessage(message);
if (HypertraceConfig.get().getDataCapture().getRpcBody().getResponse().getValue()) {

InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
if (instrumentationConfig.rpcBody().response()) {
GrpcSpanDecorator.addMessageAttribute(
message, span, HypertraceSemanticAttributes.RPC_RESPONSE_BODY);
}
Expand All @@ -84,7 +88,9 @@ public void sendMessage(RespT message) {
@Override
public void sendHeaders(Metadata headers) {
super.sendHeaders(headers);
if (HypertraceConfig.get().getDataCapture().getRpcMetadata().getResponse().getValue()) {

InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
if (instrumentationConfig.rpcMetadata().response()) {
GrpcSpanDecorator.addMetadataAttributes(
headers, span, HypertraceSemanticAttributes::rpcResponseMetadata);
}
Expand All @@ -104,7 +110,9 @@ static final class TracingServerCallListener<ReqT>
@Override
public void onMessage(ReqT message) {
delegate().onMessage(message);
if (HypertraceConfig.get().getDataCapture().getRpcBody().getRequest().getValue()) {

InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
if (instrumentationConfig.rpcBody().request()) {
GrpcSpanDecorator.addMessageAttribute(
message, span, HypertraceSemanticAttributes.RPC_REQUEST_BODY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeoutException;
import org.hypertrace.agent.core.config.EnvironmentConfig;
import org.hypertrace.agent.core.config.HypertraceConfig;
import org.hypertrace.agent.core.config.InstrumentationConfig;
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
import org.hypertrace.agent.otel.extensions.config.EnvironmentConfig;
import org.hypertrace.agent.otel.extensions.config.HypertraceConfig;
import org.hypertrace.agent.testing.AbstractInstrumenterTest;
import org.hypertrace.example.GreeterGrpc;
import org.hypertrace.example.GreeterGrpc.GreeterBlockingStub;
Expand Down Expand Up @@ -190,6 +191,7 @@ public void disabledInstrumentation_dynamicConfig()
URL configUrl = getClass().getClassLoader().getResource("ht-config-all-disabled.yaml");
System.setProperty(EnvironmentConfig.CONFIG_FILE_PROPERTY, configUrl.getPath());
HypertraceConfig.reset();
InstrumentationConfig.ConfigProvider.reset();

GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(CHANNEL);
Response response = blockingStub.sayHello(REQUEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@
import javax.ws.rs.client.ClientResponseContext;
import javax.ws.rs.client.ClientResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
import org.hypertrace.agent.config.Config.AgentConfig;
import org.hypertrace.agent.core.config.HypertraceConfig;
import org.hypertrace.agent.core.config.InstrumentationConfig;
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JaxrsClientBodyCaptureFilter implements ClientRequestFilter, ClientResponseFilter {

private static final Logger log = LoggerFactory.getLogger(JaxrsClientBodyCaptureFilter.class);
private static final InstrumentationConfig instrumentationConfig =
InstrumentationConfig.ConfigProvider.get();

@Override
public void filter(ClientRequestContext requestContext) {
Expand All @@ -47,10 +48,9 @@ public void filter(ClientRequestContext requestContext) {

Context currentContext = (Context) contextObj;
Span currentSpan = Span.fromContext(currentContext);
AgentConfig agentConfig = HypertraceConfig.get();

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

Context currentContext = (Context) contextObj;
Span currentSpan = Span.fromContext(currentContext);
AgentConfig agentConfig = HypertraceConfig.get();

try {
if (agentConfig.getDataCapture().getHttpHeaders().getResponse().getValue()) {
if (instrumentationConfig.httpHeaders().response()) {
captureHeaders(
currentSpan,
HypertraceSemanticAttributes::httpResponseHeader,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
import javax.ws.rs.ext.ReaderInterceptorContext;
import javax.ws.rs.ext.WriterInterceptor;
import javax.ws.rs.ext.WriterInterceptorContext;
import org.hypertrace.agent.config.Config.AgentConfig;
import org.hypertrace.agent.core.config.HypertraceConfig;
import org.hypertrace.agent.core.config.InstrumentationConfig;
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
import org.hypertrace.agent.core.instrumentation.SpanAndBuffer;
import org.hypertrace.agent.core.instrumentation.buffer.BoundedBuffersFactory;
Expand All @@ -46,6 +45,8 @@
public class JaxrsClientEntityInterceptor implements ReaderInterceptor, WriterInterceptor {

private static final Logger log = LoggerFactory.getLogger(JaxrsClientEntityInterceptor.class);
private static final InstrumentationConfig instrumentationConfig =
InstrumentationConfig.ConfigProvider.get();

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

MediaType mediaType = responseContext.getMediaType();
AgentConfig agentConfig = HypertraceConfig.get();
if (mediaType == null
|| !ContentTypeUtils.shouldCapture(mediaType.toString())
|| !agentConfig.getDataCapture().getHttpBody().getResponse().getValue()) {
|| !instrumentationConfig.httpBody().response()) {
return responseContext.proceed();
}

Expand Down Expand Up @@ -125,8 +125,7 @@ public void aroundWriteTo(WriterInterceptorContext requestContext)
Context context = (Context) contextObj;
Span currentSpan = Span.fromContext(context);

AgentConfig agentConfig = HypertraceConfig.get();
if (agentConfig.getDataCapture().getHttpBody().getRequest().getValue()) {
if (instrumentationConfig.httpBody().request()) {
MediaType mediaType = requestContext.getMediaType();
if (mediaType == null || !ContentTypeUtils.shouldCapture(mediaType.toString())) {
requestContext.proceed();
Expand Down
Loading