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

@ -91,8 +91,10 @@ var (
// DefaultRelabelConfig is the default Relabel configuration.
DefaultRelabelConfig = RelabelConfig{
Action: RelabelReplace,
Separator: ";",
Action: RelabelReplace,
Separator: ";",
Regex: MustNewRegexp("(.*)"),
Replacement: "$1",
}
// 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 string `yaml:"separator,omitempty"`
// 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 uint64 `yaml:"modulus,omitempty"`
// 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 {
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 {
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.
type Regexp struct {
regexp.Regexp
*regexp.Regexp
original string
}
// NewRegexp creates a new anchored Regexp and returns an error if the
// passed-in regular expression does not compile.
func NewRegexp(s string) (*Regexp, error) {
func NewRegexp(s string) (Regexp, error) {
regex, err := regexp.Compile("^(?:" + s + ")$")
if err != nil {
return nil, err
}
return &Regexp{
Regexp: *regex,
return Regexp{
Regexp: regex,
original: s,
}, nil
}, err
}
// 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)
if err != nil {
panic(err)
@ -837,13 +833,13 @@ func (re *Regexp) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err != nil {
return err
}
*re = *r
*re = r
return nil
}
// MarshalYAML implements the yaml.Marshaler interface.
func (re *Regexp) MarshalYAML() (interface{}, error) {
if re != nil {
func (re Regexp) MarshalYAML() (interface{}, error) {
if re.original != "" {
return re.original, nil
}
return nil, nil

View File

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

View File

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

View File

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