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

ddtracer/tracer: marshalled span values in encode span #832

Merged
merged 7 commits into from
Feb 25, 2021

Conversation

dianashevchenko
Copy link
Contributor

Illegal characters, such as newlines, were not escaped in encodeSpan function, which ruined json structure. Closes #791

This, in turn, corrupted log collection

Illegal characters, such as newlines, were not escaped in encodeSpan function, which ruined json structure. Closes #791
ddtrace/tracer/writer.go Outdated Show resolved Hide resolved
ddtrace/tracer/writer_test.go Outdated Show resolved Hide resolved
ddtrace/tracer/writer_test.go Outdated Show resolved Hide resolved
ddtrace/tracer/writer.go Outdated Show resolved Hide resolved
Added same marshalling to encoding metrics keys. Also, added error logging to both metrics and meta marshalling
@dianashevchenko dianashevchenko added this to the 1.29.0 milestone Feb 23, 2021
ddtrace/tracer/writer.go Outdated Show resolved Hide resolved
ddtrace/tracer/writer.go Outdated Show resolved Hide resolved
ddtrace/tracer/writer.go Outdated Show resolved Hide resolved
ddtrace/tracer/writer.go Outdated Show resolved Hide resolved
ddtrace/tracer/writer_test.go Outdated Show resolved Hide resolved
ddtrace/tracer/writer_test.go Outdated Show resolved Hide resolved
@jamestelfer
Copy link

jamestelfer commented Feb 24, 2021

I'm interested in this fix since it's affecting me too: I appreciate that it's being worked on!

Should this fix be widened such that the same technique is used in all cases where WriteString is being called with input that is not a string literal? json.Marshal will also deal properly with embedded quotes and other control characters that would otherwise render invalid JSON.

The spec has details of the cases to cover, and the Marshal docs have further information.

Lastly, should this use an explicit Encoder and opt out of HTML escaping with SetHTMLEscape(false)? Since this output isn't destined for an HTML context, this would slightly decrease the encode/decode time for the same data. I'm assuming (without any background in this codebase) that performance was one of the reasons that this didn't use a JSON encoder originally.

@gbbr
Copy link
Contributor

gbbr commented Feb 24, 2021

@jamestelfer you're right. We should use this everywhere where we write strings that are user input and aren't numeric (which get converted easily), such as s.Name, s.Resource and s.Service (I think that's it). Also agree with the HTML escaping... No need for it.

It'll likely help to create a helper function marshalString and use that where it applies.

@jamestelfer
Copy link

@gbbr / @dianashevchenko LMK if you'd like me to pitch in here! I'm currently blocked on this and would be happy to.

@gbbr
Copy link
Contributor

gbbr commented Feb 24, 2021

@jamestelfer all comments welcome and help is much appreciated!

Service, resource, and operation names were too marshalled to make sure they don't break JSON with invalid characters such as newlines
ddtrace/tracer/writer.go Outdated Show resolved Hide resolved
ddtrace/tracer/writer.go Outdated Show resolved Hide resolved
ddtrace/tracer/writer.go Outdated Show resolved Hide resolved
ddtrace/tracer/writer.go Outdated Show resolved Hide resolved
@jamestelfer
Copy link

This is looking great, thanks @dianashevchenko! I'm going to pull this branch and test it against the service I have that's failing.

@jamestelfer
Copy link

I forked this branch and merged v1, then pulled that version into my service using go mod edit -replace. I could see the trace written to a single line in CloudWatch, and the error trace appeared correctly in the Datadog UI. I'll look to pull the official version in as soon as it's released.

Looks good to me, and thanks again!

gbbr
gbbr previously approved these changes Feb 25, 2021
Copy link
Contributor

@gbbr gbbr left a comment

Choose a reason for hiding this comment

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

LGTM!

ddtrace/tracer/writer_test.go Outdated Show resolved Hide resolved
@gbbr gbbr merged commit a0e288c into v1 Feb 25, 2021
@gbbr gbbr deleted the shevchenko/sanitize-span-values branch February 25, 2021 11:07
@gbbr
Copy link
Contributor

gbbr commented Feb 25, 2021

@jamestelfer thanks for confirming that this fixes your issue.

This was referenced Mar 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ddtrace/tracer/writer: metadata doesn't support newlines
3 participants