Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Type-Safe Labels for Prometheus – Looking for Feedback and PR Support

28 views
Skip to first unread message

Eugen

unread,
Sep 19, 2024, 8:23:29 AM9/19/24
to Prometheus Developers

Hello Prometheans,

At my company, we’ve run into multiple production panics using promauto due to issues with counter.With(prometheus.Labels{})—whether it’s typos, missing/extra labels, or messing up the order when using counter.WithLabelValues().

To solve this, we built a custom wrapper that adds type safety by using a struct for labels instead of a raw map. With Go generics, it’s straightforward: you declare a type (struct with fields = labels) at the metric registration stage, and only that type can be used when calling the metric.

I’ve put together a PR for this:

https://github.com/prometheus/client_golang/pull/1598

It’s an optional sub-package (called promsafe), fully non-breaking, and easy to switch to for those already using promauto. Currently, it covers only Counter, but if the idea gains traction, I’m happy to extend it to other metrics.

I’d really appreciate your support—or even critical feedback if you’re not on board with the idea.

Bartłomiej Płotka

unread,
Nov 22, 2024, 10:13:20 AM11/22/24
to Prometheus Developers
Thanks!

The discussion is happening on the PR. Will circle back to it next week.

Kind Regards,
Bartek Plotka @bwplotka

Reply all
Reply to author
Forward
0 new messages