Skip to content

Commit

Permalink
improved upon '*' matching
Browse files Browse the repository at this point in the history
  • Loading branch information
dianashevchenko committed Apr 18, 2024
1 parent 8908f62 commit 5b3abba
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion ddtrace/tracer/rules_sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ func (sr *SamplingRule) match(s *span) bool {
defer s.Unlock()
if sr.Tags != nil {
for k, regex := range sr.Tags {
if regex == nil {
continue
}
if s.Meta != nil {
v, ok := s.Meta[k]
if ok && regex.MatchString(v) {
Expand Down Expand Up @@ -683,7 +686,7 @@ func newSingleSpanRateLimiter(mps float64) *rateLimiter {
// globMatch compiles pattern string into glob format, i.e. regular expressions with only '?'
// and '*' treated as regex metacharacters.
func globMatch(pattern string) *regexp.Regexp {
if pattern == "" {
if pattern == "" || pattern == "*" {
return nil
}
// escaping regex characters
Expand Down
14 changes: 13 additions & 1 deletion ddtrace/tracer/sampler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,11 @@ func TestRulesSampler(t *testing.T) {
spanSrv: "test-service",
spanName: "abcde",
},
{
rules: []SamplingRule{SpanTagsResourceRule(map[string]string{"hostname": "hn*", "tag": "*"}, "", "abc*", "test*", 1.0)},
spanSrv: "test-service",
spanName: "abcde",
},
{
rules: []SamplingRule{SpanNameServiceRule("", "web*", 1.0)},
spanSrv: "wEbServer",
Expand Down Expand Up @@ -904,13 +909,20 @@ func TestRulesSampler(t *testing.T) {
spanSrv: "wEbServer",
spanName: "web.reqUEst",
},
{
rules: []SamplingRule{SpanTagsResourceRule(map[string]string{"hostname": "hn*", "tag": "2*"}, "", "abc*", "test*", 1.0)},
spanSrv: "test-service",
spanName: "abcde",
},
} {
t.Run("", func(t *testing.T) {
assert := assert.New(t)
c := newConfig(WithSamplingRules(tt.rules))
rs := newRulesSampler(nil, c.spanRules, globalSampleRate())

span := makeFinishedSpan(tt.spanName, tt.spanSrv, "res-10", map[string]interface{}{"hostname": "hn-30"})
span := makeFinishedSpan(tt.spanName, tt.spanSrv, "res-10", map[string]interface{}{"hostname": "hn-30",
"tag": 20.1,
})
result := rs.SampleSpan(span)
assert.False(result)
assert.NotContains(span.Metrics, keySpanSamplingMechanism)
Expand Down

0 comments on commit 5b3abba

Please sign in to comment.