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
filters: apply replace rules only after obfuscation #477
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"context" | ||
"fmt" | ||
"net/http" | ||
"regexp" | ||
"runtime" | ||
"strings" | ||
"testing" | ||
|
@@ -13,6 +14,7 @@ import ( | |
|
||
"github.com/DataDog/datadog-trace-agent/config" | ||
"github.com/DataDog/datadog-trace-agent/fixtures" | ||
"github.com/DataDog/datadog-trace-agent/info" | ||
"github.com/DataDog/datadog-trace-agent/model" | ||
"github.com/DataDog/datadog-trace-agent/obfuscate" | ||
"github.com/stretchr/testify/assert" | ||
|
@@ -107,6 +109,71 @@ func TestFormatTrace(t *testing.T) { | |
assert.Contains(result.Meta["sql.query"], "SELECT name FROM people WHERE age = ?") | ||
} | ||
|
||
func TestProcess(t *testing.T) { | ||
t.Run("Replacer", func(t *testing.T) { | ||
// Ensures that for "sql" type spans: | ||
// • obfuscator runs before replacer | ||
// • obfuscator obfuscates both resource and "sql.query" tag | ||
// • resulting resource is obfuscated with replacements applied | ||
// • resulting "sql.query" tag is obfuscated with no replacements applied | ||
cfg := config.New() | ||
cfg.APIKey = "test" | ||
cfg.ReplaceTags = []*config.ReplaceRule{{ | ||
Name: "resource.name", | ||
Re: regexp.MustCompile("AND.*"), | ||
Repl: "...", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This replacement is something that would normally break the parser for normalization, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly, although that wasn't the case being tested here. But you're right, this is yet another bug that is resolved by this change. |
||
}} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
agent := NewAgent(ctx, cfg) | ||
defer cancel() | ||
|
||
now := time.Now() | ||
span := &model.Span{ | ||
Resource: "SELECT name FROM people WHERE age = 42 AND extra = 55", | ||
Type: "sql", | ||
Start: now.Add(-time.Second).UnixNano(), | ||
Duration: (500 * time.Millisecond).Nanoseconds(), | ||
} | ||
agent.Process(model.Trace{span}) | ||
|
||
assert := assert.New(t) | ||
assert.Equal("SELECT name FROM people WHERE age = ? ...", span.Resource) | ||
assert.Equal("SELECT name FROM people WHERE age = ? AND extra = ?", span.Meta["sql.query"]) | ||
}) | ||
|
||
t.Run("Blacklister", func(t *testing.T) { | ||
cfg := config.New() | ||
cfg.APIKey = "test" | ||
cfg.Ignore["resource"] = []string{"^INSERT.*"} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
agent := NewAgent(ctx, cfg) | ||
defer cancel() | ||
|
||
now := time.Now() | ||
spanValid := &model.Span{ | ||
Resource: "SELECT name FROM people WHERE age = 42 AND extra = 55", | ||
Type: "sql", | ||
Start: now.Add(-time.Second).UnixNano(), | ||
Duration: (500 * time.Millisecond).Nanoseconds(), | ||
} | ||
spanInvalid := &model.Span{ | ||
Resource: "INSERT INTO db VALUES (1, 2, 3)", | ||
Type: "sql", | ||
Start: now.Add(-time.Second).UnixNano(), | ||
Duration: (500 * time.Millisecond).Nanoseconds(), | ||
} | ||
|
||
stats := agent.Receiver.stats.GetTagStats(info.Tags{}) | ||
assert := assert.New(t) | ||
|
||
agent.Process(model.Trace{spanValid}) | ||
assert.EqualValues(0, stats.TracesFiltered) | ||
|
||
agent.Process(model.Trace{spanInvalid}) | ||
assert.EqualValues(1, stats.TracesFiltered) | ||
}) | ||
} | ||
|
||
func BenchmarkAgentTraceProcessing(b *testing.B) { | ||
c := config.New() | ||
c.APIKey = "test" | ||
|
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I suggest Denylister instead?
rails/rails#33677
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm more along the lines of the second comment in that thread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still worth considering though.