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

[Filebeat] add support for ndjson results to httpjson input #23428

Closed
wants to merge 4 commits into from

Conversation

leehinman
Copy link
Contributor

@leehinman leehinman commented Jan 11, 2021

What does this PR do?

adds support for ndjson results to httpjson input.

  • if response Content-Type is "application/x-ndjson", collect the json
    objects in an array and assign array to key "ndjson-results" in a new
    json object
  • if config response.force_ndjson is set, set response Content-Type
    header to "application/x-ndjson"

Why is it important?

Some HTTP API endpoints return results as ndjson not json

Checklist

  • My code follows the style guidelines of this project
    - [ ] I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
    - [ ] I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

@botelastic botelastic bot added the needs_team Indicates that the issue/PR needs a Team:* label label Jan 11, 2021
@leehinman leehinman added enhancement Filebeat Filebeat needs_backport PR is waiting to be backported to other branches. Team:Security-External Integrations labels Jan 11, 2021
@botelastic botelastic bot removed the needs_team Indicates that the issue/PR needs a Team:* label label Jan 11, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/security-external-integrations (Team:Security-External Integrations)

@elasticmachine
Copy link
Collaborator

elasticmachine commented Jan 11, 2021

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview

Expand to view the summary

Build stats

  • Build Cause: Pull request #23428 updated

  • Start Time: 2021-01-12T21:11:37.958+0000

  • Duration: 49 min 57 sec

Test stats 🧪

Test Results
Failed 0
Passed 5129
Skipped 574
Total 5703

💚 Flaky test report

Tests succeeded.

Expand to view the summary

Test stats 🧪

Test Results
Failed 0
Passed 5129
Skipped 574
Total 5703

- if response Content-Type is "application/x-ndjson", collect the json
  objects in an array and assign array to key "ndjson-results" in a new
  json object
- if config response.force_ndjson is set, set response Content-Type
  header to "application/x-ndjson"
Copy link
Contributor

@marc-gr marc-gr left a comment

Choose a reason for hiding this comment

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

Really nice improvement!

Beside the couple of comments, would be nice to add some test for ndjson if possible.

x-pack/filebeat/input/httpjson/internal/v2/pagination.go Outdated Show resolved Hide resolved
[float]
==== `response.force_ndjson`

If the Content-Type header is set to "application/x-ndjson" then the ndjson results will be available in a new array called "ndjson-results".
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add a comment about supporting ndjson at the top of the docs instead for visibility.

@@ -80,6 +83,10 @@ func (rp *responseProcessor) startProcessing(stdCtx context.Context, trCtx *tran
go func() {
defer close(ch)

if rp.forceNdjson {
resp.Header.Set("Content-Type", "application/x-ndjson")
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this just for testing purposes? If not, maybe there are other use cases for modifying response headers, and enabling writing to them in transform_xxx.go is a more general approach without adding specific config fields. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately not just for testing, some endpoints send back ndjson with content types of text/plain or application/json. So we need some way to force ndjson code path. That being said I like the idea of the general approach, let me give that a try.

Copy link
Member

Choose a reason for hiding this comment

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

After reading "if config response.force_ndjson is set, set response Content-Type
header to "application/x-ndjson" in the description, my first thought was that this sounded fairly specific. If we have several builtin handlers for different content types then maybe a generic overwrite header option would work and make future extension easier.

leehinman and others added 2 commits January 12, 2021 09:17
replace ndjson-results with root level array

Co-authored-by: Marc Guasch <marc-gr@users.noreply.github.com>
- use json.Decoder instead of json.UnMarshall
- if only 1 json object is present it becomes the root
- if more than on json object is present a root level array is
  returned
@marc-gr
Copy link
Contributor

marc-gr commented Jan 13, 2021

I opened #23478, if it looks good to you, maybe we can merge it and then you just need to create a new ndjson decoder and use the encode_as option.

@leehinman
Copy link
Contributor Author

yeah, I like the encode_as & decode_as stuff from #23478 better. Cunnighams Law at work. Closing this one.

@leehinman leehinman closed this Jan 13, 2021
@leehinman leehinman deleted the httpjson_ndjson branch May 14, 2021 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Filebeat Filebeat needs_backport PR is waiting to be backported to other branches.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants