fix: avoid metrics panic

This commit is contained in:
2026-05-26 22:24:59 +02:00
parent 4ffa6af298
commit 7279eac949

View File

@@ -129,6 +129,9 @@ func (c *counterVec) Inc(labelValues ...string) {
defer c.mu.Unlock() defer c.mu.Unlock()
key, labels := buildLabelKey(c.labelNames, labelValues) key, labels := buildLabelKey(c.labelNames, labelValues)
if labels == nil {
return
}
sample, ok := c.samples[key] sample, ok := c.samples[key]
if !ok { if !ok {
sample = &counterSample{labels: labels} sample = &counterSample{labels: labels}
@@ -166,6 +169,9 @@ func (h *histogramVec) Observe(value float64, labelValues ...string) {
defer h.mu.Unlock() defer h.mu.Unlock()
key, labels := buildLabelKey(h.labelNames, labelValues) key, labels := buildLabelKey(h.labelNames, labelValues)
if labels == nil {
return
}
sample, ok := h.samples[key] sample, ok := h.samples[key]
if !ok { if !ok {
sample = &histogramSample{ sample = &histogramSample{
@@ -206,7 +212,7 @@ func (h *histogramVec) snapshot() []histogramSample {
func buildLabelKey(labelNames []string, labelValues []string) (string, map[string]string) { func buildLabelKey(labelNames []string, labelValues []string) (string, map[string]string) {
if len(labelNames) != len(labelValues) { if len(labelNames) != len(labelValues) {
panic("label cardinality mismatch") return "", nil
} }
labels := make(map[string]string, len(labelNames)) labels := make(map[string]string, len(labelNames))