Skip to content

Commit

Permalink
add kube-json parser
Browse files Browse the repository at this point in the history
  • Loading branch information
bentol committed Sep 1, 2022
1 parent 6935c67 commit a39a049
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ func processSource(nsCfg config.NamespaceConfig, t tail.Follower, parser parser.
}

fields, err := parser.ParseString(line)
fmt.Println(fields)
if err != nil {
fmt.Printf("error while parsing line '%s': %s\n", line, err)
metrics.parseErrorsTotal.Inc()
Expand Down
24 changes: 21 additions & 3 deletions parser/jsonparser/jsonparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,39 @@ package jsonparser
import (
"encoding/json"
"fmt"
"strings"

"github.com/tidwall/gjson"
)

// JsonParser parse a JSON string.
type JsonParser struct{}
type JsonParser struct {
fnGetActualLog func(string) string
}

// NewJsonParser returns a new json parser.
func NewJsonParser() *JsonParser {
return &JsonParser{}
}

func NewKubeJsonParser() *JsonParser {
return &JsonParser{
fnGetActualLog: func(line string) string {
// the actual k8s log is wrapped in log field
line = gjson.Get(line, "log").String()
return strings.TrimSpace(line)
},
}
}

// ParseString implements the Parser interface.
// The value in the map is not necessarily a string, so it needs to be converted.
func (j *JsonParser) ParseString(line string) (map[string]string, error) {
var parsed map[string]interface{}
err := json.Unmarshal([]byte(line), &parsed)
actualLogLine := line
if j.fnGetActualLog != nil {
actualLogLine = j.fnGetActualLog(line)
}
err := json.Unmarshal([]byte(actualLogLine), &parsed)
if err != nil {
return nil, fmt.Errorf("json log parsing err: %w", err)
}
Expand Down
2 changes: 2 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func NewParser(nsCfg config.NamespaceConfig) Parser {
return kubeparser.NewKubeParser(nsCfg.Format)
case "kube-cri":
return kubeparser.NewKubeCRIParser(nsCfg.Format)
case "kube-json":
return jsonparser.NewKubeJsonParser()
case "istio":
return istioparser.NewIstioParser()
case "istio-cri":
Expand Down

0 comments on commit a39a049

Please sign in to comment.