[8.10] Exclude disabled tasks from task manager health API and Stack Monitoring (#164444) (#164620)

# Backport

This will backport the following commits from `main` to `8.10`:
- [Exclude disabled tasks from task manager health API and Stack
Monitoring (#164444)](https://github.com/elastic/kibana/pull/164444)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Mike
Côté","email":"mikecote@users.noreply.github.com"},"sourceCommit":{"committedDate":"2023-08-23T18:10:53Z","message":"Exclude
disabled tasks from task manager health API and Stack Monitoring
(#164444)\n\nResolves
https://github.com/elastic/kibana/issues/163958.\r\nResolves
https://github.com/elastic/kibana/issues/163023.\r\n\r\nIn this PR, I'm
modifying the task store's aggregate function to exclude\r\ntasks that
are disabled. This function is only used by the
monitoring\r\nfunctionality of alerting and actions plugin and the Task
Manager's\r\nhealth API which all experienced bugs where they shouldn't
be\r\nconsidering disabled task types.\r\n\r\n## To verify\r\n\r\n1.
Create 20 alerting rules running every 1s\r\n2. Call the
`/api/task_manager/_health` endpoint\r\n3. Notice capacity_estimation
stats are changing to accomodate 20 rules\r\nconstantly running\r\n4.
Disable all the alerting rules\r\n5. Call the
`/api/task_manager/_health` endpoint (wait for runtime and\r\nworkload
stats to update by looking at their timestamp and ensuring it's\r\nafter
the time you disabled the rules)\r\n6. Notice capacity_estimation stats
no longer consider the 20 rules that\r\nuse to run
constantly","sha":"140c71ad84a2527e1103256c8899e91d2145c1f4","branchLabelMapping":{"^v8.11.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Alerting","release_note:skip","Feature:Task
Manager","Feature:Actions","Team:ResponseOps","backport:prev-minor","v8.11.0"],"number":164444,"url":"https://github.com/elastic/kibana/pull/164444","mergeCommit":{"message":"Exclude
disabled tasks from task manager health API and Stack Monitoring
(#164444)\n\nResolves
https://github.com/elastic/kibana/issues/163958.\r\nResolves
https://github.com/elastic/kibana/issues/163023.\r\n\r\nIn this PR, I'm
modifying the task store's aggregate function to exclude\r\ntasks that
are disabled. This function is only used by the
monitoring\r\nfunctionality of alerting and actions plugin and the Task
Manager's\r\nhealth API which all experienced bugs where they shouldn't
be\r\nconsidering disabled task types.\r\n\r\n## To verify\r\n\r\n1.
Create 20 alerting rules running every 1s\r\n2. Call the
`/api/task_manager/_health` endpoint\r\n3. Notice capacity_estimation
stats are changing to accomodate 20 rules\r\nconstantly running\r\n4.
Disable all the alerting rules\r\n5. Call the
`/api/task_manager/_health` endpoint (wait for runtime and\r\nworkload
stats to update by looking at their timestamp and ensuring it's\r\nafter
the time you disabled the rules)\r\n6. Notice capacity_estimation stats
no longer consider the 20 rules that\r\nuse to run
constantly","sha":"140c71ad84a2527e1103256c8899e91d2145c1f4"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.11.0","labelRegex":"^v8.11.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/164444","number":164444,"mergeCommit":{"message":"Exclude
disabled tasks from task manager health API and Stack Monitoring
(#164444)\n\nResolves
https://github.com/elastic/kibana/issues/163958.\r\nResolves
https://github.com/elastic/kibana/issues/163023.\r\n\r\nIn this PR, I'm
modifying the task store's aggregate function to exclude\r\ntasks that
are disabled. This function is only used by the
monitoring\r\nfunctionality of alerting and actions plugin and the Task
Manager's\r\nhealth API which all experienced bugs where they shouldn't
be\r\nconsidering disabled task types.\r\n\r\n## To verify\r\n\r\n1.
Create 20 alerting rules running every 1s\r\n2. Call the
`/api/task_manager/_health` endpoint\r\n3. Notice capacity_estimation
stats are changing to accomodate 20 rules\r\nconstantly running\r\n4.
Disable all the alerting rules\r\n5. Call the
`/api/task_manager/_health` endpoint (wait for runtime and\r\nworkload
stats to update by looking at their timestamp and ensuring it's\r\nafter
the time you disabled the rules)\r\n6. Notice capacity_estimation stats
no longer consider the 20 rules that\r\nuse to run
constantly","sha":"140c71ad84a2527e1103256c8899e91d2145c1f4"}}]}]
BACKPORT-->

Co-authored-by: Mike Côté <mikecote@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2023-08-23 15:33:19 -04:00 committed by GitHub
parent f02c4c3d81
commit ecaaa6abaf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 6 deletions

View file

@ -381,7 +381,9 @@ describe('TaskStore', () => {
index: 'tasky',
body: {
size: 0,
query: { bool: { filter: [{ term: { type: 'task' } }] } },
query: {
bool: { filter: [{ term: { type: 'task' } }, { term: { 'task.enabled': true } }] },
},
aggs: { testAgg: { terms: { field: 'task.taskType' } } },
},
});
@ -401,7 +403,11 @@ describe('TaskStore', () => {
query: {
bool: {
must: [
{ bool: { filter: [{ term: { type: 'task' } }] } },
{
bool: {
filter: [{ term: { type: 'task' } }, { term: { 'task.enabled': true } }],
},
},
{ term: { 'task.taskType': 'bar' } },
],
},
@ -420,7 +426,9 @@ describe('TaskStore', () => {
expect(args).toMatchObject({
body: {
size: 0,
query: { bool: { filter: [{ term: { type: 'task' } }] } },
query: {
bool: { filter: [{ term: { type: 'task' } }, { term: { 'task.enabled': true } }] },
},
aggs: { testAgg: { terms: { field: 'task.taskType' } } },
runtime_mappings: { testMapping: { type: 'long', script: { source: `` } } },
},

View file

@ -477,7 +477,7 @@ export class TaskStore {
index: this.index,
ignore_unavailable: true,
track_total_hits: true,
body: ensureAggregationOnlyReturnsTaskObjects({
body: ensureAggregationOnlyReturnsEnabledTaskObjects({
query,
aggs,
runtime_mappings,
@ -595,11 +595,11 @@ function ensureQueryOnlyReturnsTaskObjects(opts: SearchOpts): SearchOpts {
};
}
function ensureAggregationOnlyReturnsTaskObjects(opts: AggregationOpts): AggregationOpts {
function ensureAggregationOnlyReturnsEnabledTaskObjects(opts: AggregationOpts): AggregationOpts {
const originalQuery = opts.query;
const filterToOnlyTasks = {
bool: {
filter: [{ term: { type: 'task' } }],
filter: [{ term: { type: 'task' } }, { term: { 'task.enabled': true } }],
},
};
const query = originalQuery

View file

@ -51,6 +51,7 @@ export function initRoutes(
validate: {
body: schema.object({
task: schema.object({
enabled: schema.boolean({ defaultValue: true }),
taskType: schema.string(),
schedule: schema.maybe(
schema.object({

View file

@ -233,6 +233,15 @@ export class SampleTaskManagerFixturePlugin
},
}),
},
taskToDisable: {
title: 'Task used for testing it being disabled',
description: '',
maxAttempts: 1,
paramsSchema: schema.object({}),
createTaskRunner: () => ({
async run() {},
}),
},
});
const taskWithTiming = {

View file

@ -34,6 +34,7 @@ export default function ({ getService }: FtrProviderContext) {
'sampleRecurringTaskWithInvalidIndirectParam',
'sampleOneTimeTaskWithInvalidIndirectParam',
'sampleTaskWithParamsSchema',
'taskToDisable',
];
// This test is meant to fail when any change is made in task manager registered types.

View file

@ -307,5 +307,33 @@ export default function ({ getService }: FtrProviderContext) {
'number'
);
});
it('should exclude disabled tasks', async () => {
const interval = '9s';
await scheduleTask({
enabled: false,
taskType: 'taskToDisable',
schedule: { interval },
});
const timestamp = new Date();
const health = await retry.try(async () => {
const result = await getHealth();
expect(new Date(result.stats.runtime.timestamp).getTime()).to.be.greaterThan(
timestamp.getTime()
);
expect(new Date(result.stats.workload.timestamp).getTime()).to.be.greaterThan(
timestamp.getTime()
);
return result;
});
expect(health.stats.runtime.value.execution.duration.taskToDisable).to.eql(undefined);
expect(health.stats.workload.value.task_types.taskToDisable).to.eql(undefined);
expect(health.stats.workload.value.schedule.find(([val]) => val === interval)).to.eql(
undefined
);
});
});
}