From e5111ac0ae0a178b53e39f1f0348894f9ee8eadc Mon Sep 17 00:00:00 2001 From: Kyra Cho Date: Thu, 5 Sep 2024 20:32:40 -0700 Subject: [PATCH 1/3] Add logging to importer --- .../src/import/saved_objects_importer.ts | 75 ++++++++++++------- .../src/saved_objects_service.ts | 1 + .../saved_objects/routes/import.test.ts | 13 ++++ .../routes/resolve_import_errors.test.ts | 13 ++++ 4 files changed, 75 insertions(+), 27 deletions(-) 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 5b94b84cefff0..c393146f829fc 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,9 +50,10 @@ export class SavedObjectsImporter implements ISavedObjectsImporter { } return hooks; }, {} as Record); + this.#log = logger; } - public import({ + public async import({ readStream, createNewCopies, namespace, @@ -57,22 +62,30 @@ export class SavedObjectsImporter implements ISavedObjectsImporter { compatibilityMode, managed, }: SavedObjectsImportOptions): Promise { - return importSavedObjectsFromStream({ - readStream, - createNewCopies, - namespace, - overwrite, - refresh, - compatibilityMode, - objectLimit: this.#importSizeLimit, - savedObjectsClient: this.#savedObjectsClient, - typeRegistry: this.#typeRegistry, - importHooks: this.#importHooks, - managed, - }); + this.#log.debug('Starting the import process'); + try { + const result = await importSavedObjectsFromStream({ + readStream, + createNewCopies, + namespace, + overwrite, + refresh, + compatibilityMode, + objectLimit: this.#importSizeLimit, + savedObjectsClient: this.#savedObjectsClient, + typeRegistry: this.#typeRegistry, + importHooks: this.#importHooks, + managed, + }); + this.#log.info(`Successfully imported ${result.successCount} objects`); + return result; + } catch (error) { + this.#log.error('Import failed', error); + throw error; + } } - public resolveImportErrors({ + public async resolveImportErrors({ readStream, createNewCopies, compatibilityMode, @@ -80,17 +93,25 @@ export class SavedObjectsImporter implements ISavedObjectsImporter { retries, managed, }: SavedObjectsResolveImportErrorsOptions): Promise { - return resolveSavedObjectsImportErrors({ - readStream, - createNewCopies, - compatibilityMode, - namespace, - retries, - objectLimit: this.#importSizeLimit, - savedObjectsClient: this.#savedObjectsClient, - typeRegistry: this.#typeRegistry, - importHooks: this.#importHooks, - managed, - }); + this.#log.debug('Resolving import errors'); + try { + const result = await resolveSavedObjectsImportErrors({ + readStream, + createNewCopies, + compatibilityMode, + namespace, + retries, + objectLimit: this.#importSizeLimit, + savedObjectsClient: this.#savedObjectsClient, + typeRegistry: this.#typeRegistry, + importHooks: this.#importHooks, + managed, + }); + this.#log.info(`Resolved errors for ${result.successCount} objects`); + return result; + } catch (error) { + this.#log.error('Error resolving import errors', error); + throw error; + } } } 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 c831d58f3a56f..522a8e1943a28 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 a81196dcbb912..917f7f1642e8c 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 e31124333cbd8..01fcf0683d1f7 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 From d909a11ee567ca036ace97ccfdc5918562d3fb98 Mon Sep 17 00:00:00 2001 From: Kyra Cho Date: Fri, 4 Oct 2024 23:43:39 +0000 Subject: [PATCH 2/3] change saved_objects_importer.ts --- .../src/import/saved_objects_importer.ts | 70 ++++++++----------- 1 file changed, 29 insertions(+), 41 deletions(-) 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 c393146f829fc..f162d3fad88b7 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 @@ -53,7 +53,7 @@ export class SavedObjectsImporter implements ISavedObjectsImporter { this.#log = logger; } - public async import({ + public import({ readStream, createNewCopies, namespace, @@ -63,29 +63,23 @@ export class SavedObjectsImporter implements ISavedObjectsImporter { managed, }: SavedObjectsImportOptions): Promise { this.#log.debug('Starting the import process'); - try { - const result = await importSavedObjectsFromStream({ - readStream, - createNewCopies, - namespace, - overwrite, - refresh, - compatibilityMode, - objectLimit: this.#importSizeLimit, - savedObjectsClient: this.#savedObjectsClient, - typeRegistry: this.#typeRegistry, - importHooks: this.#importHooks, - managed, - }); - this.#log.info(`Successfully imported ${result.successCount} objects`); - return result; - } catch (error) { - this.#log.error('Import failed', error); - throw error; - } + const result = importSavedObjectsFromStream({ + readStream, + createNewCopies, + namespace, + overwrite, + refresh, + compatibilityMode, + objectLimit: this.#importSizeLimit, + savedObjectsClient: this.#savedObjectsClient, + typeRegistry: this.#typeRegistry, + importHooks: this.#importHooks, + managed, + }); + return result; } - public async resolveImportErrors({ + public resolveImportErrors({ readStream, createNewCopies, compatibilityMode, @@ -94,24 +88,18 @@ export class SavedObjectsImporter implements ISavedObjectsImporter { managed, }: SavedObjectsResolveImportErrorsOptions): Promise { this.#log.debug('Resolving import errors'); - try { - const result = await resolveSavedObjectsImportErrors({ - readStream, - createNewCopies, - compatibilityMode, - namespace, - retries, - objectLimit: this.#importSizeLimit, - savedObjectsClient: this.#savedObjectsClient, - typeRegistry: this.#typeRegistry, - importHooks: this.#importHooks, - managed, - }); - this.#log.info(`Resolved errors for ${result.successCount} objects`); - return result; - } catch (error) { - this.#log.error('Error resolving import errors', error); - throw error; - } + const result = resolveSavedObjectsImportErrors({ + readStream, + createNewCopies, + compatibilityMode, + namespace, + retries, + objectLimit: this.#importSizeLimit, + savedObjectsClient: this.#savedObjectsClient, + typeRegistry: this.#typeRegistry, + importHooks: this.#importHooks, + managed, + }); + return result; } } From 0c9f0326b197398a55bd58684008314113f56510 Mon Sep 17 00:00:00 2001 From: Kyra Cho Date: Fri, 4 Oct 2024 23:50:43 +0000 Subject: [PATCH 3/3] change saved_objects_importer.ts --- .../src/import/saved_objects_importer.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) 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 f162d3fad88b7..f990eb13c435b 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 @@ -63,7 +63,7 @@ export class SavedObjectsImporter implements ISavedObjectsImporter { managed, }: SavedObjectsImportOptions): Promise { this.#log.debug('Starting the import process'); - const result = importSavedObjectsFromStream({ + return importSavedObjectsFromStream({ readStream, createNewCopies, namespace, @@ -76,7 +76,6 @@ export class SavedObjectsImporter implements ISavedObjectsImporter { importHooks: this.#importHooks, managed, }); - return result; } public resolveImportErrors({ @@ -88,7 +87,7 @@ export class SavedObjectsImporter implements ISavedObjectsImporter { managed, }: SavedObjectsResolveImportErrorsOptions): Promise { this.#log.debug('Resolving import errors'); - const result = resolveSavedObjectsImportErrors({ + return resolveSavedObjectsImportErrors({ readStream, createNewCopies, compatibilityMode, @@ -100,6 +99,5 @@ export class SavedObjectsImporter implements ISavedObjectsImporter { importHooks: this.#importHooks, managed, }); - return result; } }