Skip to content

Commit

Permalink
Refactor SerializeBucketName to be read-only for struct fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed Jul 13, 2016
1 parent b9791d0 commit 7e86f49
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
3 changes: 1 addition & 2 deletions plugins/outputs/librato/librato.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
20 changes: 11 additions & 9 deletions plugins/serializers/graphite/graphite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
27 changes: 9 additions & 18 deletions plugins/serializers/graphite/graphite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 7e86f49

Please sign in to comment.