Basically, if protocol is missing or invalid, it is serialized as plain/text.

- Create a GraphiteJson struct and use it for json processing.
- Have written the following test for the above features.
1) Test if it works well when there is no protocol setting
2) Test if field key is value, works well
3) Test if one field works well
4) Test if it works well for multiple filed
This commit is contained in:
Dark(김성진) 2017-01-21 13:11:01 +09:00
parent c59f4b0792
commit e76c055f33
3 changed files with 102 additions and 14 deletions

View File

@ -1293,12 +1293,6 @@ func buildGraphiteSerializer(c *serializers.Config, tbl *ast.Table) {
}
}
if (c.Protocol == "") {
log.Print("I! GraphiteSerializer protocol is empty! so, default protocol is setting as plain/text.")
c.Protocol = "plain/text"
}
delete(tbl.Fields, "prefix")
delete(tbl.Fields, "template")
delete(tbl.Fields, "protocol")

View File

@ -6,11 +6,11 @@ import (
"strings"
"github.com/influxdata/telegraf"
"encoding/json"
)
const (
DEFAULT_TEMPLATE = "host.tags.measurement.field"
DEFAULT_PROTOCOL = "plain/text"
)
var (
@ -24,6 +24,13 @@ type GraphiteSerializer struct {
Protocol string
}
// GraphiteJson struct for json processing.
type GraphiteJson struct {
Path string `json:"path"`
Value string `json:"value"`
Timestamp string `json:"timestamp"`
}
func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]byte, error) {
// Convert UnixNano to Unix timestamps
timestamp := metric.UnixNano() / 1000000000
@ -33,13 +40,7 @@ func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]byte, error) {
return []byte{}, nil
}
if s.Protocol == "" {
s.Protocol = DEFAULT_PROTOCOL
}
switch s.Protocol {
case "plain/text":
return plainTextSerialize(bucket, metric, timestamp)
case "json":
return jsonSerialize(bucket, metric, timestamp)
default:
@ -49,7 +50,19 @@ func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]byte, error) {
// jsonSerialize will serialize as json graphite format with bucket and metric.
func jsonSerialize(bucket string, metric telegraf.Metric, timestamp int64) ([]byte, error) {
return nil, nil
out := []GraphiteJson{}
for fieldName, value := range metric.Fields() {
out = append(out, GraphiteJson{
Path: sanitizedChars.Replace(InsertField(bucket, fieldName)),
Value: sanitizedChars.Replace(fmt.Sprintf("%#v", value)),
Timestamp: fmt.Sprintf("%d", timestamp),
})
}
graphiteJson, err := json.Marshal(out)
return []byte(graphiteJson), err
}
// plainTextSerialize will serialize as plain / text graphite format with bucket and metric.

View File

@ -18,6 +18,19 @@ var defaultTags = map[string]string{
"datacenter": "us-west-2",
}
var oneField = map[string]interface{}{
"usage_idle": float64(91.5),
}
var valueField = map[string]interface{}{
"value": float64(91.5),
}
var multiFields = map[string]interface{}{
"usage_idle": float64(91.5),
"usage_busy": float64(8.5),
}
const (
template1 = "tags.measurement.field"
template2 = "host.measurement.field"
@ -416,3 +429,71 @@ func TestTemplate6(t *testing.T) {
expS := "localhost.cpu0.us-west-2.cpu.FIELDNAME"
assert.Equal(t, expS, mS)
}
func TestSerializeWithoutProtocol(t *testing.T) {
// given
now := time.Now()
m, err := metric.New("cpu", defaultTags, oneField, now)
assert.NoError(t, err)
// when
s := GraphiteSerializer{}
buf, _ := s.Serialize(m)
// then
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
assert.NoError(t, err)
expS := []string{
fmt.Sprintf("localhost.cpu0.us-west-2.cpu.usage_idle 91.5 %d", now.Unix()),
}
assert.Equal(t, expS, mS)
}
func TestSerializerWithJsonProtocol1(t *testing.T) {
// given
now := time.Now()
m, err := metric.New("cpu", defaultTags, valueField, now)
assert.NoError(t, err)
// when
s := GraphiteSerializer{
Template: DEFAULT_TEMPLATE,
Protocol: "json",
}
buf, err := s.Serialize(m)
// then
mS := string(buf)
assert.NoError(t, err)
expS := fmt.Sprintf("[{\"path\":\"localhost.cpu0.us-west-2.cpu\",\"value\":\"91.5\",\"timestamp\":\"%d\"}]", now.Unix())
assert.Equal(t, expS, mS)
}
func TestSerializerWithJsonProtocol2(t *testing.T) {
// given
now := time.Now()
m, err := metric.New("cpu", defaultTags, multiFields, now)
assert.NoError(t, err)
// when
s := GraphiteSerializer{
Template: DEFAULT_TEMPLATE,
Protocol: "json",
}
buf, _ := s.Serialize(m)
// then
mS := string(buf)
assert.NoError(t, err)
expS := fmt.Sprintf("[" +
"{\"path\":\"localhost.cpu0.us-west-2.cpu.usage_idle\",\"value\":\"91.5\",\"timestamp\":\"%d\"}," +
"{\"path\":\"localhost.cpu0.us-west-2.cpu.usage_busy\",\"value\":\"8.5\",\"timestamp\":\"%d\"}" +
"]", now.Unix(), now.Unix())
assert.Equal(t, expS, mS)
}