2727import java .text .SimpleDateFormat ;
2828import java .util .ArrayList ;
2929import java .util .Date ;
30+ import java .util .HashMap ;
3031import java .util .HashSet ;
3132import java .util .List ;
33+ import java .util .Map ;
3234import java .util .Objects ;
3335import java .util .Scanner ;
3436import java .util .Set ;
@@ -61,7 +63,7 @@ public static void main(String[] args) throws IOException, InterruptedException
6163 System .out .println (getJpsOutput ());
6264 } else if (arguments .getPid () != null ) {
6365 log ("INFO" , "Attaching the Elastic APM agent to %s" , arguments .getPid ());
64- ElasticApmAttacher .attach (arguments .getPid (), arguments .getArgs ());
66+ ElasticApmAttacher .attach (arguments .getPid (), arguments .getConfig ());
6567 log ("INFO" , "Done" );
6668 } else {
6769 do {
@@ -136,7 +138,7 @@ private void onJvmStart(JvmInfo jvmInfo) {
136138 }
137139
138140 private String getAgentArgs (JvmInfo jvmInfo ) throws IOException , InterruptedException {
139- return arguments .getArgsProvider () != null ? getArgsProviderOutput (jvmInfo ) : arguments .getArgs ( );
141+ return arguments .getArgsProvider () != null ? getArgsProviderOutput (jvmInfo ) : ElasticApmAttacher . toAgentArgs ( arguments .getConfig () );
140142 }
141143
142144 private String getArgsProviderOutput (JvmInfo jvmInfo ) throws IOException , InterruptedException {
@@ -211,34 +213,34 @@ static class Arguments {
211213 private final String pid ;
212214 private final List <String > includes ;
213215 private final List <String > excludes ;
214- private final String args ;
216+ private final Map < String , String > config ;
215217 private final String argsProvider ;
216218 private final boolean help ;
217219 private final boolean list ;
218220 private final boolean continuous ;
219221
220- private Arguments (String pid , List <String > includes , List <String > excludes , String args , String argsProvider , boolean help , boolean list , boolean continuous ) {
222+ private Arguments (String pid , List <String > includes , List <String > excludes , Map < String , String > config , String argsProvider , boolean help , boolean list , boolean continuous ) {
221223 this .help = help ;
222224 this .list = list ;
223225 this .continuous = continuous ;
224- if (args != null && argsProvider != null ) {
225- throw new IllegalArgumentException ("Providing both --args and --args-provider is illegal" );
226+ if (! config . isEmpty () && argsProvider != null ) {
227+ throw new IllegalArgumentException ("Providing both --config and --args-provider is illegal" );
226228 }
227229 if (pid != null && (!includes .isEmpty () || !excludes .isEmpty () || continuous )) {
228230 throw new IllegalArgumentException ("Providing --pid and either of --include, --exclude or --continuous is illegal" );
229231 }
230232 this .pid = pid ;
231233 this .includes = includes ;
232234 this .excludes = excludes ;
233- this .args = args ;
235+ this .config = config ;
234236 this .argsProvider = argsProvider ;
235237 }
236238
237239 static Arguments parse (String ... args ) {
238240 String pid = null ;
239241 List <String > includes = new ArrayList <>();
240242 List <String > excludes = new ArrayList <>();
241- String agentArgs = null ;
243+ Map < String , String > config = new HashMap <>() ;
242244 String argsProvider = null ;
243245 boolean help = args .length == 0 ;
244246 boolean list = false ;
@@ -264,6 +266,8 @@ static Arguments parse(String... args) {
264266 case "--pid" :
265267 case "-a" :
266268 case "--args" :
269+ case "-C" :
270+ case "--config" :
267271 case "-A" :
268272 case "--args-provider" :
269273 case "-e" :
@@ -290,7 +294,14 @@ static Arguments parse(String... args) {
290294 break ;
291295 case "-a" :
292296 case "--args" :
293- agentArgs = arg ;
297+ System .err .println ("--args is deprecated in favor of --config" );
298+ for (String conf : arg .split (";" )) {
299+ config .put (conf .substring (0 , conf .indexOf ('=' )), conf .substring (conf .indexOf ('=' ) + 1 ));
300+ }
301+ break ;
302+ case "-C" :
303+ case "--config" :
304+ config .put (arg .substring (0 , arg .indexOf ('=' )), arg .substring (arg .indexOf ('=' ) + 1 ));
294305 break ;
295306 case "-A" :
296307 case "--args-provider" :
@@ -301,7 +312,7 @@ static Arguments parse(String... args) {
301312 }
302313 }
303314 }
304- return new Arguments (pid , includes , excludes , agentArgs , argsProvider , help , list , continuous );
315+ return new Arguments (pid , includes , excludes , config , argsProvider , help , list , continuous );
305316 }
306317
307318 // -ab -> -a -b
@@ -323,7 +334,7 @@ void printHelp(PrintStream out) {
323334 out .println ("SYNOPSIS" );
324335 out .println (" java -jar apm-agent-attach.jar -p <pid> [--args <agent_arguments>]" );
325336 out .println (" java -jar apm-agent-attach.jar [-i <include_pattern>...] [-e <exclude_pattern>...] [--continuous]" );
326- out .println (" [--args <agent_arguments> | --args-provider <args_provider_script>]" );
337+ out .println (" [--config <key=value>... | --args-provider <args_provider_script>]" );
327338 out .println (" java -jar apm-agent-attach.jar (--list | --help)" );
328339 out .println ();
329340 out .println ("DESCRIPTION" );
@@ -348,9 +359,14 @@ void printHelp(PrintStream out) {
348359 out .println (" (Matches the output of 'jps -l')" );
349360 out .println ();
350361 out .println (" -a, --args <agent_arguments>" );
362+ out .println (" Deprecated in favor of --config." );
351363 out .println (" If set, the arguments are used to configure the agent on the attached JVM (agentArguments of agentmain)." );
352364 out .println (" The syntax of the arguments is 'key1=value1;key2=value1,value2'." );
353365 out .println ();
366+ out .println (" -C --config <key=value>..." );
367+ out .println (" This repeatable option sets one agent configuration option." );
368+ out .println (" Example: --config server_urls=http://localhost:8200,http://localhost:8201." );
369+ out .println ();
354370 out .println (" -A, --args-provider <args_provider_script>" );
355371 out .println (" The name of a program which is called when a new JVM starts up." );
356372 out .println (" The program gets the pid and the main class name or path to the JAR file as an argument" );
@@ -372,8 +388,8 @@ List<String> getExcludes() {
372388 return excludes ;
373389 }
374390
375- String getArgs () {
376- return args ;
391+ Map < String , String > getConfig () {
392+ return config ;
377393 }
378394
379395 String getArgsProvider () {
0 commit comments