Track your own metrics

Felix edited this page Jul 15, 2016 · 8 revisions

Declarative (Annotation-Based)

Stagemonitor supports the annotations defined in metrics-annotation by instrumenting your code.

@Timed

Annotating a method with @Timed will create a timer for that method, that is automatically updated every time, the method is executed.

Stagemonitor also automatically creates timers for the following async call annotations:

org.springframework.scheduling.annotation.Async
org.springframework.scheduling.annotation.Scheduled
org.springframework.scheduling.annotation.Schedules
javax.ejb.Asynchronous
javax.ejb.Schedule

@Metered

Annotating a method with @Metered will create a meter for that method, that will keep track of the execution rate (or throughput) and execution count of the method.

@ExceptionMetered

Annotating a method with @ExceptionMetered will create a meter for that method, that will keep track of the rate (or throughput) and execution count of thrown exceptions by the method.

@Gauge

You can mark no-arg methods that have a return type with the @Gauge annotation. A gauge will be created which uses the annotated method's return value as its value. For that to work, you need to annotate the class with @MonitorGauges. Example:

@MonitorGauges
public class Queue {
    @Gauge(name = "queueSize")
    public int getQueueSize() {
        return queue.size;
    }
}

Programmatic

Use the following snippet to get a reference to the MetricRegistry

Metric2Registry registry = Stagemonitor.getMetric2Registry();
registry.timer(name("api_request_duration").tag("stage", "transform").build()).update(duration, NANOSECONDS)

Refer to the metrics documentation on how to add Gauges, Counters, Histograms, Meters, and Timers.

Display the metrics

Stagemonitor already contains a preconfigured "Custom Metrics" dashboard that contains all metrics created by the annotations. This dashboard can also be customized or you could create a completely custom one.

Best Practices

The Prometheus documentation includes useful best practices about what to measure and how to name metrics.