public class Server {
public static void main(String[] args) throws Exception {
// configure reporter
final MetricRegistry metrics = new MetricRegistry();
ElasticsearchReporter reporter = ElasticsearchReporter.forRegistry(metrics)
// support for several es nodes
.hosts("localhost:9200", "localhost:9201")
// just create an index, no date format, means one index only
.index("metrics")
.indexDateFormat(null)
// define a percolation check on all metrics
.percolationFilter(MetricFilter.ALL)
.percolationNotifier(new SystemOutNotifier())
//.percolationNotifier(new HttpNotifier())
.build();
// usually you set this to one minute
reporter.start(10, TimeUnit.SECONDS);
// start up background thread
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(new StreamMeetupComTask(metrics));
// start up web app
get(new Route("/") {
@Override
public Object handle(Request request, Response response) {
return "this is /";
}
});
post(new Route("/checkout") {
@Override
public Object handle(Request request, Response response) {
// emulate calling external payment API
Timer.Context timer = metrics.timer("payment.runtime").time();
try {
Thread.sleep(new Random().nextInt(1000));
} catch (InterruptedException e) {
// ignore
} finally {
timer.stop();
}
response.redirect("/");
return null;
}
});
before(new Filter() {
@Override
public void handle(Request request, Response response) {
metrics.counter("http.connections.concurrent").inc();
metrics.meter("http.connections.requests").mark();
metrics.counter("http.connections.stats." + request.raw().getMethod()).inc();
metrics.counter("http.connections.stats." + request.raw().getMethod() + "." + request.pathInfo()).inc();
}
});
after(new Filter() {
@Override
public void handle(Request request, Response response) {
metrics.counter("http.connections.concurrent").dec();
}
});
}