Skip to content

Commit

Permalink
Add default for relabeling
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Ostashov committed Feb 4, 2025
1 parent 7422e87 commit c5ce768
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
3 changes: 2 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.:

[source,hcl]
----
Expand All @@ -506,6 +506,7 @@ namespace "app1" {
match "^/users/[0-9]+" {
replacement = "/users/:id"
default = "/users/unknown"
}
match "^/profile" {
Expand Down
1 change: 1 addition & 0 deletions pkg/config/struct_relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type RelabelConfig struct {
type RelabelValueMatch struct {
RegexpString string `hcl:",key" yaml:"regexp"`
Replacement string `hcl:"replacement"`
DefaultValue string `hcl:"default"`

CompiledRegexp *regexp.Regexp
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/relabeling/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func (r *Relabeling) Map(sourceValue string) (string, error) {
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
}
}
sourceValue = replacement
Expand Down

0 comments on commit c5ce768

Please sign in to comment.