You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-07-15 01:04:25 +02:00
Allow dropping items from correlations + docs + cleanups (#454)
* Drop entry from correlation map Entry used to contain stuff like TTL, but right now the notion of entry was dropped from the spec. * Compute exact size of the correlations map The map will be immutable, so spend some more time to ensure that we will not unnecessarily waste some memory on a basically read-only map. * Allow dropping keys from correlations map This is to follow the spec. Before this change it was possible in an awkward way by using Foreach to gather and filter the key-value pairs, and then calling NewMap with a MultiKV MapUpdate. * Document the correlation package * Add missing license blurbs in correlation package * Add tests for deleting items in correlations * Factor out getting map size and test it This is an implementation detail that can't be tested in a black-box manner. * Fix copyright dates * Simplify/disambiguate keySet function parameters/return values * Fix typo in Apply docs * Fix test names * Explain the nonsense of dropping keys from new map
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
// Copyright 2019, OpenTelemetry Authors
|
||||
// Copyright 2020, OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -22,97 +22,15 @@ import (
|
||||
"go.opentelemetry.io/otel/api/key"
|
||||
)
|
||||
|
||||
type testCase struct {
|
||||
name string
|
||||
value MapUpdate
|
||||
init []int
|
||||
wantKVs []core.KeyValue
|
||||
}
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
name string
|
||||
value MapUpdate
|
||||
init []int
|
||||
wantKVs []core.KeyValue
|
||||
}{
|
||||
{
|
||||
name: "NewMap with MultiKV",
|
||||
value: MapUpdate{MultiKV: []core.KeyValue{
|
||||
key.Int64("key1", 1),
|
||||
key.String("key2", "val2")},
|
||||
},
|
||||
init: []int{},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.Int64("key1", 1),
|
||||
key.String("key2", "val2"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "NewMap with SingleKV",
|
||||
value: MapUpdate{SingleKV: key.String("key1", "val1")},
|
||||
init: []int{},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.String("key1", "val1"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "NewMap with MapUpdate",
|
||||
value: MapUpdate{SingleKV: key.Int64("key1", 3),
|
||||
MultiKV: []core.KeyValue{
|
||||
key.String("key1", ""),
|
||||
key.String("key2", "val2")},
|
||||
},
|
||||
init: []int{},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.String("key1", ""),
|
||||
key.String("key2", "val2"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "NewMap with empty MapUpdate",
|
||||
value: MapUpdate{MultiKV: []core.KeyValue{}},
|
||||
init: []int{},
|
||||
wantKVs: []core.KeyValue{},
|
||||
},
|
||||
{
|
||||
name: "Map with MultiKV",
|
||||
value: MapUpdate{MultiKV: []core.KeyValue{
|
||||
key.Int64("key1", 1),
|
||||
key.String("key2", "val2")},
|
||||
},
|
||||
init: []int{5},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.Int64("key1", 1),
|
||||
key.String("key2", "val2"),
|
||||
key.Int("key5", 5),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Map with SingleKV",
|
||||
value: MapUpdate{SingleKV: key.String("key1", "val1")},
|
||||
init: []int{5},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.String("key1", "val1"),
|
||||
key.Int("key5", 5),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Map with MapUpdate",
|
||||
value: MapUpdate{SingleKV: key.Int64("key1", 3),
|
||||
MultiKV: []core.KeyValue{
|
||||
key.String("key1", ""),
|
||||
key.String("key2", "val2")},
|
||||
},
|
||||
init: []int{5},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.String("key1", ""),
|
||||
key.String("key2", "val2"),
|
||||
key.Int("key5", 5),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Map with empty MapUpdate",
|
||||
value: MapUpdate{MultiKV: []core.KeyValue{}},
|
||||
init: []int{5},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.Int("key5", 5),
|
||||
},
|
||||
},
|
||||
} {
|
||||
for _, testcase := range getTestCases() {
|
||||
t.Logf("Running test case %s", testcase.name)
|
||||
var got Map
|
||||
if len(testcase.init) > 0 {
|
||||
@ -144,12 +62,225 @@ func TestMap(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSizeComputation(t *testing.T) {
|
||||
for _, testcase := range getTestCases() {
|
||||
t.Logf("Running test case %s", testcase.name)
|
||||
var initMap Map
|
||||
if len(testcase.init) > 0 {
|
||||
initMap = makeTestMap(testcase.init)
|
||||
} else {
|
||||
initMap = NewEmptyMap()
|
||||
}
|
||||
gotMap := initMap.Apply(testcase.value)
|
||||
|
||||
delSet, addSet := getModificationSets(testcase.value)
|
||||
mapSize := getNewMapSize(initMap.m, delSet, addSet)
|
||||
|
||||
if gotMap.Len() != mapSize {
|
||||
t.Errorf("Expected computed size to be %d, got %d", gotMap.Len(), mapSize)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getTestCases() []testCase {
|
||||
return []testCase{
|
||||
{
|
||||
name: "New map with MultiKV",
|
||||
value: MapUpdate{MultiKV: []core.KeyValue{
|
||||
key.Int64("key1", 1),
|
||||
key.String("key2", "val2")},
|
||||
},
|
||||
init: []int{},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.Int64("key1", 1),
|
||||
key.String("key2", "val2"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "New map with SingleKV",
|
||||
value: MapUpdate{SingleKV: key.String("key1", "val1")},
|
||||
init: []int{},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.String("key1", "val1"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "New map with both add fields",
|
||||
value: MapUpdate{SingleKV: key.Int64("key1", 3),
|
||||
MultiKV: []core.KeyValue{
|
||||
key.String("key1", ""),
|
||||
key.String("key2", "val2")},
|
||||
},
|
||||
init: []int{},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.String("key1", ""),
|
||||
key.String("key2", "val2"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "New map with empty MapUpdate",
|
||||
value: MapUpdate{},
|
||||
init: []int{},
|
||||
wantKVs: []core.KeyValue{},
|
||||
},
|
||||
{
|
||||
name: "New map with DropSingleK",
|
||||
value: MapUpdate{DropSingleK: core.Key("key1")},
|
||||
init: []int{},
|
||||
wantKVs: []core.KeyValue{},
|
||||
},
|
||||
{
|
||||
name: "New map with DropMultiK",
|
||||
value: MapUpdate{DropMultiK: []core.Key{
|
||||
core.Key("key1"), core.Key("key2"),
|
||||
}},
|
||||
init: []int{},
|
||||
wantKVs: []core.KeyValue{},
|
||||
},
|
||||
{
|
||||
name: "New map with both drop fields",
|
||||
value: MapUpdate{
|
||||
DropSingleK: core.Key("key1"),
|
||||
DropMultiK: []core.Key{
|
||||
core.Key("key1"),
|
||||
core.Key("key2"),
|
||||
},
|
||||
},
|
||||
init: []int{},
|
||||
wantKVs: []core.KeyValue{},
|
||||
},
|
||||
{
|
||||
name: "New map with all fields",
|
||||
value: MapUpdate{
|
||||
DropSingleK: core.Key("key1"),
|
||||
DropMultiK: []core.Key{
|
||||
core.Key("key1"),
|
||||
core.Key("key2"),
|
||||
},
|
||||
SingleKV: key.String("key4", "val4"),
|
||||
MultiKV: []core.KeyValue{
|
||||
key.String("key1", ""),
|
||||
key.String("key2", "val2"),
|
||||
key.String("key3", "val3"),
|
||||
},
|
||||
},
|
||||
init: []int{},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.String("key1", ""),
|
||||
key.String("key2", "val2"),
|
||||
key.String("key3", "val3"),
|
||||
key.String("key4", "val4"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Existing map with MultiKV",
|
||||
value: MapUpdate{MultiKV: []core.KeyValue{
|
||||
key.Int64("key1", 1),
|
||||
key.String("key2", "val2")},
|
||||
},
|
||||
init: []int{5},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.Int64("key1", 1),
|
||||
key.String("key2", "val2"),
|
||||
key.Int("key5", 5),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Existing map with SingleKV",
|
||||
value: MapUpdate{SingleKV: key.String("key1", "val1")},
|
||||
init: []int{5},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.String("key1", "val1"),
|
||||
key.Int("key5", 5),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Existing map with both add fields",
|
||||
value: MapUpdate{SingleKV: key.Int64("key1", 3),
|
||||
MultiKV: []core.KeyValue{
|
||||
key.String("key1", ""),
|
||||
key.String("key2", "val2")},
|
||||
},
|
||||
init: []int{5},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.String("key1", ""),
|
||||
key.String("key2", "val2"),
|
||||
key.Int("key5", 5),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Existing map with empty MapUpdate",
|
||||
value: MapUpdate{},
|
||||
init: []int{5},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.Int("key5", 5),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Existing map with DropSingleK",
|
||||
value: MapUpdate{DropSingleK: core.Key("key1")},
|
||||
init: []int{1, 5},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.Int("key5", 5),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Existing map with DropMultiK",
|
||||
value: MapUpdate{DropMultiK: []core.Key{
|
||||
core.Key("key1"), core.Key("key2"),
|
||||
}},
|
||||
init: []int{1, 5},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.Int("key5", 5),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Existing map with both drop fields",
|
||||
value: MapUpdate{
|
||||
DropSingleK: core.Key("key1"),
|
||||
DropMultiK: []core.Key{
|
||||
core.Key("key1"),
|
||||
core.Key("key2"),
|
||||
},
|
||||
},
|
||||
init: []int{1, 2, 5},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.Int("key5", 5),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Existing map with all the fields",
|
||||
value: MapUpdate{
|
||||
DropSingleK: core.Key("key1"),
|
||||
DropMultiK: []core.Key{
|
||||
core.Key("key1"),
|
||||
core.Key("key2"),
|
||||
core.Key("key5"),
|
||||
core.Key("key6"),
|
||||
},
|
||||
SingleKV: key.String("key4", "val4"),
|
||||
MultiKV: []core.KeyValue{
|
||||
key.String("key1", ""),
|
||||
key.String("key2", "val2"),
|
||||
key.String("key3", "val3"),
|
||||
},
|
||||
},
|
||||
init: []int{5, 6, 7},
|
||||
wantKVs: []core.KeyValue{
|
||||
key.String("key1", ""),
|
||||
key.String("key2", "val2"),
|
||||
key.String("key3", "val3"),
|
||||
key.String("key4", "val4"),
|
||||
key.Int("key7", 7),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func makeTestMap(ints []int) Map {
|
||||
r := make(rawMap, len(ints))
|
||||
for _, v := range ints {
|
||||
r[core.Key(fmt.Sprintf("key%d", v))] = entry{
|
||||
value: core.Int(v),
|
||||
}
|
||||
r[core.Key(fmt.Sprintf("key%d", v))] = core.Int(v)
|
||||
}
|
||||
return newMap(r)
|
||||
}
|
||||
|
Reference in New Issue
Block a user