String truncation#135
String truncation#135gregkalapos merged 12 commits intoelastic:masterfrom gregkalapos:StringTruncation
Conversation
| get => _name; | ||
| set | ||
| { | ||
| if (value.Length > Consts.PropertyMaxLength) |
There was a problem hiding this comment.
We don't truncate here anymore. This happens before serialization.
| agent.Tracer.CaptureTransaction("TestTransaction", "Test", (t) => { t.CaptureSpan(spanName.ToString(), "test", () => { }); }); | ||
|
|
||
| Assert.NotNull(payloadSender.FirstSpan); | ||
| Assert.Equal(1024, payloadSender.FirstSpan.Name.Length); |
There was a problem hiding this comment.
This isn't valid anymore. We can think about testing more in PayloadSender, but atm if we go though MockPayloadSender the trimming logic is not triggered. With Intake V2 there'll be a new PayloadSender, so I'd rather not focus on this now... we have good coverage by the other new tests I added.
There was a problem hiding this comment.
++ MockPayloadSender is only used to assert messages get send not necessarily how.
More isolated serialization tests would be helpful
|
HI @gregkalapos , |
By default we truncate everything. I basically have an inverse logic here: by default we truncate, and if we don't want to truncate something we add I added the attribute to (I think) all fields where it's needed. It'd be nice to see #134 merged (since that moves some types that are effected here), and then I'd make |
| { | ||
| internal class Stacktrace | ||
| { | ||
| [NoTruncationInJsonNet] |
There was a problem hiding this comment.
Would it be cleaner to declare the reverse? [Truncate(int chars)] instead?
There was a problem hiding this comment.
I just spotted @skynet2 comment. I tend to agree, a default truncate feels implicit rather than explicit.
There was a problem hiding this comment.
Ok, since my logic seems to be strange for basically everyone, we go with the reverse.
Just for reference: the main reason for trimming by default was this:
If we trim by default and we forget to add [DontTruncate] -> We trim the string, but the first 1024 chars and everything else is visible.
If we don't trim and forget to add [Truncate] -> The server rejects the request (the whole transaction or span), nothing is visible.
Of course we should not forget to add attributes in the first place :)
Anyway, I accept that trimming by default would be strange. Reversed the logic.
| /// Automatically applies <see cref="StringValueProvider"/> to <see cref="String"/> properties, and <see cref="StringDictionaryValueProvider"/> | ||
| /// to <see cref="Dictionary{String, String}"/> unless they are marked with <see cref="NoTruncationInJsonNetAttribute"/>. | ||
| /// </summary> | ||
| internal class StringTruncationValueResolver : CamelCasePropertyNamesContractResolver |
There was a problem hiding this comment.
Could potentially just be two JsonConverter e.g TrimmedStringJsonConverter and TagsJsonConverter (for the Dictionary<string, string> case handled here as well.
This would eschew the custom contract resolver and value providers.
There was a problem hiding this comment.
👍 Agreed, it makes things easier. Added the two converters, removed the contract resolver and the value provider.
Removed NoTruncationInJsonNetAttribute, by default we do not trancate strings.
|
@Mpdreamz thanks for the feedback! I pushed another version. I created the 2 converters, and I simply attach those to the properties that we want to trim. I think this is the easiest and simplest solution. Another option would be to activate those globally through ....will rebase this PR soon.... |
Mpdreamz
left a comment
There was a problem hiding this comment.
I left a super small suggestion, nothing worth blocking merging over 😄
Thanks for the rewrite to converters, this will also help when we later on move to a different serializer!
Co-Authored-By: gregkalapos <gergo@kalapos.net>
…into elastic-master
Removed SpanNameLengthTest. since it's coverd in SerializationTests
Codecov Report
@@ Coverage Diff @@
## master #135 +/- ##
==========================================
+ Coverage 80.03% 83.46% +3.43%
==========================================
Files 40 40
Lines 1292 1173 -119
Branches 207 187 -20
==========================================
- Hits 1034 979 -55
+ Misses 188 122 -66
- Partials 70 72 +2
Continue to review full report at Codecov.
|
Solves #129.
This is very Json.NET specific. Maybe we move to another json serializer later, but still I think it's nice to have this now, so I'd say: let's solve it for the current serializer 🚀
After this change:
stringpropertyDictionary<string, string>[NoTruncationInJsonNet]