19
19
import com .google .common .annotations .VisibleForTesting ;
20
20
import io .opentelemetry .javaagent .OpenTelemetryAgent ;
21
21
import java .lang .instrument .Instrumentation ;
22
+ import java .util .Collections ;
23
+ import java .util .HashMap ;
22
24
import java .util .List ;
25
+ import java .util .Map ;
23
26
import java .util .stream .Collectors ;
24
27
import org .hypertrace .agent .config .Config .AgentConfig ;
25
28
import org .hypertrace .agent .config .Config .PropagationFormat ;
@@ -41,6 +44,11 @@ public static void premain(String agentArgs, Instrumentation inst) {
41
44
}
42
45
43
46
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
+
44
52
setDefaultConfig ();
45
53
OpenTelemetryAgent .premain (agentArgs , inst );
46
54
}
@@ -65,4 +73,24 @@ static String toOtelPropagators(List<PropagationFormat> propagationFormats) {
65
73
.map (v -> v .name ().toLowerCase ().replaceAll ("_" , "" ))
66
74
.collect (Collectors .joining ("," ));
67
75
}
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
+ }
68
96
}
0 commit comments