...

Source file src/go.opentelemetry.io/otel/attribute/filter_test.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
    16  
    17  import "testing"
    18  
    19  func TestNewAllowKeysFilter(t *testing.T) {
    20  	keys := []string{"zero", "one", "two"}
    21  	attrs := []KeyValue{Int(keys[0], 0), Int(keys[1], 1), Int(keys[2], 2)}
    22  
    23  	t.Run("Empty", func(t *testing.T) {
    24  		empty := NewAllowKeysFilter()
    25  		for _, kv := range attrs {
    26  			if empty(kv) {
    27  				t.Errorf("empty NewAllowKeysFilter filter accepted %v", kv)
    28  			}
    29  		}
    30  	})
    31  
    32  	t.Run("Partial", func(t *testing.T) {
    33  		partial := NewAllowKeysFilter(Key(keys[0]), Key(keys[1]))
    34  		for _, kv := range attrs[:2] {
    35  			if !partial(kv) {
    36  				t.Errorf("partial NewAllowKeysFilter filter denied %v", kv)
    37  			}
    38  		}
    39  		if partial(attrs[2]) {
    40  			t.Errorf("partial NewAllowKeysFilter filter accepted %v", attrs[2])
    41  		}
    42  	})
    43  
    44  	t.Run("Full", func(t *testing.T) {
    45  		full := NewAllowKeysFilter(Key(keys[0]), Key(keys[1]), Key(keys[2]))
    46  		for _, kv := range attrs {
    47  			if !full(kv) {
    48  				t.Errorf("full NewAllowKeysFilter filter denied %v", kv)
    49  			}
    50  		}
    51  	})
    52  }
    53  
    54  func TestNewDenyKeysFilter(t *testing.T) {
    55  	keys := []string{"zero", "one", "two"}
    56  	attrs := []KeyValue{Int(keys[0], 0), Int(keys[1], 1), Int(keys[2], 2)}
    57  
    58  	t.Run("Empty", func(t *testing.T) {
    59  		empty := NewDenyKeysFilter()
    60  		for _, kv := range attrs {
    61  			if !empty(kv) {
    62  				t.Errorf("empty NewDenyKeysFilter filter denied %v", kv)
    63  			}
    64  		}
    65  	})
    66  
    67  	t.Run("Partial", func(t *testing.T) {
    68  		partial := NewDenyKeysFilter(Key(keys[0]), Key(keys[1]))
    69  		for _, kv := range attrs[:2] {
    70  			if partial(kv) {
    71  				t.Errorf("partial NewDenyKeysFilter filter accepted %v", kv)
    72  			}
    73  		}
    74  		if !partial(attrs[2]) {
    75  			t.Errorf("partial NewDenyKeysFilter filter denied %v", attrs[2])
    76  		}
    77  	})
    78  
    79  	t.Run("Full", func(t *testing.T) {
    80  		full := NewDenyKeysFilter(Key(keys[0]), Key(keys[1]), Key(keys[2]))
    81  		for _, kv := range attrs {
    82  			if full(kv) {
    83  				t.Errorf("full NewDenyKeysFilter filter accepted %v", kv)
    84  			}
    85  		}
    86  	})
    87  }
    88  

View as plain text