Skip to content

Dynamically add labels in a Counter or CounterVec #1072

@nevesgustavo

Description

@nevesgustavo

Hi Team,
I hope you are having a good weekend, I would like to have an information about CounterVec, is there a way to dynamically add Labels inside an already registered metric?

For example:

Creating a metric using two labels: status and environment

metricName := "test_purposes"
help := "tests"

counter = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: metricName,
		Help: help,
	}, []string{"status", "environment"})

	counter.WithLabelValues("success", "dev").Inc()

But when the status is error I would like to add one more label: reason, to the same metric and it's not possible

counter = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: metricName,
		Help: help,
	}, []string{"status", "environment", "reason"})

	counter.WithLabelValues("success", "dev", "npe").Inc()

Just to explain why I need it, we're migrating an implementation of datadog to prometheus and in the implementations based on datadog we have a lot of microservices sending metrics using different tags (labels) to the same metric, if I could do the same using the prometheus lib it would be great because we could only change the client without any modifications on metric publishers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions