Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Prometheus Exporter Pro v.3+: add custom metric for monitoring web requests

Hello!

In this article we will continue to talk about adding custom metrics in Prometheus Exporter Pro v3+. You can read the basics of adding custom metrics here.

In this article we will discuss how to add your custom metrics to monitor web requests.

You can find the source code for the article here.

You can find the video for this article here.

You can monitor web request out of the box. You can read my article about it.

But suppose it is not enough for you. In this case you can add your own metric to accomplish what you need. In this article I will show how you can do it.

By default we have jira_request_duration_path for Jira, confluence_request_duration_on_path for Confluence and bitbucket_request_duration_on_path for Bitbucket. This metric has default buckets which are set by the prometheus client itself. But suppose you want to set your own buckets. Let's say 4 buckets: 1sec, 5sec, 10 sec and 20sec.

Let's create this metric.

First, you need to create a collector src/main/java/ru/matveev/alexey/prometheus/jira/extension/metrics/requestmetric/RequestMetricCollector.java:

public class RequestMetricCollector extends PrometheusMetricCollector {

public RequestMetricCollector(RequestHistogram requestHistogram) {
this.getMetrics().add(requestHistogram);
}
}

It looks like other collectors. We just register our HistogramMetric.

Then we register our collector in the atlassian-plugin.xml file:

<prometheusRequestMetric key="request-metric-sample-module" name="Sample Request Metric Module"
class="ru.matveev.alexey.prometheus.jira.extension.metrics.requestmetric.RequestMetricCollector"/>

As you can see we use here prometheusRequestMetric module, not prometheusMetric module

And then we create our metric:

@Named
public class RequestHistogram extends PrometheusRequestMetric {
Histogram simpleRequestetric = Histogram.build()
.buckets(1, 5, 10, 20)
.name("simple_request_metric")
.help("Request duration on path")
.labelNames("path")
.create();

public RequestHistogram() {
super("simple_request_metric", "Request duration on path", true, true);
}


@Override
public Histogram.Timer startTimer(String path, String queryString, ServletRequest servletRequest) {
return this.simpleRequestetric.labels(path).startTimer();
}

@Override
public PrometheusMetricResult collect() {
return new PrometheusMetricResult(simpleRequestetric.collect());
}

}

We extend from the PrometheusRequestMetric class. We define our metric:

Histogram simpleRequestetric = Histogram.build()
.buckets(1, 5, 10, 20)
.name("simple_request_metric")
.help("Request duration on path")
.labelNames("path")
.create();

As you can see we define custom buckets with .buckets(1,5,10,20)

All other things look the same as for a usual metric except the startTimer method:

    public Histogram.Timer startTimer(String path, String queryString, ServletRequest servletRequest) {
return this.simpleRequestetric.labels(path).startTimer();
}

This method starts the timer for the metric and return this timer. In this method you do all your logic. For example, check if the path is valid or the query string has a certain value for a certain parameter and only in this case you start the timer to measure your metric. Also if you need more than the path and the query string you can use the servlet request to get all parameters for the request.

As the result you will see the following metric:

simple_request_metric_bucket{path="/download/resources/jira.webresources:util-lite/getOptionsFromAttributes.js",le="1.0",} 2.0
simple_request_metric_bucket{path="/download/resources/jira.webresources:util-lite/getOptionsFromAttributes.js",le="5.0",} 2.0
simple_request_metric_bucket{path="/download/resources/jira.webresources:util-lite/getOptionsFromAttributes.js",le="10.0",} 2.0
simple_request_metric_bucket{path="/download/resources/jira.webresources:util-lite/getOptionsFromAttributes.js",le="20.0",} 2.0
simple_request_metric_bucket{path="/download/resources/jira.webresources:util-lite/getOptionsFromAttributes.js",le="+Inf",} 2.0
simple_request_metric_count{path="/download/resources/jira.webresources:util-lite/getOptionsFromAttributes.js",} 2.0

That is it. You created your own custom request metric.

0 comments

Comment

Log in or Sign up to comment
TAGS
Community showcase
Published in Apps & Integrations

Send an Email or Publish to Confluence - What should you do with your release notes?

Background When you hear the words ‘Release notes’, almost always you think of an unsolicited email from a software vendor. But I am here to tell you that from our data, sending release notes via E...

106 views 1 2
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you