added helper to process floats for dogstatsd emitter
package emitter
import (
+ "errors"
"fmt"
"log"
"regexp"
@@ -21,6 +22,38 @@ type DogstatsDBConfig struct {
Prefix string `long:"datadog-prefix" description:"Prefix for all metrics to easily find them in Datadog"`
}
+func getFloatHelper(value interface{}) (f float64, err error) {
+ switch value.(type) {
+ case int:
+ f = float64(value.(int))
+ case int8:
+ f = float64(value.(int8))
+ case int16:
+ f = float64(value.(int16))
+ case int32:
+ f = float64(value.(int32))
+ case int64:
+ f = float64(value.(int64))
+ case uint:
+ f = float64(value.(uint))
+ case uint8:
+ f = float64(value.(uint8))
+ case uint16:
+ f = float64(value.(uint16))
+ case uint32:
+ f = float64(value.(uint32))
+ case uint64:
+ f = float64(value.(uint64))
+ case float32:
+ f = float64(value.(float32))
+ case float64:
+ f = value.(float64)
+ default:
+ err = errors.New("type not supported")
+ }
+ return f, err
+}
+
func init() {
metric.RegisterEmitter(&DogstatsDBConfig{})
}
@@ -65,25 +98,15 @@ func (emitter *DogstatsdEmitter) Emit(logger lager.Logger, event metric.Event) {
tags = append(tags, fmt.Sprintf("%s:%s", k, v))
}
- var value float64
-
- if i, ok := event.Value.(int); ok {
- value = float64(i)
- } else if f, ok := event.Value.(float64); ok {
- value = f
- } else {
+ value, err := getFloatHelper(event.Value)
+ if err != nil {
logger.Error("failed-to-convert-metric-for-dogstatsd", nil, lager.Data{
"metric-name": name,
})
return
}
- err := emitter.client.Gauge(
- name,
- value,
- tags,
- 1,
- )
+ err = emitter.client.Gauge(name, value, tags, 1)
if err != nil {
logger.Error("failed-to-send-metric", err)
return
