Handle Telegraf metric types and sanitize names.
This commit is contained in:
parent
9174cde438
commit
431b3355ed
|
@ -1,7 +1,9 @@
|
||||||
package signalfx
|
package signalfx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
|
@ -20,6 +22,8 @@ type SignalFx struct {
|
||||||
sink *sfxclient.HTTPDatapointSink
|
sink *sfxclient.HTTPDatapointSink
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var invalidNameCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
|
||||||
|
|
||||||
var sampleConfig = `
|
var sampleConfig = `
|
||||||
## Your organization's SignalFx API access token.
|
## Your organization's SignalFx API access token.
|
||||||
auth_token = "SuperSecretToken"
|
auth_token = "SuperSecretToken"
|
||||||
|
@ -59,21 +63,45 @@ func (s *SignalFx) Close() error {
|
||||||
func (s *SignalFx) Write(metrics []telegraf.Metric) error {
|
func (s *SignalFx) Write(metrics []telegraf.Metric) error {
|
||||||
var datapoints []*datapoint.Datapoint
|
var datapoints []*datapoint.Datapoint
|
||||||
for _, metric := range metrics {
|
for _, metric := range metrics {
|
||||||
|
// Sanitize metric name.
|
||||||
|
metricName := metric.Name()
|
||||||
|
metricName = invalidNameCharRE.ReplaceAllString(metricName, "_")
|
||||||
|
|
||||||
|
// Get a type if it's available, defaulting to Gauge.
|
||||||
|
var sfMetricType datapoint.MetricType
|
||||||
|
switch metric.Type() {
|
||||||
|
case telegraf.Counter:
|
||||||
|
sfMetricType = datapoint.Counter
|
||||||
|
case telegraf.Gauge:
|
||||||
|
sfMetricType = datapoint.Gauge
|
||||||
|
default:
|
||||||
|
sfMetricType = datapoint.Gauge
|
||||||
|
}
|
||||||
|
|
||||||
// One SignalFx metric per field.
|
// One SignalFx metric per field.
|
||||||
for fieldName, fieldValue := range metric.Fields() {
|
for fieldName, fieldValue := range metric.Fields() {
|
||||||
var value datapoint.Value
|
var sfValue datapoint.Value
|
||||||
switch fieldValue.(type) {
|
switch fieldValue.(type) {
|
||||||
case float64:
|
case float64:
|
||||||
value = datapoint.NewFloatValue(fieldValue.(float64))
|
sfValue = datapoint.NewFloatValue(fieldValue.(float64))
|
||||||
case int64:
|
case int64:
|
||||||
value = datapoint.NewIntValue(fieldValue.(int64))
|
sfValue = datapoint.NewIntValue(fieldValue.(int64))
|
||||||
default:
|
default:
|
||||||
log.Printf("Unhandled type %T for field %s\n", fieldValue, fieldName)
|
log.Printf("Unhandled type %T for field %s\n", fieldValue, fieldName)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
metricName := metric.Name() + "." + fieldName
|
// Sanitize field name.
|
||||||
datapoint := datapoint.New(metricName, metric.Tags(), value, datapoint.Gauge, metric.Time())
|
fieldName = invalidNameCharRE.ReplaceAllString(fieldName, "_")
|
||||||
|
|
||||||
|
var sfMetricName string
|
||||||
|
if fieldName == "value" {
|
||||||
|
sfMetricName = metricName
|
||||||
|
} else {
|
||||||
|
sfMetricName = fmt.Sprintf("%s.%s", metricName, fieldName)
|
||||||
|
}
|
||||||
|
|
||||||
|
datapoint := datapoint.New(sfMetricName, metric.Tags(), sfValue, sfMetricType, metric.Time())
|
||||||
datapoints = append(datapoints, datapoint)
|
datapoints = append(datapoints, datapoint)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue