Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dogstatsd_sink): switch from pure map to list of key/value pairs #627

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/godogstats/dogstatsd_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ func WithStatsdPort(port int) goDogStatsSinkOption {
}
}

// WithMogrifier adds a mogrifier to the sink. Map iteration order is randomized, to control order call multiple times.
func WithMogrifier(mogrifiers map[*regexp.Regexp]func([]string) (string, []string)) goDogStatsSinkOption {
return func(g *godogStatsSink) {
g.mogrifier = mogrifiers
for m, h := range mogrifiers {
g.mogrifier = append(g.mogrifier, mogrifierEntry{
matcher: m,
handler: h,
})
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/godogstats/dogstatsd_sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ func TestSeparateExtraTags(t *testing.T) {
func TestSinkMogrify(t *testing.T) {
g := &godogStatsSink{
mogrifier: mogrifierMap{
regexp.MustCompile(`^ratelimit\.(.*)$`): func(matches []string) (string, []string) {
return "custom." + matches[1], []string{"tag1:value1", "tag2:value2"}
{
matcher: regexp.MustCompile(`^ratelimit\.(.*)$`),
handler: func(matches []string) (string, []string) {
return "custom." + matches[1], []string{"tag1:value1", "tag2:value2"}
},
},
},
}
Expand Down
39 changes: 24 additions & 15 deletions src/godogstats/mogrifier_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ var varFinder = regexp.MustCompile(`\$\d+`) // matches $0, $1, etc.

const envPrefix = "DOG_STATSD_MOGRIFIER" // prefix for environment variables

// mogrifierMap is a map of regular expressions to functions that mogrify a name and return tags
type mogrifierMap map[*regexp.Regexp]func([]string) (string, []string)
type mogrifierEntry struct {
matcher *regexp.Regexp // the matcher determines whether a mogrifier should run on a metric at all
handler func(matches []string) (name string, tags []string) // the handler takes the list of matches, and returns metric name and list of tags
}

// mogrifierMap is an ordered map of regular expressions to functions that mogrify a name and return tags
type mogrifierMap []mogrifierEntry

// makePatternHandler returns a function that replaces $0, $1, etc. in the pattern with the corresponding match
func makePatternHandler(pattern string) func([]string) string {
Expand Down Expand Up @@ -73,32 +78,36 @@ func newMogrifierMapFromEnv(keys []string) (mogrifierMap, error) {
}
}

mogrifiers[re] = func(matches []string) (string, []string) {
name := nameHandler(matches)
tags := make([]string, 0, len(tagHandlers))
for tagKey, handler := range tagHandlers {
tagValue := handler(matches)
tags = append(tags, tagKey+":"+tagValue)
}
return name, tags
}
mogrifiers = append(mogrifiers, mogrifierEntry{
matcher: re,
handler: func(matches []string) (string, []string) {
name := nameHandler(matches)
tags := make([]string, 0, len(tagHandlers))
for tagKey, handler := range tagHandlers {
tagValue := handler(matches)
tags = append(tags, tagKey+":"+tagValue)
}
return name, tags
},
},
)

}
return mogrifiers, nil
}

// mogrify applies the first mogrifier in the map that matches the name
func (m mogrifierMap) mogrify(name string) (string, []string) {
func (m *mogrifierMap) mogrify(name string) (string, []string) {
if m == nil {
return name, nil
}
for matcher, mogrifier := range m {
matches := matcher.FindStringSubmatch(name)
for _, mogrifier := range *m {
matches := mogrifier.matcher.FindStringSubmatch(name)
if len(matches) == 0 {
continue
}

mogrifiedName, tags := mogrifier(matches)
mogrifiedName, tags := mogrifier.handler(matches)
return mogrifiedName, tags
}

Expand Down
11 changes: 7 additions & 4 deletions src/godogstats/mogrifier_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import (

func testMogrifier() mogrifierMap {
return mogrifierMap{
regexp.MustCompile(`^ratelimit\.service\.rate_limit\.(.*)\.(.*)\.(.*)$`): func(matches []string) (string, []string) {
name := "ratelimit.service.rate_limit." + matches[3]
tags := []string{"domain:" + matches[1], "descriptor:" + matches[2]}
return name, tags
{
matcher: regexp.MustCompile(`^ratelimit\.service\.rate_limit\.(.*)\.(.*)\.(.*)$`),
handler: func(matches []string) (string, []string) {
name := "ratelimit.service.rate_limit." + matches[3]
tags := []string{"domain:" + matches[1], "descriptor:" + matches[2]}
return name, tags
},
},
}
}
Expand Down