55
66package io .opentelemetry .contrib .jmxscraper ;
77
8- import io .opentelemetry .api .GlobalOpenTelemetry ;
98import io .opentelemetry .contrib .jmxscraper .client .JmxRemoteClient ;
109import io .opentelemetry .contrib .jmxscraper .config .ConfigurationException ;
1110import io .opentelemetry .contrib .jmxscraper .config .JmxScraperConfig ;
1211import io .opentelemetry .contrib .jmxscraper .config .JmxScraperConfigFactory ;
13- import io .opentelemetry .instrumentation .jmx .engine .JmxMetricInsight ;
14- import io .opentelemetry .instrumentation .jmx .engine .MetricConfiguration ;
1512import java .io .DataInputStream ;
1613import java .io .IOException ;
1714import java .io .InputStream ;
1815import java .nio .file .Files ;
1916import java .nio .file .Paths ;
2017import java .util .Arrays ;
21- import java .util .Collections ;
2218import java .util .List ;
2319import java .util .Properties ;
24- import java .util .concurrent .Executors ;
25- import java .util .concurrent .ScheduledExecutorService ;
26- import java .util .concurrent .TimeUnit ;
2720import java .util .logging .Logger ;
28- import javax .annotation .Nullable ;
2921import javax .management .MBeanServerConnection ;
3022import javax .management .remote .JMXConnector ;
3123
3224public class JmxScraper {
3325 private static final Logger logger = Logger .getLogger (JmxScraper .class .getName ());
34- private static final int EXECUTOR_TERMINATION_TIMEOUT_MS = 5000 ;
35- private final ScheduledExecutorService exec = Executors .newSingleThreadScheduledExecutor ();
36- private final JmxScraperConfig config ;
26+
3727 private final JmxRemoteClient client ;
38- private final JmxMetricInsight service ;
39- @ Nullable private MBeanServerConnection connection ;
28+
29+ // TODO depend on instrumentation 2.9.0 snapshot
30+ // private final JmxMetricInsight service;
4031
4132 /**
4233 * Main method to create and run a {@link JmxScraper} instance.
@@ -52,7 +43,6 @@ public static void main(String[] args) {
5243 JmxScraper jmxScraper = new JmxScraper (config );
5344 jmxScraper .start ();
5445
55- Runtime .getRuntime ().addShutdownHook (new Thread (jmxScraper ::shutdown ));
5646 } catch (ArgumentsParsingException e ) {
5747 System .err .println (
5848 "Usage: java -jar <path_to_jmxscraper.jar> "
@@ -106,7 +96,6 @@ private static void loadPropertiesFromPath(Properties props, String path)
10696 }
10797
10898 JmxScraper (JmxScraperConfig config ) throws ConfigurationException {
109- this .config = config ;
11099
111100 String serviceUrl = config .getServiceUrl ();
112101 if (serviceUrl == null ) {
@@ -117,46 +106,25 @@ private static void loadPropertiesFromPath(Properties props, String path)
117106 throw new ConfigurationException ("interval must be positive" );
118107 }
119108 this .client = JmxRemoteClient .createNew (serviceUrl );
120- this .service = JmxMetricInsight .createService (GlobalOpenTelemetry .get (), interval );
109+ // TODO: depend on instrumentation 2.9.0 snapshot
110+ // this.service = JmxMetricInsight.createService(GlobalOpenTelemetry.get(), interval);
121111 }
122112
123113 private void start () {
114+ @ SuppressWarnings ("unused" )
115+ MBeanServerConnection connection ;
124116 try {
125117 JMXConnector connector = client .connect ();
126118 connection = connector .getMBeanServerConnection ();
127119 } catch (IOException e ) {
128120 throw new IllegalStateException (e );
129121 }
130- service .startRemote (getMetricConfig (config ), () -> Collections .singletonList (connection ));
131- logger .info ("JMX scraping started" );
132- }
133-
134- @ SuppressWarnings ("unused" )
135- private static MetricConfiguration getMetricConfig (JmxScraperConfig config ) {
136- MetricConfiguration metricConfig = new MetricConfiguration ();
137122
138- return metricConfig ;
139- }
123+ // TODO: depend on instrumentation 2.9.0 snapshot
124+ // MetricConfiguration metricConfig = new MetricConfiguration();
125+ // TODO create JMX insight config from scraper config
126+ // service.startRemote(metricConfig, () -> Collections.singletonList(connection));
140127
141- private void shutdown () {
142- logger .info ("Shutting down JmxScraper and exporting final metrics." );
143- // Prevent new tasks to be submitted
144- exec .shutdown ();
145- try {
146- // Wait a while for existing tasks to terminate
147- if (!exec .awaitTermination (EXECUTOR_TERMINATION_TIMEOUT_MS , TimeUnit .MILLISECONDS )) {
148- // Cancel currently executing tasks
149- exec .shutdownNow ();
150- // Wait a while for tasks to respond to being cancelled
151- if (!exec .awaitTermination (EXECUTOR_TERMINATION_TIMEOUT_MS , TimeUnit .MILLISECONDS )) {
152- logger .warning ("Thread pool did not terminate in time: " + exec );
153- }
154- }
155- } catch (InterruptedException e ) {
156- // (Re-)Cancel if current thread also interrupted
157- exec .shutdownNow ();
158- // Preserve interrupt status
159- Thread .currentThread ().interrupt ();
160- }
128+ logger .info ("JMX scraping started" );
161129 }
162130}
0 commit comments