-
Notifications
You must be signed in to change notification settings - Fork 13
/
circ_http_json.go
318 lines (266 loc) · 8.01 KB
/
circ_http_json.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package circhttpjson
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net"
"net/http"
"os"
"time"
"github.com/circonus-labs/circonus-unified-agent/cua"
circmgr "github.com/circonus-labs/circonus-unified-agent/internal/circonus"
"github.com/circonus-labs/circonus-unified-agent/internal/release"
"github.com/circonus-labs/circonus-unified-agent/plugins/inputs"
"github.com/circonus-labs/go-trapmetrics"
"github.com/hashicorp/go-retryablehttp"
)
// NOTE: this input only supports direct metrics - they do NOT go through a regular output plugin
type CHJ struct {
Log cua.Logger
dest *trapmetrics.TrapMetrics
tlsCfg *tls.Config
instLogger *Logshim
TLSCN string
InstanceID string `json:"instance_id"`
URL string
TLSCAFile string
Debug bool
}
func (chj *CHJ) Init() error {
if chj.URL == "" {
return fmt.Errorf("invalid URL (empty)")
}
if chj.InstanceID == "" {
return fmt.Errorf("invalid Instance ID (empty)")
}
if chj.TLSCAFile != "" {
if err := chj.loadTLSCACert(); err != nil {
return fmt.Errorf("loading TLSCAFile: %w", err)
}
}
opts := &circmgr.MetricDestConfig{
MetricMeta: circmgr.MetricMeta{
PluginID: "circ_http_json",
InstanceID: chj.InstanceID,
},
}
dest, err := circmgr.NewMetricDestination(opts, chj.Log)
if err != nil {
return fmt.Errorf("new metric destination: %w", err)
}
chj.dest = dest
// this is needed for retryablehttp to work...
chj.instLogger = &Logshim{
logh: chj.Log,
debug: chj.Debug,
}
return nil
}
func (*CHJ) Description() string {
return "Circonus HTTP JSON retrieves HTTPTrap formatted metrics and forwards them to an HTTPTrap check"
}
func (*CHJ) SampleConfig() string {
return `
instance_id = "" # required
url = "" # required
## optional, turn on debugging for the *metric fetch* phase of the plugin
## metric submission, to the broker, will output via regular agent debug setting.
debug = false
## Optional: tls ca cert file and common name to use
## pass if URL is https and not using a public ca
# tls_ca_cert_file = ""
# tls_cn = ""
`
}
func (chj *CHJ) Gather(ctx context.Context, _ cua.Accumulator) error {
if chj.dest == nil {
return fmt.Errorf("instance_id: %s -- no metric destination configured", chj.InstanceID)
}
data, err := chj.getURL(ctx)
if err != nil {
return fmt.Errorf("instance_id: %s -- fetching metrics from %s: %w", chj.InstanceID, chj.URL, err)
}
if err := chj.hasStreamtags(data); err != nil {
return fmt.Errorf("instance_id: %s -- no streamtags found in metrics", chj.InstanceID)
}
// if err := chj.verifyJSON(data); err != nil {
// return fmt.Errorf("instance_id: %s -- invalid json from %s: %w", chj.InstanceID, chj.URL, err)
// }
if _, err := chj.dest.FlushRawJSON(ctx, data); err != nil {
return fmt.Errorf("instance_id: %s -- flushing metrics: %w", chj.InstanceID, err)
}
return nil
}
// getURL fetches the raw json from an endpoint, the JSON must adhere to circonus httptrap formatting
// can handle tagged or un-tagged json formats -- the plugin just forwards the JSON it gets to the broker
func (chj *CHJ) getURL(ctx context.Context) ([]byte, error) {
var client *http.Client
if chj.tlsCfg != nil {
client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 3 * time.Second,
FallbackDelay: -1 * time.Millisecond,
}).DialContext,
TLSClientConfig: chj.tlsCfg,
TLSHandshakeTimeout: 10 * time.Second,
DisableKeepAlives: true,
DisableCompression: false,
MaxIdleConns: 1,
MaxIdleConnsPerHost: 0,
},
}
} else {
client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 3 * time.Second,
FallbackDelay: -1 * time.Millisecond,
}).DialContext,
DisableKeepAlives: true,
DisableCompression: false,
MaxIdleConns: 1,
MaxIdleConnsPerHost: 0,
},
}
}
rinfo := release.GetInfo()
req, err := retryablehttp.NewRequest("GET", chj.URL, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
req.Header.Set("User-Agent", rinfo.Name+"/"+rinfo.Version)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.Header.Set("Connection", "close")
retryClient := retryablehttp.NewClient()
retryClient.HTTPClient = client
retryClient.Logger = chj.instLogger
defer retryClient.HTTPClient.CloseIdleConnections()
resp, err := retryClient.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, fmt.Errorf("making request: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("response status: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("reading response body: %w", err)
}
if len(body) == 0 {
return nil, fmt.Errorf("empty response body")
}
return body, nil
}
// hasStreamtags return true if there is at least one tagged metric
func (chj *CHJ) hasStreamtags(data []byte) error {
if len(data) == 0 {
return fmt.Errorf("empty json")
}
if !bytes.Contains(data, []byte("|ST[")) {
return fmt.Errorf("no streamtags found")
}
return nil
}
// loadTLSCACert reads in the configured TLS CA cert file and creates
// a tls.Config to use during metric fetching from URL
func (chj *CHJ) loadTLSCACert() error {
data, err := os.ReadFile(chj.TLSCAFile)
if err != nil {
return err
}
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(data) {
return fmt.Errorf("unable to append cert from pem %s", chj.TLSCAFile)
}
chj.tlsCfg = &tls.Config{
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: true, //nolint:gosec
VerifyConnection: func(cs tls.ConnectionState) error {
commonName := cs.PeerCertificates[0].Subject.CommonName
if commonName != cs.ServerName {
return x509.CertificateInvalidError{
Cert: cs.PeerCertificates[0],
Reason: x509.NameMismatch,
Detail: fmt.Sprintf("cn: %q, acceptable: %q", commonName, cs.ServerName),
}
}
opts := x509.VerifyOptions{
Roots: certPool,
Intermediates: x509.NewCertPool(),
}
for _, cert := range cs.PeerCertificates[1:] {
opts.Intermediates.AddCert(cert)
}
_, err := cs.PeerCertificates[0].Verify(opts)
if err != nil {
return fmt.Errorf("peer cert verify: %w", err)
}
return nil
},
}
if chj.TLSCN != "" {
chj.tlsCfg.ServerName = chj.TLSCN
}
return nil
}
func init() {
inputs.Add("circ_http_json", func() cua.Input {
return &CHJ{}
})
}
//
// use the simple validation in hasStreamtags, the method below is very expensive and should be used with caution
//
// type Metric struct {
// Value interface{} `json:"_value"`
// Timestamp *uint64 `json:"_ts,omitempty"`
// Type string `json:"_type"`
// }
// type Metrics map[string]Metric
// // verifyJSON simply unmarshals a []byte into a metrics struct (defined above)
// // if it works it is considered valid -- valid JSON formatting:
// // https://docs.circonus.com/circonus/integrations/library/json-push-httptrap/#stream-tags
// func (chj *CHJ) verifyJSON(data []byte) error {
// if len(data) == 0 {
// return fmt.Errorf("empty json")
// }
// // short circuit if a tagged metric found
// if bytes.Contains(data, []byte("|ST[")) {
// return nil
// }
// var d1 bytes.Buffer
// if err := json.Compact(&d1, data); err != nil {
// return fmt.Errorf("json compact: %w", err)
// }
// if d1.Len() == 0 {
// return fmt.Errorf("invalid JSON (empty)")
// }
// var m Metrics
// if err := json.Unmarshal(d1.Bytes(), &m); err != nil {
// return fmt.Errorf("json unmarshal: %w", err)
// }
// if len(m) == 0 {
// return fmt.Errorf("invalid JSON (no metrics)")
// }
// d2, err := json.Marshal(m)
// if err != nil {
// return fmt.Errorf("json marshal: %w", err)
// }
// if d1.Len() != len(d2) {
// return fmt.Errorf("json invalid parse len: d1:%d != d2:%d", d1.Len(), len(d2))
// }
// return nil
// }