Skip to content

Commit

Permalink
fix: handle MetricSlice to Points conversion errors (#24452)
Browse files Browse the repository at this point in the history
Correctly handle errors in converting MetricSlice
elements into model.Points. Add a test to verify
error handling.
  • Loading branch information
davidby-influx authored Nov 8, 2023
1 parent c0f467e commit 19e5c0e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
6 changes: 3 additions & 3 deletions gather/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
45 changes: 45 additions & 0 deletions gather/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gather
import (
"context"
"net/http/httptest"
"strings"
"testing"
"time"

Expand All @@ -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"
Expand Down Expand Up @@ -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")
}
}

0 comments on commit 19e5c0e

Please sign in to comment.