1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-12 10:04:29 +02:00
opentelemetry-go/propagation/baggage_test.go
Daniil Rutskiy 7de3b58ce9
Remove extra labels types (#1314)
* Remove extra labels types

Remove the following labels types: INT32, UINT32, UINT64
and FLOAT32.

Fix all extra labels types occurrences in the project.

Signed-off-by: Daniil Rutskiy <dstdfx@gmail.com>

* Update CHANGELOG.md

Signed-off-by: Daniil Rutskiy <dstdfx@gmail.com>

* Delete unused helpers

Signed-off-by: Daniil Rutskiy <dstdfx@gmail.com>

* Convert passed values into remaining types

Signed-off-by: Daniil Rutskiy <dstdfx@gmail.com>

* Clarify func description

* Fix uint64 convertion

Signed-off-by: Daniil Rutskiy <dstdfx@gmail.com>

* Fix uint conversion

Signed-off-by: Daniil Rutskiy <dstdfx@gmail.com>

* Update OTLP exporter label types

Co-authored-by: Tyler Yahn <codingalias@gmail.com>
2021-02-16 16:23:58 -08:00

249 lines
6.5 KiB
Go

// 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.
package propagation_test
import (
"context"
"net/http"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"go.opentelemetry.io/otel/internal/baggage"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/propagation"
)
func TestExtractValidBaggageFromHTTPReq(t *testing.T) {
prop := propagation.TextMapPropagator(propagation.Baggage{})
tests := []struct {
name string
header string
wantKVs []label.KeyValue
}{
{
name: "valid w3cHeader",
header: "key1=val1,key2=val2",
wantKVs: []label.KeyValue{
label.String("key1", "val1"),
label.String("key2", "val2"),
},
},
{
name: "valid w3cHeader with spaces",
header: "key1 = val1, key2 =val2 ",
wantKVs: []label.KeyValue{
label.String("key1", "val1"),
label.String("key2", "val2"),
},
},
{
name: "valid w3cHeader with properties",
header: "key1=val1,key2=val2;prop=1",
wantKVs: []label.KeyValue{
label.String("key1", "val1"),
label.String("key2", "val2;prop=1"),
},
},
{
name: "valid header with url-escaped comma",
header: "key1=val1,key2=val2%2Cval3",
wantKVs: []label.KeyValue{
label.String("key1", "val1"),
label.String("key2", "val2,val3"),
},
},
{
name: "valid header with an invalid header",
header: "key1=val1,key2=val2,a,val3",
wantKVs: []label.KeyValue{
label.String("key1", "val1"),
label.String("key2", "val2"),
},
},
{
name: "valid header with no value",
header: "key1=,key2=val2",
wantKVs: []label.KeyValue{
label.String("key1", ""),
label.String("key2", "val2"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
req.Header.Set("baggage", tt.header)
ctx := context.Background()
ctx = prop.Extract(ctx, req.Header)
gotBaggage := baggage.MapFromContext(ctx)
wantBaggage := baggage.NewMap(baggage.MapUpdate{MultiKV: tt.wantKVs})
if gotBaggage.Len() != wantBaggage.Len() {
t.Errorf(
"Got and Want Baggage are not the same size %d != %d",
gotBaggage.Len(),
wantBaggage.Len(),
)
}
totalDiff := ""
wantBaggage.Foreach(func(keyValue label.KeyValue) bool {
val, _ := gotBaggage.Value(keyValue.Key)
diff := cmp.Diff(keyValue, label.KeyValue{Key: keyValue.Key, Value: val}, cmp.AllowUnexported(label.Value{}))
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) {
prop := propagation.TextMapPropagator(propagation.Baggage{})
tests := []struct {
name string
header string
hasKVs []label.KeyValue
}{
{
name: "no key values",
header: "header1",
},
{
name: "invalid header with existing context",
header: "header2",
hasKVs: []label.KeyValue{
label.String("key1", "val1"),
label.String("key2", "val2"),
},
},
{
name: "empty header value",
header: "",
hasKVs: []label.KeyValue{
label.String("key1", "val1"),
label.String("key2", "val2"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
req.Header.Set("baggage", tt.header)
ctx := baggage.NewContext(context.Background(), tt.hasKVs...)
wantBaggage := baggage.MapFromContext(ctx)
ctx = prop.Extract(ctx, req.Header)
gotBaggage := baggage.MapFromContext(ctx)
if gotBaggage.Len() != wantBaggage.Len() {
t.Errorf(
"Got and Want Baggage are not the same size %d != %d",
gotBaggage.Len(),
wantBaggage.Len(),
)
}
totalDiff := ""
wantBaggage.Foreach(func(keyValue label.KeyValue) bool {
val, _ := gotBaggage.Value(keyValue.Key)
diff := cmp.Diff(keyValue, label.KeyValue{Key: keyValue.Key, Value: val}, cmp.AllowUnexported(label.Value{}))
if diff != "" {
totalDiff += diff + "\n"
}
return true
})
})
}
}
func TestInjectBaggageToHTTPReq(t *testing.T) {
propagator := propagation.Baggage{}
tests := []struct {
name string
kvs []label.KeyValue
wantInHeader []string
wantedLen int
}{
{
name: "two simple values",
kvs: []label.KeyValue{
label.String("key1", "val1"),
label.String("key2", "val2"),
},
wantInHeader: []string{"key1=val1", "key2=val2"},
},
{
name: "two values with escaped chars",
kvs: []label.KeyValue{
label.String("key1", "val1,val2"),
label.String("key2", "val3=4"),
},
wantInHeader: []string{"key1=val1%2Cval2", "key2=val3%3D4"},
},
{
name: "values of non-string types",
kvs: []label.KeyValue{
label.Bool("key1", true),
label.Int("key2", 123),
label.Int64("key3", 123),
label.Float64("key4", 123.567),
},
wantInHeader: []string{
"key1=true",
"key2=123",
"key3=123",
"key4=123.567",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
ctx := baggage.ContextWithMap(context.Background(), baggage.NewMap(baggage.MapUpdate{MultiKV: tt.kvs}))
propagator.Inject(ctx, req.Header)
gotHeader := req.Header.Get("baggage")
wantedLen := len(strings.Join(tt.wantInHeader, ","))
if wantedLen != len(gotHeader) {
t.Errorf(
"%s: Inject baggage incorrect length %d != %d.", tt.name, tt.wantedLen, len(gotHeader),
)
}
for _, inHeader := range tt.wantInHeader {
if !strings.Contains(gotHeader, inHeader) {
t.Errorf(
"%s: Inject baggage missing part of header: %s in %s", tt.name, inHeader, gotHeader,
)
}
}
})
}
}
func TestBaggagePropagatorGetAllKeys(t *testing.T) {
var propagator propagation.Baggage
want := []string{"baggage"}
got := propagator.Fields()
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("GetAllKeys: -got +want %s", diff)
}
}