Add consul service tags to metric (#4155)

This commit is contained in:
Leszek Charkiewicz
2018-05-17 23:24:51 +02:00
committed by Daniel Nelson
parent 2948dec6f5
commit 6d73cb85cc
4 changed files with 63 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ package consul
import (
"net/http"
"strings"
"github.com/hashicorp/consul/api"
"github.com/influxdata/telegraf"
@@ -17,6 +18,7 @@ type Consul struct {
Password string
Datacentre string
tls.ClientConfig
TagDelimiter string
// client used to connect to Consul agnet
client *api.Client
@@ -45,6 +47,11 @@ var sampleConfig = `
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = true
## Consul checks' tag splitting
# When tags are formatted like "key:value" with ":" as a delimiter then
# they will be splitted and reported as proper key:value in Telegraf
# tag_delimiter = ":"
`
func (c *Consul) Description() string {
@@ -86,7 +93,7 @@ func (c *Consul) createAPIClient() (*api.Client, error) {
return nil, err
}
config.HttpClient.Transport = &http.Transport{
config.Transport = &http.Transport{
TLSClientConfig: tlsCfg,
}
@@ -111,6 +118,19 @@ func (c *Consul) GatherHealthCheck(acc telegraf.Accumulator, checks []*api.Healt
tags["service_name"] = check.ServiceName
tags["check_id"] = check.CheckID
for _, checkTag := range check.ServiceTags {
if c.TagDelimiter != "" {
splittedTag := strings.SplitN(checkTag, c.TagDelimiter, 2)
if len(splittedTag) == 1 {
tags[checkTag] = checkTag
} else if len(splittedTag) == 2 {
tags[splittedTag[0]] = splittedTag[1]
}
} else {
tags[checkTag] = checkTag
}
}
acc.AddFields("consul_health_checks", record, tags)
}
}