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();
}
Aggregations