diff --git a/gather/metrics.go b/gather/metrics.go index 6e95c91c5e4..cdfb1642b20 100644 --- a/gather/metrics.go +++ b/gather/metrics.go @@ -31,14 +31,14 @@ type MetricsSlice []Metrics // Points convert the MetricsSlice to model.Points func (ms MetricsSlice) Points() (models.Points, error) { - ps := make([]models.Point, len(ms)) - for mi, m := range ms { + ps := make([]models.Point, 0, len(ms)) + for _, m := range ms { point, err := models.NewPoint(m.Name, models.NewTags(m.Tags), m.Fields, m.Timestamp) if err != nil { return ps, err } - ps[mi] = point + ps = append(ps, point) } return ps, nil } diff --git a/gather/scheduler_test.go b/gather/scheduler_test.go index a3375235ab2..fb906ad86e4 100644 --- a/gather/scheduler_test.go +++ b/gather/scheduler_test.go @@ -3,6 +3,7 @@ package gather import ( "context" "net/http/httptest" + "strings" "testing" "time" @@ -11,6 +12,7 @@ import ( "github.com/influxdata/influxdb/v2/mock" "github.com/influxdata/influxdb/v2/models" influxdbtesting "github.com/influxdata/influxdb/v2/testing" + dto "github.com/prometheus/client_model/go" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap/zaptest" @@ -77,3 +79,46 @@ const sampleRespSmall = ` # TYPE go_goroutines gauge go_goroutines 36 ` + +func TestMetricsToPoints(t *testing.T) { + const overflow = 3 + const goodPoints = 2 + tags := map[string]string{"one": "first", "two": "second", "three": "third"} + fields := map[string]interface{}{"first_field": 32.2} + + ms := MetricsSlice{ + { + Name: "a", + Tags: tags, + Fields: fields, + Timestamp: time.Now(), + Type: dto.MetricType_GAUGE, + }, + { + Name: "b", + Tags: tags, + Fields: fields, + Timestamp: time.Now(), + Type: dto.MetricType_GAUGE, + }, { + Name: strings.Repeat("c", models.MaxKeyLength+overflow), + Tags: tags, + Fields: fields, + Timestamp: time.Now(), + Type: dto.MetricType_GAUGE, + }, + { + Name: "d", + Tags: tags, + Fields: fields, + Timestamp: time.Now(), + Type: dto.MetricType_GAUGE, + }, + } + ps, err := ms.Points() + assert.ErrorContains(t, err, "max key length exceeded", "MetricSlice.Points did not have a 'max key length exceeded' error") + assert.Equal(t, goodPoints, len(ps), "wrong number of Points returned from MetricSlice.Points") + for _, p := range ps { + assert.NotNil(t, p, "nil Point object returned from MetricSlice.Points") + } +}