Skip to content
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

Make JSON format compatible with nulls #9110

Merged
merged 1 commit into from
Apr 20, 2021
Merged

Make JSON format compatible with nulls #9110

merged 1 commit into from
Apr 20, 2021

Conversation

jouir
Copy link
Contributor

@jouir jouir commented Apr 9, 2021

Telegraf expects either an object or an array of objects but nulls are valid JSON values. This commit detects if query returns a null or the document is a null to return an empty metric instead of an error.

For example, with this configuration:

test.json:

{"error":null,"result":{"data":null,"items_per_page":10,"total_items":0,"total_pages":0}}

test.conf:

[[inputs.file]]
  files = ["test.json"]
  data_format = "json"
  json_query = "result.data"

The query returns null:

$ cat test.json | jq ".result.data"
null

Latest version returns an error:

$ docker run -it -v $(pwd):/mnt/ -w "/mnt" --rm -e telegraf:latest telegraf -test -debug -config test.conf
2021-04-09T09:31:08Z I! Starting Telegraf 1.18.1
2021-04-09T09:31:08Z D! [agent] Initializing plugins
2021-04-09T09:31:08Z D! [agent] Starting service inputs
2021-04-09T09:31:08Z E! [inputs.file] Error in plugin: E! Query path must lead to a JSON object or array of objects, but lead to: Null
2021-04-09T09:31:08Z D! [agent] Stopping service inputs
2021-04-09T09:31:08Z D! [agent] Input channel closed
2021-04-09T09:31:08Z D! [agent] Stopped Successfully
2021-04-09T09:31:08Z E! [telegraf] Error running agent: input plugins recorded 1 errors

This commits skip the error:

$ ./telegraf -test -debug -config test.conf
2021-04-09T09:34:02Z I! Starting Telegraf 
2021-04-09T09:34:02Z D! [agent] Initializing plugins
2021-04-09T09:34:02Z D! [agent] Starting service inputs
2021-04-09T09:34:02Z D! [agent] Stopping service inputs
2021-04-09T09:34:02Z D! [agent] Input channel closed
2021-04-09T09:34:02Z D! [agent] Stopped Successfully

In the end, in both cases, no metric is written to the outputs.

Same tests for a null document:

test.json:

null

Document is valid:

$ cat test.json | jq
null

Unpatched:

$ docker run -it -v $(pwd):/mnt/ -w "/mnt" --rm -e telegraf:latest telegraf -test -debug -config test.conf
2021-04-09T09:42:17Z I! Starting Telegraf 1.18.1
2021-04-09T09:42:17Z D! [agent] Initializing plugins
2021-04-09T09:42:17Z D! [agent] Starting service inputs
2021-04-09T09:42:17Z E! [inputs.file] Error in plugin: E! Query path must lead to a JSON object or array of objects, but lead to: Null
2021-04-09T09:42:17Z D! [agent] Stopping service inputs
2021-04-09T09:42:17Z D! [agent] Input channel closed
2021-04-09T09:42:17Z D! [agent] Stopped Successfully
2021-04-09T09:42:17Z E! [telegraf] Error running agent: input plugins recorded 1 errors

Patched:

$ ./telegraf -test -debug -config test.conf
2021-04-09T09:42:33Z I! Starting Telegraf 
2021-04-09T09:42:33Z D! [agent] Initializing plugins
2021-04-09T09:42:33Z D! [agent] Starting service inputs
2021-04-09T09:42:33Z D! [agent] Stopping service inputs
2021-04-09T09:42:33Z D! [agent] Input channel closed
2021-04-09T09:42:33Z D! [agent] Stopped Successfully

Required for all PRs:

  • Updated associated README.md.
  • Wrote appropriate unit tests.

Return an empty list of metrics when:

  • result type is gjson.Null if a query is detected
  • type of parsed JSON document is nil

@telegraf-tiger telegraf-tiger bot added the feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin label Apr 9, 2021
Copy link
Contributor

@telegraf-tiger telegraf-tiger bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤝 ✅ CLA has been signed. Thank you!

Copy link
Contributor

@telegraf-tiger telegraf-tiger bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@ivorybilled ivorybilled left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to make sense to me.

Copy link
Contributor

@ssoroka ssoroka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, makes sense. I prefer []telegraf.Metric{} to calling make, but returning nil should be acceptable

plugins/parsers/json/parser.go Outdated Show resolved Hide resolved
plugins/parsers/json/parser.go Outdated Show resolved Hide resolved
Telegraf expects either an object or an array of objects but nulls are valid
JSON values. This commit detects if query returns a null or the document is a
null to return an empty metric instead of an error.

Signed-off-by: Julien Riou <julien@riou.xyz>
Copy link
Contributor

@telegraf-tiger telegraf-tiger bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ssoroka ssoroka merged commit 4336dae into influxdata:master Apr 20, 2021
reimda pushed a commit that referenced this pull request Apr 28, 2021
arstercz pushed a commit to arstercz/telegraf that referenced this pull request Mar 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants