forked from GoogleCloudPlatform/k8s-stackdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll.go
92 lines (80 loc) · 2.91 KB
/
poll.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
/*
Copyright 2017 Google Inc.
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 monitor
import (
"time"
log "github.com/golang/glog"
v3 "google.golang.org/api/monitoring/v3"
)
const maxTimeSeriesPerRequest = 200
// SourceConfig is the set of data required to configure a kubernetes
// data source (e.g., kubelet or kube-controller).
type SourceConfig struct {
Zone, Project, Cluster, Host, Instance string
Port uint
Resolution time.Duration
}
// MetricsSource is an object that provides kubernetes metrics in
// Stackdriver format, probably from a backend like the kubelet.
type MetricsSource interface {
GetTimeSeriesReq() (*v3.CreateTimeSeriesRequest, error)
Name() string
ProjectPath() string
}
// Once polls the backend and puts the data to the given service one time.
func Once(src MetricsSource, gcm *v3.Service) {
req, err := src.GetTimeSeriesReq()
if err != nil {
observeFailedScrape(src.Name())
log.Warningf("Failed to create time series request: %v", err)
return
}
observeSuccessfullScrape(src.Name())
for _, subReq := range subRequests(req) {
// Push that data to GCM's v3 API.
createCall := gcm.Projects.TimeSeries.Create(src.ProjectPath(), subReq)
if empty, err := createCall.Do(); err != nil {
log.Warningf("Failed to write time series data, empty: %v, err: %v", empty, err)
observeFailedRequest(len(subReq.TimeSeries))
jsonReq, err := subReq.MarshalJSON()
if err != nil {
log.Warningf("Failed to marshal time series as JSON")
return
}
log.Warningf("JSON GCM: %s", string(jsonReq[:]))
return
}
log.V(4).Infof("Successfully wrote TimeSeries data for %s to GCM v3 API.", src.Name())
observeSuccessfullRequest(len(subReq.TimeSeries))
}
}
func subRequests(req *v3.CreateTimeSeriesRequest) []*v3.CreateTimeSeriesRequest {
tsCount := len(req.TimeSeries)
if tsCount <= maxTimeSeriesPerRequest {
return []*v3.CreateTimeSeriesRequest{req}
}
subRequestsCount := (tsCount-1)/maxTimeSeriesPerRequest + 1
log.V(2).Infof("Splitting CreateTimeSeriesRequest into %v requests", subRequestsCount)
subReqs := make([]*v3.CreateTimeSeriesRequest, subRequestsCount)
for i := 0; i < subRequestsCount; i++ {
startIdx := i * maxTimeSeriesPerRequest
endIdx := (i + 1) * maxTimeSeriesPerRequest
if endIdx > tsCount {
endIdx = tsCount
}
subReqs[i] = &v3.CreateTimeSeriesRequest{
TimeSeries: req.TimeSeries[startIdx:endIdx],
}
}
return subReqs
}