4
4
"context"
5
5
"fmt"
6
6
"net/http"
7
+ "regexp"
7
8
"runtime"
8
9
"strings"
9
10
"testing"
@@ -13,6 +14,7 @@ import (
13
14
14
15
"github.com/DataDog/datadog-trace-agent/config"
15
16
"github.com/DataDog/datadog-trace-agent/fixtures"
17
+ "github.com/DataDog/datadog-trace-agent/info"
16
18
"github.com/DataDog/datadog-trace-agent/model"
17
19
"github.com/DataDog/datadog-trace-agent/obfuscate"
18
20
"github.com/stretchr/testify/assert"
@@ -107,6 +109,71 @@ func TestFormatTrace(t *testing.T) {
107
109
assert .Contains (result .Meta ["sql.query" ], "SELECT name FROM people WHERE age = ?" )
108
110
}
109
111
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
+
110
177
func BenchmarkAgentTraceProcessing (b * testing.B ) {
111
178
c := config .New ()
112
179
c .APIKey = "test"
0 commit comments