forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogzio.go
175 lines (141 loc) · 3.88 KB
/
logzio.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
package logzio
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/outputs"
)
const (
defaultLogzioURL = "https://listener.logz.io:8071"
logzioDescription = "Send aggregate metrics to Logz.io"
logzioType = "telegraf"
)
var sampleConfig = `
## Connection timeout, defaults to "5s" if not set.
timeout = "5s"
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Logz.io account token
token = "your logz.io token" # required
## Use your listener URL for your Logz.io account region.
# url = "https://listener.logz.io:8071"
`
type Logzio struct {
Log telegraf.Logger `toml:"-"`
Timeout config.Duration `toml:"timeout"`
Token string `toml:"token"`
URL string `toml:"url"`
tls.ClientConfig
client *http.Client
}
type TimeSeries struct {
Series []*Metric
}
type Metric struct {
Metric map[string]interface{} `json:"metrics"`
Dimensions map[string]string `json:"dimensions"`
Time time.Time `json:"@timestamp"`
Type string `json:"type"`
}
// Connect to the Output
func (l *Logzio) Connect() error {
l.Log.Debug("Connecting to logz.io output...")
if l.Token == "" || l.Token == "your logz.io token" {
return fmt.Errorf("token is required")
}
tlsCfg, err := l.ClientConfig.TLSConfig()
if err != nil {
return err
}
l.client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsCfg,
},
Timeout: time.Duration(l.Timeout),
}
return nil
}
// Close any connections to the Output
func (l *Logzio) Close() error {
l.Log.Debug("Closing logz.io output")
return nil
}
// Description returns a one-sentence description on the Output
func (l *Logzio) Description() string {
return logzioDescription
}
// SampleConfig returns the default configuration of the Output
func (l *Logzio) SampleConfig() string {
return sampleConfig
}
// Write takes in group of points to be written to the Output
func (l *Logzio) Write(metrics []telegraf.Metric) error {
if len(metrics) == 0 {
return nil
}
var buff bytes.Buffer
gz := gzip.NewWriter(&buff)
for _, metric := range metrics {
m := l.parseMetric(metric)
serialized, err := json.Marshal(m)
if err != nil {
return fmt.Errorf("unable to marshal metric, %s", err.Error())
}
_, err = gz.Write(append(serialized, '\n'))
if err != nil {
return fmt.Errorf("unable to write gzip meric, %s", err.Error())
}
}
err := gz.Close()
if err != nil {
return fmt.Errorf("unable to close gzip, %s", err.Error())
}
return l.send(buff.Bytes())
}
func (l *Logzio) send(metrics []byte) error {
req, err := http.NewRequest("POST", l.authURL(), bytes.NewBuffer(metrics))
if err != nil {
return fmt.Errorf("unable to create http.Request, %s", err.Error())
}
req.Header.Add("Content-Type", "application/json")
req.Header.Set("Content-Encoding", "gzip")
resp, err := l.client.Do(req)
if err != nil {
return fmt.Errorf("error POSTing metrics, %s", err.Error())
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 209 {
return fmt.Errorf("received bad status code, %d", resp.StatusCode)
}
return nil
}
func (l *Logzio) authURL() string {
return fmt.Sprintf("%s/?token=%s", l.URL, l.Token)
}
func (l *Logzio) parseMetric(metric telegraf.Metric) *Metric {
return &Metric{
Metric: map[string]interface{}{
metric.Name(): metric.Fields(),
},
Dimensions: metric.Tags(),
Time: metric.Time(),
Type: logzioType,
}
}
func init() {
outputs.Add("logzio", func() telegraf.Output {
return &Logzio{
URL: defaultLogzioURL,
Timeout: config.Duration(time.Second * 5),
}
})
}