This repository has been archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
filters: apply replace rules only after obfuscation
- Loading branch information
Showing
8 changed files
with
113 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package filters | ||
|
||
import ( | ||
"regexp" | ||
|
||
"github.com/DataDog/datadog-trace-agent/model" | ||
|
||
log "github.com/cihub/seelog" | ||
) | ||
|
||
// 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 *model.Span) bool { | ||
for _, entry := range f.list { | ||
if entry.MatchString(span.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 { | ||
log.Errorf("invalid resource filter: %q", entry) | ||
continue | ||
} | ||
list = append(list, rule) | ||
} | ||
return list | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package filters | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/DataDog/datadog-trace-agent/fixtures" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBlacklister(t *testing.T) { | ||
tests := []struct { | ||
filter []string | ||
resource string | ||
expectation bool | ||
}{ | ||
{[]string{"/foo/bar"}, "/foo/bar", false}, | ||
{[]string{"/foo/b.r"}, "/foo/bar", false}, | ||
{[]string{"[0-9]+"}, "/abcde", true}, | ||
{[]string{"[0-9]+"}, "/abcde123", false}, | ||
{[]string{"\\(foobar\\)"}, "(foobar)", false}, | ||
{[]string{"\\(foobar\\)"}, "(bar)", true}, | ||
{[]string{"(GET|POST) /healthcheck"}, "GET /foobar", true}, | ||
{[]string{"(GET|POST) /healthcheck"}, "GET /healthcheck", false}, | ||
{[]string{"(GET|POST) /healthcheck"}, "POST /healthcheck", false}, | ||
{[]string{"SELECT COUNT\\(\\*\\) FROM BAR"}, "SELECT COUNT(*) FROM BAR", false}, | ||
{[]string{"[123"}, "[123", true}, | ||
{[]string{"\\[123"}, "[123", false}, | ||
{[]string{"ABC+", "W+"}, "ABCCCC", false}, | ||
{[]string{"ABC+", "W+"}, "WWW", false}, | ||
} | ||
|
||
for _, test := range tests { | ||
span := fixtures.RandomSpan() | ||
span.Resource = test.resource | ||
filter := NewBlacklister(test.filter) | ||
|
||
assert.Equal(t, test.expectation, filter.Allows(span)) | ||
} | ||
} | ||
|
||
func TestCompileRules(t *testing.T) { | ||
filter := NewBlacklister([]string{"[123", "]123", "{6}"}) | ||
for i := 0; i < 100; i++ { | ||
span := fixtures.RandomSpan() | ||
assert.True(t, filter.Allows(span)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.