2016-02-06 00:36:35 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2016-11-15 18:02:55 +00:00
|
|
|
"bytes"
|
2016-02-06 00:36:35 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2016-11-22 12:51:57 +00:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2016-02-06 00:36:35 +00:00
|
|
|
)
|
|
|
|
|
2018-05-03 18:40:28 +00:00
|
|
|
var (
|
|
|
|
utf8BOM = []byte("\xef\xbb\xbf")
|
|
|
|
)
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
type JSONParser struct {
|
|
|
|
MetricName string
|
|
|
|
TagKeys []string
|
|
|
|
DefaultTags map[string]string
|
|
|
|
}
|
|
|
|
|
2016-11-15 18:02:55 +00:00
|
|
|
func (p *JSONParser) parseArray(buf []byte) ([]telegraf.Metric, error) {
|
2016-02-06 00:36:35 +00:00
|
|
|
metrics := make([]telegraf.Metric, 0)
|
|
|
|
|
2016-11-15 18:02:55 +00:00
|
|
|
var jsonOut []map[string]interface{}
|
2016-02-06 00:36:35 +00:00
|
|
|
err := json.Unmarshal(buf, &jsonOut)
|
|
|
|
if err != nil {
|
2016-11-15 18:02:55 +00:00
|
|
|
err = fmt.Errorf("unable to parse out as JSON Array, %s", err)
|
2016-02-06 00:36:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-15 18:02:55 +00:00
|
|
|
for _, item := range jsonOut {
|
|
|
|
metrics, err = p.parseObject(metrics, item)
|
|
|
|
}
|
|
|
|
return metrics, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *JSONParser) parseObject(metrics []telegraf.Metric, jsonOut map[string]interface{}) ([]telegraf.Metric, error) {
|
2016-02-06 00:36:35 +00:00
|
|
|
|
|
|
|
tags := make(map[string]string)
|
|
|
|
for k, v := range p.DefaultTags {
|
|
|
|
tags[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tag := range p.TagKeys {
|
|
|
|
switch v := jsonOut[tag].(type) {
|
|
|
|
case string:
|
|
|
|
tags[tag] = v
|
2016-09-21 17:07:35 +00:00
|
|
|
case bool:
|
|
|
|
tags[tag] = strconv.FormatBool(v)
|
|
|
|
case float64:
|
|
|
|
tags[tag] = strconv.FormatFloat(v, 'f', -1, 64)
|
2016-02-06 00:36:35 +00:00
|
|
|
}
|
|
|
|
delete(jsonOut, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
f := JSONFlattener{}
|
2016-11-15 18:02:55 +00:00
|
|
|
err := f.FlattenJSON("", jsonOut)
|
2016-02-06 00:36:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-22 12:51:57 +00:00
|
|
|
metric, err := metric.New(p.MetricName, tags, f.Fields, time.Now().UTC())
|
2016-02-06 00:36:35 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return append(metrics, metric), nil
|
|
|
|
}
|
|
|
|
|
2016-11-15 18:02:55 +00:00
|
|
|
func (p *JSONParser) Parse(buf []byte) ([]telegraf.Metric, error) {
|
2017-09-26 22:58:33 +00:00
|
|
|
buf = bytes.TrimSpace(buf)
|
2018-05-03 18:40:28 +00:00
|
|
|
buf = bytes.TrimPrefix(buf, utf8BOM)
|
2017-09-26 22:58:33 +00:00
|
|
|
if len(buf) == 0 {
|
|
|
|
return make([]telegraf.Metric, 0), nil
|
|
|
|
}
|
2016-11-15 18:02:55 +00:00
|
|
|
|
|
|
|
if !isarray(buf) {
|
|
|
|
metrics := make([]telegraf.Metric, 0)
|
|
|
|
var jsonOut map[string]interface{}
|
|
|
|
err := json.Unmarshal(buf, &jsonOut)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("unable to parse out as JSON, %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return p.parseObject(metrics, jsonOut)
|
|
|
|
}
|
|
|
|
return p.parseArray(buf)
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
func (p *JSONParser) ParseLine(line string) (telegraf.Metric, error) {
|
|
|
|
metrics, err := p.Parse([]byte(line + "\n"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(metrics) < 1 {
|
|
|
|
return nil, fmt.Errorf("Can not parse the line: %s, for data format: influx ", line)
|
|
|
|
}
|
|
|
|
|
|
|
|
return metrics[0], nil
|
|
|
|
}
|
|
|
|
|
2016-02-09 22:03:46 +00:00
|
|
|
func (p *JSONParser) SetDefaultTags(tags map[string]string) {
|
|
|
|
p.DefaultTags = tags
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
type JSONFlattener struct {
|
|
|
|
Fields map[string]interface{}
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:30:03 +00:00
|
|
|
// FlattenJSON flattens nested maps/interfaces into a fields map (ignoring bools and string)
|
2016-02-06 00:36:35 +00:00
|
|
|
func (f *JSONFlattener) FlattenJSON(
|
2016-12-20 16:30:03 +00:00
|
|
|
fieldname string,
|
|
|
|
v interface{}) error {
|
|
|
|
if f.Fields == nil {
|
|
|
|
f.Fields = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
return f.FullFlattenJSON(fieldname, v, false, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FullFlattenJSON flattens nested maps/interfaces into a fields map (including bools and string)
|
|
|
|
func (f *JSONFlattener) FullFlattenJSON(
|
2016-02-06 00:36:35 +00:00
|
|
|
fieldname string,
|
|
|
|
v interface{},
|
2016-12-20 16:30:03 +00:00
|
|
|
convertString bool,
|
|
|
|
convertBool bool,
|
2016-02-06 00:36:35 +00:00
|
|
|
) error {
|
|
|
|
if f.Fields == nil {
|
|
|
|
f.Fields = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
fieldname = strings.Trim(fieldname, "_")
|
|
|
|
switch t := v.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
for k, v := range t {
|
2016-12-20 16:30:03 +00:00
|
|
|
err := f.FullFlattenJSON(fieldname+"_"+k+"_", v, convertString, convertBool)
|
2016-02-06 00:36:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case []interface{}:
|
|
|
|
for i, v := range t {
|
|
|
|
k := strconv.Itoa(i)
|
2016-12-20 16:30:03 +00:00
|
|
|
err := f.FullFlattenJSON(fieldname+"_"+k+"_", v, convertString, convertBool)
|
2016-02-06 00:36:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
f.Fields[fieldname] = t
|
2016-12-20 16:30:03 +00:00
|
|
|
case string:
|
|
|
|
if convertString {
|
|
|
|
f.Fields[fieldname] = v.(string)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case bool:
|
|
|
|
if convertBool {
|
|
|
|
f.Fields[fieldname] = v.(bool)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case nil:
|
2016-02-06 00:36:35 +00:00
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("JSON Flattener: got unexpected type %T with value %v (%s)",
|
|
|
|
t, t, fieldname)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-15 18:02:55 +00:00
|
|
|
|
|
|
|
func isarray(buf []byte) bool {
|
|
|
|
ia := bytes.IndexByte(buf, '[')
|
|
|
|
ib := bytes.IndexByte(buf, '{')
|
|
|
|
if ia > -1 && ia < ib {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|