Fix panic if JSONNameKey is not found (#4735)

This commit is contained in:
Greg 2018-09-21 15:47:41 -07:00 committed by Daniel Nelson
parent 146a30e065
commit 4c9c31c34f
2 changed files with 8 additions and 6 deletions

View File

@ -108,7 +108,7 @@ Config:
files = ["example"]
name_key = "name"
tag_keys = ["my_tag_1"]
string_fields = ["my_field"]
string_fields = ["b_my_field"]
data_format = "json"
```
@ -127,7 +127,7 @@ Input:
Output:
```
my_json,my_tag_1=foo a=5,b_c=6,my_field="description"
my_json,my_tag_1=foo a=5,b_c=6,b_my_field="description"
```
#### Arrays

View File

@ -5,6 +5,8 @@ import (
"encoding/json"
"fmt"
"log"
"math"
"regexp"
"strconv"
"strings"
"time"
@ -13,8 +15,6 @@ import (
"github.com/influxdata/telegraf/metric"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"math"
"regexp"
)
var (
@ -94,7 +94,6 @@ func parseUnixTimestamp(jsonValue interface{}, format string) (time.Time, error)
}
func (p *JSONParser) parseObject(metrics []telegraf.Metric, jsonOut map[string]interface{}) ([]telegraf.Metric, error) {
tags := make(map[string]string)
for k, v := range p.DefaultTags {
tags[k] = v
@ -108,7 +107,10 @@ func (p *JSONParser) parseObject(metrics []telegraf.Metric, jsonOut map[string]i
//checks if json_name_key is set
if p.JSONNameKey != "" {
p.MetricName = f.Fields[p.JSONNameKey].(string)
switch field := f.Fields[p.JSONNameKey].(type) {
case string:
p.MetricName = field
}
}
//if time key is specified, set it to nTime