forked from mirrors/statsd_exporter
Add support for InfluxDB style tagging
Signed-off-by: Tony Wooster <twooster@gmail.com>
This commit is contained in:
parent
2933dd8ad0
commit
5537c504cc
2 changed files with 32 additions and 8 deletions
|
@ -147,6 +147,26 @@ func TestHandlePacket(t *testing.T) {
|
||||||
labels: map[string]string{"_09digits": "0", "tag_with_dots": "1"},
|
labels: map[string]string{"_09digits": "0", "tag_with_dots": "1"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
}, {
|
||||||
|
name: "influxdb tag extension",
|
||||||
|
in: "foo#tag1=bar,tag2=baz:100|c",
|
||||||
|
out: Events{
|
||||||
|
&CounterEvent{
|
||||||
|
metricName: "foo",
|
||||||
|
value: 100,
|
||||||
|
labels: map[string]string{"tag1": "bar", "tag2": "baz"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
name: "influxdb tag extension with tag keys unsupported by prometheus",
|
||||||
|
in: "foo#09digits=0,tag.with.dots=1:100|c",
|
||||||
|
out: Events{
|
||||||
|
&CounterEvent{
|
||||||
|
metricName: "foo",
|
||||||
|
value: 100,
|
||||||
|
labels: map[string]string{"_09digits": "0", "tag_with_dots": "1"},
|
||||||
|
},
|
||||||
|
},
|
||||||
}, {
|
}, {
|
||||||
name: "datadog tag extension",
|
name: "datadog tag extension",
|
||||||
in: "foo:100|c|#tag1:bar,tag2:baz",
|
in: "foo:100|c|#tag1:bar,tag2:baz",
|
||||||
|
|
20
exporter.go
20
exporter.go
|
@ -275,7 +275,7 @@ func buildEvent(statType, metric string, value float64, relative bool, labels ma
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleLibratoTag(component, tag string, labels map[string]string) {
|
func extractNameTag(component, tag string, labels map[string]string) {
|
||||||
// Empty tag is an error
|
// Empty tag is an error
|
||||||
if len(tag) == 0 {
|
if len(tag) == 0 {
|
||||||
tagErrors.Inc()
|
tagErrors.Inc()
|
||||||
|
@ -304,27 +304,31 @@ func handleLibratoTag(component, tag string, labels map[string]string) {
|
||||||
log.Debugf("Malformed Librato tag %s in component %s", tag, component)
|
log.Debugf("Malformed Librato tag %s in component %s", tag, component)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseLibratoTags(component string, labels map[string]string) {
|
func parseNameTag(component string, labels map[string]string) {
|
||||||
lastTagEndIndex := 0
|
lastTagEndIndex := 0
|
||||||
for i, c := range component {
|
for i, c := range component {
|
||||||
if c == ',' {
|
if c == ',' {
|
||||||
tag := component[lastTagEndIndex:i]
|
tag := component[lastTagEndIndex:i]
|
||||||
lastTagEndIndex = i + 1
|
lastTagEndIndex = i + 1
|
||||||
handleLibratoTag(component, tag, labels)
|
extractNameTag(component, tag, labels)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we're not off the end of the string, add the last tag
|
// If we're not off the end of the string, add the last tag
|
||||||
if lastTagEndIndex < len(component) {
|
if lastTagEndIndex < len(component) {
|
||||||
tag := component[lastTagEndIndex:]
|
tag := component[lastTagEndIndex:]
|
||||||
handleLibratoTag(component, tag, labels)
|
extractNameTag(component, tag, labels)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseNameAndLibratoLabels(name string, labels map[string]string) string {
|
func parseNameAndLabels(name string, labels map[string]string) string {
|
||||||
for i, c := range name {
|
for i, c := range name {
|
||||||
if c == '#' {
|
// # delimiting start of tags by Librato
|
||||||
parseLibratoTags(name[i+1:], labels)
|
// https://www.librato.com/docs/kb/collect/collection_agents/stastd/#stat-level-tags
|
||||||
|
// , delimiting start of tags by InfluxDB
|
||||||
|
// https://www.influxdata.com/blog/getting-started-with-sending-statsd-metrics-to-telegraf-influxdb/#introducing-influx-statsd
|
||||||
|
if c == '#' || c == ',' {
|
||||||
|
parseNameTag(name[i+1:], labels)
|
||||||
return name[:i]
|
return name[:i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -394,7 +398,7 @@ func lineToEvents(line string) Events {
|
||||||
}
|
}
|
||||||
|
|
||||||
labels := map[string]string{}
|
labels := map[string]string{}
|
||||||
metric := parseNameAndLibratoLabels(elements[0], labels)
|
metric := parseNameAndLabels(elements[0], labels)
|
||||||
var samples []string
|
var samples []string
|
||||||
if strings.Contains(elements[1], "|#") {
|
if strings.Contains(elements[1], "|#") {
|
||||||
// using datadog extensions, disable multi-metrics
|
// using datadog extensions, disable multi-metrics
|
||||||
|
|
Loading…
Reference in a new issue