From fd086f1ead7a64aa08a2ccbc3b66c4a8247d59d8 Mon Sep 17 00:00:00 2001 From: juananrey Date: Wed, 29 Jan 2020 09:41:46 +0100 Subject: [PATCH] Adding support for unsigned integers --- init.go | 5 ++++- metrics_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/init.go b/init.go index 40900d6..b1ba24c 100644 --- a/init.go +++ b/init.go @@ -189,6 +189,8 @@ func (in initializer) initMetricFunc(field reflect.Value, structField reflect.St labels[label.name] = value.String() case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: labels[label.name] = strconv.FormatInt(value.Int(), 10) + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + labels[label.name] = strconv.FormatUint(value.Uint(), 10) default: // Should not happen since we've already checked this in the findLabelIndexes function panic(fmt.Errorf("field %s has unsupported kind %v", label.name, label.kind)) @@ -231,7 +233,8 @@ func findLabelIndexes(typ reflect.Type, indexes map[label][]int, current ...int) } switch k := f.Type.Kind(); k { - case reflect.String, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + case reflect.String, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: default: return fmt.Errorf("field %s has unsupported type %v", labelTag, k) } diff --git a/metrics_test.go b/metrics_test.go index 3f8d1c1..1561d9d 100644 --- a/metrics_test.go +++ b/metrics_test.go @@ -179,6 +179,43 @@ func Test_LabelsWithInts(t *testing.T) { }, reportedLabels) } +func Test_LabelsWithUints(t *testing.T) { + type labelsWithUints struct { + StringValue string `label:"string_value"` + UintValue uint `label:"uint_value"` + UintValue8 uint8 `label:"uint8_value"` + UintValue16 uint16 `label:"uint16_value"` + UintValue32 uint32 `label:"uint32_value"` + UintValue64 uint64 `label:"uint64_value"` + } + + var metrics struct { + WithLabels func(labelsWithUints) prometheus.Histogram `name:"with_uints" help:"Parse uints as strings" buckets:""` + } + + gotoprom.MustInit(&metrics, "testuints") + + metrics.WithLabels(labelsWithUints{ + StringValue: "string", + UintValue: 10, + UintValue8: 20, + UintValue16: 30, + UintValue32: 40, + UintValue64: 50, + }).Observe(288.0) + + reportedLabels := retrieveReportedLabels(t, "testuints_with_uints") + + assert.Equal(t, map[string]string{ + "string_value": "string", + "uint_value": "10", + "uint8_value": "20", + "uint16_value": "30", + "uint32_value": "40", + "uint64_value": "50", + }, reportedLabels) +} + func Test_DefaultLabelValues(t *testing.T) { type labelsWithEmptyValues struct { StringWithEmpty string `label:"string_with_default" default:"none"`