diff --git a/metric/common.go b/metric/common.go index bd6e7719e..42acaae56 100644 --- a/metric/common.go +++ b/metric/common.go @@ -133,6 +133,7 @@ func (bm *baseMetric) entryForValues(labelVals []metricdata.LabelValue, newEntry } func (bm *baseMetric) upsertEntry(labelVals []metricdata.LabelValue, newEntry func() baseEntry) error { + labelVals = append(bm.constLabelValues, labelVals...) if len(labelVals) != len(bm.keys) { return errKeyValueMismatch } diff --git a/metric/gauge_test.go b/metric/gauge_test.go index 9c4f269ca..57a6f03e3 100644 --- a/metric/gauge_test.go +++ b/metric/gauge_test.go @@ -465,6 +465,29 @@ func TestInt64DerivedGaugeEntry_Update(t *testing.T) { } } +func TestInt64DerivedGaugeEntry_UpsertConstLabels(t *testing.T) { + r := NewRegistry() + q := &queueInt64{3} + g, _ := r.AddInt64DerivedGauge("g", + WithConstLabel(map[metricdata.LabelKey]metricdata.LabelValue{ + {Key: "const"}: metricdata.NewLabelValue("same"), + })) + err := g.UpsertEntry(q.ToInt64) + if err != nil { + t.Errorf("want: nil, got: %v", err) + } + ms := r.Read() + if got, want := ms[0].TimeSeries[0].Points[0].Value.(int64), int64(3); got != want { + t.Errorf("value = %v, want %v", got, want) + } + if got, want := ms[0].Descriptor.LabelKeys[0].Key, "const"; got != want { + t.Errorf("label key = %v, want %v", got, want) + } + if got, want := ms[0].TimeSeries[0].LabelValues[0].Value, "same"; got != want { + t.Errorf("label value = %v, want %v", got, want) + } +} + type queueFloat64 struct { size float64 }