Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e393c3af3f
commit
4978089aab
4963 changed files with 677545 additions and 0 deletions
50
plugins/inputs/consul_agent/README.md
Normal file
50
plugins/inputs/consul_agent/README.md
Normal file
|
@ -0,0 +1,50 @@
|
|||
# Hashicorp Consul Agent Input Plugin
|
||||
|
||||
This plugin collects metrics from a [Consul agent][agent]. Telegraf may be
|
||||
present in every node and connect to the agent locally. Tested on Consul v1.10.
|
||||
|
||||
⭐ Telegraf v1.22.0
|
||||
🏷️ server
|
||||
💻 all
|
||||
|
||||
[agent]: https://developer.hashicorp.com/consul/commands/agent
|
||||
|
||||
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
|
||||
|
||||
In addition to the plugin-specific configuration settings, plugins support
|
||||
additional global and plugin configuration settings. These settings are used to
|
||||
modify metrics, tags, and field or create aliases and configure ordering, etc.
|
||||
See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
||||
|
||||
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
|
||||
|
||||
## Configuration
|
||||
|
||||
```toml @sample.conf
|
||||
# Read metrics from the Consul Agent API
|
||||
[[inputs.consul_agent]]
|
||||
## URL for the Consul agent
|
||||
# url = "http://127.0.0.1:8500"
|
||||
|
||||
## Use auth token for authorization.
|
||||
## If both are set, an error is thrown.
|
||||
## If both are empty, no token will be used.
|
||||
# token_file = "/path/to/auth/token"
|
||||
## OR
|
||||
# token = "a1234567-40c7-9048-7bae-378687048181"
|
||||
|
||||
## Set timeout (default 5 seconds)
|
||||
# timeout = "5s"
|
||||
|
||||
## Optional TLS Config
|
||||
# tls_ca = /path/to/cafile
|
||||
# tls_cert = /path/to/certfile
|
||||
# tls_key = /path/to/keyfile
|
||||
```
|
||||
|
||||
## Metrics
|
||||
|
||||
Consul collects various metrics. For every details, please have a look at
|
||||
[Consul's documentation](https://www.consul.io/api/agent#view-metrics).
|
||||
|
||||
## Example Output
|
175
plugins/inputs/consul_agent/consul_agent.go
Normal file
175
plugins/inputs/consul_agent/consul_agent.go
Normal file
|
@ -0,0 +1,175 @@
|
|||
//go:generate ../../../tools/readme_config_includer/generator
|
||||
package consul_agent
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/config"
|
||||
"github.com/influxdata/telegraf/internal"
|
||||
"github.com/influxdata/telegraf/plugins/common/tls"
|
||||
"github.com/influxdata/telegraf/plugins/inputs"
|
||||
)
|
||||
|
||||
//go:embed sample.conf
|
||||
var sampleConfig string
|
||||
|
||||
const timeLayout = "2006-01-02 15:04:05 -0700 MST"
|
||||
|
||||
type ConsulAgent struct {
|
||||
URL string `toml:"url"`
|
||||
|
||||
TokenFile string `toml:"token_file"`
|
||||
Token string `toml:"token"`
|
||||
|
||||
ResponseTimeout config.Duration `toml:"timeout"`
|
||||
|
||||
tls.ClientConfig
|
||||
|
||||
roundTripper http.RoundTripper
|
||||
}
|
||||
|
||||
func (*ConsulAgent) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
|
||||
func (n *ConsulAgent) Init() error {
|
||||
if n.URL == "" {
|
||||
n.URL = "http://127.0.0.1:8500"
|
||||
}
|
||||
|
||||
if n.TokenFile != "" && n.Token != "" {
|
||||
return errors.New("config error: both token_file and token are set")
|
||||
}
|
||||
|
||||
if n.TokenFile != "" {
|
||||
token, err := os.ReadFile(n.TokenFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading file failed: %w", err)
|
||||
}
|
||||
n.Token = strings.TrimSpace(string(token))
|
||||
}
|
||||
|
||||
tlsCfg, err := n.ClientConfig.TLSConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("setting up TLS configuration failed: %w", err)
|
||||
}
|
||||
|
||||
n.roundTripper = &http.Transport{
|
||||
TLSHandshakeTimeout: time.Duration(n.ResponseTimeout),
|
||||
TLSClientConfig: tlsCfg,
|
||||
ResponseHeaderTimeout: time.Duration(n.ResponseTimeout),
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *ConsulAgent) Gather(acc telegraf.Accumulator) error {
|
||||
summaryMetrics, err := n.loadJSON(n.URL + "/v1/agent/metrics")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return buildConsulAgent(acc, summaryMetrics)
|
||||
}
|
||||
|
||||
func (n *ConsulAgent) loadJSON(url string) (*agentInfo, error) {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("X-Consul-Token", n.Token)
|
||||
req.Header.Add("Accept", "application/json")
|
||||
|
||||
resp, err := n.roundTripper.RoundTrip(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error making HTTP request to %q: %w", url, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("%s returned HTTP status %s", url, resp.Status)
|
||||
}
|
||||
|
||||
var metrics agentInfo
|
||||
err = json.NewDecoder(resp.Body).Decode(&metrics)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing json response: %w", err)
|
||||
}
|
||||
|
||||
return &metrics, nil
|
||||
}
|
||||
|
||||
// buildConsulAgent, it builds all the metrics and adds them to the accumulator)
|
||||
func buildConsulAgent(acc telegraf.Accumulator, agentInfo *agentInfo) error {
|
||||
t, err := internal.ParseTimestamp(timeLayout, agentInfo.Timestamp, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing time: %w", err)
|
||||
}
|
||||
|
||||
for _, counters := range agentInfo.Counters {
|
||||
fields := map[string]interface{}{
|
||||
"count": counters.Count,
|
||||
"sum": counters.Sum,
|
||||
"max": counters.Max,
|
||||
"mean": counters.Mean,
|
||||
"min": counters.Min,
|
||||
"rate": counters.Rate,
|
||||
"stddev": counters.Stddev,
|
||||
}
|
||||
tags := counters.Labels
|
||||
|
||||
acc.AddCounter(counters.Name, fields, tags, t)
|
||||
}
|
||||
|
||||
for _, gauges := range agentInfo.Gauges {
|
||||
fields := map[string]interface{}{
|
||||
"value": gauges.Value,
|
||||
}
|
||||
tags := gauges.Labels
|
||||
|
||||
acc.AddGauge(gauges.Name, fields, tags, t)
|
||||
}
|
||||
|
||||
for _, points := range agentInfo.Points {
|
||||
fields := map[string]interface{}{
|
||||
"value": points.Points,
|
||||
}
|
||||
tags := make(map[string]string)
|
||||
|
||||
acc.AddFields(points.Name, fields, tags, t)
|
||||
}
|
||||
|
||||
for _, samples := range agentInfo.Samples {
|
||||
fields := map[string]interface{}{
|
||||
"count": samples.Count,
|
||||
"sum": samples.Sum,
|
||||
"max": samples.Max,
|
||||
"mean": samples.Mean,
|
||||
"min": samples.Min,
|
||||
"rate": samples.Rate,
|
||||
"stddev": samples.Stddev,
|
||||
}
|
||||
tags := samples.Labels
|
||||
|
||||
acc.AddCounter(samples.Name, fields, tags, t)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
inputs.Add("consul_agent", func() telegraf.Input {
|
||||
return &ConsulAgent{
|
||||
ResponseTimeout: config.Duration(5 * time.Second),
|
||||
}
|
||||
})
|
||||
}
|
106
plugins/inputs/consul_agent/consul_agent_test.go
Normal file
106
plugins/inputs/consul_agent/consul_agent_test.go
Normal file
|
@ -0,0 +1,106 @@
|
|||
package consul_agent
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
)
|
||||
|
||||
func TestConsulStats(t *testing.T) {
|
||||
var applyTests = []struct {
|
||||
name string
|
||||
expected []telegraf.Metric
|
||||
}{
|
||||
{
|
||||
name: "Metrics",
|
||||
expected: []telegraf.Metric{
|
||||
testutil.MustMetric(
|
||||
"consul.rpc.request",
|
||||
map[string]string{},
|
||||
map[string]interface{}{
|
||||
"count": int(5),
|
||||
"max": float64(1),
|
||||
"mean": float64(1),
|
||||
"min": float64(1),
|
||||
"rate": float64(0.5),
|
||||
"stddev": float64(0),
|
||||
"sum": float64(5),
|
||||
},
|
||||
time.Unix(1639218930, 0),
|
||||
1,
|
||||
),
|
||||
testutil.MustMetric(
|
||||
"consul.consul.members.clients",
|
||||
map[string]string{
|
||||
"datacenter": "dc1",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"value": float64(0),
|
||||
},
|
||||
time.Unix(1639218930, 0),
|
||||
2,
|
||||
),
|
||||
testutil.MustMetric(
|
||||
"consul.api.http",
|
||||
map[string]string{
|
||||
"method": "GET",
|
||||
"path": "v1_agent_self",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"count": int(1),
|
||||
"max": float64(4.14815616607666),
|
||||
"mean": float64(4.14815616607666),
|
||||
"min": float64(4.14815616607666),
|
||||
"rate": float64(0.414815616607666),
|
||||
"stddev": float64(0),
|
||||
"sum": float64(4.14815616607666),
|
||||
},
|
||||
time.Unix(1639218930, 0),
|
||||
1,
|
||||
),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range applyTests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.RequestURI == "/v1/agent/metrics" {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
responseKeyMetrics, err := os.ReadFile("testdata/response_key_metrics.json")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if _, err = fmt.Fprintln(w, string(responseKeyMetrics)); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
plugin := &ConsulAgent{
|
||||
URL: ts.URL,
|
||||
}
|
||||
err := plugin.Init()
|
||||
require.NoError(t, err)
|
||||
|
||||
acc := testutil.Accumulator{}
|
||||
err = plugin.Gather(&acc)
|
||||
require.NoError(t, err)
|
||||
|
||||
testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics())
|
||||
})
|
||||
}
|
||||
}
|
32
plugins/inputs/consul_agent/consul_structs.go
Normal file
32
plugins/inputs/consul_agent/consul_structs.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package consul_agent
|
||||
|
||||
type agentInfo struct {
|
||||
Timestamp string
|
||||
Gauges []gaugeValue
|
||||
Points []pointValue
|
||||
Counters []sampledValue
|
||||
Samples []sampledValue
|
||||
}
|
||||
|
||||
type gaugeValue struct {
|
||||
Name string
|
||||
Value float32
|
||||
Labels map[string]string
|
||||
}
|
||||
|
||||
type pointValue struct {
|
||||
Name string
|
||||
Points []float32
|
||||
}
|
||||
|
||||
type sampledValue struct {
|
||||
Name string
|
||||
Count int
|
||||
Sum float64
|
||||
Min float64
|
||||
Max float64
|
||||
Mean float64
|
||||
Rate float64
|
||||
Stddev float64
|
||||
Labels map[string]string
|
||||
}
|
19
plugins/inputs/consul_agent/sample.conf
Normal file
19
plugins/inputs/consul_agent/sample.conf
Normal file
|
@ -0,0 +1,19 @@
|
|||
# Read metrics from the Consul Agent API
|
||||
[[inputs.consul_agent]]
|
||||
## URL for the Consul agent
|
||||
# url = "http://127.0.0.1:8500"
|
||||
|
||||
## Use auth token for authorization.
|
||||
## If both are set, an error is thrown.
|
||||
## If both are empty, no token will be used.
|
||||
# token_file = "/path/to/auth/token"
|
||||
## OR
|
||||
# token = "a1234567-40c7-9048-7bae-378687048181"
|
||||
|
||||
## Set timeout (default 5 seconds)
|
||||
# timeout = "5s"
|
||||
|
||||
## Optional TLS Config
|
||||
# tls_ca = /path/to/cafile
|
||||
# tls_cert = /path/to/certfile
|
||||
# tls_key = /path/to/keyfile
|
42
plugins/inputs/consul_agent/testdata/response_key_metrics.json
vendored
Normal file
42
plugins/inputs/consul_agent/testdata/response_key_metrics.json
vendored
Normal file
|
@ -0,0 +1,42 @@
|
|||
{
|
||||
"Timestamp": "2021-12-11 10:35:30 +0000 UTC",
|
||||
"Gauges": [
|
||||
{
|
||||
"Name": "consul.consul.members.clients",
|
||||
"Value": 0,
|
||||
"Labels": {
|
||||
"datacenter": "dc1"
|
||||
}
|
||||
}
|
||||
],
|
||||
"Points": [],
|
||||
"Counters": [
|
||||
{
|
||||
"Name": "consul.rpc.request",
|
||||
"Count": 5,
|
||||
"Rate": 0.5,
|
||||
"Sum": 5,
|
||||
"Min": 1,
|
||||
"Max": 1,
|
||||
"Mean": 1,
|
||||
"Stddev": 0,
|
||||
"Labels": {}
|
||||
}
|
||||
],
|
||||
"Samples": [
|
||||
{
|
||||
"Name": "consul.api.http",
|
||||
"Count": 1,
|
||||
"Rate": 0.414815616607666,
|
||||
"Sum": 4.14815616607666,
|
||||
"Min": 4.14815616607666,
|
||||
"Max": 4.14815616607666,
|
||||
"Mean": 4.14815616607666,
|
||||
"Stddev": 0,
|
||||
"Labels": {
|
||||
"method": "GET",
|
||||
"path": "v1_agent_self"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue