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

replace whole value instead of only matches for relabeling #118

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 3 additions & 1 deletion relabeling/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func (r *Relabeling) Map(sourceValue string) (string, error) {
replacement := ""
for i := range r.Matches {
if r.Matches[i].CompiledRegexp.MatchString(sourceValue) {
replacement = r.Matches[i].CompiledRegexp.ReplaceAllString(sourceValue, r.Matches[i].Replacement)
replacement = r.Matches[i].CompiledRegexp.ReplaceAllString(
r.Matches[i].CompiledRegexp.FindString(sourceValue),
r.Matches[i].Replacement)
break
}
}
Expand Down
17 changes: 17 additions & 0 deletions relabeling/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,21 @@ func TestRequestURIMapping(t *testing.T) {
}

assertMapping(t, r, "GET /users/12345 HTTP/1.1", "/users/:id")
assertMapping(t, r, "GET /users/12345/about HTTP/1.1", "/users/:id")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh. To be honest, this seems counterintuitive to me. I would have expected /users/12345/about to be mapped to /users/:id/about (or, in other words, I would not expect for [0-9]+ to replace anything more than 12345).

Alternatively, one could also have used ^/users/[0-9]+.* as a regex, which would effectively match the entire string and yield the same result.

}

func TestAgentMapping(t *testing.T) {
t.Parallel()

r, err := buildRelabeling(config.RelabelConfig{
Split: 0,
Matches: []config.RelabelValueMatch{
{RegexpString: "(Firefox)/(\\d+)\\.(\\d+)(pre|[ab]\\d+[a-z]*|)", Replacement: "$1"},
},
})
if err != nil {
t.Error(err)
}

assertMapping(t, r, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0", "Firefox")
}