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

Fix fluent-bit newline and tab characters #2286

Merged
merged 1 commit into from
Jul 2, 2020
Merged
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
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