Skip to content

The NewCounterFunc doesn't allow using labels on metrics #923

Description

@ppatierno

While working on the Strimzi canary project [1] I had this code at the beginning to track a metric related to produced records:

recordsProduced = promauto.NewCounterVec(prometheus.CounterOpts{
			Name:      "records_produced_total",
			Namespace: "strimzi_canary",
			Help:      "The total number of records produced",
		}, []string{"clientid", "partition"})


// at some point in the code
labels := prometheus.Labels{
			"clientid":  ps.canaryConfig.ClientID,
			"partition": strconv.Itoa(i),
		}
		recordsProduced.With(labels).Inc()

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:

var recordsProducedCounter uint64 = 0

recordsProduced = promauto.NewCounterFunc(prometheus.CounterOpts{
		Name:      "records_produced_total",
		Namespace: "strimzi_canary",
		Help:      "The total number of records produced",
	}, func() float64 {
		return float64(atomic.LoadUint64(&recordsProducedCounter))
	})

// at some point in the code
atomic.AddUint64(&recordsProducedCounter, 1)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions