-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Feature/1363: Additional configuration for JSON parser #4351
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
420dafd
add "field_tags" to json parser config
MrMaxBuilds c50bfc2
edit unittests for JSON parser
MrMaxBuilds 7f2b2a0
test case edit
MrMaxBuilds 4db6675
newJsonParser function is now unexported, must use NewParser(config)
MrMaxBuilds 885193b
change test files to reflect unexported newJsonParser func
MrMaxBuilds 674b957
readability tweak
MrMaxBuilds e25fbed
add old exported func NewJSONParser
MrMaxBuilds 9196488
comments reflect deprecated NewJsonParser
MrMaxBuilds 22e28e0
gjson path support for json parser, still needs string_field supp
MrMaxBuilds 6207324
support for string_fields
MrMaxBuilds dca5e99
update readme and unit tests
MrMaxBuilds 5e22ec4
add support for time parsing
MrMaxBuilds 867c3a7
update readme
MrMaxBuilds fa4b2f0
address daniel's comments
MrMaxBuilds 197c474
changed some comments
MrMaxBuilds e2ebce0
Merge branch 'master' into feature/1363
MrMaxBuilds ab3433a
address some of greg's comments
MrMaxBuilds 8445d14
address more of greg's comments
MrMaxBuilds aff2697
Merge branch 'master' into feature/1363
MrMaxBuilds cabf688
unspecified years in timestamp yields current year
MrMaxBuilds 145c5ef
add json_name_key config
MrMaxBuilds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,9 +104,23 @@ but can be overridden using the `name_override` config option. | |
|
|
||
| #### JSON Configuration: | ||
|
|
||
| The JSON data format supports specifying "tag keys". If specified, keys | ||
| will be searched for in the root-level of the JSON blob. If the key(s) exist, | ||
| they will be applied as tags to the Telegraf metrics. | ||
| The JSON data format supports specifying "tag_keys", "string_keys", and "json_query". | ||
| If specified, keys in "tag_keys" and "string_keys" will be searched for in the root-level | ||
| and any nested lists of the JSON blob. All int and float values are added to fields by default. | ||
| If the key(s) exist, they will be applied as tags or fields to the Telegraf metrics. | ||
| If "string_keys" is specified, the string will be added as a field. | ||
|
|
||
| The "json_query" configuration is a gjson path to an JSON object or list of JSON objects. | ||
| If this path leads to an array of values or single data point an error will be thrown. If this | ||
| configuration is specified, only the result of the query will be parsed and returned as metrics. | ||
|
|
||
| Object paths are specified using gjson path format, which is denoted by object keys | ||
| concatenated with "." to go deeper in nested JSON objects. | ||
| Additional information on gjson paths can be found here: https://github.com/tidwall/gjson#path-syntax | ||
|
|
||
| The JSON data format also supports extracting time values through the config "json_time_key" and "json_time_format". | ||
| If "json_time_key" is set, "json_time_format" must be specified. The "json_time_key" describes the name of the field containing time information. The "json_time_format" must be a recognized Go time format. | ||
| More info on time formats can be found here: https://golang.org/src/time/format.go | ||
|
|
||
| For example, if you had this configuration: | ||
|
|
||
|
|
@@ -124,11 +138,25 @@ For example, if you had this configuration: | |
| ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md | ||
| data_format = "json" | ||
|
|
||
| ## List of tag names to extract from top-level of JSON server response | ||
| ## List of tag names to extract from JSON server response | ||
| tag_keys = [ | ||
| "my_tag_1", | ||
| "my_tag_2" | ||
| ] | ||
|
|
||
| ## List of field names to extract from JSON and add as string fields | ||
| # string_fields = [] | ||
|
|
||
| ## gjson query path to specify a specific chunk of JSON to be parsed with the above configuration | ||
|
||
| ## if not specified, the whole file will be parsed. | ||
| ## gjson query paths are described here: | ||
|
||
| # json_query = "" | ||
|
|
||
| ## holds the name of the tag of timestamp | ||
| # json_time_key = "" | ||
|
|
||
| ## holds the format of timestamp to be parsed | ||
| # json_time_format = "" | ||
| ``` | ||
|
|
||
| with this JSON output from a command: | ||
|
|
@@ -173,6 +201,19 @@ For example, if the following configuration: | |
| "my_tag_1", | ||
| "my_tag_2" | ||
| ] | ||
|
|
||
| ## List of field names to extract from JSON and add as string fields | ||
| # string_fields = [] | ||
|
|
||
| ## gjson query path to specify a specific chunk of JSON to be parsed with the above configuration | ||
| ## if not specified, the whole file will be parsed | ||
| # json_query = "" | ||
|
|
||
| ## holds the name of the tag of timestamp | ||
| # json_time_key = "b_time" | ||
|
|
||
| ## holds the format of timestamp to be parsed | ||
| # json_time_format = "02 Jan 06 15:04 MST" | ||
| ``` | ||
|
|
||
| with this JSON output from a command: | ||
|
|
@@ -182,27 +223,89 @@ with this JSON output from a command: | |
| { | ||
| "a": 5, | ||
| "b": { | ||
| "c": 6 | ||
| "c": 6, | ||
| "time":"04 Jan 06 15:04 MST" | ||
| }, | ||
| "my_tag_1": "foo", | ||
| "my_tag_2": "baz" | ||
| }, | ||
| { | ||
| "a": 7, | ||
| "b": { | ||
| "c": 8 | ||
| "c": 8, | ||
| "time":"11 Jan 07 15:04 MST" | ||
| }, | ||
| "my_tag_1": "bar", | ||
| "my_tag_2": "baz" | ||
| } | ||
| ] | ||
| ``` | ||
|
|
||
| Your Telegraf metrics would get tagged with "my_tag_1" and "my_tag_2" | ||
| Your Telegraf metrics would get tagged with "my_tag_1" and "my_tag_2" and fielded with "b_c" | ||
| The metric's time will be a time.Time object, as specified by "b_time" | ||
|
|
||
| ``` | ||
| exec_mycollector,my_tag_1=foo,my_tag_2=baz b_c=6 1136387040000000000 | ||
| exec_mycollector,my_tag_1=bar,my_tag_2=baz b_c=8 1168527840000000000 | ||
| ``` | ||
|
|
||
| If you want to only use a specific portion of your JSON, use the "json_query" | ||
| configuration to specify a path to a JSON object. | ||
|
|
||
| For example, with the following config: | ||
| ```toml | ||
| [[inputs.exec]] | ||
| ## Commands array | ||
| commands = ["/usr/bin/mycollector --foo=bar"] | ||
|
|
||
| ## measurement name suffix (for separating different commands) | ||
| name_suffix = "_mycollector" | ||
|
|
||
| ## Data format to consume. | ||
| ## Each data format has its own unique set of configuration options, read | ||
| ## more about them here: | ||
| ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md | ||
| data_format = "json" | ||
|
|
||
| ## List of tag names to extract from top-level of JSON server response | ||
| tag_keys = ["first"] | ||
|
|
||
| ## List of field names to extract from JSON and add as string fields | ||
| string_fields = ["last"] | ||
|
|
||
| ## gjson query path to specify a specific chunk of JSON to be parsed with the above configuration | ||
| ## if not specified, the whole file will be parsed | ||
| json_query = "obj.friends" | ||
|
|
||
| ## holds the name of the tag of timestamp | ||
| # json_time_key = "" | ||
|
|
||
| ## holds the format of timestamp to be parsed | ||
| # json_time_format = "" | ||
| ``` | ||
|
|
||
| with this JSON as input: | ||
| ```json | ||
| { | ||
| "obj": { | ||
| "name": {"first": "Tom", "last": "Anderson"}, | ||
| "age":37, | ||
| "children": ["Sara","Alex","Jack"], | ||
| "fav.movie": "Deer Hunter", | ||
| "friends": [ | ||
| {"first": "Dale", "last": "Murphy", "age": 44}, | ||
| {"first": "Roger", "last": "Craig", "age": 68}, | ||
| {"first": "Jane", "last": "Murphy", "age": 47} | ||
| ] | ||
| } | ||
| } | ||
| ``` | ||
| You would recieve 3 metrics tagged with "first", and fielded with "last" and "age" | ||
|
|
||
| ``` | ||
| exec_mycollector,my_tag_1=foo,my_tag_2=baz a=5,b_c=6 | ||
| exec_mycollector,my_tag_1=bar,my_tag_2=baz a=7,b_c=8 | ||
| exec_mycollector, "first":"Dale" "last":"Murphy","age":44 | ||
| exec_mycollector, "first":"Roger" "last":"Craig","age":68 | ||
| exec_mycollector, "first":"Jane" "last":"Murphy","age":47 | ||
| ``` | ||
|
|
||
| # Value: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Use
https://golang.org/pkg/time/#Parseas the link. This other link is still not great but the audience of this documentation is not Go developers.Try to wrap around 78 chars