Skip to content

Commit

Permalink
Merge pull request #5419 from m4ce/master
Browse files Browse the repository at this point in the history
Adding support for matching tags multiple times in Graphite parser
  • Loading branch information
jwilder committed Feb 9, 2016
2 parents efc5de0 + b26859a commit 83b96b3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
12 changes: 6 additions & 6 deletions services/graphite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ The special value _measurement_ is used to define the measurement name. It can
* Template: `.host.resource.measurement*`
* Output: _measurement_ =`loadavg.10` _tags_ =`host=localhost resource=cpu`

### Multiple Measurement Matching
### Multiple Measurement & Tags Matching

The _measurement_ can be specified multiple times in a template to provide more control over the measurement name. Multiple values
will be joined together using the _Separator_ config variable. By default, this value is `.`.
The _measurement_ can be specified multiple times in a template to provide more control over the measurement name. Tags can also be
matched multiple times. Multiple values will be joined together using the _Separator_ config variable. By default, this value is `.`.

`servers.localhost.cpu.cpu0.user`
* Template: `.host.measurement.cpu.measurement`
* Output: _measurement_ = `cpu.user` _tags_ = `host=localhost cpu=cpu0`
`servers.localhost.localdomain.cpu.cpu0.user`
* Template: `.host.host.measurement.cpu.measurement`
* Output: _measurement_ = `cpu.user` _tags_ = `host=localhost.localdomain cpu=cpu0`

Since '.' requires queries on measurements to be double-quoted, you may want to set this to `_` to simplify querying parsed metrics.

Expand Down
14 changes: 10 additions & 4 deletions services/graphite/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ func (t *template) Apply(line string) (string, map[string]string, string, error)
fields := strings.Split(line, ".")
var (
measurement []string
tags = make(map[string]string)
tags = make(map[string][]string)
field string
)

// Set any default tags
for k, v := range t.defaultTags {
tags[k] = v
tags[k] = append(tags[k], v)
}

for i, tag := range t.tags {
Expand All @@ -241,11 +241,17 @@ func (t *template) Apply(line string) (string, map[string]string, string, error)
measurement = append(measurement, fields[i:]...)
break
} else if tag != "" {
tags[tag] = fields[i]
tags[tag] = append(tags[tag], fields[i])
}
}

return strings.Join(measurement, t.separator), tags, field, nil
// Convert to map of strings.
out_tags := make(map[string]string)
for k, values := range tags {
out_tags[k] = strings.Join(values, t.separator)
}

return strings.Join(measurement, t.separator), out_tags, field, nil
}

// matcher determines which template should be applied to a given metric
Expand Down
7 changes: 7 additions & 0 deletions services/graphite/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ func TestTemplateApply(t *testing.T) {
measurement: "cpu",
tags: map[string]string{"hostname": "server01", "region": "us-west"},
},
{
test: "metric with multiple tags",
input: "server01.example.org.cpu.us-west",
template: "hostname.hostname.hostname.measurement.region",
measurement: "cpu",
tags: map[string]string{"hostname": "server01.example.org", "region": "us-west"},
},
{
test: "no metric",
tags: make(map[string]string),
Expand Down

0 comments on commit 83b96b3

Please sign in to comment.