Skip to content

Commit

Permalink
Unify api/label and api/kv in new label package (#1060)
Browse files Browse the repository at this point in the history
* Move `api/label` to `label`

* Move `api/kv` package contents into `label` package

* Unify label package name

* Move `api/internal/rawhelpers.go` to `internal`

* Propagate replacing `api/kv` with `label` pkg

* golint

* Fix over-aggressive change

* Update Changelog
  • Loading branch information
MrAlias authored Aug 18, 2020
1 parent e44c9de commit f995380
Show file tree
Hide file tree
Showing 112 changed files with 1,583 additions and 1,612 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- The environment variable used for resource detection has been changed from `OTEL_RESOURCE_LABELS` to `OTEL_RESOURCE_ATTRIBUTES` (#1042)
- Replace `WithSyncer` with `WithBatcher` in examples. (#1044)
- Replace the `google.golang.org/grpc/codes` dependency in the API with an equivalent `go.opentelemetry.io/otel/codes` package. (#1046)
- Merge the `go.opentelemetry.io/otel/api/label` and `go.opentelemetry.io/otel/api/kv` into the new `go.opentelemetry.io/otel/label` package. (#1060)
- Unify Callback Function Naming.
Rename `*Callback` with `*Func`. (#1061)

Expand Down
4 changes: 2 additions & 2 deletions api/apitest/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"testing"
"time"

"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/internal/matchers"
"go.opentelemetry.io/otel/label"
)

type Harness struct {
Expand Down Expand Up @@ -195,7 +195,7 @@ func (h *Harness) testSpan(tracerFactory func() trace.Tracer) {
span.SetName("new name")
},
"#SetAttributes": func(span trace.Span) {
span.SetAttributes(kv.String("key1", "value"), kv.Int("key2", 123))
span.SetAttributes(label.String("key1", "value"), label.Int("key2", 123))
},
}
var mechanisms = map[string]func() trace.Span{
Expand Down
4 changes: 2 additions & 2 deletions api/correlation/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package correlation
import (
"context"

"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/label"
)

type correlationsType struct{}
Expand Down Expand Up @@ -150,7 +150,7 @@ func ContextWithMap(ctx context.Context, m Map) context.Context {

// NewContext returns a context with the map from passed context
// updated with the passed key-value pairs.
func NewContext(ctx context.Context, keyvalues ...kv.KeyValue) context.Context {
func NewContext(ctx context.Context, keyvalues ...label.KeyValue) context.Context {
return ContextWithMap(ctx, MapFromContext(ctx).Apply(MapUpdate{
MultiKV: keyvalues,
}))
Expand Down
8 changes: 4 additions & 4 deletions api/correlation/correlation_context_propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"net/url"
"strings"

"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/api/propagation"
"go.opentelemetry.io/otel/label"
)

// Temporary header name until W3C finalizes format.
Expand All @@ -45,7 +45,7 @@ func (CorrelationContext) Inject(ctx context.Context, supplier propagation.HTTPS
correlationCtx := MapFromContext(ctx)
firstIter := true
var headerValueBuilder strings.Builder
correlationCtx.Foreach(func(kv kv.KeyValue) bool {
correlationCtx.Foreach(func(kv label.KeyValue) bool {
if !firstIter {
headerValueBuilder.WriteRune(',')
}
Expand All @@ -69,7 +69,7 @@ func (CorrelationContext) Extract(ctx context.Context, supplier propagation.HTTP
}

contextValues := strings.Split(correlationContext, ",")
keyValues := make([]kv.KeyValue, 0, len(contextValues))
keyValues := make([]label.KeyValue, 0, len(contextValues))
for _, contextValue := range contextValues {
valueAndProps := strings.Split(contextValue, ";")
if len(valueAndProps) < 1 {
Expand Down Expand Up @@ -99,7 +99,7 @@ func (CorrelationContext) Extract(ctx context.Context, supplier propagation.HTTP
trimmedValueWithProps.WriteString(prop)
}

keyValues = append(keyValues, kv.Key(trimmedName).String(trimmedValueWithProps.String()))
keyValues = append(keyValues, label.String(trimmedName, trimmedValueWithProps.String()))
}

if len(keyValues) > 0 {
Expand Down
96 changes: 48 additions & 48 deletions api/correlation/correlation_context_propagator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,63 +23,63 @@ import (
"github.com/google/go-cmp/cmp"

"go.opentelemetry.io/otel/api/correlation"
"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/api/propagation"
"go.opentelemetry.io/otel/label"
)

func TestExtractValidDistributedContextFromHTTPReq(t *testing.T) {
props := propagation.New(propagation.WithExtractors(correlation.CorrelationContext{}))
tests := []struct {
name string
header string
wantKVs []kv.KeyValue
wantKVs []label.KeyValue
}{
{
name: "valid w3cHeader",
header: "key1=val1,key2=val2",
wantKVs: []kv.KeyValue{
kv.Key("key1").String("val1"),
kv.Key("key2").String("val2"),
wantKVs: []label.KeyValue{
label.String("key1", "val1"),
label.String("key2", "val2"),
},
},
{
name: "valid w3cHeader with spaces",
header: "key1 = val1, key2 =val2 ",
wantKVs: []kv.KeyValue{
kv.Key("key1").String("val1"),
kv.Key("key2").String("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: []kv.KeyValue{
kv.Key("key1").String("val1"),
kv.Key("key2").String("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: []kv.KeyValue{
kv.Key("key1").String("val1"),
kv.Key("key2").String("val2,val3"),
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: []kv.KeyValue{
kv.Key("key1").String("val1"),
kv.Key("key2").String("val2"),
wantKVs: []label.KeyValue{
label.String("key1", "val1"),
label.String("key2", "val2"),
},
},
{
name: "valid header with no value",
header: "key1=,key2=val2",
wantKVs: []kv.KeyValue{
kv.Key("key1").String(""),
kv.Key("key2").String("val2"),
wantKVs: []label.KeyValue{
label.String("key1", ""),
label.String("key2", "val2"),
},
},
}
Expand All @@ -101,9 +101,9 @@ func TestExtractValidDistributedContextFromHTTPReq(t *testing.T) {
)
}
totalDiff := ""
wantCorCtx.Foreach(func(keyValue kv.KeyValue) bool {
wantCorCtx.Foreach(func(keyValue label.KeyValue) bool {
val, _ := gotCorCtx.Value(keyValue.Key)
diff := cmp.Diff(keyValue, kv.KeyValue{Key: keyValue.Key, Value: val}, cmp.AllowUnexported(kv.Value{}))
diff := cmp.Diff(keyValue, label.KeyValue{Key: keyValue.Key, Value: val}, cmp.AllowUnexported(label.Value{}))
if diff != "" {
totalDiff += diff + "\n"
}
Expand All @@ -121,7 +121,7 @@ func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) {
tests := []struct {
name string
header string
hasKVs []kv.KeyValue
hasKVs []label.KeyValue
}{
{
name: "no key values",
Expand All @@ -130,17 +130,17 @@ func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) {
{
name: "invalid header with existing context",
header: "header2",
hasKVs: []kv.KeyValue{
kv.Key("key1").String("val1"),
kv.Key("key2").String("val2"),
hasKVs: []label.KeyValue{
label.String("key1", "val1"),
label.String("key2", "val2"),
},
},
{
name: "empty header value",
header: "",
hasKVs: []kv.KeyValue{
kv.Key("key1").String("val1"),
kv.Key("key2").String("val2"),
hasKVs: []label.KeyValue{
label.String("key1", "val1"),
label.String("key2", "val2"),
},
},
}
Expand All @@ -162,9 +162,9 @@ func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) {
)
}
totalDiff := ""
wantCorCtx.Foreach(func(keyValue kv.KeyValue) bool {
wantCorCtx.Foreach(func(keyValue label.KeyValue) bool {
val, _ := gotCorCtx.Value(keyValue.Key)
diff := cmp.Diff(keyValue, kv.KeyValue{Key: keyValue.Key, Value: val}, cmp.AllowUnexported(kv.Value{}))
diff := cmp.Diff(keyValue, label.KeyValue{Key: keyValue.Key, Value: val}, cmp.AllowUnexported(label.Value{}))
if diff != "" {
totalDiff += diff + "\n"
}
Expand All @@ -179,38 +179,38 @@ func TestInjectCorrelationContextToHTTPReq(t *testing.T) {
props := propagation.New(propagation.WithInjectors(propagator))
tests := []struct {
name string
kvs []kv.KeyValue
kvs []label.KeyValue
wantInHeader []string
wantedLen int
}{
{
name: "two simple values",
kvs: []kv.KeyValue{
kv.Key("key1").String("val1"),
kv.Key("key2").String("val2"),
kvs: []label.KeyValue{
label.String("key1", "val1"),
label.String("key2", "val2"),
},
wantInHeader: []string{"key1=val1", "key2=val2"},
},
{
name: "two values with escaped chars",
kvs: []kv.KeyValue{
kv.Key("key1").String("val1,val2"),
kv.Key("key2").String("val3=4"),
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: []kv.KeyValue{
kv.Key("key1").Bool(true),
kv.Key("key2").Int(123),
kv.Key("key3").Int64(123),
kv.Key("key4").Int32(123),
kv.Key("key5").Uint(123),
kv.Key("key6").Uint32(123),
kv.Key("key7").Uint64(123),
kv.Key("key8").Float64(123.567),
kv.Key("key9").Float32(123.567),
kvs: []label.KeyValue{
label.Bool("key1", true),
label.Int("key2", 123),
label.Int64("key3", 123),
label.Int32("key4", 123),
label.Uint("key5", 123),
label.Uint32("key6", 123),
label.Uint64("key7", 123),
label.Float64("key8", 123.567),
label.Float32("key9", 123.567),
},
wantInHeader: []string{
"key1=true",
Expand Down
28 changes: 13 additions & 15 deletions api/correlation/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@

package correlation

import (
"go.opentelemetry.io/otel/api/kv"
)
import "go.opentelemetry.io/otel/label"

type rawMap map[kv.Key]kv.Value
type keySet map[kv.Key]struct{}
type rawMap map[label.Key]label.Value
type keySet map[label.Key]struct{}

// Map is an immutable storage for correlations.
type Map struct {
Expand All @@ -32,18 +30,18 @@ type MapUpdate struct {
// DropSingleK contains a single key to be dropped from
// correlations. Use this to avoid an overhead of a slice
// allocation if there is only one key to drop.
DropSingleK kv.Key
DropSingleK label.Key
// DropMultiK contains all the keys to be dropped from
// correlations.
DropMultiK []kv.Key
DropMultiK []label.Key

// SingleKV contains a single key-value pair to be added to
// correlations. Use this to avoid an overhead of a slice
// allocation if there is only one key-value pair to add.
SingleKV kv.KeyValue
SingleKV label.KeyValue
// MultiKV contains all the key-value pairs to be added to
// correlations.
MultiKV []kv.KeyValue
MultiKV []label.KeyValue
}

func newMap(raw rawMap) Map {
Expand Down Expand Up @@ -101,7 +99,7 @@ func getModificationSets(update MapUpdate) (delSet, addSet keySet) {
deletionsCount++
}
if deletionsCount > 0 {
delSet = make(map[kv.Key]struct{}, deletionsCount)
delSet = make(map[label.Key]struct{}, deletionsCount)
for _, k := range update.DropMultiK {
delSet[k] = struct{}{}
}
Expand All @@ -115,7 +113,7 @@ func getModificationSets(update MapUpdate) (delSet, addSet keySet) {
additionsCount++
}
if additionsCount > 0 {
addSet = make(map[kv.Key]struct{}, additionsCount)
addSet = make(map[label.Key]struct{}, additionsCount)
for _, k := range update.MultiKV {
addSet[k.Key] = struct{}{}
}
Expand Down Expand Up @@ -146,14 +144,14 @@ func getNewMapSize(m rawMap, delSet, addSet keySet) int {

// Value gets a value from correlations map and returns a boolean
// value indicating whether the key exist in the map.
func (m Map) Value(k kv.Key) (kv.Value, bool) {
func (m Map) Value(k label.Key) (label.Value, bool) {
value, ok := m.m[k]
return value, ok
}

// HasValue returns a boolean value indicating whether the key exist
// in the map.
func (m Map) HasValue(k kv.Key) bool {
func (m Map) HasValue(k label.Key) bool {
_, has := m.Value(k)
return has
}
Expand All @@ -166,9 +164,9 @@ func (m Map) Len() int {
// Foreach calls a passed callback once on each key-value pair until
// all the key-value pairs of the map were iterated or the callback
// returns false, whichever happens first.
func (m Map) Foreach(f func(kv kv.KeyValue) bool) {
func (m Map) Foreach(f func(label.KeyValue) bool) {
for k, v := range m.m {
if !f(kv.KeyValue{
if !f(label.KeyValue{
Key: k,
Value: v,
}) {
Expand Down
Loading

0 comments on commit f995380

Please sign in to comment.