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

Conversation

adityacs
Copy link
Collaborator

@adityacs adityacs commented Jul 2, 2020

What this PR does / why we need it:
Fixes fluent-bit newline and tab characters

Which issue(s) this PR fixes:
Fixes #2246

Comment on lines 236 to 240
func replaceNewLineAndTab(line string) string{
replacer := strings.NewReplacer(`\n`, "\n", `\t`, "\t")
return replacer.Replace(line)
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a hacky solution. Somewhere the encoding by Json and logfmt is treating \n and \t as escaped. So, we have to replace those with actual \n and \t.

Any better ways?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well I guess the replacer can be global for sure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And so you probably don't need this function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah looks like a fine solution to me. Basically unescaping those characters.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried to see what fluentd send for those ? I'm curious to see if they do the same.

Copy link
Collaborator Author

@adityacs adityacs Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fluent-bit records are fine. I just checked the content before Marshaling the record here

js, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(records)

\n and \t looks fine here.

But after the marshaling, result from jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(records) doesn't respect \n and \t. Looks like Marshaling is escaping these characters.

Same applies to logfmt encoding as well

err := enc.EncodeKeyval(k, records[k])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is correct everything is enclosed by "" and \ must be escaped. May be this is more a Grafana issue ? when reading \\n it should read\n ? cc @aocenas WDYT ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this can be handled from Grafana side as well. However, line would be this Hello\nworld and not Hello\\nworld. So, on Grafana they can replace \n with actual newline.

@adityacs adityacs changed the title Fix fluent-bit newline and tab characters encoding Fix fluent-bit newline and tab characters Jul 2, 2020
@codecov-commenter
Copy link

codecov-commenter commented Jul 2, 2020

Codecov Report

Merging #2286 into master will decrease coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2286      +/-   ##
==========================================
- Coverage   62.45%   62.45%   -0.01%     
==========================================
  Files         158      158              
  Lines       12762    12761       -1     
==========================================
- Hits         7971     7970       -1     
  Misses       4177     4177              
  Partials      614      614              
Impacted Files Coverage Δ
cmd/fluent-bit/loki.go 82.63% <100.00%> (-0.12%) ⬇️

@@ -115,12 +118,13 @@ 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("/", "_", ".", "_", "-", "_")
keyReplacer := strings.NewReplacer("/", "_", ".", "_", "-", "_")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that could be global too while we are at it.

Copy link
Collaborator Author

@adityacs adityacs Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah initially made that global but we will get this error https://cloud.drone.io/grafana/loki/3437/1/2.
If the line contains . or / that would be replaced as well. So, just kept it local

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what you mean moving this to a new global variable, should be fine ?

Copy link
Collaborator Author

@adityacs adityacs Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example if the log message has 12.2 foo bar as value, doing replace with this global replacer

strings.NewReplacer("/", "_", ".", "_", "-", "_", `\n`,, "\n", `\t`, "\t")

will make the log message as 12_2 foo bar which is not desired.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is not what I was really asking to use a single replace, but two globals, one for key one for line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean we can have 2 separate global variables?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes !

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

@cyriltovena cyriltovena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@RomanBadiornyi
Copy link

Hi guys, we're running into the same issue. Can you please help to understand at what version this fix included? We use grafana 8.3.3, loki 2.3.0 and fluent bit 1.8.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fluent-bit plugin encoded characters
4 participants