-
Notifications
You must be signed in to change notification settings - Fork 5
/
PeriodicAvgTimerFunction.java
540 lines (410 loc) · 18.3 KB
/
PeriodicAvgTimerFunction.java
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
package com.takipi.udf.timer;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.takipi.api.client.ApiClient;
import com.takipi.api.client.data.timer.Timer;
import com.takipi.api.client.data.transaction.Stats;
import com.takipi.api.client.data.transaction.TransactionGraph;
import com.takipi.api.client.data.view.SummarizedView;
import com.takipi.api.client.request.event.BatchForceSnapshotsRequest;
import com.takipi.api.client.request.event.EventsRequest;
import com.takipi.api.client.request.label.BatchModifyLabelsRequest;
import com.takipi.api.client.request.redaction.CodeRedactionExcludeRequest;
import com.takipi.api.client.request.transactiontimer.CreateTransactionTimerRequest;
import com.takipi.api.client.request.transactiontimer.EditTransactionTimerRequest;
import com.takipi.api.client.request.transactiontimer.ToggleTransactionTimerRequest;
import com.takipi.api.client.request.transactiontimer.TransactionTimersRequest;
import com.takipi.api.client.result.event.EventResult;
import com.takipi.api.client.result.event.EventsResult;
import com.takipi.api.client.result.redaction.CodeRedactionElements;
import com.takipi.api.client.result.transactiontimer.TransactionTimersResult;
import com.takipi.api.client.util.performance.PerformanceUtil;
import com.takipi.api.client.util.performance.calc.PerformanceScore;
import com.takipi.api.client.util.performance.calc.PerformanceState;
import com.takipi.api.client.util.performance.transaction.GraphPerformanceCalculator;
import com.takipi.api.client.util.transaction.TransactionUtil;
import com.takipi.api.client.util.view.ViewUtil;
import com.takipi.api.core.url.UrlClient.Response;
import com.takipi.common.util.CollectionUtil;
import com.takipi.common.util.Pair;
import com.takipi.udf.ContextArgs;
import com.takipi.udf.input.Input;
import com.takipi.udf.input.TimeInterval;
import com.takipi.udf.util.JavaUtil;
public class PeriodicAvgTimerFunction {
private static final String TIMERS_VIEW_NAME = "My Timers";
private static final long DRFAULT_MAX_AVGTIME_THRESHOLD = TimeUnit.MINUTES.toMillis(15);
public static String validateInput(String rawInput) {
return getPeriodicAvgTimerInput(rawInput).toString();
}
static PeriodicAvgTimerInput getPeriodicAvgTimerInput(String rawInput) {
System.out.println("validateInput rawInput:" + rawInput);
if (Strings.isNullOrEmpty(rawInput)) {
throw new IllegalArgumentException("Input is empty");
}
PeriodicAvgTimerInput input;
try {
input = PeriodicAvgTimerInput.of(rawInput);
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
if (input.active_timespan == null) {
throw new IllegalArgumentException("Missing 'active_timespan'");
}
if (!input.active_timespan.isPositive()) {
throw new IllegalArgumentException("'active_timespan' must be positive");
}
if (input.baseline_timespan == null) {
throw new IllegalArgumentException("Missing 'baseline_timespan'");
}
if (!input.baseline_timespan.isPositive()) {
throw new IllegalArgumentException("'active_timespan' must be positive");
}
if (input.baseline_timespan.asMinutes() <= input.active_timespan.asMinutes()) {
throw new IllegalArgumentException("'baseline_timespan' must be larger than 'active_timespan'");
}
if (input.active_timespan_point_res <= 0) {
throw new IllegalArgumentException("'active_timespan_point_res' must be positive");
}
if (input.baseline_timespan_point_res <= 0) {
throw new IllegalArgumentException("'baseline_timespan_point_res' must be positive");
}
if (input.active_invocations_threshold <= 0) {
throw new IllegalArgumentException("'active_invocations_threshold' must be positive");
}
if (input.baseline_invocations_threshold <= 0) {
throw new IllegalArgumentException("'baseline_invocations_threshold' must be positive");
}
if (input.min_delta_threshold < 0) {
throw new IllegalArgumentException("'min_delta_threshold' can't be negative");
}
if (input.over_avg_slowing_percentage <= 0.0) {
throw new IllegalArgumentException("'over_avg_slowing_percentage' must be positive");
}
if (input.over_avg_slowing_percentage > 1.0) {
throw new IllegalArgumentException("'over_avg_slowing_percentage' can't be greater than 1.0");
}
if (input.over_avg_critical_percentage <= 0.0) {
throw new IllegalArgumentException("'over_avg_critical_percentage' must be positive");
}
if (input.over_avg_critical_percentage > 1.0) {
throw new IllegalArgumentException("'over_avg_critical_percentage' can't be greater than 1.0");
}
if (input.over_avg_critical_percentage <= input.over_avg_slowing_percentage) {
throw new IllegalArgumentException(
"'over_avg_critical_percentage' must be larger than 'over_avg_slowing_percentage'");
}
if (input.std_dev_factor <= 0.0) {
throw new IllegalArgumentException("'std_dev_factor' must be positive");
}
if (input.timer_std_dev_factor < 0.0) {
throw new IllegalArgumentException("'timer_std_dev_factor' can't be negative");
}
if (input.min_timer_threshold <= 0) {
throw new IllegalArgumentException("'min_timer_threshold' must be positive");
}
return input;
}
public static void execute(String rawContextArgs, String rawInput) {
PeriodicAvgTimerInput input = getPeriodicAvgTimerInput(rawInput);
ContextArgs args = (new Gson()).fromJson(rawContextArgs, ContextArgs.class);
System.out.println("execute context: " + rawContextArgs);
if (!args.validate()) {
throw new IllegalArgumentException("Bad context args - " + rawContextArgs);
}
if (!args.viewValidate()) {
return;
}
ApiClient apiClient = args.apiClient();
SummarizedView timersView = ViewUtil.getServiceViewByName(apiClient, args.serviceId, TIMERS_VIEW_NAME);
if (timersView == null) {
throw new IllegalStateException("Failed getting timers view.");
}
DateTime to = DateTime.now();
DateTime activeFrom = to.minusMinutes(input.active_timespan.asMinutes());
Map<String, TransactionGraph> activeTransactions = TransactionUtil.getTransactionGraphs(apiClient,
args.serviceId, args.viewId, activeFrom, to, input.active_timespan_point_res);
if (CollectionUtil.safeIsEmpty(activeTransactions)) {
return;
}
DateTime baselineFrom = to.minusMinutes(input.baseline_timespan.asMinutes());
Map<String, TransactionGraph> baselineTransactions = TransactionUtil.getTransactionGraphs(apiClient,
args.serviceId, args.viewId, baselineFrom, to, input.baseline_timespan_point_res);
GraphPerformanceCalculator calculator = GraphPerformanceCalculator.of(input.active_invocations_threshold,
input.baseline_invocations_threshold, input.min_delta_threshold, input.min_delta_threshold_percentage,
input.over_avg_slowing_percentage, input.over_avg_critical_percentage, input.std_dev_factor,
DRFAULT_MAX_AVGTIME_THRESHOLD);
Map<TransactionGraph, PerformanceScore> performance = PerformanceUtil.getPerformanceStates(activeTransactions,
baselineTransactions, calculator);
DateTimeFormatter fmt = ISODateTimeFormat.dateTime().withZoneUTC();
EventsRequest eventsRequest = EventsRequest.newBuilder().setServiceId(args.serviceId).setViewId(timersView.id)
.setFrom(baselineFrom.toString(fmt)).setTo(to.toString(fmt)).build();
Response<EventsResult> eventsResponse = apiClient.get(eventsRequest);
if (eventsResponse.isBadResponse()) {
throw new IllegalStateException("Failed getting view events.");
}
EventsResult eventsResult = eventsResponse.data;
TransactionTimersRequest transactionTimersRequest = TransactionTimersRequest.newBuilder()
.setServiceId(args.serviceId).build();
Response<TransactionTimersResult> transactionTimersResponse = apiClient.get(transactionTimersRequest);
if (transactionTimersResponse.isBadResponse()) {
throw new IllegalStateException("Failed getting timers.");
}
CodeRedactionExcludeRequest excludeRequest = CodeRedactionExcludeRequest.newBuilder()
.setServiceId(args.serviceId).build();
Response<CodeRedactionElements> excludeResponse = apiClient.get(excludeRequest);
if (excludeResponse.isBadResponse()) {
throw new IllegalStateException("Failed exclude filters.");
}
Map<TransactionGraph, List<EventResult>> eventsMap = buildTransactionEvents(activeTransactions.values(),
eventsResult.events);
boolean labelsUpdateNeeded = false;
BatchModifyLabelsRequest.Builder labelsRequestBuilder = BatchModifyLabelsRequest.newBuilder()
.setServiceId(args.serviceId).setHandleSimilarEvents(false);
Map<TransactionName, Long> newTimers = Maps.newHashMap();
Map<String, Long> updatedTimers = Maps.newHashMap();
List<String> removedTimers = Lists.newArrayList();
Set<String> existingLabels = Sets.newHashSet();
Set<String> eventsToForceSnapshot = Sets.newHashSet();
for (Map.Entry<TransactionGraph, PerformanceScore> entry : performance.entrySet()) {
TransactionGraph transaction = entry.getKey();
if (Strings.isNullOrEmpty(transaction.method_name)) {
continue;
}
List<EventResult> transactionEvents = eventsMap.get(transaction);
// Can happen if the transaction didn't have any events in the timeframe.
//
if (transactionEvents == null) {
transactionEvents = Collections.emptyList();
}
TransactionName transactionName = TransactionName.of(JavaUtil.toInternalName(transaction.class_name),
transaction.method_name);
Timer timer = getExistingTransactionTimer(transactionName,
transactionTimersResponse.data.transaction_timers);
boolean excludedTransaction = isExcludedTransaction(transaction, excludeResponse.data);
PerformanceScore score = entry.getValue();
PerformanceState state = (excludedTransaction ? PerformanceState.NO_DATA : score.state);
if (addLabelModifications(transactionEvents, state, labelsRequestBuilder, existingLabels, args.serviceId,
apiClient)) {
labelsUpdateNeeded = true;
}
if (state == PerformanceState.NO_DATA) {
if ((timer != null) && (!input.timer_always_on)) {
removedTimers.add(timer.id);
}
continue;
}
if (state == PerformanceState.OK) {
if (timer == null) {
if (!input.monitor_ok_transactions) {
// If no timer, don't create a new one for OK transaction.
//
continue;
}
} else if ((!input.monitor_ok_transactions) && (!input.timer_always_on)) {
// If timer exist and we don't maintain, remove and move on.
// If we do maintain, we want to proceed and adapt the threshold.
//
removedTimers.add(timer.id);
continue;
}
}
Stats stats = TransactionUtil.aggregateGraph(transaction);
long timerThreshold = (long) (stats.avg_time + (stats.avg_time_std_deviation * input.timer_std_dev_factor));
if (timerThreshold < input.min_timer_threshold) {
continue;
}
if (timer == null) {
newTimers.put(transactionName, timerThreshold);
} else {
updatedTimers.put(timer.id, timerThreshold);
}
for (EventResult event : transactionEvents) {
eventsToForceSnapshot.add(event.id);
}
}
for (Map.Entry<TransactionName, Long> entry : newTimers.entrySet()) {
TransactionName transactionName = entry.getKey();
long threshold = entry.getValue();
CreateTransactionTimerRequest createTransactionTimerRequest = CreateTransactionTimerRequest.newBuilder()
.setServiceId(args.serviceId).setClassName(transactionName.className)
.setMethodName(transactionName.methodName).setThreshold(threshold).build();
apiClient.post(createTransactionTimerRequest);
}
for (Map.Entry<String, Long> entry : updatedTimers.entrySet()) {
String timerId = entry.getKey();
long threshold = entry.getValue();
EditTransactionTimerRequest editTransactionTimerRequest = EditTransactionTimerRequest.newBuilder()
.setServiceId(args.serviceId).setTimerId(Integer.parseInt(timerId)).setThreshold(threshold).build();
apiClient.post(editTransactionTimerRequest);
}
for (String timerId : removedTimers) {
ToggleTransactionTimerRequest toggleTransactionTimerRequest = ToggleTransactionTimerRequest.newBuilder()
.setServiceId(args.serviceId).setTimerId(Integer.parseInt(timerId)).setEnable(false).build();
apiClient.post(toggleTransactionTimerRequest);
}
if (labelsUpdateNeeded) {
apiClient.post(labelsRequestBuilder.build());
}
if (!eventsToForceSnapshot.isEmpty()) {
BatchForceSnapshotsRequest forceSnapshotsRequest = BatchForceSnapshotsRequest.newBuilder()
.setServiceId(args.serviceId).addEventIds(eventsToForceSnapshot).build();
apiClient.post(forceSnapshotsRequest);
}
}
private static boolean addLabelModifications(List<EventResult> events, PerformanceState state,
BatchModifyLabelsRequest.Builder labelsRequestBuilder, Set<String> existingLabels, String serviceId,
ApiClient apiClient) {
boolean hasModifications = false;
for (EventResult event : events) {
Pair<Collection<String>, Collection<String>> modifications = PerformanceUtil.categorizeEvent(event,
serviceId, state, existingLabels, apiClient, false);
Collection<String> labelsToAdd = modifications.getFirst();
Collection<String> labelsToRemove = modifications.getSecond();
if ((labelsToAdd.isEmpty()) && (labelsToRemove.isEmpty())) {
continue;
}
labelsRequestBuilder.addLabelModifications(event.id, labelsToAdd, labelsToRemove);
hasModifications = true;
}
return hasModifications;
}
private static Map<TransactionGraph, List<EventResult>> buildTransactionEvents(
Collection<TransactionGraph> transactions, List<EventResult> events) {
if (CollectionUtil.safeIsEmpty(events)) {
return Collections.emptyMap();
}
Map<TransactionGraph, List<EventResult>> result = Maps.newHashMap();
for (EventResult event : events) {
if ((event.entry_point == null) || (Strings.isNullOrEmpty(event.entry_point.class_name))
|| (Strings.isNullOrEmpty(event.entry_point.method_name))) {
continue;
}
for (TransactionGraph transaction : transactions) {
if (!JavaUtil.toInternalName(event.entry_point.class_name).equals(transaction.class_name)) {
continue;
}
if ((!Strings.isNullOrEmpty(transaction.method_name))
&& (!event.entry_point.method_name.equals(transaction.method_name))) {
continue;
}
List<EventResult> transactionEvents = result.get(transaction);
if (transactionEvents == null) {
transactionEvents = Lists.newArrayList();
result.put(transaction, transactionEvents);
}
transactionEvents.add(event);
}
}
return result;
}
private static boolean isExcludedTransaction(TransactionGraph transaction,
CodeRedactionElements redactionElements) {
if (redactionElements == null) {
return false;
}
if (!CollectionUtil.safeIsEmpty(redactionElements.packages)) {
for (String packageName : redactionElements.packages) {
if (transaction.class_name.startsWith(packageName)) {
return true;
}
}
}
if (!CollectionUtil.safeIsEmpty(redactionElements.classes)) {
String simpleTransactionName = JavaUtil.toSimpleClassName(transaction.class_name);
for (String className : redactionElements.classes) {
String simpleClassName = JavaUtil.toSimpleClassName(className);
if (simpleTransactionName.equals(simpleClassName)) {
return true;
}
}
}
return false;
}
private static Timer getExistingTransactionTimer(TransactionName transactionName, List<Timer> timers) {
if (CollectionUtil.safeIsEmpty(timers)) {
return null;
}
for (Timer timer : timers) {
if ((transactionName.className.equals(JavaUtil.toInternalName(timer.class_name)))
&& (transactionName.methodName.equals(timer.method_name))) {
return timer;
}
}
return null;
}
static class TransactionName {
public final String className;
public final String methodName;
private TransactionName(String className, String methodName) {
this.className = className;
this.methodName = methodName;
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if ((o == null) || (!(o instanceof TransactionName))) {
return false;
}
TransactionName other = (TransactionName) o;
return ((this.className.equals(other.className)) && (this.methodName.equals(other.methodName)));
}
@Override
public int hashCode() {
return className.hashCode();
}
static TransactionName of(String className, String methodName) {
return new TransactionName(className, methodName);
}
}
static class PeriodicAvgTimerInput extends Input {
public TimeInterval active_timespan;
public int active_timespan_point_res;
public TimeInterval baseline_timespan;
public int baseline_timespan_point_res;
public long active_invocations_threshold;
public long baseline_invocations_threshold;
public int min_delta_threshold;
public double min_delta_threshold_percentage;
public double over_avg_slowing_percentage;
public double over_avg_critical_percentage;
public double std_dev_factor;
public double timer_std_dev_factor;
public boolean timer_always_on;
public boolean monitor_ok_transactions;
public long min_timer_threshold;
private PeriodicAvgTimerInput(String raw) {
// purposefully not calling super(raw), as we want to first set
// the default value, and only then reflectively set the fields.
//
this.timer_std_dev_factor = 1.0;
this.min_delta_threshold_percentage = 0.20;
initFields(raw);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("PeriodicAvgTimer(");
builder.append(active_timespan.asMinutes());
builder.append(")");
return builder.toString();
}
static PeriodicAvgTimerInput of(String raw) {
return new PeriodicAvgTimerInput(raw);
}
}
}