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.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Context
Currently when using a metric we can provide labels either with an ordered string list, or via prometheus.Labels map. Both seems to be very dangerous: chance of misorder, misspell, extra/missing values. Also it's not convenient, that you do not know which labels you can use until you reach the code of metric creation.
It's especially dangerous when using promauto as it fails on every issue with label.
Solution
As modern Go language has Generics support we can use it to make a strongly-typed labels support.
PR's description contains several example: single-label metric, multi-label metric, promauto-combined support, etc.
Here's example of using multi-labeled metric:
// Registering strongly-type label:typeMyCounterLabelsstruct {
promsafe.StructLabelProviderEventTypestringSuccessboolPositionuint8// yes, it's a number, but be careful with high-cardinality labelsShouldNotBeUsedstring`promsafe:"-"`
}
c:=promsafe.NewCounterVecT[MyCounterLabels](prometheus.CounterOpts{
Name: "items_counted",
})
// Using counter with strongly typed labels:// It only allows you to use &MyCounterLabels herec.With(MyCounterLabels{
EventType: "reservation", Success: true, Position: 1,
}).Inc()
The text was updated successfully, but these errors were encountered:
Context
Currently when using a metric we can provide labels either with an ordered string list, or via
prometheus.Labels
map. Both seems to be very dangerous: chance of misorder, misspell, extra/missing values. Also it's not convenient, that you do not know which labels you can use until you reach the code of metric creation.It's especially dangerous when using
promauto
as it fails on every issue with label.Solution
As modern Go language has Generics support we can use it to make a strongly-typed labels support.
I have a draft PR pushed already:
#1598
PR's description contains several example: single-label metric, multi-label metric,
promauto
-combined support, etc.Here's example of using multi-labeled metric:
The text was updated successfully, but these errors were encountered: