5
5
6
6
package io .opentelemetry .contrib .jmxscraper ;
7
7
8
- import io .opentelemetry .api .GlobalOpenTelemetry ;
9
8
import io .opentelemetry .contrib .jmxscraper .client .JmxRemoteClient ;
10
9
import io .opentelemetry .contrib .jmxscraper .config .ConfigurationException ;
11
10
import io .opentelemetry .contrib .jmxscraper .config .JmxScraperConfig ;
12
11
import io .opentelemetry .contrib .jmxscraper .config .JmxScraperConfigFactory ;
13
- import io .opentelemetry .instrumentation .jmx .engine .JmxMetricInsight ;
14
- import io .opentelemetry .instrumentation .jmx .engine .MetricConfiguration ;
15
12
import java .io .DataInputStream ;
16
13
import java .io .IOException ;
17
14
import java .io .InputStream ;
18
15
import java .nio .file .Files ;
19
16
import java .nio .file .Paths ;
20
17
import java .util .Arrays ;
21
- import java .util .Collections ;
22
18
import java .util .List ;
23
19
import java .util .Properties ;
24
- import java .util .concurrent .Executors ;
25
- import java .util .concurrent .ScheduledExecutorService ;
26
- import java .util .concurrent .TimeUnit ;
27
20
import java .util .logging .Logger ;
28
- import javax .annotation .Nullable ;
29
21
import javax .management .MBeanServerConnection ;
30
22
import javax .management .remote .JMXConnector ;
31
23
32
24
public class JmxScraper {
33
25
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
+
37
27
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;
40
31
41
32
/**
42
33
* Main method to create and run a {@link JmxScraper} instance.
@@ -52,7 +43,6 @@ public static void main(String[] args) {
52
43
JmxScraper jmxScraper = new JmxScraper (config );
53
44
jmxScraper .start ();
54
45
55
- Runtime .getRuntime ().addShutdownHook (new Thread (jmxScraper ::shutdown ));
56
46
} catch (ArgumentsParsingException e ) {
57
47
System .err .println (
58
48
"Usage: java -jar <path_to_jmxscraper.jar> "
@@ -106,7 +96,6 @@ private static void loadPropertiesFromPath(Properties props, String path)
106
96
}
107
97
108
98
JmxScraper (JmxScraperConfig config ) throws ConfigurationException {
109
- this .config = config ;
110
99
111
100
String serviceUrl = config .getServiceUrl ();
112
101
if (serviceUrl == null ) {
@@ -117,46 +106,25 @@ private static void loadPropertiesFromPath(Properties props, String path)
117
106
throw new ConfigurationException ("interval must be positive" );
118
107
}
119
108
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);
121
111
}
122
112
123
113
private void start () {
114
+ @ SuppressWarnings ("unused" )
115
+ MBeanServerConnection connection ;
124
116
try {
125
117
JMXConnector connector = client .connect ();
126
118
connection = connector .getMBeanServerConnection ();
127
119
} catch (IOException e ) {
128
120
throw new IllegalStateException (e );
129
121
}
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 ();
137
122
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));
140
127
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" );
161
129
}
162
130
}
0 commit comments