Add support for InfluxDB style tagging

Signed-off-by: Tony Wooster <twooster@gmail.com>
This commit is contained in:
Tony Wooster 2019-09-17 16:52:29 +02:00
parent 2933dd8ad0
commit 5537c504cc
2 changed files with 32 additions and 8 deletions

View file

@ -147,6 +147,26 @@ func TestHandlePacket(t *testing.T) {
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",
in: "foo:100|c|#tag1:bar,tag2:baz",

View file

@ -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
if len(tag) == 0 {
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)
}
func parseLibratoTags(component string, labels map[string]string) {
func parseNameTag(component string, labels map[string]string) {
lastTagEndIndex := 0
for i, c := range component {
if c == ',' {
tag := component[lastTagEndIndex:i]
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 lastTagEndIndex < len(component) {
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 {
if c == '#' {
parseLibratoTags(name[i+1:], labels)
// # delimiting start of tags by Librato
// 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]
}
}
@ -394,7 +398,7 @@ func lineToEvents(line string) Events {
}
labels := map[string]string{}
metric := parseNameAndLibratoLabels(elements[0], labels)
metric := parseNameAndLabels(elements[0], labels)
var samples []string
if strings.Contains(elements[1], "|#") {
// using datadog extensions, disable multi-metrics