This repository has been archived by the owner on Dec 1, 2018. It is now read-only.
/
api.go
271 lines (246 loc) · 7.81 KB
/
api.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
// 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 v1
import (
"time"
restful "github.com/emicklei/go-restful"
"k8s.io/heapster/metrics/api/v1/types"
"k8s.io/heapster/metrics/core"
metricsink "k8s.io/heapster/metrics/sinks/metric"
)
type Api struct {
runningInKubernetes bool
metricSink *metricsink.MetricSink
gkeMetrics map[string]core.MetricDescriptor
gkeLabels map[string]core.LabelDescriptor
}
// Create a new Api to serve from the specified cache.
func NewApi(runningInKubernetes bool, metricSink *metricsink.MetricSink) *Api {
gkeMetrics := make(map[string]core.MetricDescriptor)
gkeLabels := make(map[string]core.LabelDescriptor)
for _, val := range core.StandardMetrics {
gkeMetrics[val.Name] = val.MetricDescriptor
}
for _, val := range core.LabeledMetrics {
gkeMetrics[val.Name] = val.MetricDescriptor
}
gkeMetrics[core.MetricCpuLimit.Name] = core.MetricCpuLimit.MetricDescriptor
gkeMetrics[core.MetricMemoryLimit.Name] = core.MetricMemoryLimit.MetricDescriptor
for _, val := range core.CommonLabels() {
gkeLabels[val.Key] = val
}
for _, val := range core.ContainerLabels() {
gkeLabels[val.Key] = val
}
for _, val := range core.PodLabels() {
gkeLabels[val.Key] = val
}
return &Api{
runningInKubernetes: runningInKubernetes,
metricSink: metricSink,
gkeMetrics: gkeMetrics,
gkeLabels: gkeLabels,
}
}
// Register the mainApi on the specified endpoint.
func (a *Api) Register(container *restful.Container) {
ws := new(restful.WebService)
ws.Path("/api/v1/metric-export").
Doc("Exports the latest point for all Heapster metrics").
Produces(restful.MIME_JSON)
ws.Route(ws.GET("").
To(a.exportMetrics).
Doc("export the latest data point for all metrics").
Operation("exportMetrics").
Writes([]*types.Timeseries{}))
container.Add(ws)
ws = new(restful.WebService)
ws.Path("/api/v1/metric-export-schema").
Doc("Schema for metrics exported by heapster").
Produces(restful.MIME_JSON)
ws.Route(ws.GET("").
To(a.exportMetricsSchema).
Doc("export the schema for all metrics").
Operation("exportmetricsSchema").
Writes(types.TimeseriesSchema{}))
container.Add(ws)
if a.metricSink != nil {
a.RegisterModel(container)
}
}
func convertLabelDescriptor(ld core.LabelDescriptor) types.LabelDescriptor {
return types.LabelDescriptor{
Key: ld.Key,
Description: ld.Description,
}
}
func convertMetricDescriptor(md core.MetricDescriptor) types.MetricDescriptor {
result := types.MetricDescriptor{
Name: md.Name,
Description: md.Description,
Labels: make([]types.LabelDescriptor, 0, len(md.Labels)),
}
for _, label := range md.Labels {
result.Labels = append(result.Labels, convertLabelDescriptor(label))
}
switch md.Type {
case core.MetricCumulative:
result.Type = "cumulative"
case core.MetricGauge:
result.Type = "gauge"
case core.MetricDelta:
result.Type = "delta"
}
switch md.ValueType {
case core.ValueInt64:
result.ValueType = "int64"
case core.ValueFloat:
result.ValueType = "double"
}
switch md.Units {
case core.UnitsBytes:
result.Units = "bytes"
case core.UnitsMilliseconds:
result.Units = "ms"
case core.UnitsNanoseconds:
result.Units = "ns"
case core.UnitsMillicores:
result.Units = "millicores"
}
return result
}
func (a *Api) exportMetricsSchema(_ *restful.Request, response *restful.Response) {
result := types.TimeseriesSchema{
Metrics: make([]types.MetricDescriptor, 0),
CommonLabels: make([]types.LabelDescriptor, 0),
PodLabels: make([]types.LabelDescriptor, 0),
}
for _, metric := range core.StandardMetrics {
if _, found := a.gkeMetrics[metric.Name]; found {
result.Metrics = append(result.Metrics, convertMetricDescriptor(metric.MetricDescriptor))
}
}
for _, metric := range core.AdditionalMetrics {
if _, found := a.gkeMetrics[metric.Name]; found {
result.Metrics = append(result.Metrics, convertMetricDescriptor(metric.MetricDescriptor))
}
}
for _, metric := range core.LabeledMetrics {
if _, found := a.gkeMetrics[metric.Name]; found {
result.Metrics = append(result.Metrics, convertMetricDescriptor(metric.MetricDescriptor))
}
}
for _, label := range core.CommonLabels() {
if _, found := a.gkeLabels[label.Key]; found {
result.CommonLabels = append(result.CommonLabels, convertLabelDescriptor(label))
}
}
for _, label := range core.ContainerLabels() {
if _, found := a.gkeLabels[label.Key]; found {
result.CommonLabels = append(result.CommonLabels, convertLabelDescriptor(label))
}
}
for _, label := range core.PodLabels() {
if _, found := a.gkeLabels[label.Key]; found {
result.PodLabels = append(result.PodLabels, convertLabelDescriptor(label))
}
}
response.WriteEntity(result)
}
func (a *Api) exportMetrics(_ *restful.Request, response *restful.Response) {
response.WriteEntity(a.processMetricsRequest(a.metricSink.GetShortStore()))
}
func (a *Api) processMetricsRequest(shortStorage []*core.DataBatch) []*types.Timeseries {
tsmap := make(map[string]*types.Timeseries)
var newestBatch *core.DataBatch
for _, batch := range shortStorage {
if newestBatch == nil || newestBatch.Timestamp.Before(batch.Timestamp) {
newestBatch = batch
}
}
var timeseries []*types.Timeseries
if newestBatch == nil {
return timeseries
}
for key, ms := range newestBatch.MetricSets {
ts := tsmap[key]
msType := ms.Labels[core.LabelMetricSetType.Key]
switch msType {
case core.MetricSetTypeNode, core.MetricSetTypePodContainer, core.MetricSetTypeSystemContainer:
default:
continue
}
if ts == nil {
ts = &types.Timeseries{
Metrics: make(map[string][]types.Point),
Labels: make(map[string]string),
}
for labelName, labelValue := range ms.Labels {
if _, ok := a.gkeLabels[labelName]; ok {
ts.Labels[labelName] = labelValue
}
}
if msType == core.MetricSetTypeNode {
ts.Labels[core.LabelContainerName.Key] = "machine"
}
tsmap[key] = ts
}
for metricName, metricVal := range ms.MetricValues {
if _, ok := a.gkeMetrics[metricName]; ok {
processPoint(ts, newestBatch, metricName, &metricVal, nil, ms.CreateTime)
}
}
for _, metric := range ms.LabeledMetrics {
if _, ok := a.gkeMetrics[metric.Name]; ok {
processPoint(ts, newestBatch, metric.Name, &metric.MetricValue, metric.Labels, ms.CreateTime)
}
}
}
timeseries = make([]*types.Timeseries, 0, len(tsmap))
for _, ts := range tsmap {
timeseries = append(timeseries, ts)
}
return timeseries
}
func processPoint(ts *types.Timeseries, db *core.DataBatch, metricName string, metricVal *core.MetricValue, labels map[string]string, creationTime time.Time) {
points := ts.Metrics[metricName]
if points == nil {
points = make([]types.Point, 0, 1)
}
point := types.Point{
Start: db.Timestamp,
End: db.Timestamp,
}
// For cumulative metric use the provided start time.
if metricVal.MetricType == core.MetricCumulative {
point.Start = creationTime
}
var value interface{}
if metricVal.ValueType == core.ValueInt64 {
value = metricVal.IntValue
} else if metricVal.ValueType == core.ValueFloat {
value = metricVal.FloatValue
} else {
return
}
point.Value = value
if labels != nil {
point.Labels = make(map[string]string)
for key, value := range labels {
point.Labels[key] = value
}
}
points = append(points, point)
ts.Metrics[metricName] = points
}