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

Add default for relabeling #408

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ discussion around the reasoning.
Dynamic relabeling also allows you to aggregate your metrics by request path (which replaces
the experimental feature originally introduced in #23). The following example splits the content of
the `request` variable at every space (using `split`) and return the second element (index 1) of the
resulting list which is the base for the regex):
resulting list which is the base for the regex. If nothing was found by regexp, the default value will be returned. If the source variable is empty, you can specify the default_for_empty_from parameter.:

[source,hcl]
----
Expand All @@ -506,6 +506,8 @@ namespace "app1" {

match "^/users/[0-9]+" {
replacement = "/users/:id"
default = "/users/unknown"
default_for_empty_from = "error"
}

match "^/profile" {
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/struct_relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type RelabelConfig struct {
type RelabelValueMatch struct {
RegexpString string `hcl:",key" yaml:"regexp"`
Replacement string `hcl:"replacement"`
DefaultValue string `hcl:"default"`
DefaultIfSourceEmpty string `hcl:"default_for_empty_from"`

CompiledRegexp *regexp.Regexp
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/relabeling/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,23 @@ func (r *Relabeling) Map(sourceValue string) (string, error) {
return "other", nil
}



if len(r.Matches) > 0 {
replacement := ""
for i := range r.Matches {

if r.Matches[i].CompiledRegexp.MatchString(sourceValue) {
replacement = r.Matches[i].CompiledRegexp.ReplaceAllString(sourceValue, r.Matches[i].Replacement)
break
} else if r.Matches[i].DefaultValue != "" {
replacement = r.Matches[i].DefaultValue
}

if len(sourceValue) == 0 && r.Matches[i].DefaultIfSourceEmpty != "" {
replacement = r.Matches[i].DefaultIfSourceEmpty
}

}
sourceValue = replacement
}
Expand Down