Dynamically add labels in a Counter or CounterVec #1073
Replies: 5 comments 1 reply
-
The If you just translate metrics from a different system, you would use |
Beta Was this translation helpful? Give feedback.
-
Hi @beorn7, please let me know if I understood correctly, the Let me try explain my scenario: func buildMetricTags(keyValueTags []string, failureReason string) []string {
if failureReason != "" {
return append(keyValueTags,
Status, Error,
Reason, failureReason)
}
return append(keyValueTags, Status, Success)
} Sometimes sends the Reason tag and sometimes not. 2 - The idea is to create another client to expose metrics for Prometheus, so we will interface with two implementations (datadog and prometheus) and save in both places at this first time, but as you said above the prometheus lib enforces to use always the same number of labels. So with this scenario I think that we can't use the NewConstMetric because we are not getting data from datadog and we can't depend on this, so I think that refactor everyplace to send the same number of labels is the best choice at this moment, please let me know your thoughts. Thank you very much for your time. |
Beta Was this translation helpful? Give feedback.
-
You could still use
Having said that, it is possible in Prometheus to set an empty label value, with is (almost) completely equivalent to not having set that label at all. So as long as you know the maximum possible label set, you can always provide the whole set, with empty label values for those labels that you don't want to use. |
Beta Was this translation helpful? Give feedback.
-
@beorn7 I have a similar issue. I have a single metric that covers a resource state, sometimes it has 4 static labels, but sometimes I need to add optional labels for further tracking that's instance dependent. The fact that the labels all have to be defined ahead of time is fairly frustrating. Is there any way around this?
Sometimes I need to add
|
Beta Was this translation helpful? Give feedback.
-
You can use a function like this to initialise and return Sample code for type customMetrics struct {
customMetricsOne *prometheus.GaugeVec
customMetricsTwo *prometheus.GaugeVec
Initialized bool
}
func newCustomMetrics() (m *customMetrics) {
m = &customMetrics{
customMetricsOne: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "metric_name",
Help: "Metric Help Doc, can be skipped",
}, []string{"label1", "label2"}),
customMetricsTwo: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "metric_two_name",
Help: "Metric Help Doc, can be skipped",
}, []string{"label3", "label4"}),
}
prometheus.MustRegister(
m. customMetricsOne,
m. customMetricsTwo,
)
m.Initialized = true
return m
}
func main() {
if !cm.Initialized {
cm = *newCustomMetrics()
}
cm.customMetricsOne.With(prometheus.Labels{"label1": "label1 value", "label2": "label2 value"}).Set(20.45)
cm.customMetricsTow.With(prometheus.Labels{"label3": "label3 value", "label4": "label4 value"}).Set(40.45)
} |
Beta Was this translation helpful? Give feedback.
-
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
But when the status is error I would like to add one more label: reason, to the same metric and it's not possible
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.
Beta Was this translation helpful? Give feedback.
All reactions