2020-02-03 23:07:53 +02:00
|
|
|
// Copyright 2020, OpenTelemetry Authors
|
2019-06-14 22:09:41 +02:00
|
|
|
//
|
|
|
|
// 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-01-28 20:13:46 +02:00
|
|
|
package correlation
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
import (
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2020-02-03 23:07:53 +02:00
|
|
|
type rawMap map[core.Key]core.Value
|
|
|
|
type keySet map[core.Key]struct{}
|
2019-08-23 18:01:52 +02:00
|
|
|
|
2020-02-03 23:07:53 +02:00
|
|
|
// Map is an immutable storage for correlations.
|
2019-08-23 18:01:52 +02:00
|
|
|
type Map struct {
|
|
|
|
m rawMap
|
|
|
|
}
|
2019-07-01 20:27:52 +02:00
|
|
|
|
2020-02-03 23:07:53 +02:00
|
|
|
// MapUpdate contains information about correlation changes to be
|
|
|
|
// made.
|
2019-08-23 18:01:52 +02:00
|
|
|
type MapUpdate struct {
|
2020-02-03 23:07:53 +02:00
|
|
|
// 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 core.Key
|
|
|
|
// DropMultiK contains all the keys to be dropped from
|
|
|
|
// correlations.
|
|
|
|
DropMultiK []core.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.
|
2019-10-30 22:21:13 +02:00
|
|
|
SingleKV core.KeyValue
|
2020-02-03 23:07:53 +02:00
|
|
|
// MultiKV contains all the key-value pairs to be added to
|
|
|
|
// correlations.
|
|
|
|
MultiKV []core.KeyValue
|
2019-08-23 18:01:52 +02:00
|
|
|
}
|
2019-07-01 20:27:52 +02:00
|
|
|
|
2019-08-23 18:01:52 +02:00
|
|
|
func newMap(raw rawMap) Map {
|
|
|
|
return Map{
|
|
|
|
m: raw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 23:07:53 +02:00
|
|
|
// NewEmptyMap creates an empty correlations map.
|
2019-08-23 18:01:52 +02:00
|
|
|
func NewEmptyMap() Map {
|
|
|
|
return newMap(nil)
|
|
|
|
}
|
|
|
|
|
2020-02-03 23:07:53 +02:00
|
|
|
// NewMap creates a map with the contents of the update applied. In
|
|
|
|
// this function, having an update with DropSingleK or DropMultiK
|
|
|
|
// makes no sense - those fields are effectively ignored.
|
2019-08-23 18:01:52 +02:00
|
|
|
func NewMap(update MapUpdate) Map {
|
|
|
|
return NewEmptyMap().Apply(update)
|
|
|
|
}
|
|
|
|
|
2020-02-03 23:07:53 +02:00
|
|
|
// Apply creates a copy of the map with the contents of the update
|
|
|
|
// applied. Apply will first drop the keys from DropSingleK and
|
|
|
|
// DropMultiK, then add key-value pairs from SingleKV and MultiKV.
|
2019-08-23 18:01:52 +02:00
|
|
|
func (m Map) Apply(update MapUpdate) Map {
|
2020-02-03 23:07:53 +02:00
|
|
|
delSet, addSet := getModificationSets(update)
|
|
|
|
mapSize := getNewMapSize(m.m, delSet, addSet)
|
|
|
|
|
|
|
|
r := make(rawMap, mapSize)
|
2019-08-23 18:01:52 +02:00
|
|
|
for k, v := range m.m {
|
2020-02-03 23:07:53 +02:00
|
|
|
// do not copy items we want to drop
|
|
|
|
if _, ok := delSet[k]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// do not copy items we would overwrite
|
|
|
|
if _, ok := addSet[k]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2019-08-23 18:01:52 +02:00
|
|
|
r[k] = v
|
2019-06-29 01:35:43 +02:00
|
|
|
}
|
2019-07-12 00:28:38 +02:00
|
|
|
if update.SingleKV.Key.Defined() {
|
2020-02-03 23:07:53 +02:00
|
|
|
r[update.SingleKV.Key] = update.SingleKV.Value
|
2019-06-29 01:35:43 +02:00
|
|
|
}
|
2019-07-12 00:28:38 +02:00
|
|
|
for _, kv := range update.MultiKV {
|
2020-02-03 23:07:53 +02:00
|
|
|
r[kv.Key] = kv.Value
|
2019-06-29 01:35:43 +02:00
|
|
|
}
|
2019-08-23 18:01:52 +02:00
|
|
|
if len(r) == 0 {
|
|
|
|
r = nil
|
2019-06-29 01:35:43 +02:00
|
|
|
}
|
2019-08-23 18:01:52 +02:00
|
|
|
return newMap(r)
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:07:53 +02:00
|
|
|
func getModificationSets(update MapUpdate) (delSet, addSet keySet) {
|
|
|
|
deletionsCount := len(update.DropMultiK)
|
|
|
|
if update.DropSingleK.Defined() {
|
|
|
|
deletionsCount++
|
|
|
|
}
|
|
|
|
if deletionsCount > 0 {
|
|
|
|
delSet = make(map[core.Key]struct{}, deletionsCount)
|
|
|
|
for _, k := range update.DropMultiK {
|
|
|
|
delSet[k] = struct{}{}
|
|
|
|
}
|
|
|
|
if update.DropSingleK.Defined() {
|
|
|
|
delSet[update.DropSingleK] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
additionsCount := len(update.MultiKV)
|
|
|
|
if update.SingleKV.Key.Defined() {
|
|
|
|
additionsCount++
|
|
|
|
}
|
|
|
|
if additionsCount > 0 {
|
|
|
|
addSet = make(map[core.Key]struct{}, additionsCount)
|
|
|
|
for _, k := range update.MultiKV {
|
|
|
|
addSet[k.Key] = struct{}{}
|
|
|
|
}
|
|
|
|
if update.SingleKV.Key.Defined() {
|
|
|
|
addSet[update.SingleKV.Key] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNewMapSize(m rawMap, delSet, addSet keySet) int {
|
|
|
|
mapSizeDiff := 0
|
|
|
|
for k := range addSet {
|
|
|
|
if _, ok := m[k]; !ok {
|
|
|
|
mapSizeDiff++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k := range delSet {
|
|
|
|
if _, ok := m[k]; ok {
|
|
|
|
if _, inAddSet := addSet[k]; !inAddSet {
|
|
|
|
mapSizeDiff--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(m) + mapSizeDiff
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value gets a value from correlations map and returns a boolean
|
|
|
|
// value indicating whether the key exist in the map.
|
2019-08-23 18:01:52 +02:00
|
|
|
func (m Map) Value(k core.Key) (core.Value, bool) {
|
2020-02-03 23:07:53 +02:00
|
|
|
value, ok := m.m[k]
|
|
|
|
return value, ok
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:07:53 +02:00
|
|
|
// HasValue returns a boolean value indicating whether the key exist
|
|
|
|
// in the map.
|
2019-08-23 18:01:52 +02:00
|
|
|
func (m Map) HasValue(k core.Key) bool {
|
2019-06-29 01:35:43 +02:00
|
|
|
_, has := m.Value(k)
|
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
2020-02-03 23:07:53 +02:00
|
|
|
// Len returns a length of the map.
|
2019-08-23 18:01:52 +02:00
|
|
|
func (m Map) Len() int {
|
|
|
|
return len(m.m)
|
2019-06-29 01:35:43 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:07:53 +02:00
|
|
|
// 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.
|
2019-08-23 18:01:52 +02:00
|
|
|
func (m Map) Foreach(f func(kv core.KeyValue) bool) {
|
|
|
|
for k, v := range m.m {
|
2019-06-29 01:35:43 +02:00
|
|
|
if !f(core.KeyValue{
|
|
|
|
Key: k,
|
2020-02-03 23:07:53 +02:00
|
|
|
Value: v,
|
2019-06-29 01:35:43 +02:00
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|