Skip to content

Commit 35897aa

Browse files
authored
Add support for enabled in config (#262)
* Add support for enabled in config Signed-off-by: Pavol Loffay <[email protected]> * smoke test Signed-off-by: Pavol Loffay <[email protected]>
1 parent 99b0082 commit 35897aa

File tree

8 files changed

+99
-3
lines changed

8 files changed

+99
-3
lines changed

javaagent-core/src/main/java/org/hypertrace/agent/core/config/EnvironmentConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ private EnvironmentConfig() {}
3636

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

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

@@ -63,6 +64,10 @@ public static AgentConfig.Builder applyPropertiesAndEnvVars(AgentConfig.Builder
6364
if (serviceName != null) {
6465
builder.setServiceName(StringValue.newBuilder().setValue(serviceName).build());
6566
}
67+
String enabled = getProperty(ENABLED);
68+
if (enabled != null) {
69+
builder.setEnabled(BoolValue.newBuilder().setValue(Boolean.valueOf(enabled)).build());
70+
}
6671

6772
Reporting.Builder reportingBuilder = applyReporting(builder.getReporting().toBuilder());
6873
builder.setReporting(reportingBuilder);

javaagent-core/src/main/java/org/hypertrace/agent/core/config/HypertraceConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ private static AgentConfig.Builder applyDefaults(AgentConfig.Builder configBuild
162162
if (configBuilder.getServiceName().getValue().isEmpty()) {
163163
configBuilder.setServiceName(StringValue.newBuilder().setValue(DEFAULT_SERVICE_NAME).build());
164164
}
165+
if (!configBuilder.hasEnabled()) {
166+
configBuilder.setEnabled(BoolValue.newBuilder().setValue(true).build());
167+
}
165168

166169
Reporting.Builder reportingBuilder =
167170
applyReportingDefaults(configBuilder.getReporting().toBuilder());

javaagent-core/src/test/java/org/hypertrace/agent/core/config/EnvironmentConfigTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class EnvironmentConfigTest {
3636
@ClearSystemProperty(key = EnvironmentConfig.CAPTURE_HTTP_BODY_PREFIX + "request")
3737
@ClearSystemProperty(key = EnvironmentConfig.CAPTURE_BODY_MAX_SIZE_BYTES)
3838
@ClearSystemProperty(key = EnvironmentConfig.JAVAAGENT_FILTER_JAR_PATHS)
39+
@ClearSystemProperty(key = EnvironmentConfig.ENABLED)
3940
public void systemProperties() {
4041
// when tests are run in parallel the env vars/sys props set it junit-pioneer are visible to
4142
// parallel tests
@@ -48,11 +49,13 @@ public void systemProperties() {
4849
System.setProperty(EnvironmentConfig.PROPAGATION_FORMATS, "B3,TRACECONTEXT");
4950
System.setProperty(EnvironmentConfig.CAPTURE_BODY_MAX_SIZE_BYTES, "512");
5051
System.setProperty(EnvironmentConfig.JAVAAGENT_FILTER_JAR_PATHS, "/path1.jar,/path/2/jar.jar");
52+
System.setProperty(EnvironmentConfig.ENABLED, "false");
5153

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

5557
AgentConfig agentConfig = EnvironmentConfig.applyPropertiesAndEnvVars(configBuilder).build();
58+
Assertions.assertEquals(false, agentConfig.getEnabled().getValue());
5659
Assertions.assertEquals("foo", agentConfig.getServiceName().getValue());
5760
Assertions.assertEquals(
5861
Arrays.asList(PropagationFormat.B3, PropagationFormat.TRACECONTEXT),

javaagent-core/src/test/java/org/hypertrace/agent/core/config/HypertraceConfigTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class HypertraceConfigTest {
3636
public void defaultValues() throws IOException {
3737
URL resource = getClass().getClassLoader().getResource("emptyconfig.yaml");
3838
AgentConfig agentConfig = HypertraceConfig.load(resource.getPath());
39+
Assertions.assertTrue(agentConfig.getEnabled().getValue());
3940
Assertions.assertEquals("unknown", agentConfig.getServiceName().getValue());
4041
Assertions.assertEquals(
4142
HypertraceConfig.DEFAULT_REPORTING_ENDPOINT,
@@ -69,6 +70,7 @@ public void defaultValues() throws IOException {
6970
Assertions.assertEquals(
7071
true, agentConfig.getDataCapture().getRpcBody().getResponse().getValue());
7172
Assertions.assertTrue(agentConfig.hasJavaagent());
73+
Assertions.assertEquals(0, agentConfig.getJavaagent().getFilterJarPathsCount());
7274
}
7375

7476
@Test
@@ -95,6 +97,7 @@ public void jsonConfig(@TempDir File tempFolder) throws IOException {
9597

9698
private void assertConfig(AgentConfig agentConfig) {
9799
Assertions.assertEquals("service", agentConfig.getServiceName().getValue());
100+
Assertions.assertEquals(false, agentConfig.getEnabled().getValue());
98101
Assertions.assertEquals(
99102
Arrays.asList(PropagationFormat.B3), agentConfig.getPropagationFormatsList());
100103
Assertions.assertEquals(

javaagent-core/src/test/resources/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# use snake case for newly added fields
22
service_name: service
3+
enabled: false
34
propagationFormats:
45
- B3
56
reporting:

otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/HypertraceAgentConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ public class HypertraceAgentConfiguration implements PropertySource {
4141
private static final String OTEL_DEFAULT_LOG_LEVEL =
4242
"io.opentelemetry.javaagent.slf4j.simpleLogger.defaultLogLevel";
4343

44+
private static final String OTEL_ENABLED = "otel.javaagent.enabled";
45+
4446
@Override
4547
public Map<String, String> getProperties() {
4648
AgentConfig agentConfig = HypertraceConfig.get();
4749

4850
Map<String, String> configProperties = new HashMap<>();
51+
configProperties.put(OTEL_ENABLED, String.valueOf(agentConfig.getEnabled().getValue()));
4952
configProperties.put(OTEL_TRACE_EXPORTER, "zipkin");
5053
configProperties.put(
5154
OTEL_EXPORTER_ZIPKIN_SERVICE_NAME, agentConfig.getServiceName().getValue());
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 org.hypertrace.agent.smoketest;
18+
19+
import java.io.IOException;
20+
import okhttp3.Request;
21+
import okhttp3.Response;
22+
import org.awaitility.core.ConditionTimeoutException;
23+
import org.junit.jupiter.api.AfterEach;
24+
import org.junit.jupiter.api.Assertions;
25+
import org.junit.jupiter.api.BeforeEach;
26+
import org.junit.jupiter.api.Test;
27+
import org.testcontainers.containers.GenericContainer;
28+
import org.testcontainers.utility.MountableFile;
29+
30+
public class SpringBootDiabledAgentSmokeTest extends AbstractSmokeTest {
31+
32+
@Override
33+
protected String getTargetImage(int jdk) {
34+
return "ghcr.io/open-telemetry/java-test-containers:smoke-springboot-jdk"
35+
+ jdk
36+
+ "-20210209.550405798";
37+
}
38+
39+
private GenericContainer app;
40+
41+
@BeforeEach
42+
void beforeEach() {
43+
app = createAppUnderTest(8);
44+
app.addEnv("HT_ENABLED", "false");
45+
app.withCopyFileToContainer(
46+
MountableFile.forClasspathResource("/ht-config-all-disabled.yaml"),
47+
"/etc/ht-config-all-disabled.yaml");
48+
app.withEnv("HT_CONFIG_FILE", "/etc/ht-config-all-disabled.yaml");
49+
app.start();
50+
}
51+
52+
@AfterEach
53+
void afterEach() {
54+
if (app != null) {
55+
app.stop();
56+
}
57+
}
58+
59+
@Test()
60+
public void get() throws IOException {
61+
// TODO test with multiple JDK (11, 14)
62+
String url = String.format("http://localhost:%d/greeting", app.getMappedPort(8080));
63+
Request request = new Request.Builder().url(url).get().build();
64+
65+
try (Response response = client.newCall(request).execute()) {
66+
Assertions.assertEquals(response.body().string(), "Hi!");
67+
}
68+
69+
Assertions.assertThrows(
70+
ConditionTimeoutException.class,
71+
() -> {
72+
waitForTraces();
73+
});
74+
}
75+
}

smoke-tests/src/test/java/org/hypertrace/agent/smoketest/SpringBootDisabledBodyCaptureSmokeTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public class SpringBootDisabledBodyCaptureSmokeTest extends AbstractSmokeTest {
3636

3737
@Override
3838
protected String getTargetImage(int jdk) {
39-
return "open-telemetry-docker-dev.bintray.io/java/smoke-springboot-jdk" + jdk + ":latest";
39+
return "ghcr.io/open-telemetry/java-test-containers:smoke-springboot-jdk"
40+
+ jdk
41+
+ "-20210209.550405798";
4042
}
4143

4244
private GenericContainer app;
@@ -65,7 +67,9 @@ public void get() throws IOException {
6567
String url = String.format("http://localhost:%d/greeting", app.getMappedPort(8080));
6668
Request request = new Request.Builder().url(url).get().build();
6769

68-
Response response = client.newCall(request).execute();
70+
try (Response response = client.newCall(request).execute()) {
71+
Assertions.assertEquals(response.body().string(), "Hi!");
72+
}
6973
Collection<ExportTraceServiceRequest> traces = waitForTraces();
7074

7175
Object currentAgentVersion =
@@ -74,7 +78,6 @@ public void get() throws IOException {
7478
.getMainAttributes()
7579
.get(Attributes.Name.IMPLEMENTATION_VERSION);
7680

77-
Assertions.assertEquals(response.body().string(), "Hi!");
7881
Assertions.assertEquals(1, countSpansByName(traces, "/greeting"));
7982
Assertions.assertEquals(1, countSpansByName(traces, "webcontroller.greeting"));
8083
Assertions.assertTrue(

0 commit comments

Comments
 (0)