-
Notifications
You must be signed in to change notification settings - Fork 10
/
blacklister.go
59 lines (51 loc) · 1.54 KB
/
blacklister.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package filters
import (
"regexp"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/trace/pb"
"k8s.io/klog/v2"
)
// Blacklister holds a list of regular expressions which will match resources
// on spans that should be dropped.
type Blacklister struct {
list []*regexp.Regexp
}
// Allows returns true if the Blacklister permits this span.
func (f *Blacklister) Allows(span *pb.Span) bool {
for _, entry := range f.list {
if entry.MatchString(span.Resource) {
return false
}
}
return true
}
// AllowsStat returns true if the Blacklister permits this stat
func (f *Blacklister) AllowsStat(stat *pb.ClientGroupedStats) bool {
for _, entry := range f.list {
if entry.MatchString(stat.Resource) {
return false
}
}
return true
}
// NewBlacklister creates a new Blacklister based on the given list of
// regular expressions.
func NewBlacklister(exprs []string) *Blacklister {
return &Blacklister{list: compileRules(exprs)}
}
// compileRules compiles as many rules as possible from the list of expressions.
func compileRules(exprs []string) []*regexp.Regexp {
list := make([]*regexp.Regexp, 0, len(exprs))
for _, entry := range exprs {
rule, err := regexp.Compile(entry)
if err != nil {
klog.Errorf("Invalid resource filter: %q", entry)
continue
}
list = append(list, rule)
}
return list
}