2016-02-10 22:50:07 +00:00
|
|
|
package graphite
|
|
|
|
|
|
|
|
import (
|
2018-05-05 01:27:31 +00:00
|
|
|
"bytes"
|
2016-02-10 22:50:07 +00:00
|
|
|
"fmt"
|
2018-04-18 19:13:25 +00:00
|
|
|
"math"
|
2017-11-15 22:44:20 +00:00
|
|
|
"regexp"
|
2016-02-10 22:50:07 +00:00
|
|
|
"sort"
|
2018-04-18 19:13:25 +00:00
|
|
|
"strconv"
|
2016-02-10 22:50:07 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
)
|
|
|
|
|
2016-04-08 22:04:45 +00:00
|
|
|
const DEFAULT_TEMPLATE = "host.tags.measurement.field"
|
|
|
|
|
2016-07-12 23:08:03 +00:00
|
|
|
var (
|
2017-11-15 22:44:20 +00:00
|
|
|
allowedChars = regexp.MustCompile(`[^a-zA-Z0-9-:._=\p{L}]`)
|
|
|
|
hypenChars = strings.NewReplacer(
|
|
|
|
"/", "-",
|
|
|
|
"@", "-",
|
|
|
|
"*", "-",
|
|
|
|
)
|
|
|
|
dropChars = strings.NewReplacer(
|
|
|
|
`\`, "",
|
|
|
|
"..", ".",
|
|
|
|
)
|
|
|
|
|
|
|
|
fieldDeleter = strings.NewReplacer(".FIELDNAME", "", "FIELDNAME.", "")
|
2016-07-12 23:08:03 +00:00
|
|
|
)
|
2016-04-08 22:04:45 +00:00
|
|
|
|
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-11-22 12:51:57 +00:00
|
|
|
func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]byte, error) {
|
|
|
|
out := []byte{}
|
2016-02-26 20:06:56 +00:00
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
// Convert UnixNano to Unix timestamps
|
2018-03-28 00:30:51 +00:00
|
|
|
timestamp := metric.Time().UnixNano() / 1000000000
|
2016-02-10 22:50:07 +00:00
|
|
|
|
2016-07-12 23:08:03 +00:00
|
|
|
bucket := SerializeBucketName(metric.Name(), metric.Tags(), s.Template, s.Prefix)
|
2016-06-29 09:58:31 +00:00
|
|
|
if bucket == "" {
|
|
|
|
return out, nil
|
|
|
|
}
|
2016-04-08 22:04:45 +00:00
|
|
|
|
|
|
|
for fieldName, value := range metric.Fields() {
|
2018-04-18 19:13:25 +00:00
|
|
|
fieldValue := formatValue(value)
|
|
|
|
if fieldValue == "" {
|
2017-08-29 22:59:38 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-04-18 19:13:25 +00:00
|
|
|
metricString := fmt.Sprintf("%s %s %d\n",
|
2016-04-08 22:04:45 +00:00
|
|
|
// insert "field" section of template
|
2017-11-15 22:44:20 +00:00
|
|
|
sanitize(InsertField(bucket, fieldName)),
|
2018-04-18 19:13:25 +00:00
|
|
|
fieldValue,
|
2017-08-29 22:59:38 +00:00
|
|
|
timestamp)
|
|
|
|
point := []byte(metricString)
|
2016-11-22 12:51:57 +00:00
|
|
|
out = append(out, point...)
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2018-05-05 01:27:31 +00:00
|
|
|
func (s *GraphiteSerializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {
|
|
|
|
var batch bytes.Buffer
|
|
|
|
for _, m := range metrics {
|
|
|
|
buf, err := s.Serialize(m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = batch.Write(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return batch.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2018-04-18 19:13:25 +00:00
|
|
|
func formatValue(value interface{}) string {
|
|
|
|
switch v := value.(type) {
|
|
|
|
case string:
|
|
|
|
return ""
|
|
|
|
case bool:
|
|
|
|
if v {
|
|
|
|
return "1"
|
|
|
|
} else {
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
case uint64:
|
|
|
|
return strconv.FormatUint(v, 10)
|
|
|
|
case int64:
|
|
|
|
return strconv.FormatInt(v, 10)
|
|
|
|
case float64:
|
|
|
|
if math.IsNaN(v) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if math.IsInf(v, 0) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return strconv.FormatFloat(v, 'f', -1, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
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.
|
2016-07-12 23:08:03 +00:00
|
|
|
func SerializeBucketName(
|
2016-04-08 22:04:45 +00:00
|
|
|
measurement string,
|
|
|
|
tags map[string]string,
|
2016-07-12 23:08:03 +00:00
|
|
|
template string,
|
|
|
|
prefix string,
|
2016-04-08 22:04:45 +00:00
|
|
|
) string {
|
2016-07-12 21:31:08 +00:00
|
|
|
if template == "" {
|
|
|
|
template = DEFAULT_TEMPLATE
|
2016-04-08 22:04:45 +00:00
|
|
|
}
|
|
|
|
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
|
2016-07-12 21:31:08 +00:00
|
|
|
templateParts := strings.Split(template, ".")
|
2016-04-08 22:04:45 +00:00
|
|
|
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
|
|
|
|
2016-06-29 09:58:31 +00:00
|
|
|
if len(out) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-07-12 23:08:03 +00:00
|
|
|
if prefix == "" {
|
2016-08-17 15:20:32 +00:00
|
|
|
return strings.Join(out, ".")
|
2016-04-08 22:04:45 +00:00
|
|
|
}
|
2016-08-17 15:20:32 +00:00
|
|
|
return 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)
|
|
|
|
}
|
2018-01-22 20:04:16 +00:00
|
|
|
return strings.Replace(bucket, "FIELDNAME", fieldName, 1)
|
2016-04-08 22:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2017-11-15 22:44:20 +00:00
|
|
|
|
|
|
|
func sanitize(value string) string {
|
|
|
|
// Apply special hypenation rules to preserve backwards compatibility
|
|
|
|
value = hypenChars.Replace(value)
|
|
|
|
// Apply rule to drop some chars to preserve backwards compatibility
|
|
|
|
value = dropChars.Replace(value)
|
|
|
|
// Replace any remaining illegal chars
|
|
|
|
return allowedChars.ReplaceAllLiteralString(value, "_")
|
|
|
|
}
|