-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(engine): add JSON as output format for --list/--list_events #3803
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
poiana
merged 3 commits into
falcosecurity:master
from
legobrick:json_fields_events_output
Mar 17, 2026
Merged
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| /* | ||
| Copyright (C) 2026 The Falco Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| #include "field_formatter.h" | ||
| #include "formats.h" | ||
|
|
||
| using namespace falco; | ||
|
|
||
| // Factory method | ||
| std::unique_ptr<FieldFormatter> FieldFormatter::create(output_format format, bool verbose) { | ||
| switch(format) { | ||
| case output_format::JSON: | ||
| return std::make_unique<JsonFieldFormatter>(verbose); | ||
| case output_format::MARKDOWN: | ||
| return std::make_unique<MarkdownFieldFormatter>(verbose); | ||
| case output_format::TEXT: | ||
| default: | ||
| return std::make_unique<TextFieldFormatter>(verbose); | ||
| } | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // TextFieldFormatter implementation | ||
| // ============================================================================ | ||
|
|
||
| TextFieldFormatter::TextFieldFormatter(bool verbose): m_verbose(verbose) {} | ||
|
|
||
| void TextFieldFormatter::begin() { | ||
| // Nothing to do for text format | ||
| } | ||
|
|
||
| void TextFieldFormatter::print_fieldclass( | ||
| const sinsp_filter_factory::filter_fieldclass_info& fld_class, | ||
| const std::set<std::string>& event_sources) { | ||
| printf("%s\n", fld_class.as_string(m_verbose, event_sources).c_str()); | ||
| } | ||
|
|
||
| void TextFieldFormatter::print_field_name(const std::string& name) { | ||
| printf("%s\n", name.c_str()); | ||
| } | ||
|
|
||
| void TextFieldFormatter::end() { | ||
| // Nothing to do for text format | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // MarkdownFieldFormatter implementation | ||
| // ============================================================================ | ||
|
|
||
| MarkdownFieldFormatter::MarkdownFieldFormatter(bool verbose): m_verbose(verbose) {} | ||
|
|
||
| void MarkdownFieldFormatter::begin() { | ||
| // Nothing to do for markdown format | ||
| } | ||
|
|
||
| void MarkdownFieldFormatter::print_fieldclass( | ||
| const sinsp_filter_factory::filter_fieldclass_info& fld_class, | ||
| const std::set<std::string>& event_sources) { | ||
| printf("%s\n", fld_class.as_markdown(event_sources).c_str()); | ||
| } | ||
|
|
||
| void MarkdownFieldFormatter::print_field_name(const std::string& name) { | ||
| printf("%s\n", name.c_str()); | ||
| } | ||
|
|
||
| void MarkdownFieldFormatter::end() { | ||
| // Nothing to do for markdown format | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // JsonFieldFormatter implementation | ||
| // ============================================================================ | ||
|
|
||
| JsonFieldFormatter::JsonFieldFormatter(bool verbose): m_verbose(verbose) {} | ||
|
|
||
| void JsonFieldFormatter::begin() { | ||
| m_fieldclasses_array = nlohmann::json::array(); | ||
| m_fieldnames_array = nlohmann::json::array(); | ||
| m_has_fieldclasses = false; | ||
| m_has_fieldnames = false; | ||
| } | ||
|
|
||
| void JsonFieldFormatter::print_fieldclass( | ||
| const sinsp_filter_factory::filter_fieldclass_info& fld_class, | ||
| const std::set<std::string>& event_sources) { | ||
| std::string json_str = fld_class.as_json(event_sources); | ||
| if(!json_str.empty()) { | ||
| m_fieldclasses_array.push_back(nlohmann::json::parse(json_str)); | ||
| m_has_fieldclasses = true; | ||
| } | ||
| } | ||
|
|
||
| void JsonFieldFormatter::print_field_name(const std::string& name) { | ||
| m_fieldnames_array.push_back(name); | ||
| m_has_fieldnames = true; | ||
| } | ||
|
|
||
| void JsonFieldFormatter::end() { | ||
| nlohmann::json root; | ||
|
|
||
| if(m_has_fieldclasses) { | ||
| root["fieldclasses"] = m_fieldclasses_array; | ||
| printf("%s\n", root.dump(2).c_str()); | ||
| } else if(m_has_fieldnames) { | ||
| root["fieldnames"] = m_fieldnames_array; | ||
| printf("%s\n", root.dump(2).c_str()); | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| /* | ||
| Copyright (C) 2026 The Falco Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string> | ||
| #include <set> | ||
| #include <memory> | ||
| #include <nlohmann/json.hpp> | ||
|
|
||
| #include <libsinsp/sinsp.h> | ||
|
|
||
| enum class output_format; | ||
|
|
||
| namespace falco { | ||
|
|
||
| // Abstract formatter interface for field listing | ||
| class FieldFormatter { | ||
| public: | ||
| virtual ~FieldFormatter() = default; | ||
|
|
||
| // Initialize formatter | ||
| virtual void begin() = 0; | ||
|
|
||
| // Print a field class with its event sources | ||
| virtual void print_fieldclass(const sinsp_filter_factory::filter_fieldclass_info& fld_class, | ||
| const std::set<std::string>& event_sources) = 0; | ||
|
|
||
| // Print a single field name (for names_only mode) | ||
| virtual void print_field_name(const std::string& name) = 0; | ||
|
|
||
| // Finalize and output | ||
| virtual void end() = 0; | ||
|
|
||
| // Factory method | ||
| static std::unique_ptr<FieldFormatter> create(output_format format, bool verbose); | ||
| }; | ||
|
|
||
| // Text formatter (default) | ||
| class TextFieldFormatter : public FieldFormatter { | ||
| public: | ||
| explicit TextFieldFormatter(bool verbose); | ||
|
|
||
| void begin() override; | ||
| void print_fieldclass(const sinsp_filter_factory::filter_fieldclass_info& fld_class, | ||
| const std::set<std::string>& event_sources) override; | ||
| void print_field_name(const std::string& name) override; | ||
| void end() override; | ||
|
|
||
| private: | ||
| bool m_verbose; | ||
| }; | ||
|
|
||
| // Markdown formatter | ||
| class MarkdownFieldFormatter : public FieldFormatter { | ||
| public: | ||
| explicit MarkdownFieldFormatter(bool verbose); | ||
|
|
||
| void begin() override; | ||
| void print_fieldclass(const sinsp_filter_factory::filter_fieldclass_info& fld_class, | ||
| const std::set<std::string>& event_sources) override; | ||
| void print_field_name(const std::string& name) override; | ||
| void end() override; | ||
|
|
||
| private: | ||
| bool m_verbose; | ||
| }; | ||
|
|
||
| // JSON formatter | ||
| class JsonFieldFormatter : public FieldFormatter { | ||
| public: | ||
| explicit JsonFieldFormatter(bool verbose); | ||
|
|
||
| void begin() override; | ||
| void print_fieldclass(const sinsp_filter_factory::filter_fieldclass_info& fld_class, | ||
| const std::set<std::string>& event_sources) override; | ||
| void print_field_name(const std::string& name) override; | ||
| void end() override; | ||
|
|
||
| private: | ||
| bool m_verbose; | ||
| nlohmann::json m_fieldclasses_array; | ||
| nlohmann::json m_fieldnames_array; | ||
| bool m_has_fieldclasses{false}; | ||
| bool m_has_fieldnames{false}; | ||
| }; | ||
|
|
||
| } // namespace falco |
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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| /* | ||
| Copyright (C) 2026 The Falco Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| enum class output_format { TEXT, MARKDOWN, JSON }; | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.