Skip to content

Commit aaa7b9d

Browse files
committed
impl new config option and resolve yaml
1 parent f55ba57 commit aaa7b9d

File tree

3 files changed

+82
-39
lines changed

3 files changed

+82
-39
lines changed

jmx-scraper/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Supported values for `otel.jmx.target.system` and support for `otel.jmx.target.s
7676
The source of metrics definitions is controlled by `otel.jmx.target.source`:
7777

7878
- `auto` (default) : metrics definitions from `instrumentation` with fallback on `legacy` when not available.
79-
- `legacy` : metrics definitions embedded in jmx-scraper, almost equivalent to [JMX Gatherer](https://github.com/open-telemetry/opentelemetry-java-contrib/tree/main/jmx-metrics).
79+
- `legacy` : metrics definitions embedded in jmx-scraper, almost equivalent to [JMX Gatherer](https://github.com/open-telemetry/opentelemetry-java-contrib/tree/main/jmx-metrics).
8080
- `instrumentation` : metrics definitions embedded in [instrumentation/jmx-metrics](https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/jmx-metrics/library) library
8181

8282
Setting the value of `otel.jmx.target.source` allows to fit the following use-cases:

jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper/JmxScraper.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -228,28 +228,18 @@ private void start() throws IOException {
228228
private static MetricConfiguration getMetricConfig(JmxScraperConfig scraperConfig) {
229229
MetricConfiguration config = new MetricConfiguration();
230230
for (String system : scraperConfig.getTargetSystems()) {
231-
addRulesForSystem(system, config);
231+
try (InputStream yaml = scraperConfig.getTargetSystemYaml(system)) {
232+
RuleParser.get().addMetricDefsTo(config, yaml, system);
233+
} catch (IOException e) {
234+
throw new IllegalStateException("Error while loading rules for system " + system, e);
235+
}
232236
}
233237
for (String file : scraperConfig.getJmxConfig()) {
234238
addRulesFromFile(file, config);
235239
}
236240
return config;
237241
}
238242

239-
private static void addRulesForSystem(String system, MetricConfiguration conf) {
240-
String yamlResource = system + ".yaml";
241-
try (InputStream inputStream =
242-
JmxScraper.class.getClassLoader().getResourceAsStream(yamlResource)) {
243-
if (inputStream != null) {
244-
RuleParser.get().addMetricDefsTo(conf, inputStream, system);
245-
} else {
246-
throw new IllegalArgumentException("No support for system " + system);
247-
}
248-
} catch (Exception e) {
249-
throw new IllegalStateException("Error while loading rules for system " + system, e);
250-
}
251-
}
252-
253243
private static void addRulesFromFile(String file, MetricConfiguration conf) {
254244
Path path = Paths.get(file);
255245
if (!Files.isReadable(path)) {

jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper/config/JmxScraperConfig.java

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77

88
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
99
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
10+
import java.io.InputStream;
1011
import java.time.Duration;
1112
import java.util.ArrayList;
12-
import java.util.Arrays;
1313
import java.util.Collections;
1414
import java.util.HashSet;
1515
import java.util.List;
16+
import java.util.Locale;
1617
import java.util.Set;
1718
import java.util.logging.Logger;
1819
import javax.annotation.Nullable;
@@ -37,6 +38,7 @@ public class JmxScraperConfig {
3738
static final String JMX_CONFIG_LEGACY = "otel.jmx.custom.scraping.config";
3839

3940
static final String JMX_TARGET_SYSTEM = "otel.jmx.target.system";
41+
static final String JMX_TARGET_SOURCE = "otel.jmx.target.source";
4042

4143
static final String JMX_USERNAME = "otel.jmx.username";
4244
static final String JMX_PASSWORD = "otel.jmx.password";
@@ -46,28 +48,14 @@ public class JmxScraperConfig {
4648
static final String JMX_REMOTE_PROFILE = "otel.jmx.remote.profile";
4749
static final String JMX_REALM = "otel.jmx.realm";
4850

49-
private static final List<String> AVAILABLE_TARGET_SYSTEMS =
50-
Collections.unmodifiableList(
51-
Arrays.asList(
52-
"activemq",
53-
"cassandra",
54-
"hbase",
55-
"hadoop",
56-
"jetty",
57-
"jvm",
58-
"kafka",
59-
"kafka-consumer",
60-
"kafka-producer",
61-
"solr",
62-
"tomcat",
63-
"wildfly"));
64-
6551
private String serviceUrl = "";
6652

6753
private List<String> jmxConfig = Collections.emptyList();
6854

6955
private Set<String> targetSystems = Collections.emptySet();
7056

57+
private TargetSystemSource targetSystemSource = TargetSystemSource.AUTO;
58+
7159
private Duration samplingInterval = Duration.ofMinutes(1);
7260

7361
@Nullable private String username;
@@ -79,6 +67,20 @@ public class JmxScraperConfig {
7967
@Nullable private String remoteProfile;
8068
private boolean registrySsl;
8169

70+
public enum TargetSystemSource {
71+
AUTO,
72+
INSTRUMENTATION,
73+
LEGACY;
74+
75+
public static TargetSystemSource fromString(String source) {
76+
try {
77+
return TargetSystemSource.valueOf(source.toUpperCase(Locale.ROOT));
78+
} catch (IllegalArgumentException e) {
79+
throw new IllegalArgumentException("Invalid target system source: " + source, e);
80+
}
81+
}
82+
}
83+
8284
private JmxScraperConfig() {}
8385

8486
public String getServiceUrl() {
@@ -93,6 +95,57 @@ public Set<String> getTargetSystems() {
9395
return targetSystems;
9496
}
9597

98+
/**
99+
* Resolves the target system yaml from configuration
100+
*
101+
* @param system target system
102+
* @return input stream on target system yaml definitions
103+
* @throws ConfigurationException when no yaml for system is available
104+
*/
105+
public InputStream getTargetSystemYaml(String system) {
106+
InputStream yaml;
107+
switch (targetSystemSource) {
108+
case LEGACY:
109+
yaml = getTargetSystemYaml(system, TargetSystemSource.LEGACY);
110+
break;
111+
case INSTRUMENTATION:
112+
yaml = getTargetSystemYaml(system, TargetSystemSource.INSTRUMENTATION);
113+
break;
114+
case AUTO:
115+
yaml = getTargetSystemYaml(system, TargetSystemSource.INSTRUMENTATION);
116+
if (yaml == null) {
117+
yaml = getTargetSystemYaml(system, TargetSystemSource.LEGACY);
118+
}
119+
break;
120+
default:
121+
throw new IllegalStateException("unsupported target system source: " + targetSystemSource);
122+
}
123+
124+
if (yaml == null) {
125+
throw new ConfigurationException(
126+
"unsupported target system: '" + system + "', source: " + targetSystemSource);
127+
}
128+
return yaml;
129+
}
130+
131+
@Nullable
132+
private static InputStream getTargetSystemYaml(String system, TargetSystemSource source) {
133+
String path;
134+
switch (source) {
135+
case LEGACY:
136+
path = String.format("%s.yaml", system);
137+
break;
138+
case INSTRUMENTATION:
139+
path = String.format("jmx/rules/%s.yaml", system);
140+
break;
141+
case AUTO:
142+
default:
143+
throw new IllegalArgumentException("invalid source" + source);
144+
}
145+
146+
return JmxScraperConfig.class.getClassLoader().getResourceAsStream(path);
147+
}
148+
96149
public Duration getSamplingInterval() {
97150
return samplingInterval;
98151
}
@@ -164,12 +217,6 @@ public static JmxScraperConfig fromConfig(ConfigProperties config) {
164217
throw new ConfigurationException(
165218
"at least one of '" + JMX_TARGET_SYSTEM + "' or '" + JMX_CONFIG + "' must be set");
166219
}
167-
targetSystem.forEach(
168-
s -> {
169-
if (!AVAILABLE_TARGET_SYSTEMS.contains(s)) {
170-
throw new ConfigurationException("unsupported target system: '" + s + "'");
171-
}
172-
});
173220

174221
scraperConfig.jmxConfig = Collections.unmodifiableList(jmxConfig);
175222
scraperConfig.targetSystems = Collections.unmodifiableSet(new HashSet<>(targetSystem));
@@ -180,6 +227,12 @@ public static JmxScraperConfig fromConfig(ConfigProperties config) {
180227
scraperConfig.realm = config.getString("otel.jmx.realm");
181228
scraperConfig.registrySsl = config.getBoolean("otel.jmx.remote.registry.ssl", false);
182229

230+
// ensures support for target systems by resolving the yaml resource
231+
scraperConfig.targetSystems.forEach(scraperConfig::getTargetSystemYaml);
232+
233+
String source = config.getString(JMX_TARGET_SOURCE, TargetSystemSource.AUTO.name());
234+
scraperConfig.targetSystemSource = TargetSystemSource.fromString(source);
235+
183236
return scraperConfig;
184237
}
185238
}

0 commit comments

Comments
 (0)