Skip to content

Commit

Permalink
Fix fluent-bit \n and \t characters (#2286)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityacs authored Jul 2, 2020
1 parent c54be42 commit 3845204
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions cmd/fluent-bit/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import (
"github.com/grafana/loki/pkg/promtail/client"
)

var (
lineReplacer = strings.NewReplacer(`\n`, "\n", `\t`, "\t")
keyReplacer = strings.NewReplacer("/", "_", ".", "_", "-", "_")
)

type loki struct {
cfg *config
client client.Client
Expand Down Expand Up @@ -65,6 +70,7 @@ func (l *loki) sendRecord(r map[interface{}]interface{}, ts time.Time) error {
if err != nil {
return fmt.Errorf("error creating line: %v", err)
}

return l.client.Handle(lbs, ts, line)
}

Expand Down Expand Up @@ -115,12 +121,11 @@ func autoLabels(records map[string]interface{}, kuberneteslbs model.LabelSet) er
return errors.New("kubernetes labels not found, no labels will be added")
}

replacer := strings.NewReplacer("/", "_", ".", "_", "-", "_")
for k, v := range kube.(map[string]interface{}) {
switch k {
case "labels":
for m, n := range v.(map[string]interface{}) {
kuberneteslbs[model.LabelName(replacer.Replace(m))] = model.LabelValue(fmt.Sprintf("%v", n))
kuberneteslbs[model.LabelName(keyReplacer.Replace(m))] = model.LabelValue(fmt.Sprintf("%v", n))
}
case "docker_id", "pod_id", "annotations":
// do nothing
Expand Down Expand Up @@ -204,7 +209,7 @@ func createLine(records map[string]interface{}, f format) (string, error) {
if err != nil {
return "", err
}
return string(js), nil
return lineReplacer.Replace(string(js)), nil
case kvPairFormat:
buf := &bytes.Buffer{}
enc := logfmt.NewEncoder(buf)
Expand All @@ -226,7 +231,7 @@ func createLine(records map[string]interface{}, f format) (string, error) {
return "", nil
}
}
return buf.String(), nil
return lineReplacer.Replace(buf.String()), nil
default:
return "", fmt.Errorf("invalid line format: %v", f)
}
Expand Down

0 comments on commit 3845204

Please sign in to comment.