Replies: 3 comments 1 reply
-
I saw some reference here #486 but what about the need for labels? |
Beta Was this translation helpful? Give feedback.
-
If you know the labels in advance, you can use const labels: recordsProduced = promauto.NewCounterFunc(prometheus.CounterOpts{
Name: "records_produced_total",
Namespace: "strimzi_canary",
Help: "The total number of records produced",
ConstLabels: prometheus.Labels{
"clientid": ps.canaryConfig.ClientID,
"partition": strconv.Itoa(i),
},
}, func() float64 {
return float64(atomic.LoadUint64(&recordsProducedCounter))
}) See also https://pkg.go.dev/github.com/prometheus/[email protected]/prometheus#example-GaugeFunc-ConstLabels If the situation is more dynamic, you have to implement a custom |
Beta Was this translation helpful? Give feedback.
-
In my experience, implementing the Collector is not much longer (but arguably easier to understand). Historically, client_golang had way too many top-level types. So I tried to justify any addition of a new top level type with a significant benefit. The CounterFunc and GaugeFunc are elegant and simple and frequently used. Doing the same for complex metric types like Histogram or for any metric vector (AKA "with labels") would be not as simple, arguably less elegant than the Collector approach, and (presumably) not used so often. It would "pollute" the package namespace quite a bit for a rather niche demand (those few people that need something like a "CounterVecFunc" and are keen on saving a few lines of code compared to the Collector approach). |
Beta Was this translation helpful? Give feedback.
-
While working on the Strimzi canary project [1] I had this code at the beginning to track a metric related to produced records:
Now I have to make this counter to be accessible so I was thinking to use a var and then using
NewCounterFunc
so something like this:The problem I have now is that I cannot attach updated labels on the metric. The code updates just the variable of course but maybe the func should help about the labels.
Is there any other way to achieve what I need?
[1] https://github.com/strimzi/strimzi-canary
Beta Was this translation helpful? Give feedback.
All reactions