Refactor SerializeBucketName to be read-only for struct fields
This commit is contained in:
parent
7c9b312cee
commit
821d3fafa6
|
@ -153,8 +153,7 @@ func (l *Librato) Description() string {
|
|||
|
||||
func (l *Librato) buildGauges(m telegraf.Metric) ([]*Gauge, error) {
|
||||
gauges := []*Gauge{}
|
||||
serializer := graphite.GraphiteSerializer{Template: l.Template}
|
||||
bucket := serializer.SerializeBucketName(m.Name(), m.Tags())
|
||||
bucket := graphite.SerializeBucketName(m.Name(), m.Tags(), l.Template, "")
|
||||
for fieldName, value := range m.Fields() {
|
||||
gauge := &Gauge{
|
||||
Name: graphite.InsertField(bucket, fieldName),
|
||||
|
|
|
@ -10,22 +10,23 @@ import (
|
|||
|
||||
const DEFAULT_TEMPLATE = "host.tags.measurement.field"
|
||||
|
||||
var fieldDeleter = strings.NewReplacer(".FIELDNAME", "", "FIELDNAME.", "")
|
||||
var (
|
||||
fieldDeleter = strings.NewReplacer(".FIELDNAME", "", "FIELDNAME.", "")
|
||||
sanitizedChars = strings.NewReplacer("/", "-", "@", "-", "*", "-", " ", "_", "..", ".")
|
||||
)
|
||||
|
||||
type GraphiteSerializer struct {
|
||||
Prefix string
|
||||
Template string
|
||||
}
|
||||
|
||||
var sanitizedChars = strings.NewReplacer("/", "-", "@", "-", "*", "-", " ", "_", "..", ".")
|
||||
|
||||
func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]string, error) {
|
||||
func (s GraphiteSerializer) Serialize(metric telegraf.Metric) ([]string, error) {
|
||||
out := []string{}
|
||||
|
||||
// Convert UnixNano to Unix timestamps
|
||||
timestamp := metric.UnixNano() / 1000000000
|
||||
|
||||
bucket := s.SerializeBucketName(metric.Name(), metric.Tags())
|
||||
bucket := SerializeBucketName(metric.Name(), metric.Tags(), s.Template, s.Prefix)
|
||||
if bucket == "" {
|
||||
return out, nil
|
||||
}
|
||||
|
@ -51,11 +52,12 @@ func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]string, error)
|
|||
// 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(
|
||||
func SerializeBucketName(
|
||||
measurement string,
|
||||
tags map[string]string,
|
||||
template string,
|
||||
prefix string,
|
||||
) string {
|
||||
template := s.Template
|
||||
if template == "" {
|
||||
template = DEFAULT_TEMPLATE
|
||||
}
|
||||
|
@ -97,10 +99,10 @@ func (s *GraphiteSerializer) SerializeBucketName(
|
|||
return ""
|
||||
}
|
||||
|
||||
if s.Prefix == "" {
|
||||
if prefix == "" {
|
||||
return sanitizedChars.Replace(strings.Join(out, "."))
|
||||
}
|
||||
return sanitizedChars.Replace(s.Prefix + "." + strings.Join(out, "."))
|
||||
return sanitizedChars.Replace(prefix + "." + strings.Join(out, "."))
|
||||
}
|
||||
|
||||
// InsertField takes the bucket string from SerializeBucketName and replaces the
|
||||
|
|
|
@ -225,8 +225,7 @@ func TestSerializeBucketNameNoHost(t *testing.T) {
|
|||
m, err := telegraf.NewMetric("cpu", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{}
|
||||
mS := s.SerializeBucketName(m.Name(), m.Tags())
|
||||
mS := SerializeBucketName(m.Name(), m.Tags(), "", "")
|
||||
|
||||
expS := "cpu0.us-west-2.cpu.FIELDNAME"
|
||||
assert.Equal(t, expS, mS)
|
||||
|
@ -240,8 +239,7 @@ func TestSerializeBucketNameHost(t *testing.T) {
|
|||
m, err := telegraf.NewMetric("cpu", defaultTags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{}
|
||||
mS := s.SerializeBucketName(m.Name(), m.Tags())
|
||||
mS := SerializeBucketName(m.Name(), m.Tags(), "", "")
|
||||
|
||||
expS := "localhost.cpu0.us-west-2.cpu.FIELDNAME"
|
||||
assert.Equal(t, expS, mS)
|
||||
|
@ -255,8 +253,7 @@ func TestSerializeBucketNamePrefix(t *testing.T) {
|
|||
m, err := telegraf.NewMetric("cpu", defaultTags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{Prefix: "prefix"}
|
||||
mS := s.SerializeBucketName(m.Name(), m.Tags())
|
||||
mS := SerializeBucketName(m.Name(), m.Tags(), "", "prefix")
|
||||
|
||||
expS := "prefix.localhost.cpu0.us-west-2.cpu.FIELDNAME"
|
||||
assert.Equal(t, expS, mS)
|
||||
|
@ -270,8 +267,7 @@ func TestTemplate1(t *testing.T) {
|
|||
m, err := telegraf.NewMetric("cpu", defaultTags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{Template: template1}
|
||||
mS := s.SerializeBucketName(m.Name(), m.Tags())
|
||||
mS := SerializeBucketName(m.Name(), m.Tags(), template1, "")
|
||||
|
||||
expS := "cpu0.us-west-2.localhost.cpu.FIELDNAME"
|
||||
assert.Equal(t, expS, mS)
|
||||
|
@ -285,8 +281,7 @@ func TestTemplate2(t *testing.T) {
|
|||
m, err := telegraf.NewMetric("cpu", defaultTags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{Template: template2}
|
||||
mS := s.SerializeBucketName(m.Name(), m.Tags())
|
||||
mS := SerializeBucketName(m.Name(), m.Tags(), template2, "")
|
||||
|
||||
expS := "localhost.cpu.FIELDNAME"
|
||||
assert.Equal(t, expS, mS)
|
||||
|
@ -300,8 +295,7 @@ func TestTemplate3(t *testing.T) {
|
|||
m, err := telegraf.NewMetric("cpu", defaultTags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{Template: template3}
|
||||
mS := s.SerializeBucketName(m.Name(), m.Tags())
|
||||
mS := SerializeBucketName(m.Name(), m.Tags(), template3, "")
|
||||
|
||||
expS := "localhost.cpu0.us-west-2.FIELDNAME"
|
||||
assert.Equal(t, expS, mS)
|
||||
|
@ -315,8 +309,7 @@ func TestTemplate4(t *testing.T) {
|
|||
m, err := telegraf.NewMetric("cpu", defaultTags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{Template: template4}
|
||||
mS := s.SerializeBucketName(m.Name(), m.Tags())
|
||||
mS := SerializeBucketName(m.Name(), m.Tags(), template4, "")
|
||||
|
||||
expS := "localhost.cpu0.us-west-2.cpu"
|
||||
assert.Equal(t, expS, mS)
|
||||
|
@ -330,8 +323,7 @@ func TestTemplate5(t *testing.T) {
|
|||
m, err := telegraf.NewMetric("cpu", defaultTags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{Template: template5}
|
||||
mS := s.SerializeBucketName(m.Name(), m.Tags())
|
||||
mS := SerializeBucketName(m.Name(), m.Tags(), template5, "")
|
||||
|
||||
expS := "localhost.us-west-2.cpu0.cpu.FIELDNAME"
|
||||
assert.Equal(t, expS, mS)
|
||||
|
@ -345,8 +337,7 @@ func TestTemplate6(t *testing.T) {
|
|||
m, err := telegraf.NewMetric("cpu", defaultTags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{Template: template6}
|
||||
mS := s.SerializeBucketName(m.Name(), m.Tags())
|
||||
mS := SerializeBucketName(m.Name(), m.Tags(), template6, "")
|
||||
|
||||
expS := "localhost.cpu0.us-west-2.cpu.FIELDNAME"
|
||||
assert.Equal(t, expS, mS)
|
||||
|
|
Loading…
Reference in New Issue