-
Notifications
You must be signed in to change notification settings - Fork 479
/
throughput.go
203 lines (172 loc) · 7.85 KB
/
throughput.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
package appender
import (
"fmt"
"math"
"time"
"github.com/prometheus/common/model"
"github.com/kiali/kiali/graph"
"github.com/kiali/kiali/graph/telemetry/istio/util"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/prometheus"
)
const (
// ThroughputAppenderName uniquely identifies the appender: throughput
ThroughputAppenderName = "throughput"
)
// ThroughputAppender is responsible for adding throughput information to the graph. Throughput
// is represented as bytes/sec. Throughput may be for request bytes or response bytes depending
// on the options. Request throughput will be reported using source telemetry, response throughput
// using destination telemetry.
// Name: throughput
type ThroughputAppender struct {
GraphType string
InjectServiceNodes bool
Namespaces graph.NamespaceInfoMap
QueryTime int64 // unix time in seconds
Rates graph.RequestedRates
ThroughputType string
}
// Name implements Appender
func (a ThroughputAppender) Name() string {
return ThroughputAppenderName
}
// IsFinalizer implements Appender
func (a ThroughputAppender) IsFinalizer() bool {
return false
}
// AppendGraph implements Appender
func (a ThroughputAppender) AppendGraph(trafficMap graph.TrafficMap, globalInfo *graph.AppenderGlobalInfo, namespaceInfo *graph.AppenderNamespaceInfo) {
if len(trafficMap) == 0 {
return
}
// HTTP Throughput only apply to HTTP request traffic
if a.Rates.Http != graph.RateRequests {
return
}
if globalInfo.PromClient == nil {
var err error
globalInfo.PromClient, err = prometheus.NewClient()
graph.CheckError(err)
}
a.appendGraph(trafficMap, namespaceInfo.Namespace, globalInfo.PromClient)
}
func (a ThroughputAppender) appendGraph(trafficMap graph.TrafficMap, namespace string, client *prometheus.Client) {
log.Tracef("Generating [%s] throughput; namespace = %v", a.ThroughputType, namespace)
// create map to quickly look up throughput
throughputMap := make(map[string]float64)
duration := a.Namespaces[namespace].Duration
// query prometheus for throughput info in two queries:
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"
metric := fmt.Sprintf("istio_%s_bytes_sum", a.ThroughputType)
reporter := "destination"
if a.ThroughputType == "request" {
reporter = "source"
}
// query prometheus for throughput rates in two queries:
// 1) query for requests originating from a workload outside the namespace.
query := fmt.Sprintf(`sum(rate(%s{reporter="%s",source_workload_namespace!="%s",destination_service_namespace="%s"}[%vs])) by (%s) > 0`,
metric,
reporter,
namespace,
namespace,
int(duration.Seconds()), // range duration for the query
groupBy)
vector := promQuery(query, time.Unix(a.QueryTime, 0), client.GetContext(), client.API(), a)
a.populateThroughputMap(throughputMap, &vector)
// 2) query for requests originating from a workload inside of the namespace
query = fmt.Sprintf(`sum(rate(%s{reporter="%s",source_workload_namespace="%s"}[%vs])) by (%s) > 0`,
metric,
reporter,
namespace,
int(duration.Seconds()), // range duration for the query
groupBy)
vector = promQuery(query, time.Unix(a.QueryTime, 0), client.GetContext(), client.API(), a)
a.populateThroughputMap(throughputMap, &vector)
applyThroughput(trafficMap, throughputMap)
}
func applyThroughput(trafficMap graph.TrafficMap, throughputMap map[string]float64) {
for _, n := range trafficMap {
for _, e := range n.Edges {
key := fmt.Sprintf("%s %s %s", e.Source.ID, e.Dest.ID, e.Metadata[graph.ProtocolKey].(string))
if val, ok := throughputMap[key]; ok {
e.Metadata[graph.Throughput] = val
}
}
}
}
func (a ThroughputAppender) populateThroughputMap(throughputMap map[string]float64, vector *model.Vector) {
for _, s := range *vector {
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 || !destSvcNameOk || !destSvcOk || !destWlNsOk || !destWlOk || !destAppOk || !destVerOk {
log.Warningf("populateThroughputMap: Skipping %s, missing expected labels", m.String())
continue
}
sourceWlNs := string(lSourceWlNs)
sourceWl := string(lSourceWl)
sourceApp := string(lSourceApp)
sourceVer := string(lSourceVer)
destSvc := string(lDestSvc)
// handle clusters
sourceCluster, destCluster := util.HandleClusters(lSourceCluster, sourceClusterOk, lDestCluster, destClusterOk)
if util.IsBadSourceTelemetry(sourceCluster, sourceClusterOk, sourceWlNs, sourceWl, sourceApp) {
continue
}
val := float64(s.Value)
// 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
}
// Should not happen but if NaN for any reason, Just skip it
if math.IsNaN(val) {
continue
}
// 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 a.InjectServiceNodes && graph.IsOK(destSvcName) && destSvcName != graph.PassthroughCluster {
_, destNodeType, err := graph.Id(destCluster, destSvcNs, destSvcName, destWlNs, destWl, destApp, destVer, a.GraphType)
if err != nil {
log.Warningf("Skipping (t) %s, %s", m.String(), err)
continue
}
inject = (graph.NodeTypeService != destNodeType)
}
if inject {
// Only set throughput on the outgoing edge. On the incoming edge, we can't validly aggregate thoughputs of the outgoing edges
// - analogous to https://issues.redhat.com/browse/KIALI-2297, we can't assume even distribution
a.addThroughput(throughputMap, val, destCluster, destSvcNs, destSvcName, "", "", "", destCluster, destSvcNs, destSvcName, destWlNs, destWl, destApp, destVer)
} else {
a.addThroughput(throughputMap, val, sourceCluster, sourceWlNs, "", sourceWl, sourceApp, sourceVer, destCluster, destSvcNs, destSvcName, destWlNs, destWl, destApp, destVer)
}
}
}
func (a ThroughputAppender) addThroughput(throughputMap map[string]float64, val float64, sourceCluster, sourceNs, sourceSvc, sourceWl, sourceApp, sourceVer, destCluster, destSvcNs, destSvc, destWlNs, destWl, destApp, destVer string) {
sourceID, _, err := graph.Id(sourceCluster, sourceNs, sourceSvc, sourceNs, sourceWl, sourceApp, sourceVer, a.GraphType)
if err != nil {
log.Warningf("Skipping addThroughput (source), %s", err)
return
}
destID, _, err := graph.Id(destCluster, destSvcNs, destSvc, destWlNs, destWl, destApp, destVer, a.GraphType)
if err != nil {
log.Warningf("Skipping addThroughput (dest), %s", err)
return
}
key := fmt.Sprintf("%s %s http", sourceID, destID)
throughputMap[key] += val
}