-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Metrics become available only on second .Set() #1682
Comments
Hey @marevers! I'm here to help you with any bugs, questions, or contributions. Let's tackle this together! This behavior might be related to how metrics are initialized and registered in Prometheus when using In your case, it seems that the metrics are not being initialized or registered in the registry until the second call to To address this, you can try explicitly initializing the metric with a default value (e.g., // Register a metric with the exporter
func (e *exporter) registerMetrics(labels []string) {
e.Metrics.MetricVec = promauto.With(e.Reg).NewGaugeVec(prometheus.GaugeOpts{
Name: "my_metric",
Namespace: "my_metric_prefix",
Help: "Help of the metric",
}, labels)
// Initialize the metric with default label values
e.Metrics.MetricVec.WithLabelValues(make([]string, len(labels))...).Set(0)
} This approach initializes the metric with default label values, ensuring it is visible in the registry from the beginning. |
This solution unfortunately doesn't work, because I have applied a provisional fix by just running |
I am observing some strange behavior using
.WithLabelValues
and creatingGaugeVec
metrics using promauto.Here's a shortened/abbreviated rundown of how I am generating my metrics:
On an interval I am calling the following function (again, abbreviated for readability):
Now, interestingly enough, only on the second time
calculateMetrics
is called, my metrics become available/visible (onpromhttp.HandlerFor(exporter.Reg, promhttp.HandlerOpts{})
. The very first time, the metrics are seemingly correctly set but they are not visible. This causes the exporter to be delayed in providing the metrics, e.g. if the interval is set to 5 minutes, the metrics become available after 10 minutes.Is this a usage error, or is the behavior somehow bugged?
The text was updated successfully, but these errors were encountered: