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

Commit 0f579af

Browse files
committed
cmd/trace-agent: add Process function tests
1 parent 52c0d30 commit 0f579af

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

cmd/trace-agent/agent_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"regexp"
78
"runtime"
89
"strings"
910
"testing"
@@ -13,6 +14,7 @@ import (
1314

1415
"github.com/DataDog/datadog-trace-agent/config"
1516
"github.com/DataDog/datadog-trace-agent/fixtures"
17+
"github.com/DataDog/datadog-trace-agent/info"
1618
"github.com/DataDog/datadog-trace-agent/model"
1719
"github.com/DataDog/datadog-trace-agent/obfuscate"
1820
"github.com/stretchr/testify/assert"
@@ -107,6 +109,71 @@ func TestFormatTrace(t *testing.T) {
107109
assert.Contains(result.Meta["sql.query"], "SELECT name FROM people WHERE age = ?")
108110
}
109111

112+
func TestProcess(t *testing.T) {
113+
t.Run("Replacer", func(t *testing.T) {
114+
// Ensures that for "sql" type spans:
115+
// • obfuscator runs before replacer
116+
// • obfuscator obfuscates both resource and "sql.query" tag
117+
// • resulting resource is obfuscated with replacements applied
118+
// • resulting "sql.query" tag is obfuscated with no replacements applied
119+
cfg := config.New()
120+
cfg.APIKey = "test"
121+
cfg.ReplaceTags = []*config.ReplaceRule{{
122+
Name: "resource.name",
123+
Re: regexp.MustCompile("AND.*"),
124+
Repl: "...",
125+
}}
126+
ctx, cancel := context.WithCancel(context.Background())
127+
agent := NewAgent(ctx, cfg)
128+
defer cancel()
129+
130+
now := time.Now()
131+
span := &model.Span{
132+
Resource: "SELECT name FROM people WHERE age = 42 AND extra = 55",
133+
Type: "sql",
134+
Start: now.Add(-time.Second).UnixNano(),
135+
Duration: (500 * time.Millisecond).Nanoseconds(),
136+
}
137+
agent.Process(model.Trace{span})
138+
139+
assert := assert.New(t)
140+
assert.Equal("SELECT name FROM people WHERE age = ? ...", span.Resource)
141+
assert.Equal("SELECT name FROM people WHERE age = ? AND extra = ?", span.Meta["sql.query"])
142+
})
143+
144+
t.Run("Blacklister", func(t *testing.T) {
145+
cfg := config.New()
146+
cfg.APIKey = "test"
147+
cfg.Ignore["resource"] = []string{"^INSERT.*"}
148+
ctx, cancel := context.WithCancel(context.Background())
149+
agent := NewAgent(ctx, cfg)
150+
defer cancel()
151+
152+
now := time.Now()
153+
spanValid := &model.Span{
154+
Resource: "SELECT name FROM people WHERE age = 42 AND extra = 55",
155+
Type: "sql",
156+
Start: now.Add(-time.Second).UnixNano(),
157+
Duration: (500 * time.Millisecond).Nanoseconds(),
158+
}
159+
spanInvalid := &model.Span{
160+
Resource: "INSERT INTO db VALUES (1, 2, 3)",
161+
Type: "sql",
162+
Start: now.Add(-time.Second).UnixNano(),
163+
Duration: (500 * time.Millisecond).Nanoseconds(),
164+
}
165+
166+
stats := agent.Receiver.stats.GetTagStats(info.Tags{})
167+
assert := assert.New(t)
168+
169+
agent.Process(model.Trace{spanValid})
170+
assert.EqualValues(0, stats.TracesFiltered)
171+
172+
agent.Process(model.Trace{spanInvalid})
173+
assert.EqualValues(1, stats.TracesFiltered)
174+
})
175+
}
176+
110177
func BenchmarkAgentTraceProcessing(b *testing.B) {
111178
c := config.New()
112179
c.APIKey = "test"

0 commit comments

Comments
 (0)