2018-07-02 16:43:32 +00:00
|
|
|
package gjson
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/metric"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
)
|
|
|
|
|
|
|
|
type JSONPath struct {
|
|
|
|
MetricName string
|
|
|
|
TagPath map[string]string
|
|
|
|
FloatPath map[string]string
|
2018-07-02 18:20:16 +00:00
|
|
|
IntPath map[string]string
|
2018-07-02 16:43:32 +00:00
|
|
|
StrPath map[string]string
|
|
|
|
BoolPath map[string]string
|
|
|
|
DefaultTags map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *JSONPath) Parse(buf []byte) ([]telegraf.Metric, error) {
|
2018-07-02 18:20:16 +00:00
|
|
|
tags := make(map[string]string)
|
|
|
|
for k, v := range j.DefaultTags {
|
|
|
|
tags[k] = v
|
|
|
|
}
|
2018-07-02 16:43:32 +00:00
|
|
|
fields := make(map[string]interface{})
|
|
|
|
metrics := make([]telegraf.Metric, 0)
|
|
|
|
|
|
|
|
for k, v := range j.TagPath {
|
|
|
|
c := gjson.GetBytes(buf, v)
|
2018-07-02 21:56:31 +00:00
|
|
|
if c.IsArray() {
|
|
|
|
log.Printf("E! GJSON cannot assign array to field on path: %v", v)
|
|
|
|
continue
|
|
|
|
}
|
2018-07-02 18:20:16 +00:00
|
|
|
tags[k] = c.String()
|
2018-07-02 16:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range j.FloatPath {
|
|
|
|
c := gjson.GetBytes(buf, v)
|
2018-07-02 21:56:31 +00:00
|
|
|
if c.IsArray() {
|
|
|
|
log.Printf("E! GJSON cannot assign array to field on path: %v", v)
|
|
|
|
continue
|
|
|
|
}
|
2018-07-02 18:20:16 +00:00
|
|
|
fields[k] = c.Float()
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range j.IntPath {
|
|
|
|
c := gjson.GetBytes(buf, v)
|
2018-07-02 21:56:31 +00:00
|
|
|
if c.IsArray() {
|
|
|
|
log.Printf("E! GJSON cannot assign array to field on path: %v", v)
|
|
|
|
continue
|
|
|
|
}
|
2018-07-02 18:20:16 +00:00
|
|
|
fields[k] = c.Int()
|
2018-07-02 16:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range j.BoolPath {
|
|
|
|
c := gjson.GetBytes(buf, v)
|
2018-07-02 21:56:31 +00:00
|
|
|
if c.IsArray() {
|
|
|
|
log.Printf("E! GJSON cannot assign array to field on path: %v", v)
|
|
|
|
continue
|
|
|
|
}
|
2018-07-02 16:43:32 +00:00
|
|
|
if c.String() == "true" {
|
|
|
|
fields[k] = true
|
|
|
|
} else if c.String() == "false" {
|
|
|
|
fields[k] = false
|
|
|
|
} else {
|
|
|
|
log.Printf("E! Cannot decode: %v as bool", c.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range j.StrPath {
|
|
|
|
c := gjson.GetBytes(buf, v)
|
2018-07-02 21:56:31 +00:00
|
|
|
if c.IsArray() {
|
|
|
|
log.Printf("E! GJSON cannot assign array to field on path: %v", v)
|
|
|
|
continue
|
|
|
|
}
|
2018-07-02 18:20:16 +00:00
|
|
|
fields[k] = c.String()
|
2018-07-02 16:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m, err := metric.New(j.MetricName, tags, fields, time.Now())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
metrics = append(metrics, m)
|
|
|
|
return metrics, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *JSONPath) ParseLine(str string) (telegraf.Metric, error) {
|
|
|
|
m, err := j.Parse([]byte(str))
|
|
|
|
return m[0], err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *JSONPath) SetDefaultTags(tags map[string]string) {
|
|
|
|
j.DefaultTags = tags
|
|
|
|
}
|