Skip to content

UsageDropwizard

Rajiv edited this page Feb 27, 2018 · 4 revisions

Dropwizard metrics is a powerful java library to measure the behavior of critical components in your production environment.

If you are using Dropwizard Metrics to instrument your code, you can use JInsight, to report the metrics directly to Apptuit. It is as simple as 1-2-3

Note: JInsight also provides a java-agent that extracts a wide array of metrics, from a JVM process - "no coding necessary". Dropwizard integration is recommended only if these out-of-box metrics are inadequate.

1. Include maven dependencies

If you are using Maven/Gradle/Ivy, you can get the dependency definition from the JInsight project on Bintray

Alternatively you could download the latest jar from the Releases Page

Sample snippet for a Maven pom.xml:

<repositories>
  <repository>
    <id>jinsight-bintray</id>
    <url>https://dl.bintray.com/apptuitai/maven</url>
  </repository>
</repositories>
<dependencies>
  <dependency>
    <groupId>ai.apptuit</groupId>
    <artifactId>jinsight</artifactId>
    <version>${jinsight.version}</version>
  </dependency>
</dependencies>

2. Initialize the Apptuit Reporter

Configure you Access Token and schedule the reporter:

private static final MetricRegistry registry = new MetricRegistry();

static {
    initApptuitReporter(registry, hostname);
}

private static void initApptuitReporter(MetricRegistry registry, String hostname) {
    ApptuitReporterFactory factory = new ApptuitReporterFactory();
    factory.setRateUnit(TimeUnit.SECONDS);
    factory.setDurationUnit(TimeUnit.MILLISECONDS);
    factory.addGlobalTag("hostname", hostname);
    factory.setApiKey(APPTUIT_API_KEY);

    ScheduledReporter reporter = factory.build(registry);
    reporter.start(5, TimeUnit.SECONDS);
}

3. Instrument your code

Instrument your code with Dropwizard Metrics API. Refer to the Dropwizard Metrics - Getting Started Guide to understand various measuring instruments that are available.

private final Timer responses = metrics.timer(name(RequestHandler.class, "responses"));

public String handleRequest(Request request, Response response) {
    final Timer.Context context = responses.time();
    try {
        // etc;
        return "OK";
    } finally {
        context.stop();
    }
}

... and you are done!

Clone this wiki locally