Skip to content

pull main #6

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 4 commits into from
Feb 16, 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
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ subprojects {
extra.set("versions", mapOf(
"opentelemetry" to "0.15.0",
"opentelemetry_java_agent" to "0.15.1",
"byte_buddy" to "1.10.18"
"byte_buddy" to "1.10.18",
"slf4j" to "1.7.30"
))

apply<JavaPlugin>()
Expand Down
1 change: 1 addition & 0 deletions filter-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ dependencies {
api("io.opentelemetry:opentelemetry-api:${versions["opentelemetry"]}")
api(project(":javaagent-core"))
implementation("com.google.auto.service:auto-service:1.0-rc7")
implementation("org.slf4j:slf4j-api:${versions["slf4j"]}")
annotationProcessor("com.google.auto.service:auto-service:1.0-rc7")
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@

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.Iterator;
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;
import org.slf4j.LoggerFactory;

/**
* Provides access to the {@link Filter} implementations. The {@link Filter} implementation are
Expand All @@ -33,6 +39,8 @@
*/
public class FilterRegistry {

private static final Logger logger = LoggerFactory.getLogger(FilterRegistry.class);

private FilterRegistry() {}

private static Filter filter;
Expand All @@ -54,11 +62,10 @@ public static Filter getFilter() {
}

private static Filter load() {
ServiceLoader<FilterProvider> providers = ServiceLoader.load(FilterProvider.class);
ClassLoader cl = loadJars();
ServiceLoader<FilterProvider> providers = ServiceLoader.load(FilterProvider.class, cl);
List<Filter> filters = new ArrayList<>();
Iterator<FilterProvider> iterator = providers.iterator();
while (iterator.hasNext()) {
FilterProvider provider = iterator.next();
for (FilterProvider provider : providers) {
String disabled =
EnvironmentConfig.getProperty(getProviderDisabledPropertyName(provider.getClass()));
if ("true".equalsIgnoreCase(disabled)) {
Expand All @@ -70,6 +77,23 @@ private static Filter load() {
return new MultiFilter(filters);
}

private static ClassLoader loadJars() {
List<StringValue> jarPaths = HypertraceConfig.get().getJavaagent().getFilterJarPathsList();
URL[] urls = new URL[jarPaths.size()];
int i = 0;
for (StringValue jarPath : jarPaths) {
try {
URL url = new URL("file", "", -1, jarPath.getValue());
urls[i] = url;
i++;
} catch (MalformedURLException e) {
logger.warn(
String.format("Malformed URL exception for jar on path: %s", jarPath.getValue()), e);
}
}
return new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
}

public static String getProviderDisabledPropertyName(Class<?> clazz) {
return String.format("ht.filter.provider.%s.disabled", clazz.getSimpleName());
}
Expand Down
4 changes: 3 additions & 1 deletion filter-custom-opa/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ plugins {
`java-library`
}

val versions: Map<String, String> by extra

dependencies {
api(project(":filter-api"))
implementation("org.slf4j:slf4j-api:1.7.30")
implementation("org.slf4j:slf4j-api:${versions["slf4j"]}")
implementation("com.squareup.okhttp3:okhttp:3.14.9")
implementation("com.fasterxml.jackson.core:jackson-databind:2.11.3")
implementation("com.google.auto.service:auto-service:1.0-rc7")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ public static void exit(@Return Object response) {
}

byte[] bodyBytes = buffer.toByteArray();
System.out.printf("Captured response body: %s\n", new String(bodyBytes));
currentSpan.setAttribute(
HypertraceSemanticAttributes.HTTP_RESPONSE_BODY.getKey(), new String(bodyBytes));
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bodyBytes);
Expand All @@ -216,8 +215,6 @@ public static void exit(@Return Object response) {
// TODO log
e.printStackTrace();
}
} else {
System.out.println("\n\nIt is not HttpResponse #execute");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ static class TestAdvice {
public static void enter(
@Advice.Argument(0) ServletOutputStream servletOutputStream,
@Advice.Argument(1) BoundedByteArrayOutputStream buffer) {
System.out.println("adding to context");
ContextStore<ServletOutputStream, BoundedByteArrayOutputStream> contextStore =
InstrumentationContext.get(ServletOutputStream.class, BoundedByteArrayOutputStream.class);
contextStore.put(servletOutputStream, buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ public static void exit(
return;
}

System.out.println("Capturing readLine");
if (line == null) {
bufferSpanPair.captureBody(HypertraceSemanticAttributes.HTTP_REQUEST_BODY);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public TestBufferedReader(Reader in) {

@Override
public String readLine() throws IOException {
System.out.println("override readline");
return super.readLine();
}
}
2 changes: 1 addition & 1 deletion javaagent-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ val versions: Map<String, String> by extra
dependencies {
api("io.opentelemetry:opentelemetry-api:${versions["opentelemetry"]}")
api("io.opentelemetry.javaagent:opentelemetry-javaagent-api:${versions["opentelemetry_java_agent"]}")
implementation("org.slf4j:slf4j-api:1.7.30")
implementation("org.slf4j:slf4j-api:${versions["slf4j"]}")

api("com.google.protobuf:protobuf-java:3.11.4")
api("com.google.protobuf:protobuf-java-util:3.11.4")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.protobuf.StringValue;
import org.hypertrace.agent.config.Config.AgentConfig;
import org.hypertrace.agent.config.Config.DataCapture;
import org.hypertrace.agent.config.Config.JavaAgent;
import org.hypertrace.agent.config.Config.Message;
import org.hypertrace.agent.config.Config.Opa;
import org.hypertrace.agent.config.Config.Opa.Builder;
Expand All @@ -35,6 +36,7 @@ private EnvironmentConfig() {}

public static final String CONFIG_FILE_PROPERTY = HT_PREFIX + "config.file";
static final String SERVICE_NAME = HT_PREFIX + "service.name";
static final String ENABLED = HT_PREFIX + "enabled";

static final String PROPAGATION_FORMATS = HT_PREFIX + "propagation.formats";

Expand All @@ -54,11 +56,18 @@ private EnvironmentConfig() {}
public static final String CAPTURE_RPC_METADATA_PREFIX = CAPTURE_PREFIX + "rpc.metadata.";
public static final String CAPTURE_RPC_BODY_PREFIX = CAPTURE_PREFIX + "rpc.body.";

private static final String JAVAAGENT_PREFIX = HT_PREFIX + "javaagent.";
public static final String JAVAAGENT_FILTER_JAR_PATHS = JAVAAGENT_PREFIX + "filter.jar.paths";

public static AgentConfig.Builder applyPropertiesAndEnvVars(AgentConfig.Builder builder) {
String serviceName = getProperty(SERVICE_NAME);
if (serviceName != null) {
builder.setServiceName(StringValue.newBuilder().setValue(serviceName).build());
}
String enabled = getProperty(ENABLED);
if (enabled != null) {
builder.setEnabled(BoolValue.newBuilder().setValue(Boolean.valueOf(enabled)).build());
}

Reporting.Builder reportingBuilder = applyReporting(builder.getReporting().toBuilder());
builder.setReporting(reportingBuilder);
Expand All @@ -67,6 +76,20 @@ public static AgentConfig.Builder applyPropertiesAndEnvVars(AgentConfig.Builder
setDefaultsToDataCapture(builder.getDataCapture().toBuilder());
builder.setDataCapture(dataCaptureBuilder);
applyPropagationFormat(builder);
JavaAgent.Builder javaagentBuilder = applyJavaAgent(builder.getJavaagentBuilder());
builder.setJavaagent(javaagentBuilder);
return builder;
}

private static JavaAgent.Builder applyJavaAgent(JavaAgent.Builder builder) {
String filterJarPaths = getProperty(JAVAAGENT_FILTER_JAR_PATHS);
if (filterJarPaths != null) {
builder.clearFilterJarPaths();
String[] jarPaths = filterJarPaths.split(",");
for (String jarPath : jarPaths) {
builder.addFilterJarPaths(StringValue.newBuilder().setValue(jarPath));
}
}
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ private static AgentConfig.Builder applyDefaults(AgentConfig.Builder configBuild
if (configBuilder.getServiceName().getValue().isEmpty()) {
configBuilder.setServiceName(StringValue.newBuilder().setValue(DEFAULT_SERVICE_NAME).build());
}
if (!configBuilder.hasEnabled()) {
configBuilder.setEnabled(BoolValue.newBuilder().setValue(true).build());
}

Reporting.Builder reportingBuilder =
applyReportingDefaults(configBuilder.getReporting().toBuilder());
Expand Down
2 changes: 1 addition & 1 deletion javaagent-core/src/main/proto
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class EnvironmentConfigTest {
@ClearSystemProperty(key = EnvironmentConfig.PROPAGATION_FORMATS)
@ClearSystemProperty(key = EnvironmentConfig.CAPTURE_HTTP_BODY_PREFIX + "request")
@ClearSystemProperty(key = EnvironmentConfig.CAPTURE_BODY_MAX_SIZE_BYTES)
@ClearSystemProperty(key = EnvironmentConfig.JAVAAGENT_FILTER_JAR_PATHS)
@ClearSystemProperty(key = EnvironmentConfig.ENABLED)
public void systemProperties() {
// when tests are run in parallel the env vars/sys props set it junit-pioneer are visible to
// parallel tests
Expand All @@ -46,11 +48,14 @@ public void systemProperties() {
System.setProperty(EnvironmentConfig.OPA_ENABLED, "true");
System.setProperty(EnvironmentConfig.PROPAGATION_FORMATS, "B3,TRACECONTEXT");
System.setProperty(EnvironmentConfig.CAPTURE_BODY_MAX_SIZE_BYTES, "512");
System.setProperty(EnvironmentConfig.JAVAAGENT_FILTER_JAR_PATHS, "/path1.jar,/path/2/jar.jar");
System.setProperty(EnvironmentConfig.ENABLED, "false");

AgentConfig.Builder configBuilder = AgentConfig.newBuilder();
configBuilder.setServiceName(StringValue.newBuilder().setValue("foo"));

AgentConfig agentConfig = EnvironmentConfig.applyPropertiesAndEnvVars(configBuilder).build();
Assertions.assertEquals(false, agentConfig.getEnabled().getValue());
Assertions.assertEquals("foo", agentConfig.getServiceName().getValue());
Assertions.assertEquals(
Arrays.asList(PropagationFormat.B3, PropagationFormat.TRACECONTEXT),
Expand All @@ -65,5 +70,12 @@ public void systemProperties() {
Assertions.assertEquals(true, agentConfig.getReporting().getSecure().getValue());
Assertions.assertEquals(
true, agentConfig.getDataCapture().getHttpBody().getRequest().getValue());
Assertions.assertEquals(2, agentConfig.getJavaagent().getFilterJarPathsCount());
Assertions.assertEquals(
StringValue.newBuilder().setValue("/path1.jar").build(),
agentConfig.getJavaagent().getFilterJarPaths(0));
Assertions.assertEquals(
StringValue.newBuilder().setValue("/path/2/jar.jar").build(),
agentConfig.getJavaagent().getFilterJarPaths(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.hypertrace.agent.core.config;

import com.google.protobuf.StringValue;
import com.google.protobuf.util.JsonFormat;
import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -35,6 +36,7 @@ public class HypertraceConfigTest {
public void defaultValues() throws IOException {
URL resource = getClass().getClassLoader().getResource("emptyconfig.yaml");
AgentConfig agentConfig = HypertraceConfig.load(resource.getPath());
Assertions.assertTrue(agentConfig.getEnabled().getValue());
Assertions.assertEquals("unknown", agentConfig.getServiceName().getValue());
Assertions.assertEquals(
HypertraceConfig.DEFAULT_REPORTING_ENDPOINT,
Expand Down Expand Up @@ -67,6 +69,8 @@ public void defaultValues() throws IOException {
true, agentConfig.getDataCapture().getRpcBody().getRequest().getValue());
Assertions.assertEquals(
true, agentConfig.getDataCapture().getRpcBody().getResponse().getValue());
Assertions.assertTrue(agentConfig.hasJavaagent());
Assertions.assertEquals(0, agentConfig.getJavaagent().getFilterJarPathsCount());
}

@Test
Expand All @@ -93,6 +97,7 @@ public void jsonConfig(@TempDir File tempFolder) throws IOException {

private void assertConfig(AgentConfig agentConfig) {
Assertions.assertEquals("service", agentConfig.getServiceName().getValue());
Assertions.assertEquals(false, agentConfig.getEnabled().getValue());
Assertions.assertEquals(
Arrays.asList(PropagationFormat.B3), agentConfig.getPropagationFormatsList());
Assertions.assertEquals(
Expand All @@ -111,6 +116,13 @@ private void assertConfig(AgentConfig agentConfig) {
true, agentConfig.getDataCapture().getHttpBody().getRequest().getValue());
Assertions.assertEquals(
true, agentConfig.getDataCapture().getRpcBody().getRequest().getValue());
Assertions.assertEquals(2, agentConfig.getJavaagent().getFilterJarPathsCount());
Assertions.assertEquals(
StringValue.newBuilder().setValue("/path1.jar").build(),
agentConfig.getJavaagent().getFilterJarPaths(0));
Assertions.assertEquals(
StringValue.newBuilder().setValue("/path/2/jar.jar").build(),
agentConfig.getJavaagent().getFilterJarPaths(1));
}

@Test
Expand Down
5 changes: 5 additions & 0 deletions javaagent-core/src/test/resources/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# use snake case for newly added fields
service_name: service
enabled: false
propagationFormats:
- B3
reporting:
Expand All @@ -14,3 +15,7 @@ dataCapture:
request: true
response: false
httpBody:
javaagent:
filter_jar_paths:
- /path1.jar
- /path/2/jar.jar
2 changes: 1 addition & 1 deletion javaagent-tooling/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ dependencies {
instrumentationMuzzle("net.bytebuddy:byte-buddy-agent:1.10.18")
instrumentationMuzzle("com.blogspot.mydailyjava:weak-lock-free:0.15")
instrumentationMuzzle("com.google.auto.service:auto-service:1.0-rc7")
instrumentationMuzzle("org.slf4j:slf4j-api:1.7.30")
instrumentationMuzzle("org.slf4j:slf4j-api:${versions["slf4j"]}")
}
2 changes: 1 addition & 1 deletion otel-extensions/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies {
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure:${versions["opentelemetry"]}-alpha")
implementation("io.opentelemetry.javaagent:opentelemetry-javaagent-spi:${versions["opentelemetry_java_agent"]}")

implementation("org.slf4j:slf4j-api:1.7.30")
implementation("org.slf4j:slf4j-api:${versions["slf4j"]}")
implementation("com.google.auto.service:auto-service:1.0-rc7")
implementation("net.bytebuddy:byte-buddy:${versions["byte_buddy"]}")
annotationProcessor("com.google.auto.service:auto-service:1.0-rc7")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public String readContainerId() {
try (BufferedReader br = new BufferedReader(new FileReader(cgroupsPath))) {
String line;
while ((line = br.readLine()) != null) {
if (line.endsWith(".scope")) {
line = line.substring(0, line.length() - ".scope".length());
}
if (line.length() > CONTAINER_ID_LENGTH) {
String id = line.substring(line.length() - CONTAINER_ID_LENGTH);
if (!id.contains("/")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ public class HypertraceAgentConfiguration implements PropertySource {
private static final String OTEL_DEFAULT_LOG_LEVEL =
"io.opentelemetry.javaagent.slf4j.simpleLogger.defaultLogLevel";

private static final String OTEL_ENABLED = "otel.javaagent.enabled";

@Override
public Map<String, String> getProperties() {
AgentConfig agentConfig = HypertraceConfig.get();

Map<String, String> configProperties = new HashMap<>();
configProperties.put(OTEL_ENABLED, String.valueOf(agentConfig.getEnabled().getValue()));
configProperties.put(OTEL_TRACE_EXPORTER, "zipkin");
configProperties.put(
OTEL_EXPORTER_ZIPKIN_SERVICE_NAME, agentConfig.getServiceName().getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,16 @@ void readContainerId(@TempDir File tempFolder) throws IOException {
CgroupsReader cgroupsReader = new CgroupsReader(file.getPath());
Assertions.assertEquals(expected, cgroupsReader.readContainerId());
}

@Test
void readScopedContainerId(@TempDir File tempFolder) throws IOException {
File file = new File(tempFolder, "cgroup");
String expected = "736665661f3cf3ec691b2feeb2a1ec78918c0ef65381160bbb04f4c298169679";
String content =
"1:name=systemd:/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-podab2df320_6a91_4bc7_bd18_0d4328f04a8f.slice/crio-736665661f3cf3ec691b2feeb2a1ec78918c0ef65381160bbb04f4c298169679.scope";
Files.write(content.getBytes(Charsets.UTF_8), file);

CgroupsReader cgroupsReader = new CgroupsReader(file.getPath());
Assertions.assertEquals(expected, cgroupsReader.readContainerId());
}
}
Loading