Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
Merge pull request #400 from DataDog/benjamin/tag-replace-all
Browse files Browse the repository at this point in the history
Apply tag replacement to all spans instead of root only
  • Loading branch information
LotharSee authored Mar 19, 2018
2 parents dc592e7 + 660f60d commit 000725e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 30 deletions.
2 changes: 1 addition & 1 deletion cmd/trace-agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (a *Agent) Process(t model.Trace) {
}

for _, f := range a.Filters {
if f.Keep(root) {
if f.Keep(root, &t) {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion filters/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// Filter is the interface implemented by all span-filters
type Filter interface {
Keep(*model.Span) bool
Keep(*model.Span, *model.Trace) bool
}

// Setup returns a slice of all registered filters
Expand Down
4 changes: 2 additions & 2 deletions filters/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ type resourceFilter struct {
}

// Keep returns true if Span.Resource doesn't match any of the filter's rules
func (f *resourceFilter) Keep(t *model.Span) bool {
func (f *resourceFilter) Keep(root *model.Span, trace *model.Trace) bool {
for _, entry := range f.blacklist {
if entry.MatchString(t.Resource) {
if entry.MatchString(root.Resource) {
return false
}
}
Expand Down
17 changes: 11 additions & 6 deletions filters/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ func TestFilter(t *testing.T) {

for _, test := range tests {
span := newTestSpan(test.resource)
trace := model.Trace{span}
filter := newTestFilter(test.filter)

assert.Equal(t, test.expectation, filter.Keep(span))
assert.Equal(t, test.expectation, filter.Keep(span, &trace))
}
}

Expand All @@ -41,28 +42,32 @@ func TestRegexCompilationFailure(t *testing.T) {

for i := 0; i < 100; i++ {
span := fixtures.RandomSpan()
assert.True(t, filter.Keep(span))
trace := model.Trace{span}
assert.True(t, filter.Keep(span, &trace))
}
}

func TestRegexEscaping(t *testing.T) {
span := newTestSpan("[123")
trace := model.Trace{span}

filter := newTestFilter("[123")
assert.True(t, filter.Keep(span))
assert.True(t, filter.Keep(span, &trace))

filter = newTestFilter("\\[123")
assert.False(t, filter.Keep(span))
assert.False(t, filter.Keep(span, &trace))
}

func TestMultipleEntries(t *testing.T) {
filter := newTestFilter("ABC+", "W+")

span := newTestSpan("ABCCCC")
assert.False(t, filter.Keep(span))
trace := model.Trace{span}
assert.False(t, filter.Keep(span, &trace))

span = newTestSpan("WWW")
assert.False(t, filter.Keep(span))
trace = model.Trace{span}
assert.False(t, filter.Keep(span, &trace))
}

func newTestFilter(blacklist ...string) Filter {
Expand Down
35 changes: 19 additions & 16 deletions filters/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,29 @@ func newTagReplacer(c *config.AgentConfig) *tagReplacer {
}

// Keep implements Filter.
func (f tagReplacer) Keep(s *model.Span) bool {
func (f tagReplacer) Keep(root *model.Span, trace *model.Trace) bool {
for _, rule := range f.replace {
key, str, re := rule.Name, rule.Repl, rule.Re
switch key {
case "*":
for k := range s.Meta {
s.Meta[k] = re.ReplaceAllString(s.Meta[k], str)
for _, s := range *trace {
switch key {
case "*":
for k := range s.Meta {
s.Meta[k] = re.ReplaceAllString(s.Meta[k], str)
}
s.Resource = re.ReplaceAllString(s.Resource, str)
case "resource.name":
s.Resource = re.ReplaceAllString(s.Resource, str)
default:
if s.Meta == nil {
continue
}
if _, ok := s.Meta[key]; !ok {
continue
}
s.Meta[key] = re.ReplaceAllString(s.Meta[key], str)
}
s.Resource = re.ReplaceAllString(s.Resource, str)
case "resource.name":
s.Resource = re.ReplaceAllString(s.Resource, str)
default:
if s.Meta == nil {
continue
}
if _, ok := s.Meta[key]; !ok {
continue
}
s.Meta[key] = re.ReplaceAllString(s.Meta[key], str)
}
}
// always return true as the goal of this filter is only to mutate data
return true
}
13 changes: 9 additions & 4 deletions filters/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,20 @@ func TestTagReplacer(t *testing.T) {
} {
rules := parseRulesFromString(tt.rules)
tr := &tagReplacer{replace: rules}
span := replaceFilterTestSpan(tt.got)
ok := tr.Keep(span)
root := replaceFilterTestSpan(tt.got)
childSpan := replaceFilterTestSpan(tt.got)
trace := model.Trace{root, childSpan}
ok := tr.Keep(root, &trace)
assert.True(ok)
for k, v := range tt.want {
switch k {
case "resource.name":
assert.Equal(v, span.Resource)
// test that the filter applies to all spans, not only the root
assert.Equal(v, root.Resource)
assert.Equal(v, childSpan.Resource)
default:
assert.Equal(v, span.Meta[k])
assert.Equal(v, root.Meta[k])
assert.Equal(v, childSpan.Meta[k])
}
}
}
Expand Down

0 comments on commit 000725e

Please sign in to comment.