Skip to content

Commit

Permalink
Avoid replacing existing correlation map data in context when correla…
Browse files Browse the repository at this point in the history
…tion context extractor does not find any valid data (#923)

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
Aneurysm9 and MrAlias authored Jul 9, 2020
1 parent fab431e commit c719588
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add test for api.standard `HTTPClientAttributesFromHTTPRequest`. (#905)
- Bump github.com/golangci/golangci-lint from 1.27.0 to 1.28.1 in /tools. (#901, #913)
- Update otel-colector example to use the v0.5.0 collector. (#915)
- Correlation Context extractor will no longer insert an empty map into the returned context when no valid values are extracted. (#923)

## [0.7.0] - 2020-06-26

Expand Down
14 changes: 10 additions & 4 deletions api/correlation/correlation_context_propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (CorrelationContext) Inject(ctx context.Context, supplier propagation.HTTPS
func (CorrelationContext) Extract(ctx context.Context, supplier propagation.HTTPSupplier) context.Context {
correlationContext := supplier.Get(correlationContextHeader)
if correlationContext == "" {
return ContextWithMap(ctx, NewEmptyMap())
return ctx
}

contextValues := strings.Split(correlationContext, ",")
Expand Down Expand Up @@ -101,9 +101,15 @@ func (CorrelationContext) Extract(ctx context.Context, supplier propagation.HTTP

keyValues = append(keyValues, kv.Key(trimmedName).String(trimmedValueWithProps.String()))
}
return ContextWithMap(ctx, NewMap(MapUpdate{
MultiKV: keyValues,
}))

if len(keyValues) > 0 {
// Only update the context if valid values were found
return ContextWithMap(ctx, NewMap(MapUpdate{
MultiKV: keyValues,
}))
}

return ctx
}

// GetAllKeys implements HTTPPropagator.
Expand Down
37 changes: 34 additions & 3 deletions api/correlation/correlation_context_propagator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,24 +123,55 @@ func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) {
tests := []struct {
name string
header string
hasKVs []kv.KeyValue
}{
{
name: "no key values",
header: "header1",
},
{
name: "invalid header with existing context",
header: "header2",
hasKVs: []kv.KeyValue{
kv.Key("key1").String("val1"),
kv.Key("key2").String("val2"),
},
},
{
name: "empty header value",
header: "",
hasKVs: []kv.KeyValue{
kv.Key("key1").String("val1"),
kv.Key("key2").String("val2"),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
req.Header.Set("otcorrelations", tt.header)

ctx := context.Background()
ctx := correlation.NewContext(context.Background(), tt.hasKVs...)
wantCorCtx := correlation.MapFromContext(ctx)
ctx = propagation.ExtractHTTP(ctx, props, req.Header)
gotCorCtx := correlation.MapFromContext(ctx)
if gotCorCtx.Len() != 0 {
t.Errorf("Got and Want CorCtx are not the same size %d != %d", gotCorCtx.Len(), 0)
if gotCorCtx.Len() != wantCorCtx.Len() {
t.Errorf(
"Got and Want CorCtx are not the same size %d != %d",
gotCorCtx.Len(),
wantCorCtx.Len(),
)
}
totalDiff := ""
wantCorCtx.Foreach(func(keyValue kv.KeyValue) bool {
val, _ := gotCorCtx.Value(keyValue.Key)
diff := cmp.Diff(keyValue, kv.KeyValue{Key: keyValue.Key, Value: val}, cmp.AllowUnexported(value.Value{}))
if diff != "" {
totalDiff += diff + "\n"
}
return true
})
})
}
}
Expand Down

0 comments on commit c719588

Please sign in to comment.