github.com/Jeffail/benthos/v3@v3.65.0/lib/metrics/blacklist_test.go (about)

     1  package metrics
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/Jeffail/benthos/v3/lib/log"
     9  )
    10  
    11  func TestBlacklistPaths(t *testing.T) {
    12  
    13  	child := &HTTP{
    14  		local:      NewLocal(),
    15  		timestamp:  time.Now(),
    16  		pathPrefix: "",
    17  	}
    18  	b := &Blacklist{
    19  		paths:    []string{"output", "input", "metrics"},
    20  		patterns: []*regexp.Regexp{},
    21  		s:        child,
    22  		log:      log.Noop(),
    23  	}
    24  
    25  	// acceptable paths
    26  	blacklistedStats := []string{"output.broker", "input.test", "metrics.status"}
    27  	for _, v := range blacklistedStats {
    28  		b.GetCounter(v)
    29  		if _, ok := child.local.flatCounters[v]; ok {
    30  			t.Errorf("Blacklist should not set a stat in child for disallowed path: %s", v)
    31  		}
    32  	}
    33  
    34  	allowedStats := []string{"processor.value", "logs.info", "test.test"}
    35  	for _, v := range allowedStats {
    36  		b.GetCounter(v)
    37  		if _, ok := child.local.flatCounters[v]; !ok {
    38  			t.Errorf("Blacklist should set a stat in child for allowed path: %s", v)
    39  		}
    40  	}
    41  }
    42  
    43  func TestBlacklistPatterns(t *testing.T) {
    44  
    45  	child := &HTTP{
    46  		local:      NewLocal(),
    47  		timestamp:  time.Now(),
    48  		pathPrefix: "",
    49  	}
    50  	b := &Blacklist{
    51  		paths: []string{},
    52  		s:     child,
    53  		log:   log.Noop(),
    54  	}
    55  
    56  	testPatterns := []string{"^output.broker", "^input$"}
    57  	testExpressions := make([]*regexp.Regexp, len(testPatterns))
    58  	for i, v := range testPatterns {
    59  		re, err := regexp.Compile(v)
    60  		if err != nil {
    61  			t.Errorf("Error setting up regular expression to compile")
    62  			return
    63  		}
    64  		testExpressions[i] = re
    65  	}
    66  	b.patterns = testExpressions
    67  
    68  	blacklistedStats := []string{"output.broker.connection", "input", "output.broker"}
    69  	for _, v := range blacklistedStats {
    70  		b.GetCounter(v)
    71  		if _, ok := child.local.flatCounters[v]; ok {
    72  			t.Errorf("Blacklist should not set a stat in child that matches an expression: %s", v)
    73  		}
    74  	}
    75  	allowedStats := []string{"output", "input.connection", "benthos"}
    76  	for _, v := range allowedStats {
    77  		b.GetCounter(v)
    78  		if _, ok := child.local.flatCounters[v]; !ok {
    79  			t.Errorf("Blacklist should set a stat in child that does not match an expression: %s", v)
    80  		}
    81  	}
    82  }