-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow for Tags on non base level JSON format #4284
Conversation
cc76d9c
to
c9269b8
Compare
plugins/parsers/json/parser.go
Outdated
case bool: | ||
tags[name] = strconv.FormatBool(fields[name].(bool)) | ||
delete(fields, name) | ||
case int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is not possible to have ints in parsed JSON, so we can remove this case. Let's also add a warning though if we hit the default case:
log.Printf("E! [parsers.json] Unrecognized type %T", value)
plugins/parsers/json/parser.go
Outdated
for _, name := range p.TagKeys { | ||
//switch any fields in tagkeys into tags | ||
if fields[name] != nil { | ||
switch fields[name].(type) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid needing to type assert in each case, you can do this:
switch value := fields[name].(type) {
Then in each case the value will already be in the type of the case label.
plugins/parsers/json/parser.go
Outdated
func (p *JSONParser) switchFieldToTag(tags map[string]string, fields map[string]interface{}) (map[string]string, map[string]interface{}) { | ||
for _, name := range p.TagKeys { | ||
//switch any fields in tagkeys into tags | ||
if fields[name] != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick, instead of nesting the switch statement in this check, I would check and continue:
if fields[name] == nil {
continue
}
switch ....
This help with readability because the scope of the conditional is reduced.
plugins/parsers/json/parser.go
Outdated
@@ -119,6 +159,9 @@ func (f *JSONFlattener) FlattenJSON( | |||
if f.Fields == nil { | |||
f.Fields = make(map[string]interface{}) | |||
} | |||
|
|||
//bool values to be changed to allow strings, bools as fields | |||
//changed to true true, func switchFieldToTag will remove values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove this comment
plugins/parsers/json/parser_test.go
Outdated
parser := JSONParser{ | ||
MetricName: "json_test", | ||
TagKeys: []string{"total_devices", "total_threads", "shares_tester", "shares_tester3"}, | ||
//TagKeys: []string{"mytag", "a", "b_c", "b_d"}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove any commented out code
plugins/parsers/json/parser_test.go
Outdated
t.Logf("error: %v", err) | ||
t.Logf("num of metrics: %v", len(metrics)) | ||
t.Logf("metric tags: %v", metrics[0].Tags()) | ||
t.Logf("metric fields: %v", metrics[0].Fields()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of logging the values, use require to assert that the values are as expected.
if err != nil { | ||
return nil, err | ||
} | ||
|
||
metric, err := metric.New(p.MetricName, tags, f.Fields, time.Now().UTC()) | ||
tags, nFields := p.switchFieldToTag(tags, f.Fields) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that we are adjusting the values after flattening, is it still required to keep the loop above on line 47?
closes #4260 |
(cherry picked from commit 8482c40)
* origin: (39 commits) Document path tag in tail input Update changelog Added path tag to tail input plugin (influxdata#4292) Run windows tests with -short Fix postfix input handling of multi-level queues (influxdata#4333) Update changelog Add support for comma in logparser timestamp format (influxdata#4311) Update vendoring to dep from gdm (influxdata#4314) Update changelog Add new measurement with results of pgrep lookup to procstat input (influxdata#4307) Update changelog Add valuecounter aggregator plugin (influxdata#3523) Update changelog Update docker input documentation for container status Add container status tag to docker input (influxdata#4259) Drop CI support for Go 1.8 (influxdata#4309) Update changelog Fix selection of tags under nested objects in the JSON parser (influxdata#4284) Update changelog Add owner tag on partitions in burrow input (influxdata#4281) ...
…data#4284) (cherry picked from commit 8482c40)
Required for all PRs: