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 3+: add a listener metric

Hello!

In this article I will show you how you can add your own metric which is counted by a listener event.

This article is based on my previous article.

The video for the article you can watch here.

You can find the final code for this article here.

In this article we will write a metric which counts the number of times a user has been set as a watcher to an issue. To count this metric we need to use the IssueWatcherAddedEvent.

You can develop your own metric based on my previous article but I created an abstract class which lets you create listener metrics much easier. This class is called AbstractListenerMetric.

Let's create our metric:

@Named
public class WatcherCounter extends AbstractListenerMetric {

    private Counter watcherMetric =  Counter.build()
            .name("user_watched")
            .help("shows how many times a user has been set as a watcher to an issue")
            .labelNames("watcher")
            .create();


    @Inject
    protected WatcherCounter(@ComponentImport EventPublisher eventPublisher) {
        super(eventPublisher, "user_watched", "shows how many times a user has been set as a watcher to an issue", true, true);
    }

    @EventListener
    public void onUserAssigned(IssueWatcherAddedEvent event) {
        this.watcherMetric.labels(event.getApplicationUser().getKey()).inc();
    }

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

As you can see I extended this class from AbstractListenerMetric. I defined a constructor where I pass the bean of the EventPublisher class. Then I call the super constructor and that's it. The AbstractListenerClass will register the listener itself.

Then I define a method which catches the IssueWatcherAddedEvent and set a value for my metric. And then I need to add the collect method to return my metric.

After it I create a collector and register my metric in this collector:

public class ListenerMetricCollector extends PrometheusMetricCollector {

    public ListenerMetricCollector(WatcherCounter watcherCounter) {
        this.getMetrics().add(watcherCounter);
    }
}

And I register my collector in the atlassian-plugin.xml file:

<prometheusMetric key="listener-metric-sample-module" name="Sample Listener Metric Module"
                             class="ru.matveev.alexey.prometheus.jira.extension.metrics.listener.ListenerMetricCollector"/>

That is it. Now if I add a watcher to an issue I can see my metric in Prometheus:

# HELP user_watched_total shows how many times a user has been set as a watcher to an issue
# TYPE user_watched_total counter
user_watched_total{watcher="JIRAUSER10100",} 1.0

I hope this article will be useful for you if you try to add your own metric to Prometheus Exporter Pro.

0 comments

Comment

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

Marketplace Partner Spotlight: Fine Software

Happy New Year, everybody! What better way to kick off 2022 than with a new Marketplace Partner Spotlight ✨ This month, we're getting to know Daniel Franz, co-founder of Fine Software. Fine Software ...

369 views 2 15
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