Skip to content

Commit

Permalink
feat(log): Add OIBus name and OIBus ID in loki logs
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerni10 committed Jun 21, 2023
1 parent 78b4709 commit cd57a07
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const LOG_DB_NAME = 'journal.db';
}

const loggerService = new LoggerService(encryptionService);
await loggerService.start(oibusSettings.id, oibusSettings.logParameters);
await loggerService.start(oibusSettings.id, oibusSettings.name, oibusSettings.logParameters);

const proxyService = new ProxyService(repositoryService.proxyRepository, encryptionService);
const healthSignalService = new HealthSignalService(
Expand Down
13 changes: 8 additions & 5 deletions backend/src/service/logger/logger.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const encryptionService: EncryptionService = new EncryptionServiceMock('', '');

let service: LoggerService;
let logSettings: LogSettings;
const oibusId = 'MyOIBus';
const oibusId = 'MyOIBusId';
const oibusName = 'MyOIBusName';

describe('Logger', () => {
beforeEach(() => {
Expand Down Expand Up @@ -77,13 +78,14 @@ describe('Logger', () => {
tokenAddress: logSettings.loki.tokenAddress,
address: logSettings.loki.address,
id: oibusId,
name: oibusName,
interval: logSettings.loki.interval
},
level: logSettings.loki.level
}
];

await service.start(oibusId, logSettings);
await service.start(oibusId, oibusName, logSettings);

expect(pino).toHaveBeenCalledTimes(1);
expect(pino).toHaveBeenCalledWith({
Expand All @@ -105,7 +107,7 @@ describe('Logger', () => {
});

logSettings.database.maxNumberOfLogs = 0;
await service.start(oibusId, logSettings);
await service.start(oibusId, oibusName, logSettings);

expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(new Error('decrypt-error'));
Expand Down Expand Up @@ -135,13 +137,14 @@ describe('Logger', () => {
tokenAddress: logSettings.loki.tokenAddress,
address: logSettings.loki.address,
id: oibusId,
name: oibusName,
interval: logSettings.loki.interval
},
level: logSettings.loki.level
}
];

await service.start(oibusId, logSettings);
await service.start(oibusId, oibusName, logSettings);

expect(pino).toHaveBeenCalledTimes(1);
expect(pino).toHaveBeenCalledWith({
Expand Down Expand Up @@ -171,7 +174,7 @@ describe('Logger', () => {
}
];

await service.start(oibusId, logSettings);
await service.start(oibusId, oibusName, logSettings);

expect(pino).toHaveBeenCalledTimes(1);
expect(pino).toHaveBeenCalledWith({
Expand Down
3 changes: 2 additions & 1 deletion backend/src/service/logger/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class LoggerService {
/**
* Run the appropriate pino log transports according to the configuration
*/
async start(oibusId: string, logParameters: LogSettings): Promise<void> {
async start(oibusId: string, oibusName: string, logParameters: LogSettings): Promise<void> {
await createFolder(LOG_FOLDER_NAME);

const targets = [];
Expand Down Expand Up @@ -70,6 +70,7 @@ class LoggerService {
address: logParameters.loki.address,
tokenAddress: logParameters.loki.tokenAddress,
id: oibusId,
name: oibusName,
interval: logParameters.loki.interval
},
level: logParameters.loki.level
Expand Down
2 changes: 2 additions & 0 deletions backend/src/service/logger/loki-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface LokiOptions {
tokenAddress?: string;
address: string;
id: string;
oibusName: string;
interval?: number;
batchLimit?: number;
}
Expand Down Expand Up @@ -76,6 +77,7 @@ class LokiTransport {
streams.push({
stream: {
oibus: this.options.id,
oibusName: this.options.oibusName,
level: LEVEL_FORMAT[logLevel],
scope: jsonMessage.scope
},
Expand Down
4 changes: 2 additions & 2 deletions backend/src/service/reload.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ describe('reload service', () => {

it('should update log parameters', async () => {
const changeLoggerFn = jest.fn();
const newSettings = { id: 'oibusId', logParameters: {} as LogSettings, healthSignal: {} };
const newSettings = { id: 'oibusId', name: 'oibusName', logParameters: {} as LogSettings, healthSignal: {} };
service.setWebServerChangeLogger(changeLoggerFn);
await service.onUpdateOibusSettings(null, newSettings as EngineSettingsDTO);
expect(loggerService.stop).toHaveBeenCalledTimes(1);
expect(loggerService.start).toHaveBeenCalledWith(newSettings.id, newSettings.logParameters);
expect(loggerService.start).toHaveBeenCalledWith(newSettings.id, newSettings.name, newSettings.logParameters);
expect(changeLoggerFn).toHaveBeenCalledTimes(1);
expect(healthSignalService.setLogger).toHaveBeenCalledTimes(1);
expect(healthSignalService.setSettings).toHaveBeenCalledWith(newSettings.healthSignal);
Expand Down
8 changes: 6 additions & 2 deletions backend/src/service/reload.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ export default class ReloadService {
}

async onUpdateOibusSettings(oldSettings: EngineSettingsDTO | null, newSettings: EngineSettingsDTO): Promise<void> {
if (!oldSettings || JSON.stringify(oldSettings.logParameters) !== JSON.stringify(newSettings.logParameters)) {
if (
!oldSettings ||
JSON.stringify(oldSettings.logParameters) !== JSON.stringify(newSettings.logParameters) ||
oldSettings.name !== newSettings.name
) {
await this.loggerService.stop();
await this.loggerService.start(newSettings.id, newSettings.logParameters);
await this.loggerService.start(newSettings.id, newSettings.name, newSettings.logParameters);
await this.webServerChangeLoggerCallback(this.loggerService.createChildLogger('web-server'));
await this.healthSignalService.setLogger(this.loggerService.createChildLogger('health'));
await this.oibusEngine.setLogger(this.loggerService.createChildLogger('engine'));
Expand Down
1 change: 1 addition & 0 deletions shared/model/logs.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface LogStreamValuesCommandDTO {
stream: {
level: LogLevel;
oibus: string;
oibusName: string;
scope: string;
};
}
Expand Down

0 comments on commit cd57a07

Please sign in to comment.