add gjson functionality with toml added to internal.config
This commit is contained in:
67
plugins/parsers/gjson/parser.go
Normal file
67
plugins/parsers/gjson/parser.go
Normal file
@@ -0,0 +1,67 @@
|
||||
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
|
||||
StrPath map[string]string
|
||||
BoolPath map[string]string
|
||||
DefaultTags map[string]string
|
||||
}
|
||||
|
||||
func (j *JSONPath) Parse(buf []byte) ([]telegraf.Metric, error) {
|
||||
tags := j.DefaultTags
|
||||
fields := make(map[string]interface{})
|
||||
metrics := make([]telegraf.Metric, 0)
|
||||
|
||||
for k, v := range j.TagPath {
|
||||
c := gjson.GetBytes(buf, v)
|
||||
tags[k] = c.Str
|
||||
}
|
||||
|
||||
for k, v := range j.FloatPath {
|
||||
c := gjson.GetBytes(buf, v)
|
||||
fields[k] = c.Num
|
||||
}
|
||||
|
||||
for k, v := range j.BoolPath {
|
||||
c := gjson.GetBytes(buf, v)
|
||||
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)
|
||||
fields[k] = c.Str
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
40
plugins/parsers/gjson/parser_test.go
Normal file
40
plugins/parsers/gjson/parser_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package gjson
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseJsonPath(t *testing.T) {
|
||||
testString := `{
|
||||
"total_devices": 5,
|
||||
"total_threads": 10,
|
||||
"shares": {
|
||||
"total": 5,
|
||||
"accepted": 5,
|
||||
"rejected": 0,
|
||||
"avg_find_time": 4,
|
||||
"tester": "work",
|
||||
"tester2": true,
|
||||
"tester3": {
|
||||
"hello":"sup",
|
||||
"fun":"money",
|
||||
"break":9
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
jsonParser := JSONPath{
|
||||
MetricName: "jsonpather",
|
||||
TagPath: map[string]string{"hello": "shares.tester3.hello"},
|
||||
BoolPath: map[string]string{"bool": "shares.tester2"},
|
||||
}
|
||||
|
||||
metrics, err := jsonParser.Parse([]byte(testString))
|
||||
assert.NoError(t, err)
|
||||
log.Printf("m[0] name: %v, tags: %v, fields: %v", metrics[0].Name(), metrics[0].Tags(), metrics[0].Fields())
|
||||
t.Error()
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user