[8.16] Improve error logs for task manager poller (#197635) (#197803)

# Backport

This will backport the following commits from `main` to `8.16`:
- [Improve error logs for task manager poller
(#197635)](https://github.com/elastic/kibana/pull/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":"mikecote@users.noreply.github.com"},"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
<patrick.mueller@elastic.co>","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
<patrick.mueller@elastic.co>","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
<patrick.mueller@elastic.co>","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é <mikecote@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2024-10-25 23:54:02 +11:00 committed by GitHub
parent 51dcb445e9
commit b627c018bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 1 deletions

View file

@ -266,6 +266,37 @@ describe('TaskPoller', () => {
expect(handler.mock.calls[0][0].error.stack).toContain(workError.stack);
});
test('still logs errors when they are thrown as strings', async () => {
const pollInterval = 100;
const handler = jest.fn();
const workError = 'failed to work';
const poller = createTaskPoller<string, string[]>({
initialPollInterval: pollInterval,
logger: loggingSystemMock.create().get(),
pollInterval$: of(pollInterval),
pollIntervalDelay$: of(0),
work: async (...args) => {
throw workError;
},
getCapacity: () => 5,
});
poller.events$.subscribe(handler);
poller.start();
clock.tick(pollInterval);
await new Promise((resolve) => setImmediate(resolve));
const expectedError = new PollingError<string>(
'Failed to poll for work: failed to work',
PollingErrorType.WorkError,
none
);
expect(handler).toHaveBeenCalledWith(asErr(expectedError));
expect(handler.mock.calls[0][0].error.type).toEqual(PollingErrorType.WorkError);
expect(handler.mock.calls[0][0].error.stack).toBeDefined();
});
test('continues polling after work fails', async () => {
const pollInterval = 100;

View file

@ -151,7 +151,14 @@ export enum PollingErrorType {
}
function asPollingError<T>(err: Error, type: PollingErrorType, data: Option<T> = none) {
return asErr(new PollingError<T>(`Failed to poll for work: ${err.message}`, type, data, err));
return asErr(
new PollingError<T>(
`Failed to poll for work: ${err.message || err}`,
type,
data,
err instanceof Error ? err : new Error(`${err}`)
)
);
}
export class PollingError<T> extends Error {