| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package entity // import "go.opentelemetry.io/collector/pdata/xpdata/entity" |
| 5 |
|
| 6 |
import "go.opentelemetry.io/collector/pdata/pcommon" |
| 7 |
|
| 8 |
// EntityAttributeMap is a wrapper around pcommon.Map that restricts operations to only the keys |
| 9 |
// that belong to a specific set of entity attributes (either ID or Description attributes). |
| 10 |
type EntityAttributeMap struct { |
| 11 |
keys pcommon.StringSlice |
| 12 |
attributes pcommon.Map |
| 13 |
} |
| 14 |
|
| 15 |
// Get returns the Value associated with the key and true. Returned |
| 16 |
// Value is not a copy, it is a reference to the value stored in this map. It is |
| 17 |
// allowed to modify the returned value using Value.Set* functions. |
| 18 |
// |
| 19 |
// If the key does not exist in the entity's key list or in the underlying map, |
| 20 |
// returns an invalid instance and false. Calling any functions on the returned |
| 21 |
// invalid instance will cause a panic. |
| 22 |
func (m EntityAttributeMap) Get(key string) (pcommon.Value, bool) { |
| 23 |
if !m.containsKey(key) { |
| 24 |
return pcommon.Value{}, false |
| 25 |
} |
| 26 |
return m.attributes.Get(key) |
| 27 |
} |
| 28 |
|
| 29 |
// CanPut returns true if it's safe to call Put methods on the given key. |
| 30 |
// Returns true if: |
| 31 |
// - The key is already owned by this entity (in the entity's key list), OR |
| 32 |
// - The key doesn't exist in the shared attributes map (available to claim) |
| 33 |
// |
| 34 |
// Returns false if the key exists in the shared map but belongs to another entity. |
| 35 |
// |
| 36 |
// Use this method before calling Put* methods to avoid conflicts: |
| 37 |
// |
| 38 |
// if entity.IDAttributes().CanPut("service.name") { |
| 39 |
// entity.IDAttributes().PutStr("service.name", "my-service") |
| 40 |
// } |
| 41 |
func (m EntityAttributeMap) CanPut(key string) bool { |
| 42 |
if m.containsKey(key) { |
| 43 |
return true |
| 44 |
} |
| 45 |
_, exists := m.attributes.Get(key) |
| 46 |
return !exists |
| 47 |
} |
| 48 |
|
| 49 |
// PutEmpty inserts or updates an empty value to the map under given key |
| 50 |
// and returns the updated/inserted value. |
| 51 |
// The key is also added to the entity's key list if not already present. |
| 52 |
// |
| 53 |
// WARNING: This method is destructive and will overwrite any existing value in the shared |
| 54 |
// attributes map, even if it belongs to another entity. Use CanPut() to check safety first |
| 55 |
// if you need to avoid conflicts with other entities. |
| 56 |
func (m EntityAttributeMap) PutEmpty(k string) pcommon.Value { |
| 57 |
if !m.containsKey(k) { |
| 58 |
m.keys.Append(k) |
| 59 |
} |
| 60 |
return m.attributes.PutEmpty(k) |
| 61 |
} |
| 62 |
|
| 63 |
// PutStr performs the Insert or Update action. The Value is |
| 64 |
// inserted to the map that did not originally have the key. The key/value is |
| 65 |
// updated to the map where the key already existed. |
| 66 |
// The key is also added to the entity's key list if not already present. |
| 67 |
// |
| 68 |
// WARNING: This method is destructive and will overwrite any existing value in the shared |
| 69 |
// attributes map, even if it belongs to another entity. Use CanPut() to check safety first |
| 70 |
// if you need to avoid conflicts with other entities. |
| 71 |
func (m EntityAttributeMap) PutStr(k, v string) { |
| 72 |
if !m.containsKey(k) { |
| 73 |
m.keys.Append(k) |
| 74 |
} |
| 75 |
m.attributes.PutStr(k, v) |
| 76 |
} |
| 77 |
|
| 78 |
// Remove removes the entry associated with the key and returns true if the key existed. |
| 79 |
// The key is also removed from the entity's key list. |
| 80 |
func (m EntityAttributeMap) Remove(key string) bool { |
| 81 |
var keyFound bool |
| 82 |
m.keys.RemoveIf(func(k string) bool { |
| 83 |
if k == key { |
| 84 |
keyFound = true |
| 85 |
return true |
| 86 |
} |
| 87 |
return false |
| 88 |
}) |
| 89 |
if !keyFound { |
| 90 |
return false |
| 91 |
} |
| 92 |
m.attributes.Remove(key) |
| 93 |
return true |
| 94 |
} |
| 95 |
|
| 96 |
func (m EntityAttributeMap) containsKey(key string) bool { |
| 97 |
for _, k := range m.keys.All() { |
| 98 |
if k == key { |
| 99 |
return true |
| 100 |
} |
| 101 |
} |
| 102 |
return false |
| 103 |
} |