forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signalfx.go
255 lines (212 loc) · 7.19 KB
/
signalfx.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
package signalfx
import (
"context"
"errors"
"fmt"
"strings"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/outputs"
"github.com/signalfx/golib/v3/datapoint"
"github.com/signalfx/golib/v3/datapoint/dpsink"
"github.com/signalfx/golib/v3/event"
"github.com/signalfx/golib/v3/sfxclient"
)
//init initializes the plugin context
func init() {
outputs.Add("signalfx", func() telegraf.Output {
return NewSignalFx()
})
}
// SignalFx plugin context
type SignalFx struct {
AccessToken string `toml:"access_token"`
SignalFxRealm string `toml:"signalfx_realm"`
IngestURL string `toml:"ingest_url"`
IncludedEventNames []string `toml:"included_event_names"`
Log telegraf.Logger `toml:"-"`
includedEventSet map[string]bool
client dpsink.Sink
ctx context.Context
cancel context.CancelFunc
}
var sampleConfig = `
## SignalFx Org Access Token
access_token = "my-secret-token"
## The SignalFx realm that your organization resides in
signalfx_realm = "us9" # Required if ingest_url is not set
## You can optionally provide a custom ingest url instead of the
## signalfx_realm option above if you are using a gateway or proxy
## instance. This option takes precident over signalfx_realm.
ingest_url = "https://my-custom-ingest/"
## Event typed metrics are omitted by default,
## If you require an event typed metric you must specify the
## metric name in the following list.
included_event_names = ["plugin.metric_name"]
`
// GetMetricType returns the equivalent telegraf ValueType for a signalfx metric type
func GetMetricType(mtype telegraf.ValueType) (metricType datapoint.MetricType) {
switch mtype {
case telegraf.Counter:
metricType = datapoint.Counter
case telegraf.Gauge:
metricType = datapoint.Gauge
case telegraf.Summary:
metricType = datapoint.Gauge
case telegraf.Histogram:
metricType = datapoint.Gauge
case telegraf.Untyped:
metricType = datapoint.Gauge
default:
metricType = datapoint.Gauge
}
return metricType
}
// NewSignalFx - returns a new context for the SignalFx output plugin
func NewSignalFx() *SignalFx {
ctx, cancel := context.WithCancel(context.Background())
return &SignalFx{
AccessToken: "",
SignalFxRealm: "",
IngestURL: "",
IncludedEventNames: []string{""},
ctx: ctx,
cancel: cancel,
client: sfxclient.NewHTTPSink(),
}
}
// Description returns a description for the plugin
func (s *SignalFx) Description() string {
return "Send metrics and events to SignalFx"
}
// SampleConfig returns the sample configuration for the plugin
func (s *SignalFx) SampleConfig() string {
return sampleConfig
}
// Connect establishes a connection to SignalFx
func (s *SignalFx) Connect() error {
client := s.client.(*sfxclient.HTTPSink)
client.AuthToken = s.AccessToken
if s.IngestURL != "" {
client.DatapointEndpoint = datapointEndpointForIngestURL(s.IngestURL)
client.EventEndpoint = eventEndpointForIngestURL(s.IngestURL)
} else if s.SignalFxRealm != "" {
client.DatapointEndpoint = datapointEndpointForRealm(s.SignalFxRealm)
client.EventEndpoint = eventEndpointForRealm(s.SignalFxRealm)
} else {
return errors.New("signalfx_realm or ingest_url must be configured")
}
return nil
}
// Close closes any connections to SignalFx
func (s *SignalFx) Close() error {
s.cancel()
s.client.(*sfxclient.HTTPSink).Client.CloseIdleConnections()
return nil
}
func (s *SignalFx) ConvertToSignalFx(metrics []telegraf.Metric) ([]*datapoint.Datapoint, []*event.Event) {
var dps []*datapoint.Datapoint
var events []*event.Event
for _, metric := range metrics {
s.Log.Debugf("Processing the following measurement: %v", metric)
var timestamp = metric.Time()
metricType := GetMetricType(metric.Type())
for field, val := range metric.Fields() {
// Copy the metric tags because they are meant to be treated as
// immutable
var metricDims = metric.Tags()
// Generate the metric name
metricName := getMetricName(metric.Name(), field)
// Get the metric value as a datapoint value
if metricValue, err := datapoint.CastMetricValueWithBool(val); err == nil {
var dp = datapoint.New(metricName,
metricDims,
metricValue.(datapoint.Value),
metricType,
timestamp)
s.Log.Debugf("Datapoint: %v", dp.String())
dps = append(dps, dp)
} else {
// Skip if it's not an explicitly included event
if !s.isEventIncluded(metricName) {
continue
}
// We've already type checked field, so set property with value
metricProps := map[string]interface{}{"message": val}
var ev = event.NewWithProperties(metricName,
event.AGENT,
metricDims,
metricProps,
timestamp)
s.Log.Debugf("Event: %v", ev.String())
events = append(events, ev)
}
}
}
return dps, events
}
// Write call back for writing metrics
func (s *SignalFx) Write(metrics []telegraf.Metric) error {
dps, events := s.ConvertToSignalFx(metrics)
if len(dps) > 0 {
err := s.client.AddDatapoints(s.ctx, dps)
if err != nil {
return err
}
}
if len(events) > 0 {
if err := s.client.AddEvents(s.ctx, events); err != nil {
// If events error out but we successfully sent some datapoints,
// don't return an error so that it won't ever retry -- that way we
// don't send the same datapoints twice.
if len(dps) == 0 {
return err
}
s.Log.Errorf("Failed to send SignalFx event: %v", err)
}
}
return nil
}
// isEventIncluded - checks whether a metric name for an event was put on the whitelist
func (s *SignalFx) isEventIncluded(name string) bool {
if s.includedEventSet == nil {
s.includedEventSet = make(map[string]bool, len(s.includedEventSet))
for _, include := range s.IncludedEventNames {
s.includedEventSet[include] = true
}
}
return s.includedEventSet[name]
}
// getMetricName combines telegraf fields and tags into a full metric name
func getMetricName(metric string, field string) string {
name := metric
// Include field in metric name when it adds to the metric name
if field != "value" {
name = fmt.Sprintf("%s.%s", name, field)
}
return name
}
// ingestURLForRealm returns the base ingest URL for a particular SignalFx
// realm
func ingestURLForRealm(realm string) string {
return fmt.Sprintf("https://ingest.%s.signalfx.com", realm)
}
// datapointEndpointForRealm returns the endpoint to which datapoints should be
// POSTed for a particular realm.
func datapointEndpointForRealm(realm string) string {
return datapointEndpointForIngestURL(ingestURLForRealm(realm))
}
// datapointEndpointForRealm returns the endpoint to which datapoints should be
// POSTed for a particular ingest base URL.
func datapointEndpointForIngestURL(ingestURL string) string {
return strings.TrimRight(ingestURL, "/") + "/v2/datapoint"
}
// eventEndpointForRealm returns the endpoint to which events should be
// POSTed for a particular realm.
func eventEndpointForRealm(realm string) string {
return eventEndpointForIngestURL(ingestURLForRealm(realm))
}
// eventEndpointForRealm returns the endpoint to which events should be
// POSTed for a particular ingest base URL.
func eventEndpointForIngestURL(ingestURL string) string {
return strings.TrimRight(ingestURL, "/") + "/v2/event"
}