-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
nomad.go
164 lines (130 loc) · 3.55 KB
/
nomad.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//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
// Nomad configuration object
type Nomad struct {
URL string `toml:"url"`
ResponseTimeout config.Duration `toml:"response_timeout"`
tls.ClientConfig
roundTripper http.RoundTripper
}
const timeLayout = "2006-01-02 15:04:05 -0700 MST"
func init() {
inputs.Add("nomad", func() telegraf.Input {
return &Nomad{
ResponseTimeout: config.Duration(5 * time.Second),
}
})
}
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
}
// Gather, collects metrics from Nomad endpoint
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
}