2016-02-10 22:50:07 +00:00
|
|
|
package graphite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
)
|
|
|
|
|
2016-04-08 22:04:45 +00:00
|
|
|
const DEFAULT_TEMPLATE = "host.tags.measurement.field"
|
|
|
|
|
|
|
|
var fieldDeleter = strings.NewReplacer(".FIELDNAME", "", "FIELDNAME.", "")
|
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
type GraphiteSerializer struct {
|
2016-04-08 22:04:45 +00:00
|
|
|
Prefix string
|
|
|
|
Template string
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 22:19:22 +00:00
|
|
|
var sanitizedChars = strings.NewReplacer("/", "-", "@", "-", "*", "-", " ", "_", "..", ".")
|
2016-03-21 14:50:21 +00:00
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]string, error) {
|
|
|
|
out := []string{}
|
2016-02-26 20:06:56 +00:00
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
// Convert UnixNano to Unix timestamps
|
|
|
|
timestamp := metric.UnixNano() / 1000000000
|
|
|
|
|
2016-04-08 22:04:45 +00:00
|
|
|
bucket := s.SerializeBucketName(metric.Name(), metric.Tags())
|
|
|
|
|
|
|
|
for fieldName, value := range metric.Fields() {
|
|
|
|
// Convert value to string
|
|
|
|
valueS := fmt.Sprintf("%#v", value)
|
|
|
|
point := fmt.Sprintf("%s %s %d",
|
|
|
|
// insert "field" section of template
|
2016-04-28 16:52:47 +00:00
|
|
|
InsertField(bucket, fieldName),
|
2016-04-08 22:04:45 +00:00
|
|
|
valueS,
|
2016-02-26 20:06:56 +00:00
|
|
|
timestamp)
|
2016-04-08 22:04:45 +00:00
|
|
|
out = append(out, point)
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2016-04-08 22:04:45 +00:00
|
|
|
// SerializeBucketName will take the given measurement name and tags and
|
|
|
|
// produce a graphite bucket. It will use the GraphiteSerializer.Template
|
|
|
|
// to generate this, or DEFAULT_TEMPLATE.
|
|
|
|
//
|
|
|
|
// NOTE: SerializeBucketName replaces the "field" portion of the template with
|
|
|
|
// FIELDNAME. It is up to the user to replace this. This is so that
|
|
|
|
// SerializeBucketName can be called just once per measurement, rather than
|
|
|
|
// once per field. See GraphiteSerializer.InsertField() function.
|
|
|
|
func (s *GraphiteSerializer) SerializeBucketName(
|
|
|
|
measurement string,
|
|
|
|
tags map[string]string,
|
|
|
|
) string {
|
|
|
|
if s.Template == "" {
|
|
|
|
s.Template = DEFAULT_TEMPLATE
|
|
|
|
}
|
|
|
|
tagsCopy := make(map[string]string)
|
|
|
|
for k, v := range tags {
|
|
|
|
tagsCopy[k] = v
|
|
|
|
}
|
2016-02-26 20:06:56 +00:00
|
|
|
|
2016-04-08 22:04:45 +00:00
|
|
|
var out []string
|
|
|
|
templateParts := strings.Split(s.Template, ".")
|
|
|
|
for _, templatePart := range templateParts {
|
|
|
|
switch templatePart {
|
|
|
|
case "measurement":
|
|
|
|
out = append(out, measurement)
|
|
|
|
case "tags":
|
|
|
|
// we will replace this later
|
|
|
|
out = append(out, "TAGS")
|
|
|
|
case "field":
|
|
|
|
// user of SerializeBucketName needs to replace this
|
|
|
|
out = append(out, "FIELDNAME")
|
|
|
|
default:
|
|
|
|
// This is a tag being applied
|
|
|
|
if tagvalue, ok := tagsCopy[templatePart]; ok {
|
|
|
|
out = append(out, strings.Replace(tagvalue, ".", "_", -1))
|
|
|
|
delete(tagsCopy, templatePart)
|
|
|
|
}
|
|
|
|
}
|
2016-02-26 20:06:56 +00:00
|
|
|
}
|
2016-04-08 22:04:45 +00:00
|
|
|
|
|
|
|
// insert remaining tags into output name
|
|
|
|
for i, templatePart := range out {
|
|
|
|
if templatePart == "TAGS" {
|
|
|
|
out[i] = buildTags(tagsCopy)
|
|
|
|
break
|
|
|
|
}
|
2016-02-26 20:06:56 +00:00
|
|
|
}
|
2016-04-08 22:04:45 +00:00
|
|
|
|
|
|
|
if s.Prefix == "" {
|
|
|
|
return sanitizedChars.Replace(strings.Join(out, "."))
|
|
|
|
}
|
|
|
|
return sanitizedChars.Replace(s.Prefix + "." + strings.Join(out, "."))
|
2016-02-26 20:06:56 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 22:04:45 +00:00
|
|
|
// InsertField takes the bucket string from SerializeBucketName and replaces the
|
|
|
|
// FIELDNAME portion. If fieldName == "value", it will simply delete the
|
|
|
|
// FIELDNAME portion.
|
|
|
|
func InsertField(bucket, fieldName string) string {
|
|
|
|
// if the field name is "value", then dont use it
|
|
|
|
if fieldName == "value" {
|
|
|
|
return fieldDeleter.Replace(bucket)
|
|
|
|
}
|
|
|
|
return strings.Replace(bucket, "FIELDNAME", fieldName, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildTags(tags map[string]string) string {
|
2016-02-10 22:50:07 +00:00
|
|
|
var keys []string
|
|
|
|
for k := range tags {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
var tag_str string
|
|
|
|
for i, k := range keys {
|
|
|
|
tag_value := strings.Replace(tags[k], ".", "_", -1)
|
|
|
|
if i == 0 {
|
|
|
|
tag_str += tag_value
|
|
|
|
} else {
|
|
|
|
tag_str += "." + tag_value
|
|
|
|
}
|
|
|
|
}
|
2016-04-08 22:04:45 +00:00
|
|
|
return tag_str
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|