-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathstat_summary.go
More file actions
502 lines (423 loc) · 13.5 KB
/
stat_summary.go
File metadata and controls
502 lines (423 loc) · 13.5 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package public
import (
"context"
"fmt"
"math"
"strings"
"time"
proto "github.com/golang/protobuf/proto"
"github.com/prometheus/common/model"
"github.com/runconduit/conduit/controller/api/util"
pb "github.com/runconduit/conduit/controller/gen/public"
"github.com/runconduit/conduit/pkg/k8s"
log "github.com/sirupsen/logrus"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
)
type promType string
type promResult struct {
prom promType
vec model.Vector
err error
}
type resourceResult struct {
res *pb.StatTable
err error
}
const (
reqQuery = "sum(increase(response_total%s[%s])) by (%s, classification, tls)"
latencyQuantileQuery = "histogram_quantile(%s, sum(irate(response_latency_ms_bucket%s[%s])) by (le, %s))"
promRequests = promType("QUERY_REQUESTS")
promLatencyP50 = promType("0.5")
promLatencyP95 = promType("0.95")
promLatencyP99 = promType("0.99")
namespaceLabel = model.LabelName("namespace")
dstNamespaceLabel = model.LabelName("dst_namespace")
)
var promTypes = []promType{promRequests, promLatencyP50, promLatencyP95, promLatencyP99}
type podStats struct {
inMesh uint64
total uint64
failed uint64
errors map[string]*pb.PodErrors
}
func (s *grpcServer) StatSummary(ctx context.Context, req *pb.StatSummaryRequest) (*pb.StatSummaryResponse, error) {
// check for well-formed request
if req.GetSelector().GetResource() == nil {
return statSummaryError(req, "StatSummary request missing Selector Resource"), nil
}
// special case to check for services as outbound only
if isInvalidServiceRequest(req) {
return statSummaryError(req, "service only supported as a target on 'from' queries, or as a destination on 'to' queries"), nil
}
switch req.Outbound.(type) {
case *pb.StatSummaryRequest_ToResource:
if req.Outbound.(*pb.StatSummaryRequest_ToResource).ToResource.Type == k8s.All {
return statSummaryError(req, "resource type 'all' is not supported as a filter"), nil
}
case *pb.StatSummaryRequest_FromResource:
if req.Outbound.(*pb.StatSummaryRequest_FromResource).FromResource.Type == k8s.All {
return statSummaryError(req, "resource type 'all' is not supported as a filter"), nil
}
}
statTables := make([]*pb.StatTable, 0)
var resourcesToQuery []string
if req.Selector.Resource.Type == k8s.All {
resourcesToQuery = k8s.StatAllResourceTypes
} else {
resourcesToQuery = []string{req.Selector.Resource.Type}
}
// request stats for the resourcesToQuery, in parallel
resultChan := make(chan resourceResult)
for _, resource := range resourcesToQuery {
statReq := proto.Clone(req).(*pb.StatSummaryRequest)
statReq.Selector.Resource.Type = resource
go func() {
resultChan <- s.resourceQuery(ctx, statReq)
}()
}
for i := 0; i < len(resourcesToQuery); i++ {
result := <-resultChan
if result.err != nil {
return nil, util.GRPCError(result.err)
}
statTables = append(statTables, result.res)
}
rsp := pb.StatSummaryResponse{
Response: &pb.StatSummaryResponse_Ok_{ // https://github.com/golang/protobuf/issues/205
Ok: &pb.StatSummaryResponse_Ok{
StatTables: statTables,
},
},
}
return &rsp, nil
}
func statSummaryError(req *pb.StatSummaryRequest, message string) *pb.StatSummaryResponse {
return &pb.StatSummaryResponse{
Response: &pb.StatSummaryResponse_Error{
Error: &pb.ResourceError{
Resource: req.GetSelector().GetResource(),
Error: message,
},
},
}
}
func (s *grpcServer) resourceQuery(ctx context.Context, req *pb.StatSummaryRequest) resourceResult {
objects, err := s.k8sAPI.GetObjects(req.Selector.Resource.Namespace, req.Selector.Resource.Type, req.Selector.Resource.Name)
if err != nil {
return resourceResult{res: nil, err: err}
}
// TODO: make these one struct:
// string => {metav1.ObjectMeta, podCount}
objectMap := map[string]metav1.Object{}
podStatsMap := map[string]*podStats{}
for _, object := range objects {
key, err := cache.MetaNamespaceKeyFunc(object)
if err != nil {
return resourceResult{res: nil, err: err}
}
metaObj, err := meta.Accessor(object)
if err != nil {
return resourceResult{res: nil, err: err}
}
objectMap[key] = metaObj
podStats, err := s.getPodStats(object)
if err != nil {
return resourceResult{res: nil, err: err}
}
podStatsMap[key] = podStats
}
res, err := s.objectQuery(ctx, req, objectMap, podStatsMap)
if err != nil {
return resourceResult{res: nil, err: err}
}
return resourceResult{res: res, err: nil}
}
func (s *grpcServer) objectQuery(
ctx context.Context,
req *pb.StatSummaryRequest,
objects map[string]metav1.Object,
podStats map[string]*podStats,
) (*pb.StatTable, error) {
rows := make([]*pb.StatTable_PodGroup_Row, 0)
requestMetrics, err := s.getPrometheusMetrics(ctx, req, req.TimeWindow)
if err != nil {
return nil, err
}
var keys []string
if req.GetOutbound() == nil || req.GetNone() != nil {
// if this request doesn't have outbound filtering, return all rows
for key := range objects {
keys = append(keys, key)
}
} else {
// otherwise only return rows for which we have stats
for key := range requestMetrics {
keys = append(keys, key)
}
}
for _, key := range keys {
resource, ok := objects[key]
if !ok {
continue
}
row := pb.StatTable_PodGroup_Row{
Resource: &pb.Resource{
Namespace: resource.GetNamespace(),
Type: req.Selector.Resource.Type,
Name: resource.GetName(),
},
TimeWindow: req.TimeWindow,
Stats: requestMetrics[key],
}
if podStat, ok := podStats[key]; ok {
row.MeshedPodCount = podStat.inMesh
row.RunningPodCount = podStat.total
row.FailedPodCount = podStat.failed
row.ErrorsByPod = podStat.errors
}
rows = append(rows, &row)
}
rsp := pb.StatTable{
Table: &pb.StatTable_PodGroup_{
PodGroup: &pb.StatTable_PodGroup{
Rows: rows,
},
},
}
return &rsp, nil
}
func promLabelNames(resource *pb.Resource) model.LabelNames {
names := model.LabelNames{namespaceLabel}
if resource.Type != k8s.Namespaces {
names = append(names, promResourceType(resource))
}
return names
}
func promDstLabelNames(resource *pb.Resource) model.LabelNames {
names := model.LabelNames{dstNamespaceLabel}
if resource.Type != k8s.Namespaces {
names = append(names, "dst_"+promResourceType(resource))
}
return names
}
func promLabels(resource *pb.Resource) model.LabelSet {
set := model.LabelSet{}
if resource.Name != "" {
set[promResourceType(resource)] = model.LabelValue(resource.Name)
}
if resource.Type != k8s.Namespaces && resource.Namespace != "" {
set[namespaceLabel] = model.LabelValue(resource.Namespace)
}
return set
}
func promDstLabels(resource *pb.Resource) model.LabelSet {
set := model.LabelSet{}
if resource.Name != "" {
set["dst_"+promResourceType(resource)] = model.LabelValue(resource.Name)
}
if resource.Type != k8s.Namespaces && resource.Namespace != "" {
set[dstNamespaceLabel] = model.LabelValue(resource.Namespace)
}
return set
}
func promDirectionLabels(direction string) model.LabelSet {
return model.LabelSet{
model.LabelName("direction"): model.LabelValue(direction),
}
}
func promResourceType(resource *pb.Resource) model.LabelName {
return model.LabelName(k8s.ResourceTypesToProxyLabels[resource.Type])
}
func buildRequestLabels(req *pb.StatSummaryRequest) (labels model.LabelSet, labelNames model.LabelNames) {
// labelNames: the group by in the prometheus query
// labels: the labels for the resource we want to query for
switch out := req.Outbound.(type) {
case *pb.StatSummaryRequest_ToResource:
labelNames = promLabelNames(req.Selector.Resource)
labels = labels.Merge(promDstLabels(out.ToResource))
labels = labels.Merge(promLabels(req.Selector.Resource))
labels = labels.Merge(promDirectionLabels("outbound"))
case *pb.StatSummaryRequest_FromResource:
labelNames = promDstLabelNames(req.Selector.Resource)
labels = labels.Merge(promLabels(out.FromResource))
labels = labels.Merge(promDirectionLabels("outbound"))
default:
labelNames = promLabelNames(req.Selector.Resource)
labels = labels.Merge(promLabels(req.Selector.Resource))
labels = labels.Merge(promDirectionLabels("inbound"))
}
return
}
func (s *grpcServer) getPrometheusMetrics(ctx context.Context, req *pb.StatSummaryRequest, timeWindow string) (map[string]*pb.BasicStats, error) {
reqLabels, groupBy := buildRequestLabels(req)
resultChan := make(chan promResult)
// kick off 4 asynchronous queries: 1 request volume + 3 latency
go func() {
// success/failure counts
requestsQuery := fmt.Sprintf(reqQuery, reqLabels, timeWindow, groupBy)
resultVector, err := s.queryProm(ctx, requestsQuery)
resultChan <- promResult{
prom: promRequests,
vec: resultVector,
err: err,
}
}()
for _, quantile := range []promType{promLatencyP50, promLatencyP95, promLatencyP99} {
go func(quantile promType) {
latencyQuery := fmt.Sprintf(latencyQuantileQuery, quantile, reqLabels, timeWindow, groupBy)
latencyResult, err := s.queryProm(ctx, latencyQuery)
resultChan <- promResult{
prom: quantile,
vec: latencyResult,
err: err,
}
}(quantile)
}
// process results, receive one message per prometheus query type
var err error
results := []promResult{}
for i := 0; i < len(promTypes); i++ {
result := <-resultChan
if result.err != nil {
log.Errorf("queryProm failed with: %s", result.err)
err = result.err
} else {
results = append(results, result)
}
}
if err != nil {
return nil, err
}
return processPrometheusMetrics(results, groupBy), nil
}
func processPrometheusMetrics(results []promResult, groupBy model.LabelNames) map[string]*pb.BasicStats {
basicStats := make(map[string]*pb.BasicStats)
for _, result := range results {
for _, sample := range result.vec {
label := metricToKey(sample.Metric, groupBy)
if basicStats[label] == nil {
basicStats[label] = &pb.BasicStats{}
}
value := extractSampleValue(sample)
switch result.prom {
case promRequests:
switch string(sample.Metric[model.LabelName("classification")]) {
case "success":
basicStats[label].SuccessCount += value
case "failure":
basicStats[label].FailureCount += value
}
switch string(sample.Metric[model.LabelName("tls")]) {
case "true":
basicStats[label].TlsRequestCount += value
}
case promLatencyP50:
basicStats[label].LatencyMsP50 = value
case promLatencyP95:
basicStats[label].LatencyMsP95 = value
case promLatencyP99:
basicStats[label].LatencyMsP99 = value
}
}
}
return basicStats
}
func extractSampleValue(sample *model.Sample) uint64 {
value := uint64(0)
if !math.IsNaN(float64(sample.Value)) {
value = uint64(math.Round(float64(sample.Value)))
}
return value
}
func metricToKey(metric model.Metric, groupBy model.LabelNames) string {
// this needs to match keys generated by MetaNamespaceKeyFunc
values := []string{}
for _, k := range groupBy {
// return namespace/resource
values = append(values, string(metric[k]))
}
return strings.Join(values, "/")
}
func (s *grpcServer) getPodStats(obj runtime.Object) (*podStats, error) {
pods, err := s.k8sAPI.GetPodsFor(obj, true)
if err != nil {
return nil, err
}
podErrors := make(map[string]*pb.PodErrors)
meshCount := &podStats{}
for _, pod := range pods {
if pod.Status.Phase == apiv1.PodFailed {
meshCount.failed++
} else {
meshCount.total++
if isInMesh(pod) {
meshCount.inMesh++
}
}
errors := checkContainerErrors(pod.Status.ContainerStatuses, "conduit-proxy")
errors = append(errors, checkContainerErrors(pod.Status.InitContainerStatuses, "conduit-init")...)
if len(errors) > 0 {
podErrors[pod.Name] = &pb.PodErrors{Errors: errors}
}
}
meshCount.errors = podErrors
return meshCount, nil
}
func toPodError(container, image, message string) *pb.PodErrors_PodError {
return &pb.PodErrors_PodError{
Error: &pb.PodErrors_PodError_Container{
Container: &pb.PodErrors_PodError_ContainerError{
Message: message,
Container: container,
Image: image,
},
},
}
}
func checkContainerErrors(containerStatuses []apiv1.ContainerStatus, containerName string) []*pb.PodErrors_PodError {
errors := []*pb.PodErrors_PodError{}
for _, st := range containerStatuses {
if st.Name == containerName && st.State.Waiting != nil {
errors = append(errors, toPodError(st.Name, st.Image, st.State.Waiting.Message))
if st.LastTerminationState.Waiting != nil {
errors = append(errors, toPodError(st.Name, st.Image, st.LastTerminationState.Waiting.Message))
}
if st.LastTerminationState.Terminated != nil {
errors = append(errors, toPodError(st.Name, st.Image, st.LastTerminationState.Terminated.Message))
}
}
}
return errors
}
func isInMesh(pod *apiv1.Pod) bool {
_, ok := pod.Annotations[k8s.ProxyVersionAnnotation]
return ok
}
func isInvalidServiceRequest(req *pb.StatSummaryRequest) bool {
fromResource := req.GetFromResource()
if fromResource != nil {
return fromResource.Type == k8s.Services
} else {
return req.Selector.Resource.Type == k8s.Services
}
}
func (s *grpcServer) queryProm(ctx context.Context, query string) (model.Vector, error) {
log.Debugf("Query request:\n\t%+v", query)
// single data point (aka summary) query
res, err := s.prometheusAPI.Query(ctx, query, time.Time{})
if err != nil {
log.Errorf("Query(%+v) failed with: %+v", query, err)
return nil, err
}
log.Debugf("Query response:\n\t%+v", res)
if res.Type() != model.ValVector {
err = fmt.Errorf("Unexpected query result type (expected Vector): %s", res.Type())
log.Error(err)
return nil, err
}
return res.(model.Vector), nil
}