Track your own metrics

Felix edited this page Oct 4, 2017 · 10 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.

@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.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.