-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
task_runner.ts
833 lines (761 loc) · 28.3 KB
/
task_runner.ts
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import apm from 'elastic-apm-node';
import { omit } from 'lodash';
import { UsageCounter } from '@kbn/usage-collection-plugin/server';
import { v4 as uuidv4 } from 'uuid';
import { ISavedObjectsRepository, Logger } from '@kbn/core/server';
import {
ConcreteTaskInstance,
createTaskRunError,
TaskErrorSource,
throwUnrecoverableError,
} from '@kbn/task-manager-plugin/server';
import { nanosToMillis } from '@kbn/event-log-plugin/server';
import { getErrorSource, isUserError } from '@kbn/task-manager-plugin/server/task_running';
import { ActionScheduler, type RunResult } from './action_scheduler';
import {
RuleRunnerErrorStackTraceLog,
RuleTaskInstance,
RuleTaskRunResult,
RuleTaskStateAndMetrics,
RunRuleParams,
TaskRunnerContext,
} from './types';
import { getExecutorServices } from './get_executor_services';
import { ElasticsearchError, getNextRun, isRuleSnoozed, ruleExecutionStatusToRaw } from '../lib';
import {
IntervalSchedule,
RawRuleExecutionStatus,
RawRuleLastRun,
RawRuleMonitoring,
RuleExecutionStatus,
RuleExecutionStatusErrorReasons,
RuleTaskState,
RuleTypeRegistry,
} from '../types';
import { asErr, asOk, isErr, isOk, map, resolveErr, Result } from '../lib/result_type';
import { taskInstanceToAlertTaskInstance } from './alert_task_instance';
import { isAlertSavedObjectNotFoundError, isEsUnavailableError } from '../lib/is_alerting_error';
import { partiallyUpdateRuleWithEs, RULE_SAVED_OBJECT_TYPE } from '../saved_objects';
import {
AlertInstanceContext,
AlertInstanceState,
parseDuration,
RawAlertInstance,
RuleAlertData,
RuleLastRunOutcomeOrderMap,
RuleTypeParams,
RuleTypeState,
} from '../../common';
import { NormalizedRuleType, UntypedNormalizedRuleType } from '../rule_type_registry';
import { getEsErrorMessage } from '../lib/errors';
import { IN_MEMORY_METRICS, InMemoryMetrics } from '../monitoring';
import { RuleRunMetricsStore } from '../lib/rule_run_metrics_store';
import { AlertingEventLogger } from '../lib/alerting_event_logger/alerting_event_logger';
import { getDecryptedRule, validateRuleAndCreateFakeRequest } from './rule_loader';
import { TaskRunnerTimer, TaskRunnerTimerSpan } from './task_runner_timer';
import { RuleMonitoringService } from '../monitoring/rule_monitoring_service';
import { lastRunToRaw } from '../lib/last_run_status';
import { RuleRunningHandler } from './rule_running_handler';
import { RuleResultService } from '../monitoring/rule_result_service';
import { RuleTypeRunner } from './rule_type_runner';
import { initializeAlertsClient } from '../alerts_client';
import {
createTaskRunnerLogger,
withAlertingSpan,
processRunResults,
clearExpiredSnoozes,
} from './lib';
const FALLBACK_RETRY_INTERVAL = '5m';
const CONNECTIVITY_RETRY_INTERVAL = '5m';
interface TaskRunnerConstructorParams<
Params extends RuleTypeParams,
ExtractedParams extends RuleTypeParams,
RuleState extends RuleTypeState,
AlertState extends AlertInstanceState,
Context extends AlertInstanceContext,
ActionGroupIds extends string,
RecoveryActionGroupId extends string,
AlertData extends RuleAlertData
> {
context: TaskRunnerContext;
inMemoryMetrics: InMemoryMetrics;
internalSavedObjectsRepository: ISavedObjectsRepository;
ruleType: NormalizedRuleType<
Params,
ExtractedParams,
RuleState,
AlertState,
Context,
ActionGroupIds,
RecoveryActionGroupId,
AlertData
>;
taskInstance: ConcreteTaskInstance;
}
export class TaskRunner<
Params extends RuleTypeParams,
ExtractedParams extends RuleTypeParams,
RuleState extends RuleTypeState,
AlertState extends AlertInstanceState,
Context extends AlertInstanceContext,
ActionGroupIds extends string,
RecoveryActionGroupId extends string,
AlertData extends RuleAlertData
> {
private context: TaskRunnerContext;
private logger: Logger;
private taskInstance: RuleTaskInstance;
private ruleConsumer: string | null;
private ruleType: NormalizedRuleType<
Params,
ExtractedParams,
RuleState,
AlertState,
Context,
ActionGroupIds,
RecoveryActionGroupId,
AlertData
>;
private readonly executionId: string;
private readonly ruleTypeRegistry: RuleTypeRegistry;
private readonly inMemoryMetrics: InMemoryMetrics;
private readonly internalSavedObjectsRepository: ISavedObjectsRepository;
private timer: TaskRunnerTimer;
private alertingEventLogger: AlertingEventLogger;
private usageCounter?: UsageCounter;
private searchAbortController: AbortController;
private cancelled: boolean;
private stackTraceLog: RuleRunnerErrorStackTraceLog | null;
private ruleMonitoring: RuleMonitoringService;
private ruleRunning: RuleRunningHandler;
private ruleResult: RuleResultService;
private ruleTypeRunner: RuleTypeRunner<
Params,
ExtractedParams,
RuleState,
AlertState,
Context,
ActionGroupIds,
RecoveryActionGroupId,
AlertData
>;
private runDate = new Date();
constructor({
context,
inMemoryMetrics,
internalSavedObjectsRepository,
ruleType,
taskInstance,
}: TaskRunnerConstructorParams<
Params,
ExtractedParams,
RuleState,
AlertState,
Context,
ActionGroupIds,
RecoveryActionGroupId,
AlertData
>) {
this.context = context;
const loggerId = ruleType.id.startsWith('.') ? ruleType.id.substring(1) : ruleType.id;
this.logger = context.logger.get(loggerId);
this.usageCounter = context.usageCounter;
this.ruleType = ruleType;
this.ruleConsumer = null;
this.taskInstance = taskInstanceToAlertTaskInstance(taskInstance);
this.ruleTypeRegistry = context.ruleTypeRegistry;
this.searchAbortController = new AbortController();
this.cancelled = false;
this.executionId = uuidv4();
this.inMemoryMetrics = inMemoryMetrics;
this.internalSavedObjectsRepository = internalSavedObjectsRepository;
this.timer = new TaskRunnerTimer({ logger: this.logger });
this.alertingEventLogger = new AlertingEventLogger(this.context.eventLogger);
this.stackTraceLog = null;
this.ruleMonitoring = new RuleMonitoringService();
this.ruleRunning = new RuleRunningHandler(
this.internalSavedObjectsRepository,
this.logger,
loggerId
);
this.ruleTypeRunner = new RuleTypeRunner<
Params,
ExtractedParams,
RuleState,
AlertState,
Context,
ActionGroupIds,
RecoveryActionGroupId,
AlertData
>({
context: this.context,
logger: this.logger,
task: this.taskInstance,
timer: this.timer,
});
this.ruleResult = new RuleResultService();
}
private async updateRuleSavedObjectPostRun(
ruleId: string,
attributes: {
executionStatus?: RawRuleExecutionStatus;
monitoring?: RawRuleMonitoring;
nextRun?: string | null;
lastRun?: RawRuleLastRun | null;
}
) {
const client = this.context.elasticsearch.client.asInternalUser;
try {
// Future engineer -> Here we are just checking if we need to wait for
// the update of the attribute `running` in the rule's saved object
// and we are swallowing the error because we still want to move forward
// with the update of our rule since we are putting back the running attribute
// back to false
await this.ruleRunning.waitFor();
// eslint-disable-next-line no-empty
} catch {}
try {
await partiallyUpdateRuleWithEs(
client,
ruleId,
{ ...attributes, running: false },
{
ignore404: true,
refresh: false,
}
);
} catch (err) {
this.logger.error(`error updating rule for ${this.ruleType.id}:${ruleId} ${err.message}`);
}
}
private shouldLogAndScheduleActionsForAlerts() {
// if execution hasn't been cancelled, return true
if (!this.cancelled) {
return true;
}
// if execution has been cancelled, return true if EITHER alerting config or rule type indicate to proceed with scheduling actions
return !this.context.cancelAlertsOnRuleTimeout || !this.ruleType.cancelAlertsOnRuleTimeout;
}
// Usage counter for telemetry
// This keeps track of how many times action executions were skipped after rule
// execution completed successfully after the execution timeout
// This can occur when rule executors do not short circuit execution in response
// to timeout
private countUsageOfActionExecutionAfterRuleCancellation() {
if (this.cancelled && this.usageCounter) {
if (this.context.cancelAlertsOnRuleTimeout && this.ruleType.cancelAlertsOnRuleTimeout) {
// Increment usage counter for skipped actions
this.usageCounter.incrementCounter({
counterName: `alertsSkippedDueToRuleExecutionTimeout_${this.ruleType.id}`,
incrementBy: 1,
});
}
}
}
private async runRule({
fakeRequest,
rule,
apiKey,
validatedParams: params,
}: RunRuleParams<Params>): Promise<RuleTaskStateAndMetrics> {
if (apm.currentTransaction) {
apm.currentTransaction.name = `Execute Alerting Rule: "${rule.name}"`;
apm.currentTransaction.addLabels({
alerting_rule_consumer: rule.consumer,
alerting_rule_name: rule.name,
alerting_rule_tags: rule.tags.join(', '),
alerting_rule_type_id: rule.alertTypeId,
alerting_rule_params: JSON.stringify(rule.params),
});
}
const {
params: { alertId: ruleId, spaceId },
state: { previousStartedAt },
} = this.taskInstance;
const { queryDelaySettings, flappingSettings: spaceFlappingSettings } =
await this.context.rulesSettingsService.getSettings(fakeRequest, spaceId);
const ruleRunMetricsStore = new RuleRunMetricsStore();
const ruleLabel = `${this.ruleType.id}:${ruleId}: '${rule.name}'`;
const ruleFlappingSettings = rule.flapping
? {
enabled: true,
...rule.flapping,
}
: null;
const flappingSettings = spaceFlappingSettings.enabled
? ruleFlappingSettings || spaceFlappingSettings
: spaceFlappingSettings;
const ruleTypeRunnerContext = {
alertingEventLogger: this.alertingEventLogger,
flappingSettings,
maintenanceWindowsService: this.context.maintenanceWindowsService,
namespace: this.context.spaceIdToNamespace(spaceId),
queryDelaySec: queryDelaySettings.delay,
request: fakeRequest,
ruleId,
ruleLogPrefix: ruleLabel,
ruleRunMetricsStore,
spaceId,
};
const alertsClient = await withAlertingSpan('alerting:initialize-alerts-client', () =>
initializeAlertsClient<
Params,
AlertData,
AlertState,
Context,
ActionGroupIds,
RecoveryActionGroupId
>({
alertsService: this.context.alertsService,
context: ruleTypeRunnerContext,
executionId: this.executionId,
logger: this.logger,
maxAlerts: this.context.maxAlerts,
rule: {
id: rule.id,
name: rule.name,
tags: rule.tags,
consumer: rule.consumer,
revision: rule.revision,
alertDelay: rule.alertDelay,
params: rule.params,
},
ruleType: this.ruleType as UntypedNormalizedRuleType,
startedAt: this.taskInstance.startedAt,
taskInstance: this.taskInstance,
})
);
const executorServices = getExecutorServices({
context: this.context,
fakeRequest,
abortController: this.searchAbortController,
logger: this.logger,
ruleMonitoringService: this.ruleMonitoring,
ruleResultService: this.ruleResult,
ruleData: {
name: rule.name,
alertTypeId: rule.alertTypeId,
id: rule.id,
spaceId,
},
ruleTaskTimeout: this.ruleType.ruleTaskTimeout,
});
const {
state: updatedRuleTypeState,
error,
stackTrace,
} = await this.ruleTypeRunner.run({
context: ruleTypeRunnerContext,
alertsClient,
executionId: this.executionId,
executorServices,
rule,
ruleType: this.ruleType,
startedAt: this.taskInstance.startedAt!,
state: this.taskInstance.state,
validatedParams: params,
});
// if there was an error, save the stack trace and throw
if (error) {
this.stackTraceLog = stackTrace ?? null;
throw error;
}
const actionScheduler = new ActionScheduler({
rule,
ruleType: this.ruleType,
logger: this.logger,
taskRunnerContext: this.context,
taskInstance: this.taskInstance,
ruleRunMetricsStore,
apiKey,
ruleConsumer: this.ruleConsumer!,
executionId: this.executionId,
ruleLabel,
previousStartedAt: previousStartedAt ? new Date(previousStartedAt) : null,
alertingEventLogger: this.alertingEventLogger,
actionsClient: await this.context.actionsPlugin.getActionsClientWithRequest(fakeRequest),
alertsClient,
});
let actionSchedulerResult: RunResult = { throttledSummaryActions: {} };
await withAlertingSpan('alerting:schedule-actions', () =>
this.timer.runWithTimer(TaskRunnerTimerSpan.TriggerActions, async () => {
if (isRuleSnoozed(rule)) {
this.logger.debug(`no scheduling of actions for rule ${ruleLabel}: rule is snoozed.`);
} else if (!this.shouldLogAndScheduleActionsForAlerts()) {
this.logger.debug(
`no scheduling of actions for rule ${ruleLabel}: rule execution has been cancelled.`
);
this.countUsageOfActionExecutionAfterRuleCancellation();
} else {
actionSchedulerResult = await actionScheduler.run({
activeCurrentAlerts: alertsClient.getProcessedAlerts('activeCurrent'),
recoveredCurrentAlerts: alertsClient.getProcessedAlerts('recoveredCurrent'),
});
}
})
);
let alertsToReturn: Record<string, RawAlertInstance> = {};
let recoveredAlertsToReturn: Record<string, RawAlertInstance> = {};
// Only serialize alerts into task state if we're auto-recovering, otherwise
// we don't need to keep this information around.
if (this.ruleType.autoRecoverAlerts) {
const { alertsToReturn: alerts, recoveredAlertsToReturn: recovered } =
alertsClient.getAlertsToSerialize();
alertsToReturn = alerts;
recoveredAlertsToReturn = recovered;
}
return {
metrics: ruleRunMetricsStore.getMetrics(),
alertTypeState: updatedRuleTypeState || undefined,
alertInstances: alertsToReturn,
alertRecoveredInstances: recoveredAlertsToReturn,
summaryActions: actionSchedulerResult.throttledSummaryActions,
};
}
/**
* Before we actually run the rule:
* - start the RunningHandler
* - initialize the event logger
* - if rule data not loaded, load it
* - set the current APM transaction info
* - validate that rule type is enabled and params are valid
* - initialize monitoring data
* - clear expired snoozes
*/
private async prepareToRun(): Promise<RunRuleParams<Params>> {
return await this.timer.runWithTimer(TaskRunnerTimerSpan.PrepareRule, async () => {
const {
params: { alertId: ruleId, spaceId, consumer },
startedAt,
} = this.taskInstance;
// Initially use consumer as stored inside the task instance
// This allows us to populate a consumer value for event log
// `execute-start` events (which are indexed before the rule SO is read)
// and in the event of decryption errors (where we cannot read the rule SO)
// Because "consumer" is set when a rule is created, this value should be static
// for the life of a rule but there may be edge cases where migrations cause
// the consumer values to become out of sync.
if (consumer) {
this.ruleConsumer = consumer;
}
// Start the event logger so that something is logged in the
// event that rule SO decryption fails.
const namespace = this.context.spaceIdToNamespace(spaceId);
this.alertingEventLogger.initialize({
context: {
savedObjectId: ruleId,
savedObjectType: RULE_SAVED_OBJECT_TYPE,
spaceId,
executionId: this.executionId,
taskScheduledAt: this.taskInstance.scheduledAt,
...(namespace ? { namespace } : {}),
},
runDate: this.runDate,
ruleData: {
id: ruleId,
type: this.ruleType as UntypedNormalizedRuleType,
consumer: this.ruleConsumer!,
},
});
if (apm.currentTransaction) {
apm.currentTransaction.name = `Execute Alerting Rule`;
apm.currentTransaction.addLabels({
alerting_rule_space_id: spaceId,
alerting_rule_id: ruleId,
plugins: 'alerting',
});
}
this.ruleRunning.start(ruleId, this.context.spaceIdToNamespace(spaceId));
this.logger.debug(
`executing rule ${this.ruleType.id}:${ruleId} at ${this.runDate.toISOString()}`
);
if (startedAt) {
// Capture how long it took for the rule to start running after being claimed
this.timer.setDuration(TaskRunnerTimerSpan.StartTaskRun, startedAt);
}
const ruleData = await withAlertingSpan('alerting:get-decrypted-rule', () =>
getDecryptedRule(this.context, ruleId, spaceId)
);
const runRuleParams = validateRuleAndCreateFakeRequest({
ruleData,
paramValidator: this.ruleType.validate.params,
ruleId,
spaceId,
logger: this.logger,
context: this.context,
ruleTypeRegistry: this.ruleTypeRegistry,
});
// Update the consumer
this.ruleConsumer = runRuleParams.rule.consumer;
// Update the rule data in event logger
this.alertingEventLogger.addOrUpdateRuleData({
name: runRuleParams.rule.name,
consumer: runRuleParams.rule.consumer,
revision: runRuleParams.rule.revision,
});
// Set rule monitoring data
this.ruleMonitoring.setMonitoring(runRuleParams.rule.monitoring);
(async () => {
try {
await clearExpiredSnoozes({
esClient: this.context.elasticsearch.client.asInternalUser,
logger: this.logger,
rule: runRuleParams.rule,
version: runRuleParams.version,
});
} catch (e) {
// Most likely a 409 conflict error, which is ok, we'll try again at the next rule run
this.logger.debug(`Failed to clear expired snoozes: ${e.message}`);
}
})().catch(() => {});
return runRuleParams;
});
}
private async processRunResults({
schedule,
stateWithMetrics,
}: {
schedule: Result<IntervalSchedule, Error>;
stateWithMetrics: Result<RuleTaskStateAndMetrics, Error>;
}) {
const { executionStatus: execStatus, executionMetrics: execMetrics } =
await this.timer.runWithTimer(TaskRunnerTimerSpan.ProcessRuleRun, async () => {
const {
params: { alertId: ruleId },
startedAt,
schedule: taskSchedule,
} = this.taskInstance;
let nextRun: string | null = null;
if (isOk(schedule)) {
nextRun = getNextRun({ startDate: startedAt, interval: schedule.value.interval });
} else if (taskSchedule) {
nextRun = getNextRun({ startDate: startedAt, interval: taskSchedule.interval });
}
const { executionStatus, executionMetrics, lastRun, outcome } = processRunResults({
logger: this.logger,
logPrefix: `${this.ruleType.id}:${ruleId}`,
result: this.ruleResult,
runDate: this.runDate,
runResultWithMetrics: stateWithMetrics,
});
if (apm.currentTransaction) {
apm.currentTransaction.setOutcome(outcome);
}
// set start and duration based on event log
const { start, duration } = this.alertingEventLogger.getStartAndDuration();
if (null != start) {
executionStatus.lastExecutionDate = start;
}
if (null != duration) {
executionStatus.lastDuration = nanosToMillis(duration);
}
// if executionStatus indicates an error, fill in fields in
this.ruleMonitoring.addHistory({
duration: executionStatus.lastDuration,
hasError: executionStatus.error != null,
runDate: this.runDate,
});
if (!this.cancelled) {
this.inMemoryMetrics.increment(IN_MEMORY_METRICS.RULE_EXECUTIONS);
if (outcome === 'failure') {
this.inMemoryMetrics.increment(IN_MEMORY_METRICS.RULE_FAILURES);
}
if (this.logger.isLevelEnabled('debug')) {
this.logger.debug(
`Updating rule task for ${this.ruleType.id} rule with id ${ruleId} - ${JSON.stringify(
executionStatus
)} - ${JSON.stringify(lastRun)}`
);
}
await this.updateRuleSavedObjectPostRun(ruleId, {
executionStatus: ruleExecutionStatusToRaw(executionStatus),
nextRun,
lastRun: lastRunToRaw(lastRun),
monitoring: this.ruleMonitoring.getMonitoring() as RawRuleMonitoring,
});
}
if (startedAt) {
// Capture how long it took for the rule to run after being claimed
this.timer.setDuration(TaskRunnerTimerSpan.TotalRunDuration, startedAt);
}
return { executionStatus, executionMetrics };
});
this.alertingEventLogger.done({
status: execStatus,
metrics: execMetrics,
timings: this.timer.toJson(),
});
}
async run(): Promise<RuleTaskRunResult> {
this.runDate = new Date();
const {
params: { alertId: ruleId, spaceId },
startedAt,
state: originalState,
schedule: taskSchedule,
} = this.taskInstance;
this.logger = createTaskRunnerLogger({ logger: this.logger, tags: [ruleId, this.ruleType.id] });
let stateWithMetrics: Result<RuleTaskStateAndMetrics, Error>;
let schedule: Result<IntervalSchedule, Error>;
try {
const validatedRuleData = await this.prepareToRun();
stateWithMetrics = asOk(
await withAlertingSpan('alerting:run', () => this.runRule(validatedRuleData))
);
schedule = asOk(validatedRuleData.rule.schedule);
} catch (err) {
stateWithMetrics = asErr(err);
schedule = asErr(err);
}
await withAlertingSpan('alerting:process-run-results-and-update-rule', () =>
this.processRunResults({ schedule, stateWithMetrics })
);
const transformRunStateToTaskState = (
runStateWithMetrics: RuleTaskStateAndMetrics
): RuleTaskState => {
return {
...omit(runStateWithMetrics, ['metrics']),
previousStartedAt: startedAt?.toISOString(),
};
};
const getTaskRunError = (state: Result<RuleTaskStateAndMetrics, Error>) => {
if (isErr(state)) {
return {
taskRunError: createTaskRunError(state.error, getErrorSource(state.error)),
};
}
const { errors: errorsFromLastRun } = this.ruleResult.getLastRunResults();
if (errorsFromLastRun.length > 0) {
const isLastRunUserError = !errorsFromLastRun.some(
(lastRunError) => !lastRunError.userError
);
const errorSource = isLastRunUserError ? TaskErrorSource.USER : TaskErrorSource.FRAMEWORK;
const lasRunErrorMessages = errorsFromLastRun
.map((lastRunError) => lastRunError.message)
.join(',');
const errorMessage = `Executing Rule ${this.ruleType.id}:${ruleId} has resulted in the following error(s): ${lasRunErrorMessages}`;
this.logger.error(errorMessage, {
tags: [this.ruleType.id, ruleId, 'rule-run-failed', `${errorSource}-error`],
});
return {
taskRunError: createTaskRunError(new Error(errorMessage), errorSource),
};
}
return {};
};
return {
state: map<RuleTaskStateAndMetrics, ElasticsearchError, RuleTaskState>(
stateWithMetrics,
(ruleRunStateWithMetrics: RuleTaskStateAndMetrics) =>
transformRunStateToTaskState(ruleRunStateWithMetrics),
(err: ElasticsearchError) => {
const errorSource = isUserError(err) ? TaskErrorSource.USER : TaskErrorSource.FRAMEWORK;
const errorSourceTag = `${errorSource}-error`;
if (isAlertSavedObjectNotFoundError(err, ruleId)) {
const message = `Executing Rule ${spaceId}:${
this.ruleType.id
}:${ruleId} has resulted in Error: ${getEsErrorMessage(err)}`;
this.logger.debug(message, {
tags: [this.ruleType.id, ruleId, 'rule-run-failed', errorSourceTag],
});
} else {
const error = this.stackTraceLog ? this.stackTraceLog.message : err;
const stack = this.stackTraceLog ? this.stackTraceLog.stackTrace : err.stack;
const message = `Executing Rule ${spaceId}:${
this.ruleType.id
}:${ruleId} has resulted in Error: ${getEsErrorMessage(error)} - ${stack ?? ''}`;
this.logger.error(message, {
tags: [this.ruleType.id, ruleId, 'rule-run-failed', errorSourceTag],
error: { stack_trace: stack },
});
}
return originalState;
}
),
schedule: resolveErr<IntervalSchedule | undefined, Error>(schedule, (error) => {
if (isAlertSavedObjectNotFoundError(error, ruleId)) {
const spaceMessage = spaceId ? `in the "${spaceId}" space ` : '';
this.logger.warn(
`Unable to execute rule "${ruleId}" ${spaceMessage}because ${error.message} - this rule will not be rescheduled. To restart rule execution, try disabling and re-enabling this rule.`
);
throwUnrecoverableError(error);
}
let retryInterval = taskSchedule?.interval ?? FALLBACK_RETRY_INTERVAL;
// Set retry interval smaller for ES connectivity errors
if (isEsUnavailableError(error, ruleId)) {
retryInterval =
parseDuration(retryInterval) > parseDuration(CONNECTIVITY_RETRY_INTERVAL)
? CONNECTIVITY_RETRY_INTERVAL
: retryInterval;
}
return { interval: retryInterval };
}),
monitoring: this.ruleMonitoring.getMonitoring(),
...getTaskRunError(stateWithMetrics),
};
}
async cancel(): Promise<void> {
if (this.cancelled) {
return;
}
this.cancelled = true;
this.ruleTypeRunner.cancelRun();
// Write event log entry
const {
params: { alertId: ruleId, consumer },
schedule: taskSchedule,
startedAt,
} = this.taskInstance;
if (consumer && !this.ruleConsumer) {
this.ruleConsumer = consumer;
}
this.logger.debug(
`Cancelling rule type ${this.ruleType.id} with id ${ruleId} - execution exceeded rule type timeout of ${this.ruleType.ruleTaskTimeout}`
);
this.logger.debug(
`Aborting any in-progress ES searches for rule type ${this.ruleType.id} with id ${ruleId}`
);
this.searchAbortController.abort();
this.alertingEventLogger.logTimeout();
this.inMemoryMetrics.increment(IN_MEMORY_METRICS.RULE_TIMEOUTS);
let nextRun: string | null = null;
if (taskSchedule) {
nextRun = getNextRun({ startDate: startedAt, interval: taskSchedule.interval });
}
const outcomeMsg = [
`${this.ruleType.id}:${ruleId}: execution cancelled due to timeout - exceeded rule type timeout of ${this.ruleType.ruleTaskTimeout}`,
];
const date = new Date();
// Update the rule saved object with execution status
const executionStatus: RuleExecutionStatus = {
lastExecutionDate: date,
status: 'error',
error: {
reason: RuleExecutionStatusErrorReasons.Timeout,
message: outcomeMsg.join(' '),
},
};
this.logger.debug(
`Updating rule task for ${this.ruleType.id} rule with id ${ruleId} - execution error due to timeout`
);
const outcome = 'failed';
await this.updateRuleSavedObjectPostRun(ruleId, {
executionStatus: ruleExecutionStatusToRaw(executionStatus),
lastRun: {
outcome,
outcomeOrder: RuleLastRunOutcomeOrderMap[outcome],
warning: RuleExecutionStatusErrorReasons.Timeout,
outcomeMsg,
alertsCount: {},
},
monitoring: this.ruleMonitoring.getMonitoring() as RawRuleMonitoring,
nextRun: nextRun && new Date(nextRun).getTime() > date.getTime() ? nextRun : null,
});
}
}