Skip to content

UsageDropwizard

Rajiv edited this page Oct 31, 2019 · 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 metrics-apptuit, to report the metrics directly to Apptuit. It is as simple as 1-2-3

Aside: JInsight provides a java-agent that extracts a wide array of metrics, from a JVM process - "no coding necessary". If you dig metrics, you will love JInsight!

1. Include maven dependencies

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

Sample snippet for a Maven pom.xml:

<repositories>
  <repository>
    <id>apptuitai-bintray</id>
    <url>https://dl.bintray.com/apptuitai/maven</url>
  </repository>
</repositories>
<dependencies>
  <dependency>
    <groupId>ai.apptuit.metrics</groupId>
    <artifactId>metrics-apptuit-dropwizard</artifactId>
    <version>${metrics.apptuit.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