Update tracing dependencies

This commit is contained in:
Ludovic Fernandez 2019-04-05 11:58:06 +02:00 committed by Traefiker Bot
parent 4919b638f9
commit ed12366d52
98 changed files with 3371 additions and 2808 deletions

View file

@ -17,23 +17,29 @@ package metrics
import (
"fmt"
"reflect"
"strconv"
"strings"
)
// Init initializes the passed in metrics and initializes its fields using the passed in factory.
func Init(metrics interface{}, factory Factory, globalTags map[string]string) {
if err := initMetrics(metrics, factory, globalTags); err != nil {
panic(err.Error())
}
}
// initMetrics uses reflection to initialize a struct containing metrics fields
// MustInit initializes the passed in metrics and initializes its fields using the passed in factory.
//
// It uses reflection to initialize a struct containing metrics fields
// by assigning new Counter/Gauge/Timer values with the metric name retrieved
// from the `metric` tag and stats tags retrieved from the `tags` tag.
//
// Note: all fields of the struct must be exported, have a `metric` tag, and be
// of type Counter or Gauge or Timer.
func initMetrics(m interface{}, factory Factory, globalTags map[string]string) error {
//
// Errors during Init lead to a panic.
func MustInit(metrics interface{}, factory Factory, globalTags map[string]string) {
if err := Init(metrics, factory, globalTags); err != nil {
panic(err.Error())
}
}
// Init does the same as Init, but returns an error instead of
// panicking.
func Init(m interface{}, factory Factory, globalTags map[string]string) error {
// Allow user to opt out of reporting metrics by passing in nil.
if factory == nil {
factory = NullFactory
@ -42,6 +48,7 @@ func initMetrics(m interface{}, factory Factory, globalTags map[string]string) e
counterPtrType := reflect.TypeOf((*Counter)(nil)).Elem()
gaugePtrType := reflect.TypeOf((*Gauge)(nil)).Elem()
timerPtrType := reflect.TypeOf((*Timer)(nil)).Elem()
histogramPtrType := reflect.TypeOf((*Histogram)(nil)).Elem()
v := reflect.ValueOf(m).Elem()
t := v.Type()
@ -50,6 +57,7 @@ func initMetrics(m interface{}, factory Factory, globalTags map[string]string) e
for k, v := range globalTags {
tags[k] = v
}
var buckets []float64
field := t.Field(i)
metric := field.Tag.Get("metric")
if metric == "" {
@ -67,13 +75,57 @@ func initMetrics(m interface{}, factory Factory, globalTags map[string]string) e
tags[tag[0]] = tag[1]
}
}
if bucketString := field.Tag.Get("buckets"); bucketString != "" {
if field.Type.AssignableTo(timerPtrType) {
// TODO: Parse timer duration buckets
return fmt.Errorf(
"Field [%s]: Buckets are not currently initialized for timer metrics",
field.Name)
} else if field.Type.AssignableTo(histogramPtrType) {
bucketValues := strings.Split(bucketString, ",")
for _, bucket := range bucketValues {
b, err := strconv.ParseFloat(bucket, 64)
if err != nil {
return fmt.Errorf(
"Field [%s]: Bucket [%s] could not be converted to float64 in 'buckets' string [%s]",
field.Name, bucket, bucketString)
}
buckets = append(buckets, b)
}
} else {
return fmt.Errorf(
"Field [%s]: Buckets should only be defined for Timer and Histogram metric types",
field.Name)
}
}
help := field.Tag.Get("help")
var obj interface{}
if field.Type.AssignableTo(counterPtrType) {
obj = factory.Counter(metric, tags)
obj = factory.Counter(Options{
Name: metric,
Tags: tags,
Help: help,
})
} else if field.Type.AssignableTo(gaugePtrType) {
obj = factory.Gauge(metric, tags)
obj = factory.Gauge(Options{
Name: metric,
Tags: tags,
Help: help,
})
} else if field.Type.AssignableTo(timerPtrType) {
obj = factory.Timer(metric, tags)
// TODO: Add buckets once parsed (see TODO above)
obj = factory.Timer(TimerOptions{
Name: metric,
Tags: tags,
Help: help,
})
} else if field.Type.AssignableTo(histogramPtrType) {
obj = factory.Histogram(HistogramOptions{
Name: metric,
Tags: tags,
Help: help,
Buckets: buckets,
})
} else {
return fmt.Errorf(
"Field %s is not a pointer to timer, gauge, or counter",