Use same timestamp for all objects in arrays in the json parser (#7412)
This commit is contained in:
parent
3163771778
commit
e1b2ebe06d
|
@ -67,13 +67,13 @@ func New(config *Config) (*Parser, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) parseArray(data []interface{}) ([]telegraf.Metric, error) {
|
func (p *Parser) parseArray(data []interface{}, timestamp time.Time) ([]telegraf.Metric, error) {
|
||||||
results := make([]telegraf.Metric, 0)
|
results := make([]telegraf.Metric, 0)
|
||||||
|
|
||||||
for _, item := range data {
|
for _, item := range data {
|
||||||
switch v := item.(type) {
|
switch v := item.(type) {
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
metrics, err := p.parseObject(v)
|
metrics, err := p.parseObject(v, timestamp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if p.strict {
|
if p.strict {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -90,7 +90,7 @@ func (p *Parser) parseArray(data []interface{}) ([]telegraf.Metric, error) {
|
||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) parseObject(data map[string]interface{}) ([]telegraf.Metric, error) {
|
func (p *Parser) parseObject(data map[string]interface{}, timestamp time.Time) ([]telegraf.Metric, error) {
|
||||||
tags := make(map[string]string)
|
tags := make(map[string]string)
|
||||||
for k, v := range p.defaultTags {
|
for k, v := range p.defaultTags {
|
||||||
tags[k] = v
|
tags[k] = v
|
||||||
|
@ -112,8 +112,7 @@ func (p *Parser) parseObject(data map[string]interface{}) ([]telegraf.Metric, er
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//if time key is specified, set it to nTime
|
//if time key is specified, set timestamp to it
|
||||||
nTime := time.Now().UTC()
|
|
||||||
if p.timeKey != "" {
|
if p.timeKey != "" {
|
||||||
if p.timeFormat == "" {
|
if p.timeFormat == "" {
|
||||||
err := fmt.Errorf("use of 'json_time_key' requires 'json_time_format'")
|
err := fmt.Errorf("use of 'json_time_key' requires 'json_time_format'")
|
||||||
|
@ -125,7 +124,7 @@ func (p *Parser) parseObject(data map[string]interface{}) ([]telegraf.Metric, er
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
nTime, err = internal.ParseTimestamp(p.timeFormat, f.Fields[p.timeKey], p.timezone)
|
timestamp, err = internal.ParseTimestamp(p.timeFormat, f.Fields[p.timeKey], p.timezone)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -133,13 +132,13 @@ func (p *Parser) parseObject(data map[string]interface{}) ([]telegraf.Metric, er
|
||||||
delete(f.Fields, p.timeKey)
|
delete(f.Fields, p.timeKey)
|
||||||
|
|
||||||
//if the year is 0, set to current year
|
//if the year is 0, set to current year
|
||||||
if nTime.Year() == 0 {
|
if timestamp.Year() == 0 {
|
||||||
nTime = nTime.AddDate(time.Now().Year(), 0, 0)
|
timestamp = timestamp.AddDate(time.Now().Year(), 0, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tags, nFields := p.switchFieldToTag(tags, f.Fields)
|
tags, nFields := p.switchFieldToTag(tags, f.Fields)
|
||||||
metric, err := metric.New(name, tags, nFields, nTime)
|
metric, err := metric.New(name, tags, nFields, timestamp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -206,11 +205,12 @@ func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
timestamp := time.Now().UTC()
|
||||||
switch v := data.(type) {
|
switch v := data.(type) {
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
return p.parseObject(v)
|
return p.parseObject(v, timestamp)
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
return p.parseArray(v)
|
return p.parseArray(v, timestamp)
|
||||||
default:
|
default:
|
||||||
return nil, ErrWrongType
|
return nil, ErrWrongType
|
||||||
}
|
}
|
||||||
|
|
|
@ -794,6 +794,18 @@ func TestTimeErrors(t *testing.T) {
|
||||||
require.Equal(t, fmt.Errorf("JSON time key could not be found"), err)
|
require.Equal(t, fmt.Errorf("JSON time key could not be found"), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestShareTimestamp(t *testing.T) {
|
||||||
|
parser, err := New(&Config{
|
||||||
|
MetricName: "json_test",
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
metrics, err := parser.Parse([]byte(validJSONArrayMultiple))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, 2, len(metrics))
|
||||||
|
require.Equal(t, true, metrics[0].Time() == metrics[1].Time())
|
||||||
|
}
|
||||||
|
|
||||||
func TestNameKey(t *testing.T) {
|
func TestNameKey(t *testing.T) {
|
||||||
testString := `{
|
testString := `{
|
||||||
"a": 5,
|
"a": 5,
|
||||||
|
|
Loading…
Reference in New Issue