|
25 | 25 | import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics; |
26 | 26 | import io.micrometer.core.instrument.binder.system.ProcessorMetrics; |
27 | 27 | import io.micrometer.core.instrument.composite.CompositeMeterRegistry; |
| 28 | +import java.util.Arrays; |
28 | 29 | import java.util.Collections; |
| 30 | +import java.util.HashMap; |
| 31 | +import java.util.Iterator; |
| 32 | +import java.util.Map; |
29 | 33 | import java.util.concurrent.atomic.AtomicInteger; |
30 | 34 | import org.apache.commons.cli.Option; |
31 | 35 | import org.apache.commons.cli.Options; |
@@ -72,7 +76,7 @@ public void configure(ConfigurationContext context) { |
72 | 76 | if (args == null || args.length == 0) { |
73 | 77 | tags = Tags.of("command_line", ""); |
74 | 78 | } else { |
75 | | - tags = Tags.of("command_line", String.join(" ", args)); |
| 79 | + tags = Tags.of("command_line", commandLineMetrics(args, context.metricsOptions())); |
76 | 80 | } |
77 | 81 | meterRegistry.gauge(metricsPrefix + "args", tags, argumentsGauge); |
78 | 82 | } |
@@ -114,4 +118,29 @@ static Collection<Tag> parseTags(String argument) { |
114 | 118 | return tags; |
115 | 119 | } |
116 | 120 | } |
| 121 | + |
| 122 | + static String commandLineMetrics(String [] args, Options metricsOptions) { |
| 123 | + Map<String, Boolean> filteredOptions = new HashMap<>(); |
| 124 | + filteredOptions.put("--uri", true); |
| 125 | + filteredOptions.put("-h", true); |
| 126 | + for (Option option : metricsOptions.getOptions()) { |
| 127 | + filteredOptions.put("-" + option.getOpt(), option.hasArg()); |
| 128 | + if (option.hasLongOpt()) { |
| 129 | + filteredOptions.put("--" + option.getLongOpt(), option.hasArg()); |
| 130 | + } |
| 131 | + } |
| 132 | + Collection<String> filtered = new ArrayList<>(); |
| 133 | + Iterator<String> iterator = Arrays.stream(args).iterator(); |
| 134 | + while(iterator.hasNext()) { |
| 135 | + String option = iterator.next(); |
| 136 | + if (filteredOptions.containsKey(option)) { |
| 137 | + if (filteredOptions.get(option)) { |
| 138 | + iterator.next(); |
| 139 | + } |
| 140 | + } else { |
| 141 | + filtered.add(option); |
| 142 | + } |
| 143 | + } |
| 144 | + return String.join(" ", filtered); |
| 145 | + } |
117 | 146 | } |
0 commit comments