diff --git a/x-pack/plugins/task_manager/server/metrics/task_metrics_collector.test.ts b/x-pack/plugins/task_manager/server/metrics/task_metrics_collector.test.ts index 72e1519573728..07ab063b7c980 100644 --- a/x-pack/plugins/task_manager/server/metrics/task_metrics_collector.test.ts +++ b/x-pack/plugins/task_manager/server/metrics/task_metrics_collector.test.ts @@ -143,8 +143,15 @@ describe('TaskManagerMetricsCollector', () => { type: 'long', script: { source: ` + def taskStatus = doc['task.status']; def runAt = doc['task.runAt']; - if(!runAt.empty) { + + if (taskStatus.empty) { + emit(0); + return; + } + + if(taskStatus == 'idle') { emit((new Date().getTime() - runAt.value.getMillis()) / 1000); } else { def retryAt = doc['task.retryAt']; diff --git a/x-pack/plugins/task_manager/server/metrics/task_metrics_collector.ts b/x-pack/plugins/task_manager/server/metrics/task_metrics_collector.ts index 70f47527807ba..a35bb5ddf5a72 100644 --- a/x-pack/plugins/task_manager/server/metrics/task_metrics_collector.ts +++ b/x-pack/plugins/task_manager/server/metrics/task_metrics_collector.ts @@ -102,8 +102,15 @@ export class TaskManagerMetricsCollector implements ITaskEventEmitter