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

Add tabulated output to write_stdout #231

Merged
merged 3 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ Following is the supported API format for writing to standard output:

<pre>
stdout:
format: the format of each line: printf (default) or json
format: the format of each line: printf (default - writes using golang's default map printing), fields (writes one key and value field per line) or json

</pre>
## Aggregate metrics API
Following is the supported API format for specifying metrics aggregations:
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/write_stdout.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package api

type WriteStdout struct {
Format string `yaml:"format,omitempty" json:"format,omitempty" doc:"the format of each line: printf (default) or json"`
Format string `yaml:"format,omitempty" json:"format,omitempty" doc:"the format of each line: printf (default - writes using golang's default map printing), fields (writes one key and value field per line) or json"`
}
17 changes: 17 additions & 0 deletions pkg/pipeline/write/write_stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ package write
import (
"encoding/json"
"fmt"
"os"
"sort"
"text/tabwriter"
"time"

"github.com/netobserv/flowlogs-pipeline/pkg/config"
Expand All @@ -39,6 +42,20 @@ func (t *writeStdout) Write(in []config.GenericMap) {
txt, _ := json.Marshal(v)
fmt.Println(string(txt))
}
} else if t.format == "fields" {
for _, v := range in {
var order sort.StringSlice
for fieldName := range v {
order = append(order, fieldName)
}
order.Sort()
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
fmt.Fprintf(w, "\n\nFlow record at %s:\n", time.Now().Format(time.StampMilli))
for _, field := range order {
fmt.Fprintf(w, "%v\t=\t%v\n", field, v[field])
}
w.Flush()
}
} else {
for _, v := range in {
fmt.Printf("%s: %v\n", time.Now().Format(time.StampMilli), v)
Expand Down