Add optional, backward compatible logger to PG engine/indexer

Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
Eric Peterson
2022-11-29 14:22:47 +01:00
parent 448e16bad7
commit e48fc1f1ae
6 changed files with 38 additions and 3 deletions
+12
View File
@@ -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,
});
```
@@ -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<PgSearchEngine>;
// (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)
@@ -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:^",
@@ -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<PgSearchEngine> {
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 }),
});
}
@@ -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<void> {
+1
View File
@@ -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