1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-30 21:20:04 +02:00

Avoid replacing existing correlation map data in context when correlation context extractor does not find any valid data (#923)

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
Anthony Mirabella 2020-07-09 15:02:49 -04:00 committed by GitHub
parent fab431e455
commit c719588733
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 7 deletions

View File

@ -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

View File

@ -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, ",")
@ -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.

View File

@ -123,11 +123,28 @@ 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 {
@ -135,12 +152,26 @@ func TestExtractInvalidDistributedContextFromHTTPReq(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
})
})
}
}