Skip to content

Commit

Permalink
Merge ebd8244 into ce2b19b
Browse files Browse the repository at this point in the history
  • Loading branch information
colega committed May 8, 2019
2 parents ce2b19b + ebd8244 commit 6dc22f3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 15 deletions.
4 changes: 3 additions & 1 deletion init.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ func findLabelIndexes(typ reflect.Type, indexes map[label][]int, current ...int)
for i := 0; i < typ.NumField(); i++ {
f := typ.Field(i)
if f.Type.Kind() == reflect.Struct {
findLabelIndexes(f.Type, indexes, append(current, i)...)
if err := findLabelIndexes(f.Type, indexes, append(current, i)...); err != nil {
return err
}
} else {

labelTag, ok := f.Tag.Lookup("label")
Expand Down
86 changes: 72 additions & 14 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,6 @@ func Test_DefaultLabelValues(t *testing.T) {
}, reportedLabels)
}

func Test_LabelsWithUnsupportedFields(t *testing.T) {
type labelsWithUnsupportedFields struct {
StringValue string `label:"string_value"`
FloatValue float64 `label:"float_value"`
}

var metrics struct {
WithLabels func(labelsWithUnsupportedFields) prometheus.Counter `name:"with_unsupported_fields" help:"Can't parse float labels"`
}

err := gotoprom.Init(&metrics, "test")
assert.NotNil(t, err)
}

func Test_HistogramWithUnsupportedBuckets(t *testing.T) {
var metrics struct {
Histogram func() prometheus.Observer `name:"with_broken_buckets" help:"Wrong buckets" buckets:"0.005, +inf"`
Expand All @@ -217,6 +203,78 @@ func Test_HistogramWithUnsupportedBuckets(t *testing.T) {
assert.NotNil(t, err)
}

func Test_WrongLabels(t *testing.T) {
t.Run("unsupported fields", func(t *testing.T) {
type labelsWithUnsupportedFields struct {
StringValue string `label:"string_value"`
FloatValue float64 `label:"float_value"`
}

var metrics struct {
WithLabels func(labelsWithUnsupportedFields) prometheus.Counter `name:"with_unsupported_fields" help:"Can't parse float labels"`
}

err := gotoprom.Init(&metrics, "test")
assert.NotNil(t, err)
})

t.Run("no label tag", func(t *testing.T) {
type labelsWithUnsupportedFields struct {
StringValue string `label:"string_value"`
StringWithoutTag string
}

var metrics struct {
WithLabels func(labelsWithUnsupportedFields) prometheus.Counter `name:"with_unsupported_fields" help:"Tag is missing"`
}

err := gotoprom.Init(&metrics, "test")
assert.NotNil(t, err)
})

t.Run("same label registered twice", func(t *testing.T) {
type labelsWithUnsupportedFields struct {
StringValue string `label:"string_value"`
SameStringValue string `label:"string_value"`
}

var metrics struct {
WithLabels func(labelsWithUnsupportedFields) prometheus.Counter `name:"with_unsupported_fields" help:"Same string value"`
}

err := gotoprom.Init(&metrics, "test")
assert.NotNil(t, err)
})

t.Run("embedded struct is wrong", func(t *testing.T) {
type embeddedStructLabels struct {
SameStringValue string `label:"string_value"`
}
type labelsWithUnsupportedFields struct {
StringValue string `label:"string_value"`
embeddedStructLabels
}

var metrics struct {
WithLabels func(labelsWithUnsupportedFields) prometheus.Counter `name:"with_unsupported_fields" help:"Same string value in the embedded struct"`
}

err := gotoprom.Init(&metrics, "test")
assert.NotNil(t, err)
})

t.Run("labels are not a struct", func(t *testing.T) {
type labels string

var metrics struct {
WithLabels func(labels) prometheus.Counter `name:"with_unsupported_fields" help:"Labels are not a struct"`
}

err := gotoprom.Init(&metrics, "test")
assert.NotNil(t, err)
})
}

func retrieveReportedLabels(t *testing.T, metric string) map[string]string {
mfs, err := prometheus.DefaultGatherer.Gather()
assert.Nil(t, err)
Expand Down

0 comments on commit 6dc22f3

Please sign in to comment.