Hi Brian,
Thank you so much for your answer. I appreciate and agree with your
comments, for my use case using the second approach would be nice but using
the golang official lib https://github.com/prometheus/client_golang it's
not possible to create the same metric more than once even using different
labels.
Thank you.
Em sábado, 25 de junho de 2022 às 12:43:35 UTC-3, Brian Candler escreveu:
> > But when the status is error I would like to add one more label: reason,
> to the same metric and it's not possible
>
> That's because it wouldn't be the same metric.
>
> In Prometheus, it's the metric name together with the complete set of
> labels which form the identity of a timeseries. If you add another label,
> then you've created a completely different timeseries.
>
> The normal way to handle this is to create separate counters (metrics) for
> each outcome:
>
> test_purposes{result="success",environment="dev"} 0
> test_purposes{result="npe",environment="dev"} 0
> test_purposes{result="oops",environment="dev"} 0
>
> Then you increment the appropriate one, depending on the result.
>
> You could also use your scheme:
>
> test_purposes{status="success",environment="dev"} 0
> test_purposes{status="error",reason="npe",environment="dev"} 0
> test_purposes{status="error",reason="oops",environment="dev"} 0
>
> Again you'd just create three counters as three separate metrics.
> Personally I find the first version easier to work with in queries, because
> it's easier to aggregate when the labels are consistent, but either way
> will work.
>
> On Saturday, 25 June 2022 at 15:41:04 UTC+1 [email protected] wrote:
>
>> Hi Guys,
>> 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
>> ```go
>> metricName := "test_purposes"
>> help := "tests"
>>
>> counter = promauto.NewCounterVec(prometheus.CounterOpts{
>> Name: metricName,
>> Help: help,
>> }, []string{"status", "environment"})
>>
>> counter.WithLabelValues("success", "dev")
>> ```
>>
>> But when the status is error I would like to add one more label: reason,
>> to the same metric and it's not possible
>>
>> ```go
>> counter = promauto.NewCounterVec(prometheus.CounterOpts{
>> Name: metricName,
>> Help: help,
>> }, []string{"status", "environment", "reason"})
>>
>> counter.WithLabelValues("success", "dev", "npe")
>> ```
>>
>> 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.
>>
>
--
You received this message because you are subscribed to the Google Groups
"Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/prometheus-users/51633f6b-344b-402b-85ec-28f9b7ec7a77n%40googlegroups.com.