-
Notifications
You must be signed in to change notification settings - Fork 474
/
istio.go
951 lines (850 loc) · 38.4 KB
/
istio.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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
// Package istio provides the Istio implementation of graph/TelemetryProvider.
package istio
// Istio.go is responsible for generating TrafficMaps using Istio telemetry. It implements the
// TelemetryVendor interface.
//
// The algorithm:
// Step 1) For each namespace:
// a) Query Prometheus (istio-requests-total metric) to retrieve the source-destination
// dependencies. Build a traffic map to provide a full representation of nodes and edges.
//
// b) Apply any requested appenders to alter or append-to the namespace traffic-map.
//
// c) Merge the namespace traffic-map into the final traffic-map
//
// Step 2) For the global traffic map
// a) Apply standard and requested finalizers to alter or append-to the final traffic-map
//
// b) Convert the final traffic-map to the requested vendor configiration (i.e. Cytoscape) and return
//
// Supports three vendor-specific query parameters:
// aggregate: Must be a valid metric attribute (default: request_operation)
// responseTime: Must be one of: avg | 50 | 95 | 99
// throughputType: request | response (default: response)
//
import (
"context"
"crypto/md5"
"fmt"
"regexp"
"strings"
"time"
prom_v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/kiali/kiali/graph"
"github.com/kiali/kiali/graph/telemetry"
"github.com/kiali/kiali/graph/telemetry/istio/appender"
"github.com/kiali/kiali/graph/telemetry/istio/util"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/observability"
"github.com/kiali/kiali/prometheus"
"github.com/kiali/kiali/prometheus/internalmetrics"
)
const (
tsHash graph.MetadataKey = "tsHash"
tsHashMap graph.MetadataKey = "tsHashMap"
)
var grpcMetric = regexp.MustCompile(`istio_.*_messages`)
// BuildNamespacesTrafficMap is required by the graph/TelemetryVendor interface
func BuildNamespacesTrafficMap(ctx context.Context, o graph.TelemetryOptions, client *prometheus.Client, globalInfo *graph.AppenderGlobalInfo) graph.TrafficMap {
var end observability.EndFunc
ctx, end = observability.StartSpan(ctx, "BuildNamespacesTrafficMap",
observability.Attribute("package", "istio"),
)
defer end()
log.Tracef("Build [%s] graph for [%d] namespaces [%v]", o.GraphType, len(o.Namespaces), o.Namespaces)
appenders, finalizers := appender.ParseAppenders(o)
trafficMap := graph.NewTrafficMap()
for _, namespace := range o.Namespaces {
log.Tracef("Build traffic map for namespace [%v]", namespace)
namespaceTrafficMap := buildNamespaceTrafficMap(ctx, namespace.Name, o, client)
// The appenders can add/remove/alter nodes for the namespace
namespaceInfo := graph.NewAppenderNamespaceInfo(namespace.Name)
for _, a := range appenders {
var appenderEnd observability.EndFunc
_, appenderEnd = observability.StartSpan(ctx, "Appender "+a.Name(),
observability.Attribute("package", "istio"),
observability.Attribute("namespace", namespace.Name),
)
appenderTimer := internalmetrics.GetGraphAppenderTimePrometheusTimer(a.Name())
a.AppendGraph(namespaceTrafficMap, globalInfo, namespaceInfo)
appenderTimer.ObserveDuration()
appenderEnd()
}
// Merge this namespace into the final TrafficMap
telemetry.MergeTrafficMaps(trafficMap, namespace.Name, namespaceTrafficMap)
}
// The finalizers can perform final manipulations on the complete graph
for _, f := range finalizers {
f.AppendGraph(trafficMap, globalInfo, nil)
}
if graph.GraphTypeService == o.GraphType {
trafficMap = telemetry.ReduceToServiceGraph(trafficMap)
}
return trafficMap
}
// buildNamespaceTrafficMap returns a map of all namespace nodes (key=id). All
// nodes either directly send and/or receive requests from a node in the namespace.
func buildNamespaceTrafficMap(ctx context.Context, namespace string, o graph.TelemetryOptions, client *prometheus.Client) graph.TrafficMap {
var end observability.EndFunc
_, end = observability.StartSpan(ctx, "buildNamespaceTrafficMap",
observability.Attribute("package", "istio"),
observability.Attribute("namespace", namespace),
)
defer end()
// create map to aggregate traffic by protocol and response code
trafficMap := graph.NewTrafficMap()
duration := o.Namespaces[namespace].Duration
idleCondition := "> 0"
if o.IncludeIdleEdges {
idleCondition = ""
}
// HTTP/GRPC request traffic
if o.Rates.Http == graph.RateRequests || o.Rates.Grpc == graph.RateRequests {
metric := "istio_requests_total"
groupBy := "source_cluster,source_workload_namespace,source_workload,source_canonical_service,source_canonical_revision,destination_cluster,destination_service_namespace,destination_service,destination_service_name,destination_workload_namespace,destination_workload,destination_canonical_service,destination_canonical_revision,request_protocol,response_code,grpc_response_status,response_flags"
// 0) Incoming: query source telemetry to capture unserviced namespace services' incoming traffic
query := fmt.Sprintf(`sum(rate(%s{reporter="source",source_workload_namespace!="%s",destination_workload_namespace="unknown",destination_workload="unknown",destination_service=~"^.+\\.%s\\..+$"} [%vs])) by (%s) %s`,
metric,
namespace,
namespace,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
incomingVector := promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &incomingVector, metric, o)
// 1) Incoming: query destination telemetry to capture namespace services' incoming traffic
query = fmt.Sprintf(`sum(rate(%s{reporter="destination",destination_workload_namespace="%s"} [%vs])) by (%s) %s`,
metric,
namespace,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
incomingVector = promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &incomingVector, metric, o)
// 2) Outgoing: query source telemetry to capture namespace workloads' outgoing traffic
query = fmt.Sprintf(`sum(rate(%s{reporter="source",source_workload_namespace="%s"} [%vs])) by (%s) %s`,
metric,
namespace,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
outgoingVector := promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &outgoingVector, metric, o)
}
// GRPC Message traffic
if o.Rates.Grpc != graph.RateNone && o.Rates.Grpc != graph.RateRequests {
var metrics []string
groupBy := "source_cluster,source_workload_namespace,source_workload,source_canonical_service,source_canonical_revision,destination_cluster,destination_service_namespace,destination_service,destination_service_name,destination_workload_namespace,destination_workload,destination_canonical_service,destination_canonical_revision"
switch o.Rates.Grpc {
case graph.RateReceived:
metrics = []string{"istio_response_messages_total"}
case graph.RateSent:
metrics = []string{"istio_request_messages_total"}
case graph.RateTotal:
metrics = []string{"istio_request_messages_total", "istio_response_messages_total"}
default:
metrics = []string{}
}
for _, metric := range metrics {
// 0) Incoming: query source telemetry to capture unserviced namespace services' incoming traffic
query := fmt.Sprintf(`sum(rate(%s{reporter="source",source_workload_namespace!="%s",destination_workload_namespace="unknown",destination_workload="unknown",destination_service=~"^.+\\.%s\\..+$"} [%vs])) by (%s) %s`,
metric,
namespace,
namespace,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
incomingVector := promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &incomingVector, metric, o)
// 1) Incoming: query destination telemetry to capture namespace services' incoming traffic query = fmt.Sprintf(`sum(rate(%s{reporter="destination",destination_service_namespace="%s"} [%vs])) by (%s) %s`,
query = fmt.Sprintf(`sum(rate(%s{reporter="destination",destination_workload_namespace="%s"} [%vs])) by (%s) %s`,
metric,
namespace,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
incomingVector = promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &incomingVector, metric, o)
// 2) Outgoing: query source telemetry to capture namespace workloads' outgoing traffic
query = fmt.Sprintf(`sum(rate(%s{reporter="source",source_workload_namespace="%s"} [%vs])) by (%s) %s`,
metric,
namespace,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
outgoingVector := promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &outgoingVector, metric, o)
}
}
// TCP Byte traffic
if o.Rates.Tcp != graph.RateNone {
var metrics []string
groupBy := "source_cluster,source_workload_namespace,source_workload,source_canonical_service,source_canonical_revision,destination_cluster,destination_service_namespace,destination_service,destination_service_name,destination_workload_namespace,destination_workload,destination_canonical_service,destination_canonical_revision,response_flags"
switch o.Rates.Tcp {
case graph.RateReceived:
metrics = []string{"istio_tcp_received_bytes_total"}
case graph.RateSent:
metrics = []string{"istio_tcp_sent_bytes_total"}
case graph.RateTotal:
metrics = []string{"istio_tcp_sent_bytes_total", "istio_tcp_received_bytes_total"}
default:
metrics = []string{}
}
for _, metric := range metrics {
// 0) Incoming: query source telemetry to capture unserviced namespace services' incoming traffic
query := fmt.Sprintf(`sum(rate(%s{reporter="source",source_workload_namespace!="%s",destination_workload_namespace="unknown",destination_workload="unknown",destination_service=~"^.+\\.%s\\..+$"} [%vs])) by (%s) %s`,
metric,
namespace,
namespace,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
incomingVector := promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &incomingVector, metric, o)
// 1) Incoming: query destination telemetry to capture namespace services' incoming traffic query = fmt.Sprintf(`sum(rate(%s{reporter="destination",destination_service_namespace="%s"} [%vs])) by (%s) %s`,
query = fmt.Sprintf(`sum(rate(%s{reporter="destination",destination_workload_namespace="%s"} [%vs])) by (%s) %s`,
metric,
namespace,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
incomingVector = promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &incomingVector, metric, o)
// 2) Outgoing: query source telemetry to capture namespace workloads' outgoing traffic
query = fmt.Sprintf(`sum(rate(%s{reporter="source",source_workload_namespace="%s"} [%vs])) by (%s) %s`,
metric,
namespace,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
outgoingVector := promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &outgoingVector, metric, o)
}
}
return trafficMap
}
func populateTrafficMap(trafficMap graph.TrafficMap, vector *model.Vector, metric string, o graph.TelemetryOptions) {
isRequests := true
protocol := ""
switch {
case grpcMetric.MatchString(metric):
isRequests = false
protocol = graph.GRPC.Name
case strings.HasPrefix(metric, "istio_tcp"):
isRequests = false
protocol = graph.TCP.Name
}
skipRequestsGrpc := isRequests && o.Rates.Grpc != graph.RateRequests
skipRequestsHttp := isRequests && o.Rates.Http != graph.RateRequests
for _, s := range *vector {
val := float64(s.Value)
m := s.Metric
lSourceCluster, sourceClusterOk := m["source_cluster"]
lSourceWlNs, sourceWlNsOk := m["source_workload_namespace"]
lSourceWl, sourceWlOk := m["source_workload"]
lSourceApp, sourceAppOk := m["source_canonical_service"]
lSourceVer, sourceVerOk := m["source_canonical_revision"]
lDestCluster, destClusterOk := m["destination_cluster"]
lDestSvcNs, destSvcNsOk := m["destination_service_namespace"]
lDestSvc, destSvcOk := m["destination_service"]
lDestSvcName, destSvcNameOk := m["destination_service_name"]
lDestWlNs, destWlNsOk := m["destination_workload_namespace"]
lDestWl, destWlOk := m["destination_workload"]
lDestApp, destAppOk := m["destination_canonical_service"]
lDestVer, destVerOk := m["destination_canonical_revision"]
if !sourceWlNsOk || !sourceWlOk || !sourceAppOk || !sourceVerOk || !destSvcNsOk || !destSvcOk || !destSvcNameOk || !destWlNsOk || !destWlOk || !destAppOk || !destVerOk {
log.Warningf("Skipping %s, missing expected TS labels", m.String())
continue
}
sourceWlNs := string(lSourceWlNs)
sourceWl := string(lSourceWl)
sourceApp := string(lSourceApp)
sourceVer := string(lSourceVer)
destSvc := string(lDestSvc)
flags := ""
if isRequests || protocol == graph.TCP.Name {
lFlags, flagsOk := m["response_flags"]
if !flagsOk {
log.Warningf("Skipping %s, missing expected TS labels", m.String())
continue
}
flags = string(lFlags)
}
// handle clusters
sourceCluster, destCluster := util.HandleClusters(lSourceCluster, sourceClusterOk, lDestCluster, destClusterOk)
if util.IsBadSourceTelemetry(sourceCluster, sourceClusterOk, sourceWlNs, sourceWl, sourceApp) {
continue
}
// handle unusual destinations
destCluster, destSvcNs, destSvcName, destWlNs, destWl, destApp, destVer, _ := util.HandleDestination(sourceCluster, sourceWlNs, sourceWl, destCluster, string(lDestSvcNs), string(lDestSvc), string(lDestSvcName), string(lDestWlNs), string(lDestWl), string(lDestApp), string(lDestVer))
if util.IsBadDestTelemetry(destCluster, destClusterOk, destSvcNs, destSvc, destSvcName, destWl) {
continue
}
var code string
if isRequests {
lProtocol, protocolOk := m["request_protocol"]
lCode, codeOk := m["response_code"]
lGrpc, grpcOk := m["grpc_response_status"]
if !protocolOk || !codeOk {
log.Warningf("Skipping %s, missing expected HTTP/GRPC TS labels", m.String())
continue
}
protocol = string(lProtocol)
if skipRequestsGrpc && protocol == graph.GRPC.Name || skipRequestsHttp && protocol == graph.HTTP.Name {
continue
}
// set response code in a backward compatible way
code = util.HandleResponseCode(protocol, string(lCode), grpcOk, string(lGrpc))
}
// make code more readable by setting "host" because "destSvc" holds destination.service.host | request.host | "unknown"
host := destSvc
// don't inject a service node if any of:
// - destSvcName is not set
// - destSvcName is PassthroughCluster (see https://github.com/kiali/kiali/issues/4488)
// - dest node is already a service node
inject := false
if o.InjectServiceNodes && graph.IsOK(destSvcName) && destSvcName != graph.PassthroughCluster {
_, destNodeType, err := graph.Id(destCluster, destSvcNs, destSvcName, destWlNs, destWl, destApp, destVer, o.GraphType)
if err != nil {
log.Warningf("Skipping %s, %s", m.String(), err)
continue
}
inject = (graph.NodeTypeService != destNodeType)
}
addTraffic(trafficMap, metric, inject, val, protocol, code, flags, host, sourceCluster, sourceWlNs, "", sourceWl, sourceApp, sourceVer, destCluster, destSvcNs, destSvcName, destWlNs, destWl, destApp, destVer, o)
}
}
func addTraffic(trafficMap graph.TrafficMap, metric string, inject bool, val float64, protocol, code, flags, host, sourceCluster, sourceNs, sourceSvc, sourceWl, sourceApp, sourceVer, destCluster, destSvcNs, destSvcName, destWlNs, destWl, destApp, destVer string, o graph.TelemetryOptions) {
source, _, err := addNode(trafficMap, sourceCluster, sourceNs, sourceSvc, sourceNs, sourceWl, sourceApp, sourceVer, o)
if err != nil {
log.Warningf("Skipping addTraffic (source), %s", err)
return
}
dest, _, err := addNode(trafficMap, destCluster, destSvcNs, destSvcName, destWlNs, destWl, destApp, destVer, o)
if err != nil {
log.Warningf("Skipping addTraffic (dest), %s", err)
return
}
// Istio can generate duplicate metrics by reporting from both the source and destination proxies. To avoid
// processing the same information twice we keep track of the time series applied to a particular edge. The
// edgeTSHash incorporates information about the time series' source, destination and metric information,
// and uses that unique TS has to protect against applying the same intomation twice.
edgeTSHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%s:%s:%s:%s:%s:%s", metric, source.Metadata[tsHash], dest.Metadata[tsHash], code, flags, host))))
if inject {
injectedService, _, err := addNode(trafficMap, destCluster, destSvcNs, destSvcName, "", "", "", "", o)
if err != nil {
log.Warningf("Skipping addTraffic (inject), %s", err)
return
}
if addEdgeTraffic(trafficMap, val, protocol, code, flags, host, source, injectedService, edgeTSHash, o) {
addToDestServices(injectedService.Metadata, destCluster, destSvcNs, destSvcName)
addEdgeTraffic(trafficMap, val, protocol, code, flags, host, injectedService, dest, edgeTSHash, o)
addToDestServices(dest.Metadata, destCluster, destSvcNs, destSvcName)
}
} else {
if addEdgeTraffic(trafficMap, val, protocol, code, flags, host, source, dest, edgeTSHash, o) {
addToDestServices(dest.Metadata, destCluster, destSvcNs, destSvcName)
}
}
}
// addEdgeTraffic uses edgeTSHash that the metric information has not been applied to the edge. Returns true
// if the the metric information is applied, false if it determined to be a duplicate.
func addEdgeTraffic(trafficMap graph.TrafficMap, val float64, protocol, code, flags, host string, source, dest *graph.Node, edgeTSHash string, o graph.TelemetryOptions) bool {
var edge *graph.Edge
for _, e := range source.Edges {
if dest.ID == e.Dest.ID && e.Metadata[graph.ProtocolKey] == protocol {
edge = e
break
}
}
if nil == edge {
edge = source.AddEdge(dest)
edge.Metadata[graph.ProtocolKey] = protocol
edge.Metadata[tsHashMap] = make(map[string]bool)
}
if _, ok := edge.Metadata[tsHashMap].(map[string]bool)[edgeTSHash]; !ok {
edge.Metadata[tsHashMap].(map[string]bool)[edgeTSHash] = true
graph.AddToMetadata(protocol, val, code, flags, host, source.Metadata, dest.Metadata, edge.Metadata)
return true
}
return false
}
func addToDestServices(md graph.Metadata, cluster, namespace, service string) {
if !graph.IsOK(service) {
return
}
destServices, ok := md[graph.DestServices]
if !ok {
destServices = graph.NewDestServicesMetadata()
md[graph.DestServices] = destServices
}
destService := graph.ServiceName{Cluster: cluster, Namespace: namespace, Name: service}
destServices.(graph.DestServicesMetadata)[destService.Key()] = destService
}
func addNode(trafficMap graph.TrafficMap, cluster, serviceNs, service, workloadNs, workload, app, version string, o graph.TelemetryOptions) (*graph.Node, bool, error) {
id, nodeType, err := graph.Id(cluster, serviceNs, service, workloadNs, workload, app, version, o.GraphType)
if err != nil {
return nil, false, err
}
node, found := trafficMap[id]
if !found {
namespace := workloadNs
if !graph.IsOK(namespace) {
namespace = serviceNs
}
newNode := graph.NewNodeExplicit(id, cluster, namespace, workload, app, version, service, nodeType, o.GraphType)
node = newNode
trafficMap[id] = node
}
node.Metadata["tsHash"] = timeSeriesHash(cluster, serviceNs, service, workloadNs, workload, app, version)
return node, found, nil
}
func timeSeriesHash(cluster, serviceNs, service, workloadNs, workload, app, version string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%s:%s:%s:%s:%s:%s:%s", cluster, serviceNs, service, workloadNs, workload, app, version))))
}
// BuildNodeTrafficMap is required by the graph/TelemtryVendor interface
func BuildNodeTrafficMap(o graph.TelemetryOptions, client *prometheus.Client, globalInfo *graph.AppenderGlobalInfo) (graph.TrafficMap, error) {
if o.NodeOptions.Aggregate != "" {
return handleAggregateNodeTrafficMap(o, client, globalInfo), nil
}
n, err := graph.NewNode(o.NodeOptions.Cluster, o.NodeOptions.Namespace, o.NodeOptions.Service, o.NodeOptions.Namespace, o.NodeOptions.Workload, o.NodeOptions.App, o.NodeOptions.Version, o.GraphType)
if err != nil {
log.Warningf("Skipping NodeTrafficMap (bad node), %s", err)
return nil, err
}
log.Tracef("Build graph for node [%+v]", n)
appenders, finalizers := appender.ParseAppenders(o)
trafficMap := buildNodeTrafficMap(o.Cluster, o.NodeOptions.Namespace, n, o, client)
namespaceInfo := graph.NewAppenderNamespaceInfo(o.NodeOptions.Namespace)
for _, a := range appenders {
appenderTimer := internalmetrics.GetGraphAppenderTimePrometheusTimer(a.Name())
a.AppendGraph(trafficMap, globalInfo, namespaceInfo)
appenderTimer.ObserveDuration()
}
// The finalizers can perform final manipulations on the complete graph
for _, f := range finalizers {
f.AppendGraph(trafficMap, globalInfo, nil)
}
// Note that this is where we would call reduceToServiceGraph for graphTypeService but
// the current decision is to not reduce the node graph to provide more detail. This may be
// confusing to users, we'll see...
return trafficMap, nil
}
// buildNodeTrafficMap returns a map of all nodes requesting or requested by the target node (key=id). Node graphs
// are from the perspective of the node, as such we use destination telemetry for incoming traffic and source telemetry
// for outgoing traffic.
func buildNodeTrafficMap(cluster, namespace string, n *graph.Node, o graph.TelemetryOptions, client *prometheus.Client) graph.TrafficMap {
// create map to aggregate traffic by protocol and response code
trafficMap := graph.NewTrafficMap()
duration := o.Namespaces[namespace].Duration
idleCondition := "> 0"
if o.IncludeIdleEdges {
idleCondition = ""
}
// only narrow by cluster if it is set on the target node
var sourceCluster, destCluster string
if cluster != graph.Unknown {
sourceCluster = fmt.Sprintf(`,source_cluster="%s"`, cluster)
destCluster = fmt.Sprintf(`,destination_cluster="%s"`, cluster)
}
// HTTP/GRPC Traffic
if o.Rates.Http == graph.RateRequests || o.Rates.Grpc == graph.RateRequests {
metric := "istio_requests_total"
groupBy := "source_cluster,source_workload_namespace,source_workload,source_canonical_service,source_canonical_revision,destination_cluster,destination_service_namespace,destination_service,destination_service_name,destination_workload_namespace,destination_workload,destination_canonical_service,destination_canonical_revision,request_protocol,response_code,grpc_response_status,response_flags"
// query prometheus for request traffic in two queries:
// 1) query for incoming traffic
var query string
switch n.NodeType {
case graph.NodeTypeWorkload:
query = fmt.Sprintf(`sum(rate(%s{reporter="destination"%s,destination_workload_namespace="%s",destination_workload="%s"} [%vs])) by (%s) %s`,
metric,
destCluster,
namespace,
n.Workload,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
case graph.NodeTypeApp:
if graph.IsOK(n.Version) {
query = fmt.Sprintf(`sum(rate(%s{reporter="destination"%s,destination_service_namespace="%s",destination_canonical_service="%s",destination_canonical_revision="%s"} [%vs])) by (%s) %s`,
metric,
destCluster,
namespace,
n.App,
n.Version,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
} else {
query = fmt.Sprintf(`sum(rate(%s{reporter="destination"%s,destination_service_namespace="%s",destination_canonical_service="%s"} [%vs])) by (%s) %s`,
metric,
destCluster,
namespace,
n.App,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
}
case graph.NodeTypeService:
// Service nodes require two queries for incoming
// 1.a) query source telemetry for requests to the service that could not be serviced
query = fmt.Sprintf(`sum(rate(%s{reporter="source"%s,destination_workload="unknown",destination_service=~"^%s\\.%s\\..*$"} [%vs])) by (%s) %s`,
metric,
destCluster,
n.Service,
namespace,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
vector := promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &vector, metric, o)
// 1.b) query dest telemetry for requests to the service, serviced by service workloads
query = fmt.Sprintf(`sum(rate(%s{reporter="destination"%s,destination_service_namespace="%s",destination_service=~"^%s\\.%s\\..*$"} [%vs])) by (%s) %s`,
metric,
destCluster,
namespace,
n.Service,
namespace,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
default:
graph.Error(fmt.Sprintf("NodeType [%s] not supported", n.NodeType))
}
inVector := promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &inVector, metric, o)
// 2) query for outbound traffic
switch n.NodeType {
case graph.NodeTypeWorkload:
query = fmt.Sprintf(`sum(rate(%s{reporter="source"%s,source_workload_namespace="%s",source_workload="%s"} [%vs])) by (%s) %s`,
metric,
sourceCluster,
namespace,
n.Workload,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
case graph.NodeTypeApp:
if graph.IsOK(n.Version) {
query = fmt.Sprintf(`sum(rate(%s{reporter="source"%s,source_workload_namespace="%s",source_canonical_service="%s",source_canonical_revision="%s"} [%vs])) by (%s) %s`,
metric,
sourceCluster,
namespace,
n.App,
n.Version,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
} else {
query = fmt.Sprintf(`sum(rate(%s{reporter="source"%s,source_workload_namespace="%s",source_canonical_service="%s"} [%vs])) by (%s) %s`,
metric,
sourceCluster,
namespace,
n.App,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
}
case graph.NodeTypeService:
query = ""
default:
graph.Error(fmt.Sprintf("NodeType [%s] not supported", n.NodeType))
}
outVector := promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &outVector, metric, o)
}
// gRPC message traffic
if o.Rates.Grpc != graph.RateNone && o.Rates.Grpc != graph.RateRequests {
var metrics []string
groupBy := "source_cluster,source_workload_namespace,source_workload,source_canonical_service,source_canonical_revision,destination_cluster,destination_service_namespace,destination_service,destination_service_name,destination_workload_namespace,destination_workload,destination_canonical_service,destination_canonical_revision"
switch o.Rates.Grpc {
case graph.RateReceived:
metrics = []string{"istio_response_messages_total"}
case graph.RateSent:
metrics = []string{"istio_request_messages_total"}
case graph.RateTotal:
metrics = []string{"istio_request_messages_total", "istio_response_messages_total"}
default:
metrics = []string{}
}
for _, metric := range metrics {
var query string
switch n.NodeType {
case graph.NodeTypeWorkload:
query = fmt.Sprintf(`sum(rate(%s{reporter="destination"%s,destination_workload_namespace="%s",destination_workload="%s"} [%vs])) by (%s) %s`,
metric,
destCluster,
namespace,
n.Workload,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
case graph.NodeTypeApp:
if graph.IsOK(n.Version) {
query = fmt.Sprintf(`sum(rate(%s{reporter="destination"%s,destination_service_namespace="%s",destination_canonical_service="%s",destination_canonical_revision="%s"} [%vs])) by (%s) %s`,
metric,
destCluster,
namespace,
n.App,
n.Version,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
} else {
query = fmt.Sprintf(`sum(rate(%s{reporter="destination"%s,destination_service_namespace="%s",destination_canonical_service="%s"} [%vs])) by (%s) %s`,
metric,
destCluster,
namespace,
n.App,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
}
case graph.NodeTypeService:
// TODO: Do we need to handle requests from unknown in a special way (like in HTTP above)? Not sure how gRPC-messages is reported from unknown.
query = fmt.Sprintf(`sum(rate(%s{reporter="destination"%s,destination_service_namespace="%s",destination_service=~"^%s\\.%s\\..*$"} [%vs])) by (%s) %s`,
metric,
destCluster,
namespace,
n.Service,
namespace,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
default:
graph.Error(fmt.Sprintf("NodeType [%s] not supported", n.NodeType))
}
incomingVector := promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &incomingVector, metric, o)
// 2) query for outbound traffic
switch n.NodeType {
case graph.NodeTypeWorkload:
query = fmt.Sprintf(`sum(rate(%s{reporter="source"%s,source_workload_namespace="%s",source_workload="%s"} [%vs])) by (%s) %s`,
metric,
sourceCluster,
namespace,
n.Workload,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
case graph.NodeTypeApp:
if graph.IsOK(n.Version) {
query = fmt.Sprintf(`sum(rate(%s{reporter="source"%s,source_workload_namespace="%s",source_canonical_service="%s",source_canonical_revision="%s"} [%vs])) by (%s) %s`,
metric,
sourceCluster,
namespace,
n.App,
n.Version,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
} else {
query = fmt.Sprintf(`sum(rate(%s{reporter="source"%s,source_workload_namespace="%s",source_canonical_service="%s"} [%vs])) by (%s) %s`,
metric,
sourceCluster,
namespace,
n.App,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
}
case graph.NodeTypeService:
query = ""
default:
graph.Error(fmt.Sprintf("NodeType [%s] not supported", n.NodeType))
}
outgoingVector := promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &outgoingVector, metric, o)
}
}
// TCP byte traffic
if o.Rates.Tcp != graph.RateNone {
var metrics []string
groupBy := "source_cluster,source_workload_namespace,source_workload,source_canonical_service,source_canonical_revision,destination_cluster,destination_service_namespace,destination_service,destination_service_name,destination_workload_namespace,destination_workload,destination_canonical_service,destination_canonical_revision,response_flags"
switch o.Rates.Tcp {
case graph.RateReceived:
metrics = []string{"istio_tcp_received_bytes_total"}
case graph.RateSent:
metrics = []string{"istio_tcp_sent_bytes_total"}
case graph.RateTotal:
metrics = []string{"istio_tcp_sent_bytes_total", "istio_tcp_received_bytes_total"}
default:
metrics = []string{}
}
for _, metric := range metrics {
var query string
switch n.NodeType {
case graph.NodeTypeWorkload:
query = fmt.Sprintf(`sum(rate(%s{reporter="destination"%s,destination_workload_namespace="%s",destination_workload="%s"} [%vs])) by (%s) %s`,
metric,
destCluster,
namespace,
n.Workload,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
case graph.NodeTypeApp:
if graph.IsOK(n.Version) {
query = fmt.Sprintf(`sum(rate(%s{reporter="destination"%s,destination_service_namespace="%s",destination_canonical_service="%s",destination_canonical_revision="%s"} [%vs])) by (%s) %s`,
metric,
destCluster,
namespace,
n.App,
n.Version,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
} else {
query = fmt.Sprintf(`sum(rate(%s{reporter="destination"%s,destination_service_namespace="%s",destination_canonical_service="%s"} [%vs])) by (%s) %s`,
metric,
destCluster,
namespace,
n.App,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
}
case graph.NodeTypeService:
// TODO: Do we need to handle requests from unknown in a special way (like in HTTP above)? Not sure how tcp is reported from unknown.
query = fmt.Sprintf(`sum(rate(%s{reporter="destination"%s,destination_service_namespace="%s",destination_service=~"^%s\\.%s\\..*$"} [%vs])) by (%s) %s`,
metric,
destCluster,
namespace,
n.Service,
namespace,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
default:
graph.Error(fmt.Sprintf("NodeType [%s] not supported", n.NodeType))
}
incomingVector := promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &incomingVector, metric, o)
// 2) query for outbound traffic
switch n.NodeType {
case graph.NodeTypeWorkload:
query = fmt.Sprintf(`sum(rate(%s{reporter="source"%s,source_workload_namespace="%s",source_workload="%s"} [%vs])) by (%s) %s`,
metric,
sourceCluster,
namespace,
n.Workload,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
case graph.NodeTypeApp:
if graph.IsOK(n.Version) {
query = fmt.Sprintf(`sum(rate(%s{reporter="source"%s,source_workload_namespace="%s",source_canonical_service="%s",source_canonical_revision="%s"} [%vs])) by (%s) %s`,
metric,
sourceCluster,
namespace,
n.App,
n.Version,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
} else {
query = fmt.Sprintf(`sum(rate(%s{reporter="source"%s,source_workload_namespace="%s",source_canonical_service="%s"} [%vs])) by (%s) %s`,
metric,
sourceCluster,
namespace,
n.App,
int(duration.Seconds()), // range duration for the query
groupBy,
idleCondition)
}
case graph.NodeTypeService:
query = ""
default:
graph.Error(fmt.Sprintf("NodeType [%s] not supported", n.NodeType))
}
outgoingVector := promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &outgoingVector, metric, o)
}
}
return trafficMap
}
func handleAggregateNodeTrafficMap(o graph.TelemetryOptions, client *prometheus.Client, globalInfo *graph.AppenderGlobalInfo) graph.TrafficMap {
n := graph.NewAggregateNode(o.NodeOptions.Cluster, o.NodeOptions.Namespace, o.NodeOptions.Aggregate, o.NodeOptions.AggregateValue, o.NodeOptions.Service, o.NodeOptions.App)
log.Tracef("Build graph for aggregate node [%+v]", n)
if !o.Appenders.All {
o.Appenders.AppenderNames = append(o.Appenders.AppenderNames, appender.AggregateNodeAppenderName)
}
appenders, finalizers := appender.ParseAppenders(o)
trafficMap := buildAggregateNodeTrafficMap(o.NodeOptions.Namespace, n, o, client)
namespaceInfo := graph.NewAppenderNamespaceInfo(o.NodeOptions.Namespace)
for _, a := range appenders {
appenderTimer := internalmetrics.GetGraphAppenderTimePrometheusTimer(a.Name())
a.AppendGraph(trafficMap, globalInfo, namespaceInfo)
appenderTimer.ObserveDuration()
}
// The finalizers can perform final manipulations on the complete graph
for _, f := range finalizers {
f.AppendGraph(trafficMap, globalInfo, nil)
}
return trafficMap
}
// buildAggregateNodeTrafficMap returns a map of all incoming and outgoing traffic from the perspective of the aggregate. Aggregates
// are always generated for serviced requests and therefore via destination telemetry.
func buildAggregateNodeTrafficMap(namespace string, n graph.Node, o graph.TelemetryOptions, client *prometheus.Client) graph.TrafficMap {
interval := o.Namespaces[namespace].Duration
// create map to aggregate traffic by response code
trafficMap := graph.NewTrafficMap()
// It takes only one prometheus query to get everything involving the target operation
serviceFragment := ""
if n.Service != "" {
serviceFragment = fmt.Sprintf(`,destination_service_name="%s"`, n.Service)
}
metric := "istio_requests_total"
groupBy := "source_cluster,source_workload_namespace,source_workload,source_canonical_service,source_canonical_revision,destination_cluster,destination_service_namespace,destination_service,destination_service_name,destination_workload_namespace,destination_workload,destination_canonical_service,destination_canonical_revision,request_protocol,response_code,grpc_response_status,response_flags"
httpQuery := fmt.Sprintf(`sum(rate(%s{reporter="destination",destination_service_namespace="%s",%s="%s"%s}[%vs])) by (%s) > 0`,
metric,
namespace,
n.Metadata[graph.Aggregate],
n.Metadata[graph.AggregateValue],
serviceFragment,
int(interval.Seconds()), // range duration for the query
groupBy)
/* It's not clear that request classification makes sense for TCP metrics. Because it costs us queries I'm
removing the support for now, we can add it back if someone presents a valid use case. (same for gRCP message metrics)
tcpQuery := fmt.Sprintf(`sum(rate(%s{reporter="destination",destination_service_namespace="%s",%s="%s"}[%vs])) by (%s) > 0`,
"istio_tcp_sent_bytes_total",
namespace,
n.Metadata[graph.Aggregate],
n.Metadata[graph.AggregateValue],
int(interval.Seconds()), // range duration for the query
groupBy)
query := fmt.Sprintf(`(%s) OR (%s)`, httpQuery, tcpQuery)
*/
query := httpQuery
vector := promQuery(query, time.Unix(o.QueryTime, 0), client.API())
populateTrafficMap(trafficMap, &vector, metric, o)
return trafficMap
}
// TODO: Can this be combined with graph.telemetry.istio.appender.promQuery?
func promQuery(query string, queryTime time.Time, api prom_v1.API) model.Vector {
if query == "" {
return model.Vector{}
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// add scope if necessary
query = util.AddQueryScope(query)
// wrap with a round() to be in line with metrics api
query = fmt.Sprintf("round(%s,0.001)", query)
log.Tracef("Graph query:\n%s@time=%v (now=%v, %v)\n", query, queryTime.Format(graph.TF), time.Now().Format(graph.TF), queryTime.Unix())
promtimer := internalmetrics.GetPrometheusProcessingTimePrometheusTimer("Graph-Generation")
value, warnings, err := api.Query(ctx, query, queryTime)
if len(warnings) > 0 {
log.Warningf("promQuery. Prometheus Warnings: [%s]", strings.Join(warnings, ","))
}
graph.CheckUnavailable(err)
promtimer.ObserveDuration() // notice we only collect metrics for successful prom queries
switch t := value.Type(); t {
case model.ValVector: // Instant Vector
return value.(model.Vector)
default:
graph.Error(fmt.Sprintf("No handling for type %v!\n", t))
}
return nil
}