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(tracestate): drop right-most member in tracestate #2592

Merged
merged 11 commits into from Feb 23, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Changed

- Jaeger exporter takes into additional 70 bytes overhead into consideration when sending UDP packets (#2489, #2512)
- For tracestate's members, prepend the new element and remove the oldest one, which is over capacity (#2592)
1046102779 marked this conversation as resolved.
Show resolved Hide resolved
- Zipkin exporter exports `Resource` attributes as the `Tags` field. (#2589)

### Deprecated
Expand Down
13 changes: 3 additions & 10 deletions trace/tracestate.go
Expand Up @@ -179,17 +179,10 @@ func (ts TraceState) Insert(key, value string) (TraceState, error) {
}
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

cTS := ts.Delete(key)
if cTS.Len()+1 > maxListMembers {
// TODO (MrAlias): When the second version of the Trace Context
// specification is published this needs to not return an error.
// Instead it should drop the "right-most" member and insert the new
// member at the front.
//
// https://github.com/w3c/trace-context/pull/448
return ts, fmt.Errorf("failed to insert: %w", errMemberNumber)
if cTS.Len()+1 <= maxListMembers {
cTS.list = append(cTS.list, member{})
}

cTS.list = append(cTS.list, member{})
// When the number of members exceeds capacity, drop the "right-most".
copy(cTS.list[1:], cTS.list)
cTS.list[0] = m

Expand Down
16 changes: 11 additions & 5 deletions trace/tracestate_test.go
Expand Up @@ -482,14 +482,20 @@ func TestTraceStateInsert(t *testing.T) {
err: errInvalidKey,
},
{
name: "too many entries",
name: "drop the right-most member(oldest) in queue",
tracestate: maxMembers,
key: "keyx",
value: "valx",
expected: maxMembers,
err: errMemberNumber,
},
}
expected: func() TraceState {
1046102779 marked this conversation as resolved.
Show resolved Hide resolved
// Prepend the new element and remove the oldest one, which is over capacity.
return TraceState{
list: append(
[]member{{Key: "keyx", Value: "valx"}},
maxMembers.list[:len(maxMembers.list)-1]...,
),
}
}(),
}}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down