Skip to content

Commit 3024aef

Browse files
committed
support dynamic attach
1 parent 353ec10 commit 3024aef

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/OpenTelemetryAgent.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.javaagent;
77

8+
import io.opentelemetry.javaagent.bootstrap.AgentArgUtil;
89
import io.opentelemetry.javaagent.bootstrap.AgentInitializer;
910
import io.opentelemetry.javaagent.bootstrap.InstrumentationHolder;
1011
import io.opentelemetry.javaagent.bootstrap.JavaagentFileHolder;
@@ -15,6 +16,7 @@
1516
import java.net.URL;
1617
import java.util.jar.JarFile;
1718
import java.util.jar.Manifest;
19+
import javax.annotation.Nullable;
1820

1921
/**
2022
* Premain-Class for the OpenTelemetry Java agent.
@@ -42,15 +44,17 @@
4244
public final class OpenTelemetryAgent {
4345

4446
public static void premain(String agentArgs, Instrumentation inst) {
45-
startAgent(inst, true);
47+
startAgent(inst, agentArgs, true);
4648
}
4749

4850
public static void agentmain(String agentArgs, Instrumentation inst) {
49-
startAgent(inst, false);
51+
startAgent(inst, agentArgs, false);
5052
}
5153

52-
private static void startAgent(Instrumentation inst, boolean fromPremain) {
54+
private static void startAgent(
55+
Instrumentation inst, @Nullable String agentArgs, boolean fromPremain) {
5356
try {
57+
AgentArgUtil.setSystemProperties(agentArgs);
5458
File javaagentFile = installBootstrapJar(inst);
5559
InstrumentationHolder.setInstrumentation(inst);
5660
JavaagentFileHolder.setJavaagentFile(javaagentFile);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.bootstrap;
7+
8+
import javax.annotation.Nullable;
9+
10+
public class AgentArgUtil {
11+
12+
private AgentArgUtil() {}
13+
14+
@SuppressWarnings("SystemOut")
15+
public static void setSystemProperties(@Nullable String agentArgs) {
16+
boolean debug = false;
17+
if (agentArgs != null && !agentArgs.isEmpty()) {
18+
String[] options = agentArgs.split(";");
19+
for (String option : options) {
20+
String[] keyValue = option.split("=");
21+
if (keyValue.length == 2) {
22+
if (keyValue[0].equals("otel.javaagent.debug")) {
23+
debug = Boolean.parseBoolean(keyValue[1]);
24+
} else {
25+
System.setProperty(keyValue[0], keyValue[1]);
26+
if (debug) {
27+
System.out.println("Setting property [" + keyValue[0] + "] = " + keyValue[1]);
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}
34+
}

smoke-tests/src/test/java/io/opentelemetry/smoketest/AbstractTestContainerManager.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ protected Map<String, String> getAgentEnvironment(
2727
jvmArgsEnvVarName,
2828
"-Xmx512m -javaagent:/"
2929
+ TARGET_AGENT_FILENAME
30+
// args passed to the agent directly
31+
+ "=otel.javaagent.debug=true;otel.bsp.schedule.delay=10ms"
3032
// Liberty20Jdk11, Payara6Jdk11 and Payara6Jdk17 fail with
3133
// java.util.zip.ZipException: Invalid CEN header (invalid zip64 extra data field size)
3234
+ " -Djdk.util.zip.disableZip64ExtraFieldValidation=true");
@@ -36,13 +38,11 @@ protected Map<String, String> getAgentEnvironment(
3638
environment.put("OTEL_EXPORTER_OTLP_PROTOCOL", "grpc");
3739

3840
environment.put("OTEL_BSP_MAX_EXPORT_BATCH_SIZE", "1");
39-
environment.put("OTEL_BSP_SCHEDULE_DELAY", "10ms");
4041
environment.put("OTEL_METRIC_EXPORT_INTERVAL", "1000");
4142
environment.put("OTEL_EXPORTER_OTLP_ENDPOINT", "http://" + BACKEND_ALIAS + ":8080");
4243
if (setServiceName) {
4344
environment.put("OTEL_RESOURCE_ATTRIBUTES", "service.name=smoke-test");
4445
}
45-
environment.put("OTEL_JAVAAGENT_DEBUG", "true");
4646
environment.put("OTEL_EXPERIMENTAL_JAVASCRIPT_SNIPPET", "<script>console.log(hi)</script>");
4747
environment.put("OTEL_INSTRUMENTATION_RUNTIME_TELEMETRY_PACKAGE_EMITTER_ENABLED", "true");
4848
return environment;

0 commit comments

Comments
 (0)