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

Add logging to importer. Closes issue #192212 #192234

Merged
merged 5 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,15 +28,18 @@ export class SavedObjectsImporter implements ISavedObjectsImporter {
readonly #typeRegistry: ISavedObjectTypeRegistry;
readonly #importSizeLimit: number;
readonly #importHooks: Record<string, SavedObjectsImportHook[]>;
readonly #log: Logger;

constructor({
savedObjectsClient,
typeRegistry,
importSizeLimit,
logger,
}: {
savedObjectsClient: SavedObjectsClientContract;
typeRegistry: ISavedObjectTypeRegistry;
importSizeLimit: number;
logger: Logger;
}) {
this.#savedObjectsClient = savedObjectsClient;
this.#typeRegistry = typeRegistry;
Expand All @@ -46,9 +50,10 @@ export class SavedObjectsImporter implements ISavedObjectsImporter {
}
return hooks;
}, {} as Record<string, SavedObjectsImportHook[]>);
this.#log = logger;
}

public import({
public async import({
readStream,
createNewCopies,
namespace,
Expand All @@ -57,40 +62,56 @@ export class SavedObjectsImporter implements ISavedObjectsImporter {
compatibilityMode,
managed,
}: SavedObjectsImportOptions): Promise<SavedObjectsImportResponse> {
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({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awaiting a resolved promise from importSavedObjectsFromStream is a problem when importing many saved objects and can cause Kibana to run out of memory.
import shouldn't await on importSavedObjectsFromStream, we have to stick with calling the method directly.

I'd rather move the log line to before returning importSavedObjectsFromStream and revert the other changes here,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See node streams for more info

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({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't 'simply' change this....

readStream,
createNewCopies,
compatibilityMode,
namespace,
retries,
managed,
}: SavedObjectsResolveImportErrorsOptions): Promise<SavedObjectsImportResponse> {
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({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar comment as for importSavedObjectsFromStream

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -56,6 +57,17 @@ describe(`POST ${URL}`, () => {
references: [],
managed: false,
};
const mockLogger: jest.Mocked<Logger> = {
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());
Expand All @@ -76,6 +88,7 @@ describe(`POST ${URL}`, () => {
savedObjectsClient,
typeRegistry: handlerContext.savedObjects.typeRegistry,
importSizeLimit: 10000,
logger: mockLogger,
});
handlerContext.savedObjects.getImporter = jest
.fn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -61,6 +62,17 @@ describe(`POST ${URL}`, () => {
references: [],
managed: false,
};
const mockLogger: jest.Mocked<Logger> = {
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());
Expand All @@ -82,6 +94,7 @@ describe(`POST ${URL}`, () => {
savedObjectsClient,
typeRegistry: handlerContext.savedObjects.typeRegistry,
importSizeLimit: 10000,
logger: mockLogger,
});

handlerContext.savedObjects.getImporter = jest
Expand Down