Skip to content

Commit a12a70a

Browse files
[8.x] Improve error logs for task manager poller (#197635) (#197804)
# Backport This will backport the following commits from `main` to `8.x`: - [Improve error logs for task manager poller (#197635)](#197635) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Mike Côté","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-25T11:12:44Z","message":"Improve error logs for task manager poller (#197635)\n\nI noticed some scenarios we see error logs from the task poller like\r\n`Failed to poll for work: undefined` making me think `err.message` is\r\nempty in some situations. I'm modifying the code to handle string\r\nsituations if ever they occur by performing `err.message || err` and to\r\nalso include a stack trace when strings are passed-in.\r\n\r\n---------\r\n\r\nCo-authored-by: Patrick Mueller <[email protected]>","sha":"81b63c60eb6d1fe623f2e177cd55d2f285f79590","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Feature:Task Manager","Team:ResponseOps","v9.0.0","backport:prev-minor","v8.16.0","v8.17.0"],"title":"Improve error logs for task manager poller","number":197635,"url":"https://github.com/elastic/kibana/pull/197635","mergeCommit":{"message":"Improve error logs for task manager poller (#197635)\n\nI noticed some scenarios we see error logs from the task poller like\r\n`Failed to poll for work: undefined` making me think `err.message` is\r\nempty in some situations. I'm modifying the code to handle string\r\nsituations if ever they occur by performing `err.message || err` and to\r\nalso include a stack trace when strings are passed-in.\r\n\r\n---------\r\n\r\nCo-authored-by: Patrick Mueller <[email protected]>","sha":"81b63c60eb6d1fe623f2e177cd55d2f285f79590"}},"sourceBranch":"main","suggestedTargetBranches":["8.16","8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/197635","number":197635,"mergeCommit":{"message":"Improve error logs for task manager poller (#197635)\n\nI noticed some scenarios we see error logs from the task poller like\r\n`Failed to poll for work: undefined` making me think `err.message` is\r\nempty in some situations. I'm modifying the code to handle string\r\nsituations if ever they occur by performing `err.message || err` and to\r\nalso include a stack trace when strings are passed-in.\r\n\r\n---------\r\n\r\nCo-authored-by: Patrick Mueller <[email protected]>","sha":"81b63c60eb6d1fe623f2e177cd55d2f285f79590"}},{"branch":"8.16","label":"v8.16.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Mike Côté <[email protected]>
1 parent b7ad3e2 commit a12a70a

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

x-pack/plugins/task_manager/server/polling/task_poller.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,37 @@ describe('TaskPoller', () => {
266266
expect(handler.mock.calls[0][0].error.stack).toContain(workError.stack);
267267
});
268268

269+
test('still logs errors when they are thrown as strings', async () => {
270+
const pollInterval = 100;
271+
272+
const handler = jest.fn();
273+
const workError = 'failed to work';
274+
const poller = createTaskPoller<string, string[]>({
275+
initialPollInterval: pollInterval,
276+
logger: loggingSystemMock.create().get(),
277+
pollInterval$: of(pollInterval),
278+
pollIntervalDelay$: of(0),
279+
work: async (...args) => {
280+
throw workError;
281+
},
282+
getCapacity: () => 5,
283+
});
284+
poller.events$.subscribe(handler);
285+
poller.start();
286+
287+
clock.tick(pollInterval);
288+
await new Promise((resolve) => setImmediate(resolve));
289+
290+
const expectedError = new PollingError<string>(
291+
'Failed to poll for work: failed to work',
292+
PollingErrorType.WorkError,
293+
none
294+
);
295+
expect(handler).toHaveBeenCalledWith(asErr(expectedError));
296+
expect(handler.mock.calls[0][0].error.type).toEqual(PollingErrorType.WorkError);
297+
expect(handler.mock.calls[0][0].error.stack).toBeDefined();
298+
});
299+
269300
test('continues polling after work fails', async () => {
270301
const pollInterval = 100;
271302

x-pack/plugins/task_manager/server/polling/task_poller.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,14 @@ export enum PollingErrorType {
151151
}
152152

153153
function asPollingError<T>(err: Error, type: PollingErrorType, data: Option<T> = none) {
154-
return asErr(new PollingError<T>(`Failed to poll for work: ${err.message}`, type, data, err));
154+
return asErr(
155+
new PollingError<T>(
156+
`Failed to poll for work: ${err.message || err}`,
157+
type,
158+
data,
159+
err instanceof Error ? err : new Error(`${err}`)
160+
)
161+
);
155162
}
156163

157164
export class PollingError<T> extends Error {

0 commit comments

Comments
 (0)