Merge pull request #1226 from prometheus/rlbldef

Add new defaults for relabel configurations
This commit is contained in:
Fabian Reinartz 2015-11-17 15:18:21 +01:00
commit 6c3a2eab7c
4 changed files with 40 additions and 28 deletions

View file

@ -93,6 +93,8 @@ var (
DefaultRelabelConfig = RelabelConfig{ DefaultRelabelConfig = RelabelConfig{
Action: RelabelReplace, Action: RelabelReplace,
Separator: ";", Separator: ";",
Regex: MustNewRegexp("(.*)"),
Replacement: "$1",
} }
// DefaultDNSSDConfig is the default DNS SD configuration. // DefaultDNSSDConfig is the default DNS SD configuration.
@ -769,7 +771,7 @@ type RelabelConfig struct {
// Separator is the string between concatenated values from the source labels. // Separator is the string between concatenated values from the source labels.
Separator string `yaml:"separator,omitempty"` Separator string `yaml:"separator,omitempty"`
// Regex against which the concatenation is matched. // Regex against which the concatenation is matched.
Regex *Regexp `yaml:"regex,omitempty"` Regex Regexp `yaml:"regex,omitempty"`
// Modulus to take of the hash of concatenated values from the source labels. // Modulus to take of the hash of concatenated values from the source labels.
Modulus uint64 `yaml:"modulus,omitempty"` Modulus uint64 `yaml:"modulus,omitempty"`
// The label to which the resulting string is written in a replacement. // The label to which the resulting string is written in a replacement.
@ -790,9 +792,6 @@ func (c *RelabelConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := unmarshal((*plain)(c)); err != nil { if err := unmarshal((*plain)(c)); err != nil {
return err return err
} }
if c.Regex == nil && c.Action != RelabelHashMod {
return fmt.Errorf("relabel configuration requires a regular expression")
}
if c.Modulus == 0 && c.Action == RelabelHashMod { if c.Modulus == 0 && c.Action == RelabelHashMod {
return fmt.Errorf("relabel configuration for hashmod requires non-zero modulus") return fmt.Errorf("relabel configuration for hashmod requires non-zero modulus")
} }
@ -801,25 +800,22 @@ func (c *RelabelConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
// Regexp encapsulates a regexp.Regexp and makes it YAML marshallable. // Regexp encapsulates a regexp.Regexp and makes it YAML marshallable.
type Regexp struct { type Regexp struct {
regexp.Regexp *regexp.Regexp
original string original string
} }
// NewRegexp creates a new anchored Regexp and returns an error if the // NewRegexp creates a new anchored Regexp and returns an error if the
// passed-in regular expression does not compile. // passed-in regular expression does not compile.
func NewRegexp(s string) (*Regexp, error) { func NewRegexp(s string) (Regexp, error) {
regex, err := regexp.Compile("^(?:" + s + ")$") regex, err := regexp.Compile("^(?:" + s + ")$")
if err != nil { return Regexp{
return nil, err Regexp: regex,
}
return &Regexp{
Regexp: *regex,
original: s, original: s,
}, nil }, err
} }
// MustNewRegexp works like NewRegexp, but panics if the regular expression does not compile. // MustNewRegexp works like NewRegexp, but panics if the regular expression does not compile.
func MustNewRegexp(s string) *Regexp { func MustNewRegexp(s string) Regexp {
re, err := NewRegexp(s) re, err := NewRegexp(s)
if err != nil { if err != nil {
panic(err) panic(err)
@ -837,13 +833,13 @@ func (re *Regexp) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err != nil { if err != nil {
return err return err
} }
*re = *r *re = r
return nil return nil
} }
// MarshalYAML implements the yaml.Marshaler interface. // MarshalYAML implements the yaml.Marshaler interface.
func (re *Regexp) MarshalYAML() (interface{}, error) { func (re Regexp) MarshalYAML() (interface{}, error) {
if re != nil { if re.original != "" {
return re.original, nil return re.original, nil
} }
return nil, nil return nil, nil

View file

@ -89,6 +89,19 @@ var expectedConf = &Config{
Regex: MustNewRegexp("(.*)some-[regex]"), Regex: MustNewRegexp("(.*)some-[regex]"),
Replacement: "foo-${1}", Replacement: "foo-${1}",
Action: RelabelReplace, Action: RelabelReplace,
}, {
SourceLabels: model.LabelNames{"abc"},
TargetLabel: "cde",
Separator: ";",
Regex: DefaultRelabelConfig.Regex,
Replacement: DefaultRelabelConfig.Replacement,
Action: RelabelReplace,
}, {
TargetLabel: "abc",
Separator: ";",
Regex: DefaultRelabelConfig.Regex,
Replacement: "static",
Action: RelabelReplace,
}, },
}, },
}, },
@ -128,11 +141,14 @@ var expectedConf = &Config{
SourceLabels: model.LabelNames{"job"}, SourceLabels: model.LabelNames{"job"},
Regex: MustNewRegexp("(.*)some-[regex]"), Regex: MustNewRegexp("(.*)some-[regex]"),
Separator: ";", Separator: ";",
Replacement: DefaultRelabelConfig.Replacement,
Action: RelabelDrop, Action: RelabelDrop,
}, },
{ {
SourceLabels: model.LabelNames{"__address__"}, SourceLabels: model.LabelNames{"__address__"},
TargetLabel: "__tmp_hash", TargetLabel: "__tmp_hash",
Regex: DefaultRelabelConfig.Regex,
Replacement: DefaultRelabelConfig.Replacement,
Modulus: 8, Modulus: 8,
Separator: ";", Separator: ";",
Action: RelabelHashMod, Action: RelabelHashMod,
@ -141,11 +157,13 @@ var expectedConf = &Config{
SourceLabels: model.LabelNames{"__tmp_hash"}, SourceLabels: model.LabelNames{"__tmp_hash"},
Regex: MustNewRegexp("1"), Regex: MustNewRegexp("1"),
Separator: ";", Separator: ";",
Replacement: DefaultRelabelConfig.Replacement,
Action: RelabelKeep, Action: RelabelKeep,
}, },
{ {
Regex: MustNewRegexp("1"), Regex: MustNewRegexp("1"),
Separator: ";", Separator: ";",
Replacement: DefaultRelabelConfig.Replacement,
Action: RelabelLabelMap, Action: RelabelLabelMap,
}, },
}, },
@ -154,6 +172,7 @@ var expectedConf = &Config{
SourceLabels: model.LabelNames{"__name__"}, SourceLabels: model.LabelNames{"__name__"},
Regex: MustNewRegexp("expensive_metric.*"), Regex: MustNewRegexp("expensive_metric.*"),
Separator: ";", Separator: ";",
Replacement: DefaultRelabelConfig.Replacement,
Action: RelabelDrop, Action: RelabelDrop,
}, },
}, },
@ -308,9 +327,6 @@ var expectedErrors = []struct {
}, { }, {
filename: "regex.bad.yml", filename: "regex.bad.yml",
errMsg: "error parsing regexp", errMsg: "error parsing regexp",
}, {
filename: "regex_missing.bad.yml",
errMsg: "relabel configuration requires a regular expression",
}, { }, {
filename: "modulus_missing.bad.yml", filename: "modulus_missing.bad.yml",
errMsg: "relabel configuration for hashmod requires non-zero modulus", errMsg: "relabel configuration for hashmod requires non-zero modulus",

View file

@ -44,6 +44,10 @@ scrape_configs:
target_label: job target_label: job
replacement: foo-${1} replacement: foo-${1}
# action defaults to 'replace' # action defaults to 'replace'
- source_labels: [abc]
target_label: cde
- replacement: static
target_label: abc
bearer_token_file: valid_token_file bearer_token_file: valid_token_file

View file

@ -1,4 +0,0 @@
scrape_configs:
- job_name: prometheus
relabel_configs:
- source_labels: ['blub']