forked from linkerd/linkerd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetricUtils.jsx
297 lines (265 loc) · 7.93 KB
/
MetricUtils.jsx
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
import Percentage from './Percentage.js';
import PropTypes from 'prop-types';
import _ from 'lodash';
const getPodCategorization = pod => {
if (pod.added && pod.status === "Running") {
return "good";
} else if (pod.status === "Pending" || pod.status === "Running") {
return "default";
} else if (pod.status === "Failed") {
return "poor";
}
return ""; // Terminating | Succeeded | Unknown
};
export const getSuccessRateClassification = (rate, successRateLabels = srArcClassLabels) => {
if (_.isNull(rate)) {
return successRateLabels.default;
}
if (rate < 0.9) {
return successRateLabels.poor;
} else if (rate < 0.95) {
return successRateLabels.warning;
} else {
return successRateLabels.good;
}
};
const srArcClassLabels = {
good: "good",
warning: "warning",
poor: "poor",
default: "default"
};
const getTotalRequests = row => {
let success = parseInt(_.get(row, ["stats", "successCount"], 0), 10);
let failure = parseInt(_.get(row, ["stats", "failureCount"], 0), 10);
return success + failure;
};
const getRequestRate = row => {
if (_.isEmpty(row.stats)) {
return null;
}
let seconds = 0;
if (row.timeWindow === "10s") { seconds = 10; }
if (row.timeWindow === "1m") { seconds = 60; }
if (row.timeWindow === "10m") { seconds = 600; }
if (row.timeWindow === "1h") { seconds = 3600; }
if (seconds === 0) {
return null;
} else {
return getTotalRequests(row) / seconds;
}
};
const getSuccessRate = row => {
if (_.isEmpty(row.stats)) {
return null;
}
let success = parseInt(row.stats.successCount, 10);
let failure = parseInt(row.stats.failureCount, 10);
if (success + failure === 0) {
return null;
} else {
return success / (success + failure);
}
};
const getTlsRequestPercentage = row => {
if (_.isEmpty(row.stats)) {
return null;
}
let tlsRequests = parseInt(_.get(row, ["stats", "tlsRequestCount"], 0), 10);
return new Percentage(tlsRequests, getTotalRequests(row));
};
const getLatency = row => {
if (_.isEmpty(row.stats)) {
return {};
} else {
return {
P50: parseInt(row.stats.latencyMsP50, 10),
P95: parseInt(row.stats.latencyMsP95, 10),
P99: parseInt(row.stats.latencyMsP99, 10),
};
}
};
export const getPodsByDeployment = pods => {
return _(pods)
.reject(p => _.isEmpty(p.deployment) || p.controlPlane)
.groupBy('deployment')
.map((componentPods, name) => {
let podsWithStatus = _.chain(componentPods)
.map(p => {
return _.merge({}, p, { value: getPodCategorization(p) });
})
.reject(p => _.isEmpty(p.value))
.value();
return {
name: name,
added: _.every(componentPods, 'added'),
pods: _.sortBy(podsWithStatus, 'name')
};
})
.reject(p => _.isEmpty(p.pods))
.sortBy('name')
.value();
};
export const getComponentPods = componentPods => {
return _.chain(componentPods)
.map( p => {
return { name: p.name, value: getPodCategorization(p) };
})
.reject(p => _.isEmpty(p.value))
.sortBy("name")
.value();
};
const processStatTable = table => {
return _(table.podGroup.rows).map(row => {
let runningPodCount = parseInt(row.runningPodCount, 10);
let meshedPodCount = parseInt(row.meshedPodCount, 10);
return {
key: `${row.resource.namespace}-${row.resource.type}-${row.resource.name}`,
name: row.resource.name,
namespace: row.resource.namespace,
type: row.resource.type,
totalRequests: getTotalRequests(row),
requestRate: getRequestRate(row),
successRate: getSuccessRate(row),
latency: getLatency(row),
tlsRequestPercent: getTlsRequestPercentage(row),
added: runningPodCount > 0 && meshedPodCount > 0,
pods: {
totalPods: row.runningPodCount,
meshedPods: row.meshedPodCount,
meshedPercentage: new Percentage(meshedPodCount, runningPodCount)
},
errors: row.errorsByPod
};
})
.compact()
.sortBy("name")
.value();
};
export const DefaultRoute = "[default]";
export const processTopRoutesResults = rows => {
return _.map(rows, row => ({
route: row.route || DefaultRoute,
tooltip: !_.isEmpty(row.route) ? null : "Traffic does not match any configured routes",
authority: row.authority,
totalRequests: getTotalRequests(row),
requestRate: getRequestRate(row),
successRate: getSuccessRate(row),
latency: getLatency(row),
tlsRequestPercent: getTlsRequestPercentage(row),
}
));
};
export const processSingleResourceRollup = rawMetrics => {
let result = processMultiResourceRollup(rawMetrics);
if (_.size(result) > 1) {
console.error("Multi metric returned; expected single metric.");
}
if (_.isEmpty(result)) {
return [];
}
return _.values(result)[0];
};
export const processMultiResourceRollup = rawMetrics => {
if (_.isEmpty(rawMetrics.ok) || _.isEmpty(rawMetrics.ok.statTables)) {
return {};
}
let metricsByResource = {};
_.each(rawMetrics.ok.statTables, table => {
if (_.isEmpty(table.podGroup.rows)) {
return;
}
// assumes all rows in a podGroup have the same resource type
let resource = _.get(table, ["podGroup", "rows", 0, "resource", "type"]);
metricsByResource[resource] = processStatTable(table);
});
return metricsByResource;
};
export const groupResourcesByNs = apiRsp => {
let statTables = _.get(apiRsp, ["ok", "statTables"]);
let authoritiesByNs = {};
let resourcesByNs = _.reduce(statTables, (mem, table) => {
_.each(table.podGroup.rows, row => {
// filter out resources that aren't meshed. note that authorities don't
// have pod counts and therefore can't be filtered out here
if (row.meshedPodCount === "0" && row.resource.type !== "authority") {
return;
}
if (!mem[row.resource.namespace]) {
mem[row.resource.namespace] = [];
authoritiesByNs[row.resource.namespace] = [];
}
switch (row.resource.type.toLowerCase()) {
case "service":
break;
case "authority":
authoritiesByNs[row.resource.namespace].push(row.resource.name);
break;
default:
mem[row.resource.namespace].push(`${row.resource.type}/${row.resource.name}`);
}
});
return mem;
}, {});
return {
authoritiesByNs,
resourcesByNs
};
};
export const excludeResourcesFromRollup = (rollupMetrics, resourcesToExclude) => {
_.each(resourcesToExclude, resource => {
delete rollupMetrics[resource];
});
return rollupMetrics;
};
export const emptyMetric = {
key: "",
name: "",
namespace: "",
type: "",
totalRequests: null,
requestRate: null,
successRate: null,
latency: null,
tlsRequestPercent: null,
added: false,
pods: {
totalPods: null,
meshedPods: null,
meshedPercentage: null
}
};
export const metricsPropType = PropTypes.shape({
ok: PropTypes.shape({
statTables: PropTypes.arrayOf(PropTypes.shape({
podGroup: PropTypes.shape({
rows: PropTypes.arrayOf(PropTypes.shape({
failedPodCount: PropTypes.string,
meshedPodCount: PropTypes.string,
resource: PropTypes.shape({
name: PropTypes.string,
namespace: PropTypes.string,
type: PropTypes.string,
}).isRequired,
runningPodCount: PropTypes.string,
stats: PropTypes.shape({
failureCount: PropTypes.string,
latencyMsP50: PropTypes.string,
latencyMsP95: PropTypes.string,
latencyMsP99: PropTypes.string,
tlsRequestCount: PropTypes.string,
successCount: PropTypes.string,
}),
timeWindow: PropTypes.string,
}).isRequired),
}),
}).isRequired).isRequired,
}),
});
export const processedMetricsPropType = PropTypes.shape({
name: PropTypes.string.isRequired,
namespace: PropTypes.string.isRequired,
totalRequests: PropTypes.number,
requestRate: PropTypes.number,
successRate: PropTypes.number,
});