Skip to content

Commit 2593b9e

Browse files
committed
write_stdout: add tabulated data format
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 <[email protected]>
1 parent c418a01 commit 2593b9e

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Following is the supported API format for writing to standard output:
142142

143143
<pre>
144144
stdout:
145-
format: the format of each line: printf (default) or json
145+
format: the format of each line: printf (default), fields or json
146146
</pre>
147147
## Aggregate metrics API
148148
Following is the supported API format for specifying metrics aggregations:

pkg/api/write_stdout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package api
22

33
type WriteStdout struct {
4-
Format string `yaml:"format,omitempty" json:"format,omitempty" doc:"the format of each line: printf (default) or json"`
4+
Format string `yaml:"format,omitempty" json:"format,omitempty" doc:"the format of each line: printf (default), fields or json"`
55
}

pkg/pipeline/write/write_stdout.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ package write
2020
import (
2121
"encoding/json"
2222
"fmt"
23+
"os"
24+
"sort"
25+
"text/tabwriter"
2326
"time"
2427

2528
"github.com/netobserv/flowlogs-pipeline/pkg/config"
@@ -39,6 +42,20 @@ func (t *writeStdout) Write(in []config.GenericMap) {
3942
txt, _ := json.Marshal(v)
4043
fmt.Println(string(txt))
4144
}
45+
} else if t.format == "fields" {
46+
for _, v := range in {
47+
var order sort.StringSlice
48+
for fieldName := range v {
49+
order = append(order, fieldName)
50+
}
51+
order.Sort()
52+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
53+
fmt.Fprintf(w, "\n\nFlow record at %s:\n", time.Now().Format(time.StampMilli))
54+
for _, field := range order {
55+
fmt.Fprintf(w, "%v\t=\t%v\n", field, v[field])
56+
}
57+
w.Flush()
58+
}
4259
} else {
4360
for _, v := range in {
4461
fmt.Printf("%s: %v\n", time.Now().Format(time.StampMilli), v)

0 commit comments

Comments
 (0)