Skip to content

Commit 165fcb8

Browse files
committed
Initial commit of JmxScraper code. Application compiles and runs as standalone and can read config file.
1 parent 5a82aac commit 165fcb8

File tree

12 files changed

+610
-18
lines changed

12 files changed

+610
-18
lines changed

.github/component_owners.yml

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ components:
4141
- sfriberg
4242
jmx-metrics:
4343
- breedx-splk
44+
jmx-scraper:
45+
- breedx-splk
46+
- robsunday
47+
- sylvainjuge
4448
maven-extension:
4549
- cyrille-leclerc
4650
- kenfinnigan

jmx-scrapper/README.md renamed to jmx-scraper/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# JMX Metric Scrapper
1+
# JMX Metric Scraper
22

33
This utility provides a way to query JMX metrics and export them to an OTLP endpoint.
44
The JMX MBeans and their metrics mapping is defined in YAML.

jmx-scrapper/build.gradle.kts renamed to jmx-scraper/build.gradle.kts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ plugins {
66
id("otel.publish-conventions")
77
}
88

9-
description = "JMX metrics scrapper"
10-
otelJava.moduleName.set("io.opentelemetry.contrib.jmxscrapper")
9+
description = "JMX metrics scraper"
10+
otelJava.moduleName.set("io.opentelemetry.contrib.jmxscraper")
1111

12-
application.mainClass.set("io.opentelemetry.contrib.jmxscrapper.JmxMetrics")
12+
application.mainClass.set("io.opentelemetry.contrib.jmxscraper.JmxScraper")
1313

1414
dependencies {
1515
implementation("io.opentelemetry:opentelemetry-api")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.jmxscraper;
7+
8+
import io.opentelemetry.contrib.jmxscraper.config.ConfigurationException;
9+
import io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig;
10+
import io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfigFactory;
11+
import io.opentelemetry.contrib.jmxscraper.jmx.JmxClient;
12+
import java.net.MalformedURLException;
13+
import java.util.Arrays;
14+
import java.util.concurrent.Executors;
15+
import java.util.concurrent.ScheduledExecutorService;
16+
import java.util.concurrent.TimeUnit;
17+
import java.util.logging.Logger;
18+
19+
public class JmxScraper {
20+
private static final Logger logger = Logger.getLogger(JmxScraper.class.getName());
21+
private final ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
22+
private final JmxScraperConfig config;
23+
24+
JmxScraper(JmxScraperConfig config) {
25+
this.config = config;
26+
27+
try {
28+
@SuppressWarnings("unused") // TODO: Temporary
29+
JmxClient jmxClient = new JmxClient(config);
30+
} catch (MalformedURLException e) {
31+
throw new ConfigurationException("Malformed serviceUrl: ", e);
32+
}
33+
}
34+
35+
@SuppressWarnings("FutureReturnValueIgnored") // TODO: Temporary
36+
private void start() {
37+
exec.scheduleWithFixedDelay(
38+
() -> {
39+
logger.fine("JMX scraping triggered");
40+
// try {
41+
// runner.run();
42+
// } catch (Throwable e) {
43+
// logger.log(Level.SEVERE, "Error gathering JMX metrics", e);
44+
// }
45+
},
46+
0,
47+
config.getIntervalMilliseconds(),
48+
TimeUnit.MILLISECONDS);
49+
logger.info("JMX scraping started");
50+
}
51+
52+
private void shutdown() {
53+
logger.info("Shutting down JmxScraper and exporting final metrics.");
54+
exec.shutdown();
55+
}
56+
57+
/**
58+
* Main method to create and run a {@link JmxScraper} instance.
59+
*
60+
* @param args - must be of the form "-config {jmx_config_path,'-'}"
61+
*/
62+
public static void main(String[] args) {
63+
JmxScraperConfigFactory factory = new JmxScraperConfigFactory();
64+
JmxScraperConfig config = factory.createConfigFromArgs(Arrays.asList(args));
65+
66+
JmxScraper jmxScraper = new JmxScraper(config);
67+
jmxScraper.start();
68+
69+
Runtime.getRuntime()
70+
.addShutdownHook(
71+
new Thread() {
72+
@Override
73+
public void run() {
74+
jmxScraper.shutdown();
75+
}
76+
});
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.jmxscraper.config;
7+
8+
public class ConfigurationException extends RuntimeException {
9+
private static final long serialVersionUID = 0L;
10+
11+
public ConfigurationException(String message, Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
public ConfigurationException(String message) {
16+
super(message);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.jmxscraper.config;
7+
8+
import java.util.Collections;
9+
import java.util.Set;
10+
11+
/** This class keeps application settings */
12+
public class JmxScraperConfig {
13+
String serviceUrl = "";
14+
String customJmxScrapingConfig = "";
15+
String targetSystem = "";
16+
Set<String> targetSystems = Collections.emptySet();
17+
int intervalMilliseconds;
18+
String metricsExporterType = "";
19+
20+
String otlpExporterEndpoint = "";
21+
22+
String prometheusExporterHost = "";
23+
int prometheusExporterPort;
24+
25+
String username = "";
26+
String password = "";
27+
String realm = "";
28+
String remoteProfile = "";
29+
boolean registrySsl;
30+
31+
JmxScraperConfig() {}
32+
33+
public String getServiceUrl() {
34+
return serviceUrl;
35+
}
36+
37+
public String getCustomJmxScrapingConfig() {
38+
return customJmxScrapingConfig;
39+
}
40+
41+
public String getTargetSystem() {
42+
return targetSystem;
43+
}
44+
45+
public Set<String> getTargetSystems() {
46+
return targetSystems;
47+
}
48+
49+
public int getIntervalMilliseconds() {
50+
return intervalMilliseconds;
51+
}
52+
53+
public String getMetricsExporterType() {
54+
return metricsExporterType;
55+
}
56+
57+
public String getOtlpExporterEndpoint() {
58+
return otlpExporterEndpoint;
59+
}
60+
61+
public String getPrometheusExporterHost() {
62+
return prometheusExporterHost;
63+
}
64+
65+
public int getPrometheusExporterPort() {
66+
return prometheusExporterPort;
67+
}
68+
69+
public String getUsername() {
70+
return username;
71+
}
72+
73+
public String getPassword() {
74+
return password;
75+
}
76+
77+
public String getRealm() {
78+
return realm;
79+
}
80+
81+
public String getRemoteProfile() {
82+
return remoteProfile;
83+
}
84+
85+
public boolean isRegistrySsl() {
86+
return registrySsl;
87+
}
88+
}

0 commit comments

Comments
 (0)