fix(json_v2): remove dead code (#9908)

This commit is contained in:
Sebastian Spaink 2021-10-13 13:04:02 -05:00 committed by GitHub
parent 0be92db8af
commit 061b4094cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 59 additions and 84 deletions

View File

@ -258,9 +258,6 @@ func (p *Parser) expandArray(result MetricNode) ([]telegraf.Metric, error) {
p.Log.Debugf("Found object in query ignoring it please use 'object' to gather metrics from objects") p.Log.Debugf("Found object in query ignoring it please use 'object' to gather metrics from objects")
return results, nil return results, nil
} }
if result.IncludeCollection == nil && (len(p.objectConfig.FieldPaths) > 0 || len(p.objectConfig.TagPaths) > 0) {
result.IncludeCollection = p.existsInpathResults(result.Index, result.Raw)
}
r, err := p.combineObject(result) r, err := p.combineObject(result)
if err != nil { if err != nil {
return nil, err return nil, err
@ -272,7 +269,7 @@ func (p *Parser) expandArray(result MetricNode) ([]telegraf.Metric, error) {
if result.IsArray() { if result.IsArray() {
var err error var err error
if result.IncludeCollection == nil && (len(p.objectConfig.FieldPaths) > 0 || len(p.objectConfig.TagPaths) > 0) { if result.IncludeCollection == nil && (len(p.objectConfig.FieldPaths) > 0 || len(p.objectConfig.TagPaths) > 0) {
result.IncludeCollection = p.existsInpathResults(result.Index, result.Raw) result.IncludeCollection = p.existsInpathResults(result.Index)
} }
result.ForEach(func(_, val gjson.Result) bool { result.ForEach(func(_, val gjson.Result) bool {
m := metric.New( m := metric.New(
@ -282,23 +279,16 @@ func (p *Parser) expandArray(result MetricNode) ([]telegraf.Metric, error) {
p.timestamp, p.timestamp,
) )
if val.IsObject() { if val.IsObject() {
if p.iterateObjects { n := result
n := result n.ParentIndex += val.Index
n.ParentIndex += val.Index n.Metric = m
n.Metric = m n.Result = val
n.Result = val r, err := p.combineObject(n)
if n.IncludeCollection == nil && (len(p.objectConfig.FieldPaths) > 0 || len(p.objectConfig.TagPaths) > 0) { if err != nil {
n.IncludeCollection = p.existsInpathResults(n.Index, n.Raw) return false
}
r, err := p.combineObject(n)
if err != nil {
return false
}
results = append(results, r...)
} else {
p.Log.Debugf("Found object in query ignoring it please use 'object' to gather metrics from objects")
} }
results = append(results, r...)
if len(results) != 0 { if len(results) != 0 {
for _, newResult := range results { for _, newResult := range results {
mergeMetric(result.Metric, newResult) mergeMetric(result.Metric, newResult)
@ -307,19 +297,11 @@ func (p *Parser) expandArray(result MetricNode) ([]telegraf.Metric, error) {
return true return true
} }
for _, f := range result.Metric.FieldList() { mergeMetric(result.Metric, m)
m.AddField(f.Key, f.Value)
}
for _, f := range result.Metric.TagList() {
m.AddTag(f.Key, f.Value)
}
n := result n := result
n.ParentIndex += val.Index n.ParentIndex += val.Index
n.Metric = m n.Metric = m
n.Result = val n.Result = val
if n.IncludeCollection == nil && (len(p.objectConfig.FieldPaths) > 0 || len(p.objectConfig.TagPaths) > 0) {
n.IncludeCollection = p.existsInpathResults(n.Index, n.Raw)
}
r, err := p.expandArray(n) r, err := p.expandArray(n)
if err != nil { if err != nil {
return false return false
@ -355,7 +337,7 @@ func (p *Parser) expandArray(result MetricNode) ([]telegraf.Metric, error) {
pathResult = result.IncludeCollection pathResult = result.IncludeCollection
} else { } else {
// Verify that the result should be included based on the results of fieldpaths and tag paths // Verify that the result should be included based on the results of fieldpaths and tag paths
pathResult = p.existsInpathResults(result.ParentIndex, result.Raw) pathResult = p.existsInpathResults(result.ParentIndex)
} }
if pathResult == nil { if pathResult == nil {
return results, nil return results, nil
@ -392,17 +374,18 @@ func (p *Parser) expandArray(result MetricNode) ([]telegraf.Metric, error) {
return results, nil return results, nil
} }
func (p *Parser) existsInpathResults(index int, raw string) *PathResult { func (p *Parser) existsInpathResults(index int) *PathResult {
for _, f := range p.subPathResults { for _, f := range p.subPathResults {
if f.result.Index == 0 { if f.result.Index == index {
for _, i := range f.result.Indexes {
if i == index {
return &f
}
}
} else if f.result.Index == index {
return &f return &f
} }
// Indexes will be populated with all the elements that match on a `#(...)#` query
for _, i := range f.result.Indexes {
if i == index {
return &f
}
}
} }
return nil return nil
} }
@ -435,10 +418,6 @@ func (p *Parser) processObjects(input []byte, objects []JSONObject) ([]telegraf.
p.subPathResults = append(p.subPathResults, r) p.subPathResults = append(p.subPathResults, r)
} }
if result.Type == gjson.Null {
return nil, fmt.Errorf("GJSON Path returned null")
}
rootObject := MetricNode{ rootObject := MetricNode{
ParentIndex: 0, ParentIndex: 0,
Metric: metric.New( Metric: metric.New(
@ -578,33 +557,31 @@ func (p *Parser) SetDefaultTags(tags map[string]string) {
func (p *Parser) convertType(input gjson.Result, desiredType string, name string) (interface{}, error) { func (p *Parser) convertType(input gjson.Result, desiredType string, name string) (interface{}, error) {
switch inputType := input.Value().(type) { switch inputType := input.Value().(type) {
case string: case string:
if desiredType != "string" { switch desiredType {
switch desiredType { case "uint":
case "uint": r, err := strconv.ParseUint(inputType, 10, 64)
r, err := strconv.ParseUint(inputType, 10, 64) if err != nil {
if err != nil { return nil, fmt.Errorf("Unable to convert field '%s' to type uint: %v", name, err)
return nil, fmt.Errorf("unable to convert field '%s' to type uint: %v", name, err)
}
return r, nil
case "int":
r, err := strconv.ParseInt(inputType, 10, 64)
if err != nil {
return nil, fmt.Errorf("unable to convert field '%s' to type int: %v", name, err)
}
return r, nil
case "float":
r, err := strconv.ParseFloat(inputType, 64)
if err != nil {
return nil, fmt.Errorf("unable to convert field '%s' to type float: %v", name, err)
}
return r, nil
case "bool":
r, err := strconv.ParseBool(inputType)
if err != nil {
return nil, fmt.Errorf("unable to convert field '%s' to type bool: %v", name, err)
}
return r, nil
} }
return r, nil
case "int":
r, err := strconv.ParseInt(inputType, 10, 64)
if err != nil {
return nil, fmt.Errorf("Unable to convert field '%s' to type int: %v", name, err)
}
return r, nil
case "float":
r, err := strconv.ParseFloat(inputType, 64)
if err != nil {
return nil, fmt.Errorf("Unable to convert field '%s' to type float: %v", name, err)
}
return r, nil
case "bool":
r, err := strconv.ParseBool(inputType)
if err != nil {
return nil, fmt.Errorf("Unable to convert field '%s' to type bool: %v", name, err)
}
return r, nil
} }
case bool: case bool:
switch desiredType { switch desiredType {
@ -624,22 +601,20 @@ func (p *Parser) convertType(input gjson.Result, desiredType string, name string
return uint64(0), nil return uint64(0), nil
} }
case float64: case float64:
if desiredType != "float" { switch desiredType {
switch desiredType { case "string":
case "string": return fmt.Sprint(inputType), nil
return fmt.Sprint(inputType), nil case "int":
case "int": return input.Int(), nil
return input.Int(), nil case "uint":
case "uint": return input.Uint(), nil
return input.Uint(), nil case "bool":
case "bool": if inputType == 0 {
if inputType == 0 { return false, nil
return false, nil } else if inputType == 1 {
} else if inputType == 1 { return true, nil
return true, nil } else {
} else { return nil, fmt.Errorf("Unable to convert field '%s' to type bool", name)
return nil, fmt.Errorf("unable to convert field '%s' to type bool", name)
}
} }
} }
default: default: