Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make telemetry task use a schedule instead of scheduling explicitly for midnight #153380

Merged
merged 5 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions x-pack/plugins/actions/server/usage/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
*/

import { Logger, CoreSetup } from '@kbn/core/server';
import moment from 'moment';
import {
RunContext,
TaskManagerSetupContract,
TaskManagerStartContract,
IntervalSchedule,
} from '@kbn/task-manager-plugin/server';
import { PreConfiguredAction } from '../types';
import { getTotalCount, getInUseTotalCount, getExecutionsPerDayCount } from './actions_telemetry';

export const TELEMETRY_TASK_TYPE = 'actions_telemetry';

export const TASK_ID = `Actions-${TELEMETRY_TASK_TYPE}`;
export const SCHEDULE: IntervalSchedule = { interval: '1d' };

export function initializeActionsTelemetry(
logger: Logger,
Expand Down Expand Up @@ -71,6 +72,7 @@ async function scheduleTasks(logger: Logger, taskManager: TaskManagerStartContra
taskType: TELEMETRY_TASK_TYPE,
state: {},
params: {},
schedule: SCHEDULE,
});
} catch (e) {
logger.debug(`Error scheduling task, received ${e.message}`);
Expand Down Expand Up @@ -133,14 +135,12 @@ export function telemetryTaskRunner(
count_connector_types_by_action_run_outcome_per_day:
totalExecutionsPerDay.countRunOutcomeByConnectorType,
},
runAt: getNextMidnight(),
// Useful for setting a schedule for the old tasks that don't have one
// or to update the schedule if ever the frequency changes in code
schedule: SCHEDULE,
};
});
},
};
};
}

function getNextMidnight() {
return moment().add(1, 'd').startOf('d').toDate();
}
16 changes: 9 additions & 7 deletions x-pack/plugins/alerting/server/usage/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*/

import { Logger, CoreSetup } from '@kbn/core/server';
import moment from 'moment';
import {
RunContext,
TaskManagerSetupContract,
TaskManagerStartContract,
IntervalSchedule,
} from '@kbn/task-manager-plugin/server';

import { getFailedAndUnrecognizedTasksPerDay } from './lib/get_telemetry_from_task_manager';
Expand All @@ -23,6 +23,7 @@ import {
export const TELEMETRY_TASK_TYPE = 'alerting_telemetry';

export const TASK_ID = `Alerting-${TELEMETRY_TASK_TYPE}`;
export const SCHEDULE: IntervalSchedule = { interval: '1d' };

export function initializeAlertingTelemetry(
logger: Logger,
Expand Down Expand Up @@ -69,6 +70,7 @@ async function scheduleTasks(logger: Logger, taskManager: TaskManagerStartContra
taskType: TELEMETRY_TASK_TYPE,
state: {},
params: {},
schedule: SCHEDULE,
});
} catch (e) {
logger.debug(`Error scheduling task, received ${e.message}`);
Expand Down Expand Up @@ -189,22 +191,22 @@ export function telemetryTaskRunner(
percentile_num_alerts_by_type_per_day:
dailyExecutionCounts.alertsPercentilesByType,
},
runAt: getNextMidnight(),
// Useful for setting a schedule for the old tasks that don't have one
// or to update the schedule if ever the frequency changes in code
schedule: SCHEDULE,
};
}
)
.catch((errMsg) => {
logger.warn(`Error executing alerting telemetry task: ${errMsg}`);
return {
state: {},
runAt: getNextMidnight(),
// Useful for setting a schedule for the old tasks that don't have one
// or to update the schedule if ever the frequency changes in code
schedule: SCHEDULE,
};
});
},
};
};
}

function getNextMidnight() {
return moment().add(1, 'd').startOf('d').toDate();
}
1 change: 1 addition & 0 deletions x-pack/plugins/task_manager/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type {
EphemeralTask,
TaskRunCreatorFunction,
RunContext,
IntervalSchedule,
} from './task';

export { TaskStatus } from './task';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export default function createAlertingAndActionsTelemetryTests({ getService }: F
const esTestIndexTool = new ESTestIndexTool(es, retry);
const supertestWithoutAuth = getService('supertestWithoutAuth');

// FLAKY: https://github.com/elastic/kibana/issues/140973
describe.skip('telemetry', () => {
describe('telemetry', () => {
const alwaysFiringRuleId: { [key: string]: string } = {};

beforeEach(async () => {
Expand Down