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 full regex support for mapping configuration #21

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 17 additions & 8 deletions mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,24 @@ func (m *metricMapper) initFromString(fileContents string) error {
continue
}

if !metricLineRE.MatchString(line) {
return fmt.Errorf("Line %d: expected metric match line, got: %s", i, line)
}
if strings.HasPrefix(line, "~") {
metricRe := strings.TrimPrefix(line, "~")
var err error
currentMapping.regex, err = regexp.Compile(metricRe)
if err != nil {
return fmt.Errorf("Line %d: error compiling regex %s: %s", i, metricRe, err)
}
} else {
if !metricLineRE.MatchString(line) {
return fmt.Errorf("Line %d: expected metric match line, got: %s", i, line)
}

// Translate the glob-style metric match line into a proper regex that we
// can use to match metrics later on.
metricRe := strings.Replace(line, ".", "\\.", -1)
metricRe = strings.Replace(metricRe, "*", "([^.]+)", -1)
currentMapping.regex = regexp.MustCompile("^" + metricRe + "$")
// Translate the glob-style metric match line into a proper regex that we
// can use to match metrics later on.
metricRe := strings.Replace(line, ".", "\\.", -1)
metricRe = strings.Replace(metricRe, "*", "([^.]+)", -1)
currentMapping.regex = regexp.MustCompile("^" + metricRe + "$")
}

state = METRIC_DEFINITION

Expand Down
50 changes: 50 additions & 0 deletions mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,56 @@ func TestMetricMapper(t *testing.T) {
`,
configBad: true,
},
// Config with full regex.
{
config: `
~^test\.dispatcher\.(.*)\.(.*)\.(.*)$
name="dispatch_events"
processor="$1"
action="$2"
result="$3"
job="test_dispatcher"
`,
mappings: map[string]map[string]string{
"test.dispatcher.FooProcessor.send.succeeded": {
"name": "dispatch_events",
"processor": "FooProcessor",
"action": "send",
"result": "succeeded",
"job": "test_dispatcher",
},
"foo.bar.baz": {},
},
},
// Config with full regex with bad regex
{
config: `
~^test\.dispatcher\.(.*)\.(*)\.(.*)$
name="dispatch_events"
processor="$1"
action="$2"
result="$3"
job="test_dispatcher"
`,
configBad: true,
},
// Config with full regex and partial match.
{
config: `
~^test\.dispatcher\.event-(.*?)\..*$
name="dispatch_events"
event="$1"
job="test_dispatcher"
`,
mappings: map[string]map[string]string{
"test.dispatcher.event-FooProcessor.send.succeeded": {
"name": "dispatch_events",
"event": "FooProcessor",
"job": "test_dispatcher",
},
"foo.bar.baz": {},
},
},
}

mapper := metricMapper{}
Expand Down