Debian Codegraph @

attribute/filter.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package attribute // import "go.opentelemetry.io/otel/attribute"
5
6 // Filter supports removing certain attributes from attribute sets. When
7 // the filter returns true, the attribute will be kept in the filtered
8 // attribute set. When the filter returns false, the attribute is excluded
9 // from the filtered attribute set, and the attribute instead appears in
10 // the removed list of excluded attributes.
11 type Filter func(KeyValue) bool
12
13 // NewAllowKeysFilter returns a Filter that only allows attributes with one of
14 // the provided keys.
15 //
16 // If keys is empty a deny-all filter is returned.
17 func NewAllowKeysFilter(keys ...Key) Filter {
18 if len(keys) == 0 {
19 return func(KeyValue) bool { return false }
20 }
21
22 allowed := make(map[Key]struct{}, len(keys))
23 for _, k := range keys {
24 allowed[k] = struct{}{}
25 }
26 return func(kv KeyValue) bool {
27 _, ok := allowed[kv.Key]
28 return ok
29 }
30 }
31
32 // NewDenyKeysFilter returns a Filter that only allows attributes
33 // that do not have one of the provided keys.
34 //
35 // If keys is empty an allow-all filter is returned.
36 func NewDenyKeysFilter(keys ...Key) Filter {
37 if len(keys) == 0 {
38 return func(KeyValue) bool { return true }
39 }
40
41 forbid := make(map[Key]struct{}, len(keys))
42 for _, k := range keys {
43 forbid[k] = struct{}{}
44 }
45 return func(kv KeyValue) bool {
46 _, ok := forbid[kv.Key]
47 return !ok
48 }
49 }