197 lines
4.5 KiB
Go
197 lines
4.5 KiB
Go
package consul_metrics
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/influxdata/telegraf"
|
|
"github.com/influxdata/telegraf/config"
|
|
"github.com/influxdata/telegraf/plugins/common/tls"
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
)
|
|
|
|
// Consul_metrics configuration object
|
|
type ConsulMetrics struct {
|
|
URL string `toml:"url"`
|
|
|
|
TokenFile string `toml:"token_file"`
|
|
Token string `toml:"token"`
|
|
|
|
ResponseTimeout config.Duration `toml:"timeout"`
|
|
|
|
tls.ClientConfig
|
|
|
|
roundTripper http.RoundTripper
|
|
}
|
|
|
|
const timeLayout = "2006-01-02 15:04:05 -0700 MST"
|
|
|
|
const sampleConfig = `
|
|
## URL for the Consul agent
|
|
# url = "http://127.0.0.1:8500"
|
|
|
|
## Use auth token for authorization.
|
|
## Only one of the options can be set. Leave empty to not use any token.
|
|
# 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
|
|
`
|
|
|
|
func init() {
|
|
inputs.Add("consul_metrics", func() telegraf.Input {
|
|
return &ConsulMetrics{
|
|
ResponseTimeout: config.Duration(5 * time.Second),
|
|
}
|
|
})
|
|
}
|
|
|
|
// SampleConfig returns a sample config
|
|
func (n *ConsulMetrics) SampleConfig() string {
|
|
return sampleConfig
|
|
}
|
|
|
|
// Description returns a description of the plugin
|
|
func (n *ConsulMetrics) Description() string {
|
|
return "Read metrics from the Consul API"
|
|
}
|
|
|
|
func (n *ConsulMetrics) 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: %v", err)
|
|
}
|
|
n.Token = strings.TrimSpace(string(token))
|
|
}
|
|
|
|
tlsCfg, err := n.ClientConfig.TLSConfig()
|
|
if err != nil {
|
|
return fmt.Errorf("setting up TLS configuration failed: %v", err)
|
|
}
|
|
|
|
n.roundTripper = &http.Transport{
|
|
TLSHandshakeTimeout: time.Duration(n.ResponseTimeout),
|
|
TLSClientConfig: tlsCfg,
|
|
ResponseHeaderTimeout: time.Duration(n.ResponseTimeout),
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Gather, collects metrics from Consul endpoint
|
|
func (n *ConsulMetrics) Gather(acc telegraf.Accumulator) error {
|
|
summaryMetrics, err := n.loadJSON(n.URL + "/v1/agent/metrics")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return buildConsulMetrics(acc, summaryMetrics)
|
|
}
|
|
|
|
func (n *ConsulMetrics) loadJSON(url string) (*MetricsInfo, error) {
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Header.Set("Authorization", "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 %s: %s", 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 MetricsInfo
|
|
err = json.NewDecoder(resp.Body).Decode(&metrics)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error parsing json response: %s", err)
|
|
}
|
|
|
|
return &metrics, nil
|
|
}
|
|
|
|
// buildConsulMetrics, it builds all the metrics and adds them to the accumulator)
|
|
func buildConsulMetrics(acc telegraf.Accumulator, metricsInfo *MetricsInfo) error {
|
|
t, err := time.Parse(timeLayout, metricsInfo.Timestamp)
|
|
if err != nil {
|
|
return fmt.Errorf("error parsing time: %s", err)
|
|
}
|
|
|
|
for _, counters := range metricsInfo.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 metricsInfo.Gauges {
|
|
fields := map[string]interface{}{
|
|
"value": gauges.Value,
|
|
}
|
|
tags := gauges.Labels
|
|
|
|
acc.AddGauge(gauges.Name, fields, tags, t)
|
|
}
|
|
|
|
for _, points := range metricsInfo.Points {
|
|
fields := map[string]interface{}{
|
|
"value": points.Points,
|
|
}
|
|
tags := make(map[string]string)
|
|
|
|
acc.AddFields(points.Name, fields, tags, t)
|
|
}
|
|
|
|
for _, samples := range metricsInfo.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
|
|
}
|