Wednesday, December 9, 2015

Using Prometheus with Java in a Jersey project

Step 1, add the dependency to your project. If you're using maven, add the dependency:


Step 2, create a class and register some metrics. Here I have a summary and some counters:

public class Metrics {

    public static final Summary requestLatency =
            .help("Request latency in seconds.").register();

    public static final Counter requestFailures =
            .help("Request failures.").register();

    public static final Counter requestsTotal =

    public static final Counter uploadedFilesSucceeded =
            .help("Total files uploaded to S3.").register();

    public static final Counter tmpFilesNotCleared =
            .help("Total files that could not be removed from cache").register();


Step 3, set a class level variable in your main class to instantiate your Metrics class:

private final Metrics metrics = new Metrics();

Step 4, manipulate your metrics in your code as needed. For example you need to call the inc() function on your counters to increment them. You can add to the requestLatency metric as follows:

public String Something() {
    try {
        Summary.Timer timer = Metrics.requestLatency.startTimer();
        // do some work ...
    } catch (Exception e) {
        return e.getMessage();
    return "It works!";

Step 5, add a web method to dish out your metrics. This is the part that I did not like from other online examples (e.g. ... since they use the Metrics Servlet provided by Prometheus and all that does is user the writer to gather the data for you, so you need to mess with registering and deploying a new servlet within your code. TOO MUCH HASSLE. Just do what the Prometheus servlet does. Load the registry and use the TextFormat class to get your metrics. No need to deploy a servlet! :

public String Metrics() {
    StringWriter writer = new StringWriter();
    try {
                writer, CollectorRegistry.defaultRegistry.metricFamilySamples());
    } catch (Exception e) {
        return e.getMessage();
    return writer.toString();


Here is some sample output from one of our apps using this technique:

# HELP requests_failures_total Request failures.
# TYPE requests_failures_total counter
requests_failures_total 0.0
# HELP temp_files_not_cleared Total files that could not be removed from cache
# TYPE temp_files_not_cleared counter
temp_files_not_cleared 0.0
# HELP requests_latency_seconds Request latency in seconds.
# TYPE requests_latency_seconds summary
requests_latency_seconds_count 0.25
requests_latency_seconds_sum 1.0
# HELP upload_file_success Total file uploaded to S3.
# TYPE upload_file_success counter
upload_file_success 25874588.0
# HELP requests_failures_total Request failures.
# TYPE requests_failures_total counter
requests_failures_total 2.0