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
49
plugins/inputs/nomad/README.md
Normal file
49
plugins/inputs/nomad/README.md
Normal file
|
@ -0,0 +1,49 @@
|
|||
# Hashicorp Nomad Input Plugin
|
||||
|
||||
This plugin collects metrics from every [Nomad agent][nomad] of the specified
|
||||
cluster. Telegraf may be present in every node and connect to the agent locally.
|
||||
|
||||
⭐ Telegraf v1.22.0
|
||||
🏷️ server
|
||||
💻 all
|
||||
|
||||
[nomad]: https://www.nomadproject.io/
|
||||
|
||||
## 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 Nomad API
|
||||
[[inputs.nomad]]
|
||||
## URL for the Nomad agent
|
||||
# url = "http://127.0.0.1:4646"
|
||||
|
||||
## Set response_timeout (default 5 seconds)
|
||||
# response_timeout = "5s"
|
||||
|
||||
## Optional TLS Config
|
||||
# tls_ca = /path/to/cafile
|
||||
# tls_cert = /path/to/certfile
|
||||
# tls_key = /path/to/keyfile
|
||||
```
|
||||
|
||||
## Metrics
|
||||
|
||||
Both Nomad servers and agents collect various metrics. For every details, please
|
||||
have a look at [Nomad metrics][metrics] and [Nomad telemetry][telemetry]
|
||||
ocumentation.
|
||||
|
||||
[metrics]: https://www.nomadproject.io/docs/operations/metrics
|
||||
[telemetry]: https://www.nomadproject.io/docs/operations/telemetry
|
||||
|
||||
## Example Output
|
||||
|
||||
There is no predefined metric format, so output depends on plugin input.
|
160
plugins/inputs/nomad/nomad.go
Normal file
160
plugins/inputs/nomad/nomad.go
Normal file
|
@ -0,0 +1,160 @@
|
|||
//go:generate ../../../tools/readme_config_includer/generator
|
||||
package nomad
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"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 Nomad struct {
|
||||
URL string `toml:"url"`
|
||||
ResponseTimeout config.Duration `toml:"response_timeout"`
|
||||
tls.ClientConfig
|
||||
|
||||
roundTripper http.RoundTripper
|
||||
}
|
||||
|
||||
func (*Nomad) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
|
||||
func (n *Nomad) Init() error {
|
||||
if n.URL == "" {
|
||||
n.URL = "http://127.0.0.1:4646"
|
||||
}
|
||||
|
||||
tlsCfg, err := n.ClientConfig.TLSConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("setting up TLS configuration failed: %w", err)
|
||||
}
|
||||
|
||||
n.roundTripper = &http.Transport{
|
||||
TLSHandshakeTimeout: 5 * time.Second,
|
||||
TLSClientConfig: tlsCfg,
|
||||
ResponseHeaderTimeout: time.Duration(n.ResponseTimeout),
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *Nomad) Gather(acc telegraf.Accumulator) error {
|
||||
summaryMetrics := &metricsSummary{}
|
||||
err := n.loadJSON(n.URL+"/v1/metrics", summaryMetrics)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = buildNomadMetrics(acc, summaryMetrics)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *Nomad) loadJSON(url string, v interface{}) error {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := n.roundTripper.RoundTrip(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error making HTTP request to %q: %w", url, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("%s returned HTTP status %s", url, resp.Status)
|
||||
}
|
||||
|
||||
err = json.NewDecoder(resp.Body).Decode(v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing json response: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildNomadMetrics, it builds all the metrics and adds them to the accumulator)
|
||||
func buildNomadMetrics(acc telegraf.Accumulator, summaryMetrics *metricsSummary) error {
|
||||
t, err := internal.ParseTimestamp(timeLayout, summaryMetrics.Timestamp, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing time: %w", err)
|
||||
}
|
||||
|
||||
for _, counters := range summaryMetrics.Counters {
|
||||
tags := counters.DisplayLabels
|
||||
|
||||
fields := map[string]interface{}{
|
||||
"count": counters.Count,
|
||||
"rate": counters.Rate,
|
||||
"sum": counters.Sum,
|
||||
"sumsq": counters.SumSq,
|
||||
"min": counters.Min,
|
||||
"max": counters.Max,
|
||||
"mean": counters.Mean,
|
||||
}
|
||||
acc.AddCounter(counters.Name, fields, tags, t)
|
||||
}
|
||||
|
||||
for _, gauges := range summaryMetrics.Gauges {
|
||||
tags := gauges.DisplayLabels
|
||||
|
||||
fields := map[string]interface{}{
|
||||
"value": gauges.Value,
|
||||
}
|
||||
|
||||
acc.AddGauge(gauges.Name, fields, tags, t)
|
||||
}
|
||||
|
||||
for _, points := range summaryMetrics.Points {
|
||||
tags := make(map[string]string)
|
||||
|
||||
fields := map[string]interface{}{
|
||||
"value": points.Points,
|
||||
}
|
||||
|
||||
acc.AddFields(points.Name, fields, tags, t)
|
||||
}
|
||||
|
||||
for _, samples := range summaryMetrics.Samples {
|
||||
tags := samples.DisplayLabels
|
||||
|
||||
fields := map[string]interface{}{
|
||||
"count": samples.Count,
|
||||
"rate": samples.Rate,
|
||||
"sum": samples.Sum,
|
||||
"stddev": samples.Stddev,
|
||||
"sumsq": samples.SumSq,
|
||||
"min": samples.Min,
|
||||
"max": samples.Max,
|
||||
"mean": samples.Mean,
|
||||
}
|
||||
acc.AddCounter(samples.Name, fields, tags, t)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
inputs.Add("nomad", func() telegraf.Input {
|
||||
return &Nomad{
|
||||
ResponseTimeout: config.Duration(5 * time.Second),
|
||||
}
|
||||
})
|
||||
}
|
54
plugins/inputs/nomad/nomad_metrics.go
Normal file
54
plugins/inputs/nomad/nomad_metrics.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package nomad
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type metricsSummary struct {
|
||||
Timestamp string `json:"timestamp"`
|
||||
Gauges []gaugeValue `json:"gauges"`
|
||||
Points []pointValue `json:"points"`
|
||||
Counters []sampledValue `json:"counters"`
|
||||
Samples []sampledValue `json:"samples"`
|
||||
}
|
||||
|
||||
type gaugeValue struct {
|
||||
Name string `json:"name"`
|
||||
Hash string `json:"-"`
|
||||
Value float32 `json:"value"`
|
||||
|
||||
Labels []label `json:"-"`
|
||||
DisplayLabels map[string]string `json:"Labels"`
|
||||
}
|
||||
|
||||
type pointValue struct {
|
||||
Name string `json:"name"`
|
||||
Points []float32 `json:"points"`
|
||||
}
|
||||
|
||||
type sampledValue struct {
|
||||
Name string `json:"name"`
|
||||
Hash string `json:"-"`
|
||||
*AggregateSample
|
||||
Mean float64 `json:"mean"`
|
||||
Stddev float64 `json:"stddev"`
|
||||
|
||||
Labels []label `json:"-"`
|
||||
DisplayLabels map[string]string `json:"Labels"`
|
||||
}
|
||||
|
||||
// AggregateSample needs to be exported, because JSON decode cannot set embedded pointer to unexported struct
|
||||
type AggregateSample struct {
|
||||
Count int `json:"count"`
|
||||
Rate float64 `json:"rate"`
|
||||
Sum float64 `json:"sum"`
|
||||
SumSq float64 `json:"-"`
|
||||
Min float64 `json:"min"`
|
||||
Max float64 `json:"max"`
|
||||
LastUpdated time.Time `json:"-"`
|
||||
}
|
||||
|
||||
type label struct {
|
||||
Name string `json:"name"`
|
||||
Value string `json:"value"`
|
||||
}
|
114
plugins/inputs/nomad/nomad_test.go
Normal file
114
plugins/inputs/nomad/nomad_test.go
Normal file
|
@ -0,0 +1,114 @@
|
|||
package nomad
|
||||
|
||||
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 TestNomadStats(t *testing.T) {
|
||||
var applyTests = []struct {
|
||||
name string
|
||||
expected []telegraf.Metric
|
||||
}{
|
||||
{
|
||||
name: "Metrics",
|
||||
expected: []telegraf.Metric{
|
||||
testutil.MustMetric(
|
||||
"nomad.nomad.rpc.query",
|
||||
map[string]string{
|
||||
"host": "node1",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"count": int(7),
|
||||
"max": float64(1),
|
||||
"min": float64(1),
|
||||
"mean": float64(1),
|
||||
"rate": float64(0.7),
|
||||
"sum": float64(7),
|
||||
"sumsq": float64(0),
|
||||
},
|
||||
time.Unix(1636843140, 0),
|
||||
1,
|
||||
),
|
||||
testutil.MustMetric(
|
||||
"nomad.client.allocated.cpu",
|
||||
map[string]string{
|
||||
"node_scheduling_eligibility": "eligible",
|
||||
"host": "node1",
|
||||
"node_id": "2bbff078-8473-a9de-6c5e-42b4e053e12f",
|
||||
"datacenter": "dc1",
|
||||
"node_class": "none",
|
||||
"node_status": "ready",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"value": float32(500),
|
||||
},
|
||||
time.Unix(1636843140, 0),
|
||||
2,
|
||||
),
|
||||
testutil.MustMetric(
|
||||
"nomad.memberlist.gossip",
|
||||
map[string]string{
|
||||
"host": "node1",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"count": int(20),
|
||||
"max": float64(0.03747599944472313),
|
||||
"mean": float64(0.013159099989570678),
|
||||
"min": float64(0.003459000028669834),
|
||||
"rate": float64(0.026318199979141355),
|
||||
"stddev": float64(0.009523742715522742),
|
||||
"sum": float64(0.26318199979141355),
|
||||
"sumsq": float64(0),
|
||||
},
|
||||
time.Unix(1636843140, 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/metrics" {
|
||||
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
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
plugin := &Nomad{
|
||||
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())
|
||||
})
|
||||
}
|
||||
}
|
12
plugins/inputs/nomad/sample.conf
Normal file
12
plugins/inputs/nomad/sample.conf
Normal file
|
@ -0,0 +1,12 @@
|
|||
# Read metrics from the Nomad API
|
||||
[[inputs.nomad]]
|
||||
## URL for the Nomad agent
|
||||
# url = "http://127.0.0.1:4646"
|
||||
|
||||
## Set response_timeout (default 5 seconds)
|
||||
# response_timeout = "5s"
|
||||
|
||||
## Optional TLS Config
|
||||
# tls_ca = /path/to/cafile
|
||||
# tls_cert = /path/to/certfile
|
||||
# tls_key = /path/to/keyfile
|
48
plugins/inputs/nomad/testdata/response_key_metrics.json
vendored
Normal file
48
plugins/inputs/nomad/testdata/response_key_metrics.json
vendored
Normal file
|
@ -0,0 +1,48 @@
|
|||
{
|
||||
"Counters": [
|
||||
{
|
||||
"Count": 7,
|
||||
"Labels": {
|
||||
"host": "node1"
|
||||
},
|
||||
"Max": 1,
|
||||
"Mean": 1,
|
||||
"Min": 1,
|
||||
"Name": "nomad.nomad.rpc.query",
|
||||
"Rate": 0.7,
|
||||
"Stddev": 0,
|
||||
"Sum": 7
|
||||
}
|
||||
],
|
||||
"Gauges": [
|
||||
{
|
||||
"Labels": {
|
||||
"node_scheduling_eligibility": "eligible",
|
||||
"host": "node1",
|
||||
"node_id": "2bbff078-8473-a9de-6c5e-42b4e053e12f",
|
||||
"datacenter": "dc1",
|
||||
"node_class": "none",
|
||||
"node_status": "ready"
|
||||
},
|
||||
"Name": "nomad.client.allocated.cpu",
|
||||
"Value": 500
|
||||
}
|
||||
],
|
||||
"Points": [],
|
||||
"Samples": [
|
||||
{
|
||||
"Count": 20,
|
||||
"Labels": {
|
||||
"host": "node1"
|
||||
},
|
||||
"Max": 0.03747599944472313,
|
||||
"Mean": 0.013159099989570678,
|
||||
"Min": 0.003459000028669834,
|
||||
"Name": "nomad.memberlist.gossip",
|
||||
"Rate": 0.026318199979141355,
|
||||
"Stddev": 0.009523742715522742,
|
||||
"Sum": 0.26318199979141355
|
||||
}
|
||||
],
|
||||
"Timestamp": "2021-11-13 22:39:00 +0000 UTC"
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue