Skip to content

Commit f371958

Browse files
authored
Parse agent args and pass it to system properties (#125)
* Parse agent args and pass it to system properties Signed-off-by: Pavol Loffay <[email protected]> * fix Signed-off-by: Pavol Loffay <[email protected]>
1 parent 85ede52 commit f371958

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

javaagent/src/main/java/org/hypertrace/agent/instrument/HypertraceAgent.java

+28
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import com.google.common.annotations.VisibleForTesting;
2020
import io.opentelemetry.javaagent.OpenTelemetryAgent;
2121
import java.lang.instrument.Instrumentation;
22+
import java.util.Collections;
23+
import java.util.HashMap;
2224
import java.util.List;
25+
import java.util.Map;
2326
import java.util.stream.Collectors;
2427
import org.hypertrace.agent.config.Config.AgentConfig;
2528
import org.hypertrace.agent.config.Config.PropagationFormat;
@@ -41,6 +44,11 @@ public static void premain(String agentArgs, Instrumentation inst) {
4144
}
4245

4346
public static void agentmain(String agentArgs, Instrumentation inst) {
47+
Map<String, String> parsedArgs = parseAgentArgs(agentArgs);
48+
for (Map.Entry<String, String> argEntry : parsedArgs.entrySet()) {
49+
System.setProperty(argEntry.getKey(), argEntry.getValue());
50+
}
51+
4452
setDefaultConfig();
4553
OpenTelemetryAgent.premain(agentArgs, inst);
4654
}
@@ -65,4 +73,24 @@ static String toOtelPropagators(List<PropagationFormat> propagationFormats) {
6573
.map(v -> v.name().toLowerCase().replaceAll("_", ""))
6674
.collect(Collectors.joining(","));
6775
}
76+
77+
// Expected format is "arg1=val1,arg2=val2,arg3=val3"
78+
private static Map<String, String> parseAgentArgs(String agentArgs) {
79+
if (agentArgs == null) {
80+
return Collections.emptyMap();
81+
}
82+
String[] agentArgsArr = agentArgs.split(",");
83+
Map<String, String> argsMap = new HashMap<>(agentArgsArr.length);
84+
for (String arg : agentArgsArr) {
85+
String[] splitAgentArg = arg.split("=");
86+
if (splitAgentArg.length != 2) {
87+
throw new IllegalArgumentException(
88+
String.format(
89+
"Agent args is not well formed: %s. Use the format \"arg1=val1,arg2=val2,arg3=val3\"",
90+
arg));
91+
}
92+
argsMap.put(splitAgentArg[0], splitAgentArg[1]);
93+
}
94+
return argsMap;
95+
}
6896
}

0 commit comments

Comments
 (0)