...

Source file src/go.opentelemetry.io/otel/attribute/filter.go

Documentation: go.opentelemetry.io/otel/attribute

     1  // Copyright The OpenTelemetry Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package attribute // import "go.opentelemetry.io/otel/attribute"
    16  
    17  // Filter supports removing certain attributes from attribute sets. When
    18  // the filter returns true, the attribute will be kept in the filtered
    19  // attribute set. When the filter returns false, the attribute is excluded
    20  // from the filtered attribute set, and the attribute instead appears in
    21  // the removed list of excluded attributes.
    22  type Filter func(KeyValue) bool
    23  
    24  // NewAllowKeysFilter returns a Filter that only allows attributes with one of
    25  // the provided keys.
    26  //
    27  // If keys is empty a deny-all filter is returned.
    28  func NewAllowKeysFilter(keys ...Key) Filter {
    29  	if len(keys) <= 0 {
    30  		return func(kv KeyValue) bool { return false }
    31  	}
    32  
    33  	allowed := make(map[Key]struct{})
    34  	for _, k := range keys {
    35  		allowed[k] = struct{}{}
    36  	}
    37  	return func(kv KeyValue) bool {
    38  		_, ok := allowed[kv.Key]
    39  		return ok
    40  	}
    41  }
    42  
    43  // NewDenyKeysFilter returns a Filter that only allows attributes
    44  // that do not have one of the provided keys.
    45  //
    46  // If keys is empty an allow-all filter is returned.
    47  func NewDenyKeysFilter(keys ...Key) Filter {
    48  	if len(keys) <= 0 {
    49  		return func(kv KeyValue) bool { return true }
    50  	}
    51  
    52  	forbid := make(map[Key]struct{})
    53  	for _, k := range keys {
    54  		forbid[k] = struct{}{}
    55  	}
    56  	return func(kv KeyValue) bool {
    57  		_, ok := forbid[kv.Key]
    58  		return !ok
    59  	}
    60  }
    61  

View as plain text