From e48fc1f1ae82672151f8ebf1f85de34072693812 Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Tue, 29 Nov 2022 14:22:47 +0100 Subject: [PATCH] Add optional, backward compatible logger to PG engine/indexer Signed-off-by: Eric Peterson --- .changeset/search-antibacterial-wipe.md | 12 ++++++++++++ plugins/search-backend-module-pg/api-report.md | 6 +++++- plugins/search-backend-module-pg/package.json | 3 ++- .../src/PgSearchEngine/PgSearchEngine.ts | 14 +++++++++++++- .../src/PgSearchEngine/PgSearchEngineIndexer.ts | 5 +++++ yarn.lock | 1 + 6 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 .changeset/search-antibacterial-wipe.md diff --git a/.changeset/search-antibacterial-wipe.md b/.changeset/search-antibacterial-wipe.md new file mode 100644 index 0000000000..d69edabd3f --- /dev/null +++ b/.changeset/search-antibacterial-wipe.md @@ -0,0 +1,12 @@ +--- +'@backstage/plugin-search-backend-module-pg': minor +--- + +Added the option to pass a logger to `PgSearchEngine` during instantiation. You may do so as follows: + +```diff +const searchEngine = await PgSearchEngine.fromConfig(env.config, { + database: env.database, ++ logger: env.logger, +}); +``` diff --git a/plugins/search-backend-module-pg/api-report.md b/plugins/search-backend-module-pg/api-report.md index d35bac66aa..3c6096090e 100644 --- a/plugins/search-backend-module-pg/api-report.md +++ b/plugins/search-backend-module-pg/api-report.md @@ -8,6 +8,7 @@ import { Config } from '@backstage/config'; import { IndexableDocument } from '@backstage/plugin-search-common'; import { IndexableResultSet } from '@backstage/plugin-search-common'; import { Knex } from 'knex'; +import { Logger } from 'winston'; import { PluginDatabaseManager } from '@backstage/backend-common'; import { SearchEngine } from '@backstage/plugin-search-common'; import { SearchQuery } from '@backstage/plugin-search-common'; @@ -84,11 +85,12 @@ export interface DocumentResultRow { // @public (undocumented) export class PgSearchEngine implements SearchEngine { // @deprecated - constructor(databaseStore: DatabaseStore, config: Config); + constructor(databaseStore: DatabaseStore, config: Config, logger?: Logger); // @deprecated (undocumented) static from(options: { database: PluginDatabaseManager; config: Config; + logger?: Logger; }): Promise; // (undocumented) static fromConfig( @@ -126,6 +128,7 @@ export type PgSearchEngineIndexerOptions = { batchSize: number; type: string; databaseStore: DatabaseStore; + logger?: Logger; }; // @public @@ -144,6 +147,7 @@ export type PgSearchHighlightOptions = { // @public export type PgSearchOptions = { database: PluginDatabaseManager; + logger?: Logger; }; // @public (undocumented) diff --git a/plugins/search-backend-module-pg/package.json b/plugins/search-backend-module-pg/package.json index 25ee3cf696..2e0f46f24a 100644 --- a/plugins/search-backend-module-pg/package.json +++ b/plugins/search-backend-module-pg/package.json @@ -29,7 +29,8 @@ "@backstage/plugin-search-common": "workspace:^", "knex": "^2.0.0", "lodash": "^4.17.21", - "uuid": "^8.3.2" + "uuid": "^8.3.2", + "winston": "^3.2.1" }, "devDependencies": { "@backstage/backend-test-utils": "workspace:^", diff --git a/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts b/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts index f53c433c2f..d2cf80e0ca 100644 --- a/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts +++ b/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts @@ -28,6 +28,7 @@ import { PgSearchQuery, } from '../database'; import { v4 as uuid } from 'uuid'; +import { Logger } from 'winston'; import { Config } from '@backstage/config'; /** @@ -62,6 +63,7 @@ export type PgSearchQueryTranslator = ( */ export type PgSearchOptions = { database: PluginDatabaseManager; + logger?: Logger; }; /** @@ -82,12 +84,17 @@ export type PgSearchHighlightOptions = { /** @public */ export class PgSearchEngine implements SearchEngine { + private readonly logger?: Logger; private readonly highlightOptions: PgSearchHighlightOptions; /** * @deprecated This will be marked as private in a future release, please us fromConfig instead */ - constructor(private readonly databaseStore: DatabaseStore, config: Config) { + constructor( + private readonly databaseStore: DatabaseStore, + config: Config, + logger?: Logger, + ) { const uuidTag = uuid(); const highlightConfig = config.getOptionalConfig( 'search.pg.highlightOptions', @@ -107,6 +114,7 @@ export class PgSearchEngine implements SearchEngine { highlightConfig?.getOptionalString('fragmentDelimiter') ?? ' ... ', }; this.highlightOptions = highlightOptions; + this.logger = logger; } /** @@ -115,10 +123,12 @@ export class PgSearchEngine implements SearchEngine { static async from(options: { database: PluginDatabaseManager; config: Config; + logger?: Logger; }): Promise { return new PgSearchEngine( await DatabaseDocumentStore.create(options.database), options.config, + options.logger, ); } @@ -126,6 +136,7 @@ export class PgSearchEngine implements SearchEngine { return new PgSearchEngine( await DatabaseDocumentStore.create(options.database), config, + options.logger, ); } @@ -170,6 +181,7 @@ export class PgSearchEngine implements SearchEngine { batchSize: 1000, type, databaseStore: this.databaseStore, + logger: this.logger?.child({ documentType: type }), }); } diff --git a/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngineIndexer.ts b/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngineIndexer.ts index e3dbe82751..63dceec4dc 100644 --- a/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngineIndexer.ts +++ b/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngineIndexer.ts @@ -14,9 +14,11 @@ * limitations under the License. */ +import { getVoidLogger } from '@backstage/backend-common'; import { BatchSearchEngineIndexer } from '@backstage/plugin-search-backend-node'; import { IndexableDocument } from '@backstage/plugin-search-common'; import { Knex } from 'knex'; +import { Logger } from 'winston'; import { DatabaseStore } from '../database'; /** @public */ @@ -24,10 +26,12 @@ export type PgSearchEngineIndexerOptions = { batchSize: number; type: string; databaseStore: DatabaseStore; + logger?: Logger; }; /** @public */ export class PgSearchEngineIndexer extends BatchSearchEngineIndexer { + private logger: Logger; private store: DatabaseStore; private type: string; private tx: Knex.Transaction | undefined; @@ -36,6 +40,7 @@ export class PgSearchEngineIndexer extends BatchSearchEngineIndexer { super({ batchSize: options.batchSize }); this.store = options.databaseStore; this.type = options.type; + this.logger = options.logger || getVoidLogger(); } async initialize(): Promise { diff --git a/yarn.lock b/yarn.lock index e3d1b5eb88..c3a2a2a844 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7535,6 +7535,7 @@ __metadata: knex: ^2.0.0 lodash: ^4.17.21 uuid: ^8.3.2 + winston: ^3.2.1 languageName: unknown linkType: soft