This repository has been archived by the owner on Dec 1, 2018. It is now read-only.
/
gcm.go
341 lines (302 loc) · 9.27 KB
/
gcm.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gcm
import (
"fmt"
"net/url"
"strings"
"sync"
"time"
gce_util "k8s.io/heapster/common/gce"
"k8s.io/heapster/metrics/core"
"github.com/golang/glog"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
gcm "google.golang.org/api/cloudmonitoring/v2beta2"
gce "google.golang.org/cloud/compute/metadata"
)
const (
metricDomain = "kubernetes.io"
customApiPrefix = "custom.cloudmonitoring.googleapis.com"
maxNumLabels = 10
// The largest number of timeseries we can write to per request.
maxTimeseriesPerRequest = 200
)
type MetricFilter int8
const (
metricsAll MetricFilter = iota
metricsOnlyAutoscaling
)
type gcmSink struct {
sync.RWMutex
registered bool
project string
metricFilter MetricFilter
gcmService *gcm.Service
}
func (sink *gcmSink) Name() string {
return "GCM Sink"
}
func getReq() *gcm.WriteTimeseriesRequest {
return &gcm.WriteTimeseriesRequest{Timeseries: make([]*gcm.TimeseriesPoint, 0)}
}
func fullLabelName(name string) string {
// handle correctly GCE specific labels
if !strings.Contains(name, "compute.googleapis.com") {
return fmt.Sprintf("%s/%s/label/%s", customApiPrefix, metricDomain, name)
}
return name
}
func fullMetricName(name string) string {
return fmt.Sprintf("%s/%s/%s", customApiPrefix, metricDomain, name)
}
func (sink *gcmSink) getTimeseriesPoint(timestamp time.Time, labels map[string]string, metric string, val core.MetricValue, createTime time.Time) *gcm.TimeseriesPoint {
point := &gcm.Point{
Start: timestamp.Format(time.RFC3339),
End: timestamp.Format(time.RFC3339),
}
switch val.ValueType {
case core.ValueInt64:
point.Int64Value = &val.IntValue
case core.ValueFloat:
v := float64(val.FloatValue)
point.DoubleValue = &v
default:
glog.Errorf("Type not supported %v in %v", val.ValueType, metric)
return nil
}
// For cumulative metric use the provided start time.
if val.MetricType == core.MetricCumulative {
point.Start = createTime.Format(time.RFC3339)
}
finalLabels := make(map[string]string)
if core.IsNodeAutoscalingMetric(metric) {
// All and autoscaling. Do not populate for other filters.
if sink.metricFilter != metricsAll &&
sink.metricFilter != metricsOnlyAutoscaling {
return nil
}
finalLabels[fullLabelName(core.LabelHostname.Key)] = labels[core.LabelHostname.Key]
finalLabels[fullLabelName(core.LabelGCEResourceID.Key)] = labels[core.LabelHostID.Key]
finalLabels[fullLabelName(core.LabelGCEResourceType.Key)] = "instance"
} else {
// Only all.
if sink.metricFilter != metricsAll {
return nil
}
supportedLables := core.GcmLabels()
for key, value := range labels {
if _, ok := supportedLables[key]; ok {
finalLabels[fullLabelName(key)] = value
}
}
}
desc := &gcm.TimeseriesDescriptor{
Project: sink.project,
Labels: finalLabels,
Metric: fullMetricName(metric),
}
return &gcm.TimeseriesPoint{Point: point, TimeseriesDesc: desc}
}
func (sink *gcmSink) getTimeseriesPointForLabeledMetrics(timestamp time.Time, labels map[string]string, metric core.LabeledMetric, createTime time.Time) *gcm.TimeseriesPoint {
// Only all. There are no atuoscaling labeled metrics.
if sink.metricFilter != metricsAll {
return nil
}
point := &gcm.Point{
Start: timestamp.Format(time.RFC3339),
End: timestamp.Format(time.RFC3339),
}
switch metric.ValueType {
case core.ValueInt64:
point.Int64Value = &metric.IntValue
case core.ValueFloat:
v := float64(metric.FloatValue)
point.DoubleValue = &v
default:
glog.Errorf("Type not supported %v in %v", metric.ValueType, metric)
return nil
}
// For cumulative metric use the provided start time.
if metric.MetricType == core.MetricCumulative {
point.Start = createTime.Format(time.RFC3339)
}
finalLabels := make(map[string]string)
supportedLables := core.GcmLabels()
for key, value := range labels {
if _, ok := supportedLables[key]; ok {
finalLabels[fullLabelName(key)] = value
}
}
for key, value := range metric.Labels {
if _, ok := supportedLables[key]; ok {
finalLabels[fullLabelName(key)] = value
}
}
desc := &gcm.TimeseriesDescriptor{
Project: sink.project,
Labels: finalLabels,
Metric: fullMetricName(metric.Name),
}
return &gcm.TimeseriesPoint{Point: point, TimeseriesDesc: desc}
}
func (sink *gcmSink) sendRequest(req *gcm.WriteTimeseriesRequest) {
_, err := sink.gcmService.Timeseries.Write(sink.project, req).Do()
if err != nil {
glog.Errorf("Error while sending request to GCM %v", err)
} else {
glog.V(4).Infof("Successfully sent %v timeserieses to GCM", len(req.Timeseries))
}
}
func (sink *gcmSink) ExportData(dataBatch *core.DataBatch) {
if err := sink.registerAllMetrics(); err != nil {
glog.Warningf("Error during metrics registration: %v", err)
return
}
req := getReq()
for _, metricSet := range dataBatch.MetricSets {
for metric, val := range metricSet.MetricValues {
point := sink.getTimeseriesPoint(dataBatch.Timestamp, metricSet.Labels, metric, val, metricSet.CreateTime)
if point != nil {
req.Timeseries = append(req.Timeseries, point)
}
if len(req.Timeseries) >= maxTimeseriesPerRequest {
sink.sendRequest(req)
req = getReq()
}
}
for _, metric := range metricSet.LabeledMetrics {
point := sink.getTimeseriesPointForLabeledMetrics(dataBatch.Timestamp, metricSet.Labels, metric, metricSet.CreateTime)
if point != nil {
req.Timeseries = append(req.Timeseries, point)
}
if len(req.Timeseries) >= maxTimeseriesPerRequest {
sink.sendRequest(req)
req = getReq()
}
}
}
if len(req.Timeseries) > 0 {
sink.sendRequest(req)
}
}
func (sink *gcmSink) Stop() {
// nothing needs to be done.
}
func (sink *gcmSink) registerAllMetrics() error {
return sink.register(core.AllMetrics)
}
// Adds the specified metrics or updates them if they already exist.
func (sink *gcmSink) register(metrics []core.Metric) error {
sink.Lock()
defer sink.Unlock()
if sink.registered {
return nil
}
for _, metric := range metrics {
metricName := fullMetricName(metric.MetricDescriptor.Name)
if _, err := sink.gcmService.MetricDescriptors.Delete(sink.project, metricName).Do(); err != nil {
glog.Infof("[GCM] Deleting metric %v failed: %v", metricName, err)
}
labels := make([]*gcm.MetricDescriptorLabelDescriptor, 0)
// Node autoscaling metrics have special labels.
if core.IsNodeAutoscalingMetric(metric.MetricDescriptor.Name) {
// All and autoscaling. Do not populate for other filters.
if sink.metricFilter != metricsAll &&
sink.metricFilter != metricsOnlyAutoscaling {
continue
}
for _, l := range core.GcmNodeAutoscalingLabels() {
labels = append(labels, &gcm.MetricDescriptorLabelDescriptor{
Key: fullLabelName(l.Key),
Description: l.Description,
})
}
} else {
// Only all.
if sink.metricFilter != metricsAll {
continue
}
for _, l := range core.GcmLabels() {
labels = append(labels, &gcm.MetricDescriptorLabelDescriptor{
Key: fullLabelName(l.Key),
Description: l.Description,
})
}
}
t := &gcm.MetricDescriptorTypeDescriptor{
MetricType: metric.MetricDescriptor.Type.String(),
ValueType: metric.MetricDescriptor.ValueType.String(),
}
desc := &gcm.MetricDescriptor{
Name: metricName,
Project: sink.project,
Description: metric.MetricDescriptor.Description,
Labels: labels,
TypeDescriptor: t,
}
if _, err := sink.gcmService.MetricDescriptors.Create(sink.project, desc).Do(); err != nil {
return err
}
}
sink.registered = true
return nil
}
func CreateGCMSink(uri *url.URL) (core.DataSink, error) {
if len(uri.Scheme) > 0 {
return nil, fmt.Errorf("scheme should not be set for GCM sink")
}
if len(uri.Host) > 0 {
return nil, fmt.Errorf("host should not be set for GCM sink")
}
opts, err := url.ParseQuery(uri.RawQuery)
metrics := "all"
if len(opts["metrics"]) > 0 {
metrics = opts["metrics"][0]
}
var metricFilter MetricFilter = metricsAll
switch metrics {
case "all":
metricFilter = metricsAll
case "autoscaling":
metricFilter = metricsOnlyAutoscaling
default:
return nil, fmt.Errorf("invalid metrics parameter: %s", metrics)
}
if err := gce_util.EnsureOnGCE(); err != nil {
return nil, err
}
// Detect project ID
projectId, err := gce.ProjectID()
if err != nil {
return nil, err
}
// Create Google Cloud Monitoring service.
client := oauth2.NewClient(oauth2.NoContext, google.ComputeTokenSource(""))
gcmService, err := gcm.New(client)
if err != nil {
return nil, err
}
sink := &gcmSink{
registered: false,
project: projectId,
gcmService: gcmService,
metricFilter: metricFilter,
}
glog.Infof("created GCM sink")
if err := sink.registerAllMetrics(); err != nil {
glog.Warningf("Error during metrics registration: %v", err)
}
return sink, nil
}