add kafka new consumer support to kafka_consumer

+kafka new consumer support to kafka_consumer
+timestamp selector to json parser, can specify the timestamp field
+timestamp formatter to json parser, can parse the timestamp in timestamp field
This commit is contained in:
Kirk Young
2016-07-27 09:22:02 -07:00
parent 412f5b5acb
commit b6926c36e8
4 changed files with 192 additions and 44 deletions

View File

@@ -11,9 +11,11 @@ import (
)
type JSONParser struct {
MetricName string
TagKeys []string
DefaultTags map[string]string
MetricName string
TagKeys []string
DefaultTags map[string]string
TimestampSelector string
TimestampFormatter string
}
func (p *JSONParser) Parse(buf []byte) ([]telegraf.Metric, error) {
@@ -31,6 +33,25 @@ func (p *JSONParser) Parse(buf []byte) ([]telegraf.Metric, error) {
tags[k] = v
}
timestampStr := ""
timestampData := jsonOut[p.TimestampSelector]
if timestampData != nil {
timestampStr = timestampData.(string)
}
if p.TimestampFormatter == "" {
p.TimestampFormatter = time.RFC3339Nano
}
timestamp := time.Now().UTC()
if timestampStr != "" {
timestampTmp, err := time.Parse(p.TimestampFormatter, timestampStr)
if err != nil {
return nil, err
}
timestamp = timestampTmp
}
for _, tag := range p.TagKeys {
switch v := jsonOut[tag].(type) {
case string:
@@ -45,7 +66,7 @@ func (p *JSONParser) Parse(buf []byte) ([]telegraf.Metric, error) {
return nil, err
}
metric, err := telegraf.NewMetric(p.MetricName, tags, f.Fields, time.Now().UTC())
metric, err := telegraf.NewMetric(p.MetricName, tags, f.Fields, timestamp)
if err != nil {
return nil, err