From 4883638c51206f52520180b84db8171fa4b1356b Mon Sep 17 00:00:00 2001 From: Kyra Cho Date: Mon, 7 Oct 2024 18:58:32 -0700 Subject: [PATCH] Add logging to importer. Closes issue #192212 (#192234) ## Summary Hello, this closes issue #192212 by adding a custom logger to the importer, making debugging issues easier. ### Checklist Delete any items that are not applicable to this PR. - [N/A] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [N/A] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios TODO - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed TODO - [N/A] Any UI touched in this PR is usable by keyboard only (learn more about [keyboard accessibility](https://webaim.org/techniques/keyboard/)) - [N/A] Any UI touched in this PR does not create any new axe failures (run axe in browser: [FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/), [Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US)) - [N/A] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [N/A] This renders correctly on smaller devices using a responsive layout. (You can test this [in your browser](https://www.browserstack.com/guide/responsive-testing-on-local-server)) - [N/A] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers) ### For maintainers - [X] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) (no API changes) --------- Co-authored-by: Elastic Machine (cherry picked from commit c36a8943982b92afd593a521d5a0bf2de06deb01) --- .../src/import/saved_objects_importer.ts | 7 +++++++ .../src/saved_objects_service.ts | 1 + .../saved_objects/routes/import.test.ts | 13 +++++++++++++ .../routes/resolve_import_errors.test.ts | 13 +++++++++++++ 4 files changed, 34 insertions(+) diff --git a/packages/core/saved-objects/core-saved-objects-import-export-server-internal/src/import/saved_objects_importer.ts b/packages/core/saved-objects/core-saved-objects-import-export-server-internal/src/import/saved_objects_importer.ts index 5b94b84cefff..f990eb13c435 100644 --- a/packages/core/saved-objects/core-saved-objects-import-export-server-internal/src/import/saved_objects_importer.ts +++ b/packages/core/saved-objects/core-saved-objects-import-export-server-internal/src/import/saved_objects_importer.ts @@ -9,6 +9,7 @@ import type { SavedObjectsImportResponse } from '@kbn/core-saved-objects-common'; import type { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server'; +import type { Logger } from '@kbn/logging'; import type { ISavedObjectTypeRegistry, ISavedObjectsImporter, @@ -27,15 +28,18 @@ export class SavedObjectsImporter implements ISavedObjectsImporter { readonly #typeRegistry: ISavedObjectTypeRegistry; readonly #importSizeLimit: number; readonly #importHooks: Record; + readonly #log: Logger; constructor({ savedObjectsClient, typeRegistry, importSizeLimit, + logger, }: { savedObjectsClient: SavedObjectsClientContract; typeRegistry: ISavedObjectTypeRegistry; importSizeLimit: number; + logger: Logger; }) { this.#savedObjectsClient = savedObjectsClient; this.#typeRegistry = typeRegistry; @@ -46,6 +50,7 @@ export class SavedObjectsImporter implements ISavedObjectsImporter { } return hooks; }, {} as Record); + this.#log = logger; } public import({ @@ -57,6 +62,7 @@ export class SavedObjectsImporter implements ISavedObjectsImporter { compatibilityMode, managed, }: SavedObjectsImportOptions): Promise { + this.#log.debug('Starting the import process'); return importSavedObjectsFromStream({ readStream, createNewCopies, @@ -80,6 +86,7 @@ export class SavedObjectsImporter implements ISavedObjectsImporter { retries, managed, }: SavedObjectsResolveImportErrorsOptions): Promise { + this.#log.debug('Resolving import errors'); return resolveSavedObjectsImportErrors({ readStream, createNewCopies, diff --git a/packages/core/saved-objects/core-saved-objects-server-internal/src/saved_objects_service.ts b/packages/core/saved-objects/core-saved-objects-server-internal/src/saved_objects_service.ts index c831d58f3a56..522a8e1943a2 100644 --- a/packages/core/saved-objects/core-saved-objects-server-internal/src/saved_objects_service.ts +++ b/packages/core/saved-objects/core-saved-objects-server-internal/src/saved_objects_service.ts @@ -376,6 +376,7 @@ export class SavedObjectsService savedObjectsClient, typeRegistry: this.typeRegistry, importSizeLimit: options?.importSizeLimit ?? this.config!.maxImportExportSize, + logger: this.logger.get('importer'), }), getTypeRegistry: () => this.typeRegistry, getDefaultIndex: () => MAIN_SAVED_OBJECT_INDEX, diff --git a/src/core/server/integration_tests/saved_objects/routes/import.test.ts b/src/core/server/integration_tests/saved_objects/routes/import.test.ts index a81196dcbb91..917f7f1642e8 100644 --- a/src/core/server/integration_tests/saved_objects/routes/import.test.ts +++ b/src/core/server/integration_tests/saved_objects/routes/import.test.ts @@ -13,6 +13,7 @@ import supertest from 'supertest'; import { SavedObjectsErrorHelpers } from '@kbn/core-saved-objects-server'; import { savedObjectsClientMock } from '@kbn/core-saved-objects-api-server-mocks'; import type { ICoreUsageStatsClient } from '@kbn/core-usage-data-base-server-internal'; +import type { Logger, LogLevelId } from '@kbn/logging'; import { coreUsageStatsClientMock, coreUsageDataServiceMock, @@ -56,6 +57,17 @@ describe(`POST ${URL}`, () => { references: [], managed: false, }; + const mockLogger: jest.Mocked = { + debug: jest.fn(), + info: jest.fn(), + error: jest.fn(), + warn: jest.fn(), + trace: jest.fn(), + fatal: jest.fn(), + log: jest.fn(), + isLevelEnabled: jest.fn((level: LogLevelId) => true), + get: jest.fn(() => mockLogger), + }; beforeEach(async () => { ({ server, httpSetup, handlerContext } = await setupServer()); @@ -76,6 +88,7 @@ describe(`POST ${URL}`, () => { savedObjectsClient, typeRegistry: handlerContext.savedObjects.typeRegistry, importSizeLimit: 10000, + logger: mockLogger, }); handlerContext.savedObjects.getImporter = jest .fn() diff --git a/src/core/server/integration_tests/saved_objects/routes/resolve_import_errors.test.ts b/src/core/server/integration_tests/saved_objects/routes/resolve_import_errors.test.ts index e31124333cbd..01fcf0683d1f 100644 --- a/src/core/server/integration_tests/saved_objects/routes/resolve_import_errors.test.ts +++ b/src/core/server/integration_tests/saved_objects/routes/resolve_import_errors.test.ts @@ -12,6 +12,7 @@ jest.mock('uuid'); import supertest from 'supertest'; import { savedObjectsClientMock } from '@kbn/core-saved-objects-api-server-mocks'; import type { ICoreUsageStatsClient } from '@kbn/core-usage-data-base-server-internal'; +import type { Logger, LogLevelId } from '@kbn/logging'; import { coreUsageStatsClientMock, coreUsageDataServiceMock, @@ -61,6 +62,17 @@ describe(`POST ${URL}`, () => { references: [], managed: false, }; + const mockLogger: jest.Mocked = { + debug: jest.fn(), + info: jest.fn(), + error: jest.fn(), + warn: jest.fn(), + trace: jest.fn(), + fatal: jest.fn(), + log: jest.fn(), + isLevelEnabled: jest.fn((level: LogLevelId) => true), + get: jest.fn(() => mockLogger), + }; beforeEach(async () => { ({ server, httpSetup, handlerContext } = await setupServer()); @@ -82,6 +94,7 @@ describe(`POST ${URL}`, () => { savedObjectsClient, typeRegistry: handlerContext.savedObjects.typeRegistry, importSizeLimit: 10000, + logger: mockLogger, }); handlerContext.savedObjects.getImporter = jest