Search in sources :

Example 1 with NewtsReporter

use of org.opennms.newts.reporter.metrics.NewtsReporter in project newts by OpenNMS.

the class ImportRunner method execute.

public void execute(String... args) throws Exception {
    CmdLineParser parser = new CmdLineParser(this);
    try {
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        // handling of wrong arguments
        System.err.println(e.getMessage());
        parser.printUsage(System.err);
        return;
    }
    // Setup the slf4j metrics reporter
    MetricRegistry metrics = new MetricRegistry();
    final long start = System.currentTimeMillis();
    metrics.register("elapsed-seconds", new Gauge<Double>() {

        @Override
        public Double getValue() {
            return (System.currentTimeMillis() - start) / 1000.0;
        }
    });
    final ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).outputTo(System.err).convertRatesTo(SECONDS).convertDurationsTo(MILLISECONDS).build();
    reporter.start(10, SECONDS);
    if (m_restUrl == null) {
        // we are using a direct importer so use a NewtsReporter for storing metrics
        NewtsReporter newtsReporter = NewtsReporter.forRegistry(metrics).name("importer").convertRatesTo(SECONDS).convertDurationsTo(MILLISECONDS).build(repository());
        newtsReporter.start(1, SECONDS);
    }
    LOG.debug("Scanning {} for GSOD data files...", m_source);
    // walk the files in the directory given
    Observable<Sample> samples = fileTreeWalker(m_source.toPath()).subscribeOn(Schedulers.io()).map(meter(metrics.meter("files"), Path.class)).map(reportFile()).mergeMap(lines()).filter(exclude("YEARMODA")).mergeMap(samples()).map(adjustTime()).map(meter(metrics.meter("samples"), Sample.class));
    Observable<List<Sample>> batches = samples.buffer(m_samplesPerBatch);
    Observable<Boolean> doImport = m_restUrl != null ? restPoster(batches, metrics) : directPoster(batches, metrics);
    System.err.println("doImport = " + doImport);
    // GO!!!
    final AtomicReference<Subscription> subscription = new AtomicReference<>();
    final AtomicBoolean failed = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(1);
    Subscription s = doImport.subscribe(new Observer<Boolean>() {

        @Override
        public void onCompleted() {
            System.err.println("Finished Importing Everything!");
            reporter.report();
            latch.countDown();
            System.exit(0);
        }

        @Override
        public void onError(Throwable e) {
            failed.set(true);
            System.err.println("Error importing!");
            e.printStackTrace();
            try {
                // latch.await();
                Subscription s = subscription.get();
                if (s != null)
                    s.unsubscribe();
            } catch (Exception ex) {
                System.err.println("Failed to close httpClient!");
                ex.printStackTrace();
            } finally {
            // dumpThreads();
            }
        }

        @Override
        public void onNext(Boolean t) {
            System.err.println("Received a boolen: " + t);
        }
    });
    subscription.set(s);
    if (failed.get()) {
        s.unsubscribe();
    }
    // latch.countDown();
    System.err.println("Return from Subscribe!");
    latch.await();
// dumpThreads();
}
Also used : Path(java.nio.file.Path) CmdLineParser(org.kohsuke.args4j.CmdLineParser) ConsoleReporter(com.codahale.metrics.ConsoleReporter) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ParseException(java.text.ParseException) CmdLineException(org.kohsuke.args4j.CmdLineException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) List(java.util.List) NewtsReporter(org.opennms.newts.reporter.metrics.NewtsReporter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Subscription(rx.Subscription) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

ConsoleReporter (com.codahale.metrics.ConsoleReporter)1 MetricRegistry (com.codahale.metrics.MetricRegistry)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Path (java.nio.file.Path)1 ParseException (java.text.ParseException)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CmdLineException (org.kohsuke.args4j.CmdLineException)1 CmdLineParser (org.kohsuke.args4j.CmdLineParser)1 Sample (org.opennms.newts.api.Sample)1 NewtsReporter (org.opennms.newts.reporter.metrics.NewtsReporter)1 Subscription (rx.Subscription)1