|
| 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 | +} |
0 commit comments