2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-11-13 17:34:24 +02:00
|
|
|
package propagation_test
|
2020-02-20 20:31:21 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2020-10-05 17:25:09 +02:00
|
|
|
"go.opentelemetry.io/otel/internal/baggage"
|
2020-08-18 05:25:03 +02:00
|
|
|
"go.opentelemetry.io/otel/label"
|
2020-11-13 17:34:24 +02:00
|
|
|
"go.opentelemetry.io/otel/propagation"
|
2020-02-20 20:31:21 +02:00
|
|
|
)
|
|
|
|
|
2020-09-09 20:13:37 +02:00
|
|
|
func TestExtractValidBaggageFromHTTPReq(t *testing.T) {
|
2020-11-13 17:34:24 +02:00
|
|
|
prop := propagation.TextMapPropagator(propagation.Baggage{})
|
2020-02-20 20:31:21 +02:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
header string
|
2020-08-18 05:25:03 +02:00
|
|
|
wantKVs []label.KeyValue
|
2020-02-20 20:31:21 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid w3cHeader",
|
|
|
|
header: "key1=val1,key2=val2",
|
2020-08-18 05:25:03 +02:00
|
|
|
wantKVs: []label.KeyValue{
|
|
|
|
label.String("key1", "val1"),
|
|
|
|
label.String("key2", "val2"),
|
2020-02-20 20:31:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid w3cHeader with spaces",
|
|
|
|
header: "key1 = val1, key2 =val2 ",
|
2020-08-18 05:25:03 +02:00
|
|
|
wantKVs: []label.KeyValue{
|
|
|
|
label.String("key1", "val1"),
|
|
|
|
label.String("key2", "val2"),
|
2020-02-20 20:31:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid w3cHeader with properties",
|
|
|
|
header: "key1=val1,key2=val2;prop=1",
|
2020-08-18 05:25:03 +02:00
|
|
|
wantKVs: []label.KeyValue{
|
|
|
|
label.String("key1", "val1"),
|
|
|
|
label.String("key2", "val2;prop=1"),
|
2020-02-20 20:31:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid header with url-escaped comma",
|
|
|
|
header: "key1=val1,key2=val2%2Cval3",
|
2020-08-18 05:25:03 +02:00
|
|
|
wantKVs: []label.KeyValue{
|
|
|
|
label.String("key1", "val1"),
|
|
|
|
label.String("key2", "val2,val3"),
|
2020-02-20 20:31:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid header with an invalid header",
|
|
|
|
header: "key1=val1,key2=val2,a,val3",
|
2020-08-18 05:25:03 +02:00
|
|
|
wantKVs: []label.KeyValue{
|
|
|
|
label.String("key1", "val1"),
|
|
|
|
label.String("key2", "val2"),
|
2020-02-20 20:31:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid header with no value",
|
|
|
|
header: "key1=,key2=val2",
|
2020-08-18 05:25:03 +02:00
|
|
|
wantKVs: []label.KeyValue{
|
|
|
|
label.String("key1", ""),
|
|
|
|
label.String("key2", "val2"),
|
2020-02-20 20:31:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
2020-10-20 19:51:17 +02:00
|
|
|
req.Header.Set("baggage", tt.header)
|
2020-02-20 20:31:21 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2021-02-17 18:04:49 +02:00
|
|
|
ctx = prop.Extract(ctx, propagation.HeaderCarrier(req.Header))
|
2020-09-09 20:13:37 +02:00
|
|
|
gotBaggage := baggage.MapFromContext(ctx)
|
|
|
|
wantBaggage := baggage.NewMap(baggage.MapUpdate{MultiKV: tt.wantKVs})
|
|
|
|
if gotBaggage.Len() != wantBaggage.Len() {
|
2020-02-20 20:31:21 +02:00
|
|
|
t.Errorf(
|
2020-09-09 20:13:37 +02:00
|
|
|
"Got and Want Baggage are not the same size %d != %d",
|
|
|
|
gotBaggage.Len(),
|
|
|
|
wantBaggage.Len(),
|
2020-02-20 20:31:21 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
totalDiff := ""
|
2020-09-09 20:13:37 +02:00
|
|
|
wantBaggage.Foreach(func(keyValue label.KeyValue) bool {
|
|
|
|
val, _ := gotBaggage.Value(keyValue.Key)
|
2020-08-18 05:25:03 +02:00
|
|
|
diff := cmp.Diff(keyValue, label.KeyValue{Key: keyValue.Key, Value: val}, cmp.AllowUnexported(label.Value{}))
|
2020-02-20 20:31:21 +02:00
|
|
|
if diff != "" {
|
|
|
|
totalDiff += diff + "\n"
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if totalDiff != "" {
|
|
|
|
t.Errorf("Extract Tracecontext: %s: -got +want %s", tt.name, totalDiff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) {
|
2020-11-13 17:34:24 +02:00
|
|
|
prop := propagation.TextMapPropagator(propagation.Baggage{})
|
2020-02-20 20:31:21 +02:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
header string
|
2020-08-18 05:25:03 +02:00
|
|
|
hasKVs []label.KeyValue
|
2020-02-20 20:31:21 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no key values",
|
|
|
|
header: "header1",
|
|
|
|
},
|
2020-07-09 21:02:49 +02:00
|
|
|
{
|
|
|
|
name: "invalid header with existing context",
|
|
|
|
header: "header2",
|
2020-08-18 05:25:03 +02:00
|
|
|
hasKVs: []label.KeyValue{
|
|
|
|
label.String("key1", "val1"),
|
|
|
|
label.String("key2", "val2"),
|
2020-07-09 21:02:49 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty header value",
|
|
|
|
header: "",
|
2020-08-18 05:25:03 +02:00
|
|
|
hasKVs: []label.KeyValue{
|
|
|
|
label.String("key1", "val1"),
|
|
|
|
label.String("key2", "val2"),
|
2020-07-09 21:02:49 +02:00
|
|
|
},
|
|
|
|
},
|
2020-02-20 20:31:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
2020-10-20 19:51:17 +02:00
|
|
|
req.Header.Set("baggage", tt.header)
|
2020-02-20 20:31:21 +02:00
|
|
|
|
2020-09-09 20:13:37 +02:00
|
|
|
ctx := baggage.NewContext(context.Background(), tt.hasKVs...)
|
|
|
|
wantBaggage := baggage.MapFromContext(ctx)
|
2021-02-17 18:04:49 +02:00
|
|
|
ctx = prop.Extract(ctx, propagation.HeaderCarrier(req.Header))
|
2020-09-09 20:13:37 +02:00
|
|
|
gotBaggage := baggage.MapFromContext(ctx)
|
|
|
|
if gotBaggage.Len() != wantBaggage.Len() {
|
2020-07-09 21:02:49 +02:00
|
|
|
t.Errorf(
|
2020-09-09 20:13:37 +02:00
|
|
|
"Got and Want Baggage are not the same size %d != %d",
|
|
|
|
gotBaggage.Len(),
|
|
|
|
wantBaggage.Len(),
|
2020-07-09 21:02:49 +02:00
|
|
|
)
|
2020-02-20 20:31:21 +02:00
|
|
|
}
|
2020-07-09 21:02:49 +02:00
|
|
|
totalDiff := ""
|
2020-09-09 20:13:37 +02:00
|
|
|
wantBaggage.Foreach(func(keyValue label.KeyValue) bool {
|
|
|
|
val, _ := gotBaggage.Value(keyValue.Key)
|
2020-08-18 05:25:03 +02:00
|
|
|
diff := cmp.Diff(keyValue, label.KeyValue{Key: keyValue.Key, Value: val}, cmp.AllowUnexported(label.Value{}))
|
2020-07-09 21:02:49 +02:00
|
|
|
if diff != "" {
|
|
|
|
totalDiff += diff + "\n"
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2020-02-20 20:31:21 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 20:13:37 +02:00
|
|
|
func TestInjectBaggageToHTTPReq(t *testing.T) {
|
2020-11-13 17:34:24 +02:00
|
|
|
propagator := propagation.Baggage{}
|
2020-02-20 20:31:21 +02:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2020-08-18 05:25:03 +02:00
|
|
|
kvs []label.KeyValue
|
2020-02-20 20:31:21 +02:00
|
|
|
wantInHeader []string
|
|
|
|
wantedLen int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "two simple values",
|
2020-08-18 05:25:03 +02:00
|
|
|
kvs: []label.KeyValue{
|
|
|
|
label.String("key1", "val1"),
|
|
|
|
label.String("key2", "val2"),
|
2020-02-20 20:31:21 +02:00
|
|
|
},
|
|
|
|
wantInHeader: []string{"key1=val1", "key2=val2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "two values with escaped chars",
|
2020-08-18 05:25:03 +02:00
|
|
|
kvs: []label.KeyValue{
|
|
|
|
label.String("key1", "val1,val2"),
|
|
|
|
label.String("key2", "val3=4"),
|
2020-02-20 20:31:21 +02:00
|
|
|
},
|
|
|
|
wantInHeader: []string{"key1=val1%2Cval2", "key2=val3%3D4"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "values of non-string types",
|
2020-08-18 05:25:03 +02:00
|
|
|
kvs: []label.KeyValue{
|
|
|
|
label.Bool("key1", true),
|
|
|
|
label.Int("key2", 123),
|
|
|
|
label.Int64("key3", 123),
|
2021-02-17 02:23:58 +02:00
|
|
|
label.Float64("key4", 123.567),
|
2020-02-20 20:31:21 +02:00
|
|
|
},
|
|
|
|
wantInHeader: []string{
|
|
|
|
"key1=true",
|
|
|
|
"key2=123",
|
|
|
|
"key3=123",
|
2021-02-17 02:23:58 +02:00
|
|
|
"key4=123.567",
|
2020-02-20 20:31:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
2020-09-09 20:13:37 +02:00
|
|
|
ctx := baggage.ContextWithMap(context.Background(), baggage.NewMap(baggage.MapUpdate{MultiKV: tt.kvs}))
|
2021-02-17 18:04:49 +02:00
|
|
|
propagator.Inject(ctx, propagation.HeaderCarrier(req.Header))
|
2020-02-20 20:31:21 +02:00
|
|
|
|
2020-10-20 19:51:17 +02:00
|
|
|
gotHeader := req.Header.Get("baggage")
|
2020-02-20 20:31:21 +02:00
|
|
|
wantedLen := len(strings.Join(tt.wantInHeader, ","))
|
|
|
|
if wantedLen != len(gotHeader) {
|
|
|
|
t.Errorf(
|
2020-10-20 19:51:17 +02:00
|
|
|
"%s: Inject baggage incorrect length %d != %d.", tt.name, tt.wantedLen, len(gotHeader),
|
2020-02-20 20:31:21 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
for _, inHeader := range tt.wantInHeader {
|
|
|
|
if !strings.Contains(gotHeader, inHeader) {
|
|
|
|
t.Errorf(
|
2020-10-20 19:51:17 +02:00
|
|
|
"%s: Inject baggage missing part of header: %s in %s", tt.name, inHeader, gotHeader,
|
2020-02-20 20:31:21 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 17:25:09 +02:00
|
|
|
func TestBaggagePropagatorGetAllKeys(t *testing.T) {
|
2020-11-13 17:34:24 +02:00
|
|
|
var propagator propagation.Baggage
|
2020-10-20 19:51:17 +02:00
|
|
|
want := []string{"baggage"}
|
2020-10-02 21:27:16 +02:00
|
|
|
got := propagator.Fields()
|
2020-02-20 20:31:21 +02:00
|
|
|
if diff := cmp.Diff(got, want); diff != "" {
|
|
|
|
t.Errorf("GetAllKeys: -got +want %s", diff)
|
|
|
|
}
|
|
|
|
}
|