input consul - added metric_version flag (#7875)

Co-authored-by: Вячеслав Митрофанов <vmitrofanov@mfms.ru>
This commit is contained in:
Vyacheslav Mitrofanov 2020-10-07 18:31:02 +03:00 committed by GitHub
parent cc089e6eb6
commit a452feb18d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 111 additions and 7 deletions

View File

@ -17,6 +17,14 @@ report those stats already using StatsD protocol if needed.
## URI scheme for the Consul server, one of "http", "https"
# scheme = "http"
## Metric version controls the mapping from Consul metrics into
## Telegraf metrics. Version 2 moved all fields with string values
## to tags.
##
## example: metric_version = 1; deprecated in 1.16
## metric_version = 2; recommended version
# metric_version = 1
## ACL token used in every request
# token = ""
@ -41,7 +49,7 @@ report those stats already using StatsD protocol if needed.
```
### Metrics:
##### metric_version = 1:
- consul_health_checks
- tags:
- node (node that check/service is registered on)
@ -55,9 +63,23 @@ report those stats already using StatsD protocol if needed.
- critical (integer)
- warning (integer)
##### metric_version = 2:
- consul_health_checks
- tags:
- node (node that check/service is registered on)
- service_name
- check_id
- check_name
- service_id
- status
- fields:
- passing (integer)
- critical (integer)
- warning (integer)
`passing`, `critical`, and `warning` are integer representations of the health
check state. A value of `1` represents that the status was the state of the
the health check at this sample.
the health check at this sample. `status` is string representation of the same state.
## Example output

View File

@ -19,7 +19,9 @@ type Consul struct {
Datacentre string // deprecated in 1.10; use Datacenter
Datacenter string
tls.ClientConfig
TagDelimiter string
TagDelimiter string
MetricVersion int
Log telegraf.Logger
// client used to connect to Consul agnet
client *api.Client
@ -32,6 +34,13 @@ var sampleConfig = `
## URI scheme for the Consul server, one of "http", "https"
# scheme = "http"
## Metric version controls the mapping from Consul metrics into
## Telegraf metrics.
##
## example: metric_version = 1; deprecated in 1.15
## metric_version = 2; recommended version
# metric_version = 1
## ACL token used in every request
# token = ""
@ -55,6 +64,14 @@ var sampleConfig = `
# tag_delimiter = ":"
`
func (c *Consul) Init() error {
if c.MetricVersion != 2 {
c.Log.Warnf("Use of deprecated configuration: 'metric_version = 1'; please update to 'metric_version = 2'")
}
return nil
}
func (c *Consul) Description() string {
return "Gather health check statuses from services registered in Consul"
}
@ -110,15 +127,21 @@ func (c *Consul) GatherHealthCheck(acc telegraf.Accumulator, checks []*api.Healt
record := make(map[string]interface{})
tags := make(map[string]string)
record["check_name"] = check.Name
record["service_id"] = check.ServiceID
record["status"] = check.Status
record["passing"] = 0
record["critical"] = 0
record["warning"] = 0
record[check.Status] = 1
if c.MetricVersion == 2 {
tags["check_name"] = check.Name
tags["service_id"] = check.ServiceID
tags["status"] = check.Status
} else {
record["check_name"] = check.Name
record["service_id"] = check.ServiceID
record["status"] = check.Status
}
tags["node"] = check.Node
tags["service_name"] = check.ServiceName
tags["check_id"] = check.CheckID

View File

@ -76,3 +76,62 @@ func TestGatherHealthCheckWithDelimitedTags(t *testing.T) {
acc.AssertContainsTaggedFields(t, "consul_health_checks", expectedFields, expectedTags)
}
func TestGatherHealthCheckV2(t *testing.T) {
expectedFields := map[string]interface{}{
"passing": 1,
"critical": 0,
"warning": 0,
}
expectedTags := map[string]string{
"node": "localhost",
"check_id": "foo.health123",
"check_name": "foo.health",
"status": "passing",
"service_id": "foo.123",
"service_name": "foo",
"bar": "bar",
"env:sandbox": "env:sandbox",
"tagkey:value:stillvalue": "tagkey:value:stillvalue",
}
var acc testutil.Accumulator
consul := &Consul{
MetricVersion: 2,
}
consul.GatherHealthCheck(&acc, sampleChecks)
acc.AssertContainsTaggedFields(t, "consul_health_checks", expectedFields, expectedTags)
}
func TestGatherHealthCheckWithDelimitedTagsV2(t *testing.T) {
expectedFields := map[string]interface{}{
"passing": 1,
"critical": 0,
"warning": 0,
}
expectedTags := map[string]string{
"node": "localhost",
"check_id": "foo.health123",
"check_name": "foo.health",
"status": "passing",
"service_id": "foo.123",
"service_name": "foo",
"bar": "bar",
"env": "sandbox",
"tagkey": "value:stillvalue",
}
var acc testutil.Accumulator
consul := &Consul{
MetricVersion: 2,
TagDelimiter: ":",
}
consul.GatherHealthCheck(&acc, sampleChecks)
acc.AssertContainsTaggedFields(t, "consul_health_checks", expectedFields, expectedTags)
}