Skip to content

Commit d64e171

Browse files
author
Vitaly Ostashov
committed
Add default for relabeling
1 parent 7422e87 commit d64e171

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

README.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ discussion around the reasoning.
489489
Dynamic relabeling also allows you to aggregate your metrics by request path (which replaces
490490
the experimental feature originally introduced in #23). The following example splits the content of
491491
the `request` variable at every space (using `split`) and return the second element (index 1) of the
492-
resulting list which is the base for the regex):
492+
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.:
493493

494494
[source,hcl]
495495
----
@@ -506,6 +506,8 @@ namespace "app1" {
506506
507507
match "^/users/[0-9]+" {
508508
replacement = "/users/:id"
509+
default = "/users/unknown"
510+
default_for_empty_from = "error"
509511
}
510512
511513
match "^/profile" {

pkg/config/struct_relabel.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type RelabelConfig struct {
2424
type RelabelValueMatch struct {
2525
RegexpString string `hcl:",key" yaml:"regexp"`
2626
Replacement string `hcl:"replacement"`
27+
DefaultValue string `hcl:"default"`
28+
DefaultIfSourceEmpty string `hcl:"default_for_empty_from"`
2729

2830
CompiledRegexp *regexp.Regexp
2931
}

pkg/relabeling/mapping.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,23 @@ func (r *Relabeling) Map(sourceValue string) (string, error) {
3030
return "other", nil
3131
}
3232

33+
34+
3335
if len(r.Matches) > 0 {
3436
replacement := ""
3537
for i := range r.Matches {
38+
3639
if r.Matches[i].CompiledRegexp.MatchString(sourceValue) {
3740
replacement = r.Matches[i].CompiledRegexp.ReplaceAllString(sourceValue, r.Matches[i].Replacement)
3841
break
42+
} else if r.Matches[i].DefaultValue != "" {
43+
replacement = r.Matches[i].DefaultValue
3944
}
45+
46+
if len(sourceValue) == 0 && r.Matches[i].DefaultIfSourceEmpty != "" {
47+
replacement = r.Matches[i].DefaultIfSourceEmpty
48+
}
49+
4050
}
4151
sourceValue = replacement
4252
}

0 commit comments

Comments
 (0)