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"
|
2019-07-17 22:03:09 +00:00
|
|
|
"errors"
|
2016-02-06 00:36:35 +00:00
|
|
|
"fmt"
|
2018-06-14 20:17:32 +00:00
|
|
|
"log"
|
2016-02-06 00:36:35 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2019-07-15 23:48:19 +00:00
|
|
|
"github.com/influxdata/telegraf/filter"
|
2019-02-06 01:43:35 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-11-22 12:51:57 +00:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2019-07-15 23:48:19 +00:00
|
|
|
"github.com/tidwall/gjson"
|
2016-02-06 00:36:35 +00:00
|
|
|
)
|
|
|
|
|
2018-05-03 18:40:28 +00:00
|
|
|
var (
|
2019-07-17 22:03:09 +00:00
|
|
|
utf8BOM = []byte("\xef\xbb\xbf")
|
|
|
|
ErrWrongType = errors.New("must be an object or an array of objects")
|
2018-05-03 18:40:28 +00:00
|
|
|
)
|
|
|
|
|
2019-07-15 23:48:19 +00:00
|
|
|
type Config struct {
|
|
|
|
MetricName string
|
|
|
|
TagKeys []string
|
|
|
|
NameKey string
|
|
|
|
StringFields []string
|
|
|
|
Query string
|
|
|
|
TimeKey string
|
|
|
|
TimeFormat string
|
|
|
|
Timezone string
|
|
|
|
DefaultTags map[string]string
|
2019-10-23 21:06:39 +00:00
|
|
|
Strict bool
|
2019-07-15 23:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Parser struct {
|
|
|
|
metricName string
|
|
|
|
tagKeys []string
|
|
|
|
stringFields filter.Filter
|
|
|
|
nameKey string
|
|
|
|
query string
|
|
|
|
timeKey string
|
|
|
|
timeFormat string
|
|
|
|
timezone string
|
|
|
|
defaultTags map[string]string
|
2019-10-23 21:06:39 +00:00
|
|
|
strict bool
|
2019-07-15 23:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(config *Config) (*Parser, error) {
|
|
|
|
stringFilter, err := filter.Compile(config.StringFields)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Parser{
|
|
|
|
metricName: config.MetricName,
|
|
|
|
tagKeys: config.TagKeys,
|
|
|
|
nameKey: config.NameKey,
|
|
|
|
stringFields: stringFilter,
|
|
|
|
query: config.Query,
|
|
|
|
timeKey: config.TimeKey,
|
|
|
|
timeFormat: config.TimeFormat,
|
|
|
|
timezone: config.Timezone,
|
|
|
|
defaultTags: config.DefaultTags,
|
2019-10-23 21:06:39 +00:00
|
|
|
strict: config.Strict,
|
2019-07-15 23:48:19 +00:00
|
|
|
}, nil
|
2016-02-06 00:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 22:03:09 +00:00
|
|
|
func (p *Parser) parseArray(data []interface{}) ([]telegraf.Metric, error) {
|
|
|
|
results := make([]telegraf.Metric, 0)
|
|
|
|
|
|
|
|
for _, item := range data {
|
|
|
|
switch v := item.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
metrics, err := p.parseObject(v)
|
|
|
|
if err != nil {
|
2019-10-23 21:06:39 +00:00
|
|
|
if p.strict {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
continue
|
2019-07-17 22:03:09 +00:00
|
|
|
}
|
|
|
|
results = append(results, metrics...)
|
|
|
|
default:
|
|
|
|
return nil, ErrWrongType
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2018-08-23 02:26:48 +00:00
|
|
|
}
|
2016-11-15 18:02:55 +00:00
|
|
|
}
|
2019-07-17 22:03:09 +00:00
|
|
|
|
|
|
|
return results, nil
|
2016-11-15 18:02:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 22:03:09 +00:00
|
|
|
func (p *Parser) parseObject(data map[string]interface{}) ([]telegraf.Metric, error) {
|
2016-02-06 00:36:35 +00:00
|
|
|
tags := make(map[string]string)
|
2019-07-15 23:48:19 +00:00
|
|
|
for k, v := range p.defaultTags {
|
2016-02-06 00:36:35 +00:00
|
|
|
tags[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
f := JSONFlattener{}
|
2019-07-17 22:03:09 +00:00
|
|
|
err := f.FullFlattenJSON("", data, true, true)
|
2016-02-06 00:36:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-15 23:48:19 +00:00
|
|
|
name := p.metricName
|
|
|
|
|
2018-08-23 02:26:48 +00:00
|
|
|
//checks if json_name_key is set
|
2019-07-15 23:48:19 +00:00
|
|
|
if p.nameKey != "" {
|
|
|
|
switch field := f.Fields[p.nameKey].(type) {
|
2018-09-21 22:47:41 +00:00
|
|
|
case string:
|
2019-07-15 23:48:19 +00:00
|
|
|
name = field
|
2018-09-21 22:47:41 +00:00
|
|
|
}
|
2018-08-23 02:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//if time key is specified, set it to nTime
|
|
|
|
nTime := time.Now().UTC()
|
2019-07-15 23:48:19 +00:00
|
|
|
if p.timeKey != "" {
|
|
|
|
if p.timeFormat == "" {
|
2018-08-23 02:26:48 +00:00
|
|
|
err := fmt.Errorf("use of 'json_time_key' requires 'json_time_format'")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-15 23:48:19 +00:00
|
|
|
if f.Fields[p.timeKey] == nil {
|
2018-08-23 02:26:48 +00:00
|
|
|
err := fmt.Errorf("JSON time key could not be found")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-16 23:32:14 +00:00
|
|
|
nTime, err = internal.ParseTimestamp(p.timeFormat, f.Fields[p.timeKey], p.timezone)
|
2019-02-06 01:43:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-08-23 02:26:48 +00:00
|
|
|
}
|
2018-06-14 20:17:32 +00:00
|
|
|
|
2019-07-15 23:48:19 +00:00
|
|
|
delete(f.Fields, p.timeKey)
|
2018-11-03 00:53:45 +00:00
|
|
|
|
2018-08-23 02:26:48 +00:00
|
|
|
//if the year is 0, set to current year
|
|
|
|
if nTime.Year() == 0 {
|
|
|
|
nTime = nTime.AddDate(time.Now().Year(), 0, 0)
|
|
|
|
}
|
|
|
|
}
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2018-08-23 02:26:48 +00:00
|
|
|
tags, nFields := p.switchFieldToTag(tags, f.Fields)
|
2019-07-15 23:48:19 +00:00
|
|
|
metric, err := metric.New(name, tags, nFields, nTime)
|
2016-02-06 00:36:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-17 22:03:09 +00:00
|
|
|
return []telegraf.Metric{metric}, nil
|
2016-02-06 00:36:35 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 20:17:32 +00:00
|
|
|
//will take in field map with strings and bools,
|
|
|
|
//search for TagKeys that match fieldnames and add them to tags
|
|
|
|
//will delete any strings/bools that shouldn't be fields
|
|
|
|
//assumes that any non-numeric values in TagKeys should be displayed as tags
|
2019-07-15 23:48:19 +00:00
|
|
|
func (p *Parser) switchFieldToTag(tags map[string]string, fields map[string]interface{}) (map[string]string, map[string]interface{}) {
|
|
|
|
for _, name := range p.tagKeys {
|
2018-06-14 20:17:32 +00:00
|
|
|
//switch any fields in tagkeys into tags
|
|
|
|
if fields[name] == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch value := fields[name].(type) {
|
|
|
|
case string:
|
|
|
|
tags[name] = value
|
|
|
|
delete(fields, name)
|
|
|
|
case bool:
|
|
|
|
tags[name] = strconv.FormatBool(value)
|
|
|
|
delete(fields, name)
|
|
|
|
case float64:
|
|
|
|
tags[name] = strconv.FormatFloat(value, 'f', -1, 64)
|
|
|
|
delete(fields, name)
|
|
|
|
default:
|
|
|
|
log.Printf("E! [parsers.json] Unrecognized type %T", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//remove any additional string/bool values from fields
|
2019-07-15 23:48:19 +00:00
|
|
|
for fk := range fields {
|
|
|
|
switch fields[fk].(type) {
|
2019-09-16 23:30:34 +00:00
|
|
|
case string, bool:
|
2019-07-15 23:48:19 +00:00
|
|
|
if p.stringFields != nil && p.stringFields.Match(fk) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
delete(fields, fk)
|
2018-06-14 20:17:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tags, fields
|
|
|
|
}
|
|
|
|
|
2019-07-15 23:48:19 +00:00
|
|
|
func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) {
|
|
|
|
if p.query != "" {
|
|
|
|
result := gjson.GetBytes(buf, p.query)
|
2018-08-23 02:26:48 +00:00
|
|
|
buf = []byte(result.Raw)
|
|
|
|
if !result.IsArray() && !result.IsObject() {
|
|
|
|
err := fmt.Errorf("E! Query path must lead to a JSON object or array of objects, but lead to: %v", result.Type)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-07-17 22:03:09 +00:00
|
|
|
var data interface{}
|
|
|
|
err := json.Unmarshal(buf, &data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := data.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
return p.parseObject(v)
|
|
|
|
case []interface{}:
|
|
|
|
return p.parseArray(v)
|
|
|
|
default:
|
|
|
|
return nil, ErrWrongType
|
2016-11-15 18:02:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-15 23:48:19 +00:00
|
|
|
func (p *Parser) ParseLine(line string) (telegraf.Metric, error) {
|
2016-02-06 00:36:35 +00:00
|
|
|
metrics, err := p.Parse([]byte(line + "\n"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(metrics) < 1 {
|
2018-08-23 02:26:48 +00:00
|
|
|
return nil, fmt.Errorf("can not parse the line: %s, for data format: json ", line)
|
2016-02-06 00:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return metrics[0], nil
|
|
|
|
}
|
|
|
|
|
2019-07-15 23:48:19 +00:00
|
|
|
func (p *Parser) SetDefaultTags(tags map[string]string) {
|
|
|
|
p.defaultTags = tags
|
2016-02-09 22:03:46 +00:00
|
|
|
}
|
|
|
|
|
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{})
|
|
|
|
}
|
2018-06-14 20:17:32 +00:00
|
|
|
|
2016-12-20 16:30:03 +00:00
|
|
|
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
|
|
|
|
}
|