backend-app-api: flip around config and logger dependency
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -165,7 +165,6 @@ export const lifecycleFactory: () => ServiceFactory<LifecycleService>;
|
||||
|
||||
// @public
|
||||
export function loadBackendConfig(options: {
|
||||
logger: LoggerService;
|
||||
remote?: LoadConfigOptionsRemote;
|
||||
argv: string[];
|
||||
}): Promise<{
|
||||
@@ -260,7 +259,7 @@ export const urlReaderFactory: () => ServiceFactory<UrlReader>;
|
||||
// @public
|
||||
export class WinstonLogger implements RootLoggerService {
|
||||
// (undocumented)
|
||||
addRedactions(redactions: string[]): void;
|
||||
addRedactions(redactions: Iterable<string>): void;
|
||||
// (undocumented)
|
||||
child(meta: LogMeta): LoggerService;
|
||||
static colorFormat(): Format;
|
||||
|
||||
@@ -14,19 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { LoggerService } from '@backstage/backend-plugin-api';
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
import { ObservableConfigProxy } from './ObservableConfigProxy';
|
||||
|
||||
describe('ObservableConfigProxy', () => {
|
||||
const errLogger = {
|
||||
error: (message: string) => {
|
||||
throw new Error(message);
|
||||
},
|
||||
} as unknown as LoggerService;
|
||||
|
||||
it('should notify subscribers', () => {
|
||||
const config = new ObservableConfigProxy(errLogger);
|
||||
const config = new ObservableConfigProxy();
|
||||
|
||||
const fn = jest.fn();
|
||||
const sub = config.subscribe(fn);
|
||||
@@ -51,7 +44,7 @@ describe('ObservableConfigProxy', () => {
|
||||
});
|
||||
|
||||
it('should forward subscriptions', () => {
|
||||
const config1 = new ObservableConfigProxy(errLogger);
|
||||
const config1 = new ObservableConfigProxy();
|
||||
|
||||
const fn1 = jest.fn();
|
||||
const fn2 = jest.fn();
|
||||
@@ -119,7 +112,7 @@ describe('ObservableConfigProxy', () => {
|
||||
});
|
||||
|
||||
it('should make sub configs available as expected', () => {
|
||||
const config = new ObservableConfigProxy(errLogger);
|
||||
const config = new ObservableConfigProxy();
|
||||
|
||||
config.setConfig(new ConfigReader({ a: { x: 1 } }));
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { ConfigService, LoggerService } from '@backstage/backend-plugin-api';
|
||||
import { ConfigService } from '@backstage/backend-plugin-api';
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
import { JsonValue } from '@backstage/types';
|
||||
|
||||
@@ -24,7 +24,6 @@ export class ObservableConfigProxy implements ConfigService {
|
||||
private readonly subscribers: (() => void)[] = [];
|
||||
|
||||
constructor(
|
||||
private readonly logger: LoggerService,
|
||||
private readonly parent?: ObservableConfigProxy,
|
||||
private parentKey?: string,
|
||||
) {
|
||||
@@ -42,7 +41,7 @@ export class ObservableConfigProxy implements ConfigService {
|
||||
try {
|
||||
subscriber();
|
||||
} catch (error) {
|
||||
this.logger.error(`Config subscriber threw error, ${error}`);
|
||||
console.error(`Config subscriber threw error, ${error}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,11 +88,11 @@ export class ObservableConfigProxy implements ConfigService {
|
||||
return this.select(false)?.getOptional(key);
|
||||
}
|
||||
getConfig(key: string): ConfigService {
|
||||
return new ObservableConfigProxy(this.logger, this, key);
|
||||
return new ObservableConfigProxy(this, key);
|
||||
}
|
||||
getOptionalConfig(key: string): ConfigService | undefined {
|
||||
if (this.select(false)?.has(key)) {
|
||||
return new ObservableConfigProxy(this.logger, this, key);
|
||||
return new ObservableConfigProxy(this, key);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -68,8 +68,6 @@ export async function createConfigSecretEnumerator(options: {
|
||||
* @public
|
||||
*/
|
||||
export async function loadBackendConfig(options: {
|
||||
logger: LoggerService;
|
||||
// process.argv or any other overrides
|
||||
remote?: LoadConfigOptionsRemote;
|
||||
argv: string[];
|
||||
}): Promise<{ config: Config }> {
|
||||
@@ -84,14 +82,14 @@ export async function loadBackendConfig(options: {
|
||||
|
||||
let currentCancelFunc: (() => void) | undefined = undefined;
|
||||
|
||||
const config = new ObservableConfigProxy(options.logger);
|
||||
const config = new ObservableConfigProxy();
|
||||
const { appConfigs } = await loadConfig({
|
||||
configRoot: paths.targetRoot,
|
||||
configTargets: configTargets,
|
||||
remote: options.remote,
|
||||
watch: {
|
||||
onChange(newConfigs) {
|
||||
options.logger.info(
|
||||
console.info(
|
||||
`Reloaded config from ${newConfigs.map(c => c.context).join(', ')}`,
|
||||
);
|
||||
|
||||
@@ -112,7 +110,7 @@ export async function loadBackendConfig(options: {
|
||||
},
|
||||
});
|
||||
|
||||
options.logger.info(
|
||||
console.info(
|
||||
`Loaded config from ${appConfigs.map(c => c.context).join(', ')}`,
|
||||
);
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ export interface WinstonLoggerOptions {
|
||||
*/
|
||||
export class WinstonLogger implements RootLoggerService {
|
||||
#winston: Logger;
|
||||
#addRedactions?: RootLoggerService['addRedactions'];
|
||||
#addRedactions?: (redactions: Iterable<string>) => void;
|
||||
|
||||
/**
|
||||
* Creates a {@link WinstonLogger} instance.
|
||||
@@ -143,7 +143,7 @@ export class WinstonLogger implements RootLoggerService {
|
||||
|
||||
private constructor(
|
||||
winston: Logger,
|
||||
addRedactions?: RootLoggerService['addRedactions'],
|
||||
addRedactions?: (redactions: Iterable<string>) => void,
|
||||
) {
|
||||
this.#winston = winston;
|
||||
this.#addRedactions = addRedactions;
|
||||
@@ -169,7 +169,7 @@ export class WinstonLogger implements RootLoggerService {
|
||||
return new WinstonLogger(this.#winston.child(meta));
|
||||
}
|
||||
|
||||
addRedactions(redactions: string[]) {
|
||||
addRedactions(redactions: Iterable<string>) {
|
||||
this.#addRedactions?.(redactions);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,10 +19,7 @@ import {
|
||||
createServiceFactory,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import { LoadConfigOptionsRemote } from '@backstage/config-loader';
|
||||
import {
|
||||
createConfigSecretEnumerator,
|
||||
loadBackendConfig,
|
||||
} from '../../../config';
|
||||
import { loadBackendConfig } from '../../../config';
|
||||
|
||||
/** @public */
|
||||
export interface ConfigFactoryOptions {
|
||||
@@ -40,22 +37,11 @@ export interface ConfigFactoryOptions {
|
||||
/** @public */
|
||||
export const configFactory = createServiceFactory({
|
||||
service: coreServices.config,
|
||||
deps: {
|
||||
logger: coreServices.rootLogger,
|
||||
},
|
||||
async factory({ logger }, options?: ConfigFactoryOptions) {
|
||||
const argv = options?.argv ?? process.argv;
|
||||
const secretEnumerator = await createConfigSecretEnumerator({ logger });
|
||||
|
||||
const { config } = await loadBackendConfig({
|
||||
argv,
|
||||
logger,
|
||||
remote: options?.remote,
|
||||
});
|
||||
|
||||
logger.addRedactions(secretEnumerator(config));
|
||||
config.subscribe?.(() => logger.addRedactions(secretEnumerator(config)));
|
||||
deps: {},
|
||||
async factory({}, options?: ConfigFactoryOptions) {
|
||||
const { argv = process.argv, remote } = options ?? {};
|
||||
|
||||
const { config } = await loadBackendConfig({ argv, remote });
|
||||
return config;
|
||||
},
|
||||
});
|
||||
|
||||
+12
-3
@@ -20,13 +20,16 @@ import {
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import { WinstonLogger } from '../../../logging';
|
||||
import { transports, format } from 'winston';
|
||||
import { createConfigSecretEnumerator } from '../../../config';
|
||||
|
||||
/** @public */
|
||||
export const rootLoggerFactory = createServiceFactory({
|
||||
service: coreServices.rootLogger,
|
||||
deps: {},
|
||||
async factory() {
|
||||
return WinstonLogger.create({
|
||||
deps: {
|
||||
config: coreServices.config,
|
||||
},
|
||||
async factory({ config }) {
|
||||
const logger = WinstonLogger.create({
|
||||
meta: {
|
||||
service: 'backstage',
|
||||
},
|
||||
@@ -37,5 +40,11 @@ export const rootLoggerFactory = createServiceFactory({
|
||||
: WinstonLogger.colorFormat(),
|
||||
transports: [new transports.Console()],
|
||||
});
|
||||
|
||||
const secretEnumerator = await createConfigSecretEnumerator({ logger });
|
||||
logger.addRedactions(secretEnumerator(config));
|
||||
config.subscribe?.(() => logger.addRedactions(secretEnumerator(config)));
|
||||
|
||||
return logger;
|
||||
},
|
||||
});
|
||||
|
||||
@@ -282,10 +282,7 @@ export interface RootHttpRouterService {
|
||||
export interface RootLifecycleService extends LifecycleService {}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface RootLoggerService extends LoggerService {
|
||||
// (undocumented)
|
||||
addRedactions(redactions: Iterable<string>): void;
|
||||
}
|
||||
export interface RootLoggerService extends LoggerService {}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface SchedulerService extends PluginTaskScheduler {}
|
||||
|
||||
@@ -17,6 +17,4 @@
|
||||
import { LoggerService } from './LoggerService';
|
||||
|
||||
/** @public */
|
||||
export interface RootLoggerService extends LoggerService {
|
||||
addRedactions(redactions: Iterable<string>): void;
|
||||
}
|
||||
export interface RootLoggerService extends LoggerService {}
|
||||
|
||||
Reference in New Issue
Block a user