forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
warp10.go
291 lines (237 loc) · 6.48 KB
/
warp10.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
package warp10
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"math"
"net/http"
"sort"
"strconv"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/outputs"
)
const (
defaultClientTimeout = 15 * time.Second
)
// Warp10 output plugin
type Warp10 struct {
Prefix string `toml:"prefix"`
WarpURL string `toml:"warp_url"`
Token string `toml:"token"`
Timeout internal.Duration `toml:"timeout"`
PrintErrorBody bool `toml:"print_error_body"`
MaxStringErrorSize int `toml:"max_string_error_size"`
client *http.Client
tls.ClientConfig
}
var sampleConfig = `
# Prefix to add to the measurement.
prefix = "telegraf."
# URL of the Warp 10 server
warp_url = "http://localhost:8080"
# Write token to access your app on warp 10
token = "Token"
# Warp 10 query timeout
# timeout = "15s"
## Print Warp 10 error body
# print_error_body = false
## Max string error size
# max_string_error_size = 511
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
`
// MetricLine Warp 10 metrics
type MetricLine struct {
Metric string
Timestamp int64
Value string
Tags string
}
func (w *Warp10) createClient() (*http.Client, error) {
tlsCfg, err := w.ClientConfig.TLSConfig()
if err != nil {
return nil, err
}
if w.Timeout.Duration == 0 {
w.Timeout.Duration = defaultClientTimeout
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsCfg,
Proxy: http.ProxyFromEnvironment,
},
Timeout: w.Timeout.Duration,
}
return client, nil
}
// Connect to warp10
func (w *Warp10) Connect() error {
client, err := w.createClient()
if err != nil {
return err
}
w.client = client
return nil
}
// GenWarp10Payload compute Warp 10 metrics payload
func (w *Warp10) GenWarp10Payload(metrics []telegraf.Metric) string {
collectString := make([]string, 0)
for _, mm := range metrics {
for _, field := range mm.FieldList() {
metric := &MetricLine{
Metric: fmt.Sprintf("%s%s", w.Prefix, mm.Name()+"."+field.Key),
Timestamp: mm.Time().UnixNano() / 1000,
}
metricValue, err := buildValue(field.Value)
if err != nil {
log.Printf("E! [outputs.warp10] Could not encode value: %v", err)
continue
}
metric.Value = metricValue
tagsSlice := buildTags(mm.TagList())
metric.Tags = strings.Join(tagsSlice, ",")
messageLine := fmt.Sprintf("%d// %s{%s} %s\n", metric.Timestamp, metric.Metric, metric.Tags, metric.Value)
collectString = append(collectString, messageLine)
}
}
return fmt.Sprint(strings.Join(collectString, ""))
}
// Write metrics to Warp10
func (w *Warp10) Write(metrics []telegraf.Metric) error {
payload := w.GenWarp10Payload(metrics)
if payload == "" {
return nil
}
req, err := http.NewRequest("POST", w.WarpURL+"/api/v0/update", bytes.NewBufferString(payload))
req.Header.Set("X-Warp10-Token", w.Token)
req.Header.Set("Content-Type", "text/plain")
resp, err := w.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if w.PrintErrorBody {
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf(w.WarpURL + ": " + w.HandleError(string(body), w.MaxStringErrorSize))
}
if len(resp.Status) < w.MaxStringErrorSize {
return fmt.Errorf(w.WarpURL + ": " + resp.Status)
}
return fmt.Errorf(w.WarpURL + ": " + resp.Status[0:w.MaxStringErrorSize])
}
return nil
}
func buildTags(tags []*telegraf.Tag) []string {
tagsString := make([]string, len(tags)+1)
indexSource := 0
for index, tag := range tags {
tagsString[index] = fmt.Sprintf("%s=%s", tag.Key, tag.Value)
indexSource = index
}
indexSource++
tagsString[indexSource] = fmt.Sprintf("source=telegraf")
sort.Strings(tagsString)
return tagsString
}
func buildValue(v interface{}) (string, error) {
var retv string
switch p := v.(type) {
case int64:
retv = intToString(p)
case string:
retv = fmt.Sprintf("'%s'", strings.Replace(p, "'", "\\'", -1))
case bool:
retv = boolToString(p)
case uint64:
if p <= uint64(math.MaxInt64) {
retv = strconv.FormatInt(int64(p), 10)
} else {
retv = strconv.FormatInt(math.MaxInt64, 10)
}
case float64:
retv = floatToString(float64(p))
default:
return "", fmt.Errorf("unsupported type: %T", v)
}
return retv, nil
}
func intToString(inputNum int64) string {
return strconv.FormatInt(inputNum, 10)
}
func boolToString(inputBool bool) string {
return strconv.FormatBool(inputBool)
}
func uIntToString(inputNum uint64) string {
return strconv.FormatUint(inputNum, 10)
}
func floatToString(inputNum float64) string {
return strconv.FormatFloat(inputNum, 'f', 6, 64)
}
// SampleConfig get config
func (w *Warp10) SampleConfig() string {
return sampleConfig
}
// Description get description
func (w *Warp10) Description() string {
return "Write metrics to Warp 10"
}
// Close close
func (w *Warp10) Close() error {
return nil
}
// Init Warp10 struct
func (w *Warp10) Init() error {
if w.MaxStringErrorSize <= 0 {
w.MaxStringErrorSize = 511
}
return nil
}
func init() {
outputs.Add("warp10", func() telegraf.Output {
return &Warp10{}
})
}
// HandleError read http error body and return a corresponding error
func (w *Warp10) HandleError(body string, maxStringSize int) string {
if body == "" {
return "Empty return"
}
if strings.Contains(body, "Invalid token") {
return "Invalid token"
}
if strings.Contains(body, "Write token missing") {
return "Write token missing"
}
if strings.Contains(body, "Token Expired") {
return "Token Expired"
}
if strings.Contains(body, "Token revoked") {
return "Token revoked"
}
if strings.Contains(body, "exceed your Monthly Active Data Streams limit") || strings.Contains(body, "exceed the Monthly Active Data Streams limit") {
return "Exceeded Monthly Active Data Streams limit"
}
if strings.Contains(body, "Daily Data Points limit being already exceeded") {
return "Exceeded Daily Data Points limit"
}
if strings.Contains(body, "Application suspended or closed") {
return "Application suspended or closed"
}
if strings.Contains(body, "broken pipe") {
return "broken pipe"
}
if len(body) < maxStringSize {
return body
}
return body[0:maxStringSize]
}