-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackdriver.go
277 lines (229 loc) · 7.07 KB
/
stackdriver.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
package fastlystats
import (
"context"
"fmt"
"reflect"
"sync"
"time"
monitoring "cloud.google.com/go/monitoring/apiv3"
"go.uber.org/zap"
"google.golang.org/genproto/googleapis/api/metric"
"google.golang.org/genproto/googleapis/api/monitoredres"
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
)
// timeSeriesBatchSize is the number of time series to send in one request.
// TimeSeries will be batched and this number of time series will be sent with
// each request.
//
// This is limited by a quota, see https://cloud.google.com/monitoring/quotas
// This cannot be higher than 200 as of writing this.
const timeSeriesBatchSize = 200
// maxReportTimeout is the maximum time to wait for a batch to reach `timeSeriesBatchSize`
// entries before sending a batch with fewer time series.
const maxReportTimeout = 2 * time.Second
type StackdriverExporter struct {
metricClient *monitoring.MetricClient
ch <-chan *FastlyMeanStats
timeSeriesCh chan *monitoringpb.TimeSeries
googleCloudProject string
nodeID string
}
func SetupMetricDescriptors(ctx context.Context, googleCloudProject string) {
ll := zap.S()
metricClient, err := monitoring.NewMetricClient(ctx)
if err != nil {
ll.Fatal(err)
}
ll.Infof("Setting up metric descriptors")
for _, m := range MetricDescriptors {
name := fmt.Sprintf("projects/%s/metricDescriptors/%s", googleCloudProject, m.Type)
ll.Infof("Recreating metric '%s'", m.Type)
err = metricClient.DeleteMetricDescriptor(ctx, &monitoringpb.DeleteMetricDescriptorRequest{
Name: name,
})
if status.Code(err) != codes.OK && status.Code(err) != codes.NotFound {
ll.Warnf("Failed to delete metric (will attempt to create anyway): %v", err)
}
_, err := metricClient.CreateMetricDescriptor(ctx, &monitoringpb.CreateMetricDescriptorRequest{
Name: fmt.Sprintf("projects/%s", googleCloudProject),
MetricDescriptor: m,
})
if err != nil {
ll.Warnf("Failed to create metric '%s': %v", m.Type, err)
}
}
}
func NewStackdriverExporter(project string, nodeID string, ch <-chan *FastlyMeanStats) (*StackdriverExporter, error) {
metricClient, err := monitoring.NewMetricClient(context.Background())
if err != nil {
return nil, err
}
return &StackdriverExporter{
metricClient: metricClient,
ch: ch,
timeSeriesCh: make(chan *monitoringpb.TimeSeries, timeSeriesBatchSize),
googleCloudProject: project,
nodeID: nodeID,
}, nil
}
func (s *StackdriverExporter) Run(ctx context.Context) {
ll := zap.S()
ll.Infof("starting stackdriver exporter to project %s", s.googleCloudProject)
tasks := 2
wg := sync.WaitGroup{}
wg.Add(tasks)
ictx, cancel := context.WithCancel(ctx)
go func() {
defer cancel()
defer wg.Done()
s.timeSeriesWorker(ictx)
}()
go func() {
defer cancel()
defer wg.Done()
s.timeSeriesReporter(ictx)
}()
wg.Wait()
}
func (s *StackdriverExporter) timeSeries(stats *FastlyMeanStats) []*monitoringpb.TimeSeries {
var result []*monitoringpb.TimeSeries
t := reflect.TypeOf(*stats.Stats)
v := reflect.ValueOf(*stats.Stats)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
metricName := field.Tag.Get("mapstructure")
metricKind := metric.MetricDescriptor_GAUGE
valueType := metric.MetricDescriptor_VALUE_TYPE_UNSPECIFIED
switch t.Field(i).Type.Kind() {
case reflect.Uint64:
valueType = metric.MetricDescriptor_INT64
case reflect.Float64:
valueType = metric.MetricDescriptor_DOUBLE
}
var getValue valuer
switch valueType {
case metric.MetricDescriptor_INT64:
getValue = int64Valuer
case metric.MetricDescriptor_DOUBLE:
getValue = doubleValuer
default:
zap.S().Debugf("no valuer for type %v: skipping metric '%s'", valueType, metricName)
continue
}
ts := &monitoringpb.TimeSeries{
Metric: &metric.Metric{
Type: fmt.Sprintf("custom.googleapis.com/fastly/%s", metricName),
},
MetricKind: metricKind,
ValueType: valueType,
Points: []*monitoringpb.Point{{Value: getValue(v.Field(i))}},
}
result = append(result, ts)
}
return result
}
func (s *StackdriverExporter) sendTimeSeries(
ctx context.Context,
intervalStart, intervalEnd uint64,
resource *monitoredres.MonitoredResource,
timeSeries []*monitoringpb.TimeSeries,
) error {
for i, ts := range timeSeries {
// Set some common values for all time series
timeSeries[i].Resource = resource
for j := range ts.Points {
timeSeries[i].Points[j].Interval = &monitoringpb.TimeInterval{
// StartTime is set to End Time, because it's not supported that these
// differ yet (it gives an API error).
StartTime: timestamppb.New(time.Unix(int64(intervalEnd), 0)),
EndTime: timestamppb.New(time.Unix(int64(intervalEnd), 0)),
}
}
// Send it on the channel for batching and reporting
select {
case s.timeSeriesCh <- timeSeries[i]:
case <-ctx.Done():
return ctx.Err()
}
}
return nil
}
func (s *StackdriverExporter) timeSeriesWorker(ctx context.Context) {
ll := zap.S().With("type", "producer")
ll.Infof("started worker")
for {
select {
case meanStats, ok := <-s.ch:
if !ok {
ll.Infof("channel closed, not exporting any more stats")
return
}
monitoredResource := &monitoredres.MonitoredResource{
Type: "generic_node",
Labels: map[string]string{
"project_id": s.googleCloudProject,
"location": "global",
"namespace": "fastly",
"node_id": s.nodeID,
},
}
if err := s.sendTimeSeries(ctx, meanStats.IntervalStart, meanStats.IntervalEnd, monitoredResource, s.timeSeries(meanStats)); err != nil {
zap.S().Warnf("failed to send time series: %v", err)
}
case <-ctx.Done():
ll.Infof("context done, exiting")
return
}
}
}
func (s *StackdriverExporter) timeSeriesReporter(ctx context.Context) {
ll := zap.S().With("type", "reporter")
batch := make([]*monitoringpb.TimeSeries, 0, timeSeriesBatchSize)
var timeoutCh <-chan time.Time
report := func() {
if err := s.reportBatch(ctx, batch); err != nil {
ll.Warnf("failed to report timeseries: %v", err)
}
batch = batch[:0]
timeoutCh = nil
}
for {
select {
case <-timeoutCh:
report()
case ts, ok := <-s.timeSeriesCh:
if !ok {
ll.Infof("time series chan closed, exiting")
return
}
batch = append(batch, ts)
if timeoutCh == nil {
timeoutCh = time.After(maxReportTimeout)
}
if len(batch) == cap(batch) {
report()
}
case <-ctx.Done():
ll.Infof("context done, exiting")
return
}
}
}
func (s *StackdriverExporter) reportBatch(ctx context.Context, batch []*monitoringpb.TimeSeries) error {
if len(batch) == 0 {
return nil
}
start := time.Now()
err := s.metricClient.CreateTimeSeries(ctx, &monitoringpb.CreateTimeSeriesRequest{
Name: fmt.Sprintf("projects/%s", s.googleCloudProject),
TimeSeries: batch,
})
if err != nil {
return fmt.Errorf("failed to create %d time series: %v", len(batch), err)
}
zap.S().Debugf("successfully reported %d time series in %v", len(batch), time.Since(start))
return nil
}