Skip to content

Commit

Permalink
write_stdout: add tabulated data format
Browse files Browse the repository at this point in the history
Add a new write_stdout format called "fields". If specified, write a
tabulated list of fields. It's easier to read than raw json or text
outputs.

Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
  • Loading branch information
amorenoz committed Jun 20, 2022
1 parent ddba882 commit 68d603e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ 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), fields, 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" json:"format" doc:"the format of each line: printf (default) or json"`
Format string `yaml:"format" json:"format" doc:"the format of each line: printf (default), fields, or json"`
}
11 changes: 11 additions & 0 deletions pkg/pipeline/write/write_stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package write
import (
"encoding/json"
"fmt"
"os"
"text/tabwriter"
"time"

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

0 comments on commit 68d603e

Please sign in to comment.