Support for multiple samples within same metric (#1181)

Signed-off-by: João Vilaça <jvilaca@redhat.com>

Signed-off-by: João Vilaça <jvilaca@redhat.com>
This commit is contained in:
João Vilaça 2022-12-15 15:07:45 +00:00 committed by GitHub
parent fae2f6306b
commit e29ed9f2cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"sort" "sort"
"strconv"
"strings" "strings"
"sync" "sync"
"unicode/utf8" "unicode/utf8"
@ -933,6 +934,10 @@ func checkMetricConsistency(
h.WriteString(lp.GetValue()) h.WriteString(lp.GetValue())
h.Write(separatorByteSlice) h.Write(separatorByteSlice)
} }
if dtoMetric.TimestampMs != nil {
h.WriteString(strconv.FormatInt(*(dtoMetric.TimestampMs), 10))
h.Write(separatorByteSlice)
}
hSum := h.Sum64() hSum := h.Sum64()
if _, exists := metricHashes[hSum]; exists { if _, exists := metricHashes[hSum]; exists {
return fmt.Errorf( return fmt.Errorf(

View File

@ -1288,3 +1288,48 @@ func ExampleRegistry_grouping() {
}(i) }(i)
} }
} }
type customCollector struct {
collectFunc func(ch chan<- prometheus.Metric)
}
func (co *customCollector) Describe(_ chan<- *prometheus.Desc) {}
func (co *customCollector) Collect(ch chan<- prometheus.Metric) {
co.collectFunc(ch)
}
// TestCheckMetricConsistency
func TestCheckMetricConsistency(t *testing.T) {
reg := prometheus.NewRegistry()
timestamp := time.Now()
desc := prometheus.NewDesc("metric_a", "", nil, nil)
metric := prometheus.MustNewConstMetric(desc, prometheus.CounterValue, 1)
validCollector := &customCollector{
collectFunc: func(ch chan<- prometheus.Metric) {
ch <- prometheus.NewMetricWithTimestamp(timestamp.Add(-1*time.Minute), metric)
ch <- prometheus.NewMetricWithTimestamp(timestamp, metric)
},
}
reg.MustRegister(validCollector)
_, err := reg.Gather()
if err != nil {
t.Error("metric validation should succeed:", err)
}
reg.Unregister(validCollector)
invalidCollector := &customCollector{
collectFunc: func(ch chan<- prometheus.Metric) {
ch <- prometheus.NewMetricWithTimestamp(timestamp, metric)
ch <- prometheus.NewMetricWithTimestamp(timestamp, metric)
},
}
reg.MustRegister(invalidCollector)
_, err = reg.Gather()
if err == nil {
t.Error("metric validation should return an error")
}
reg.Unregister(invalidCollector)
}