Skip to content

Commit

Permalink
Use different logger to make sure heartbeat is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Siegrift committed Oct 30, 2023
1 parent 1e44788 commit 1a92b46
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
7 changes: 4 additions & 3 deletions packages/pusher/src/heartbeat/heartbeat.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as promiseUtilsModule from '@api3/promise-utils';

import { config, parseHeartbeatLog } from '../../test/fixtures';
import { logger } from '../logger';
import * as stateModule from '../state';
import { loadRawConfig } from '../validation/config';

import { heartbeatLogger } from './logger';

import { initiateHeartbeat, logHeartbeat, createHash } from '.';

// eslint-disable-next-line jest/no-hooks
Expand All @@ -30,12 +31,12 @@ describe(logHeartbeat.name, () => {
it('sends the correct heartbeat log', async () => {
const state = stateModule.getInitialState(config);
jest.spyOn(stateModule, 'getState').mockReturnValue(state);
jest.spyOn(logger, 'info');
jest.spyOn(heartbeatLogger, 'info').mockImplementation();
jest.advanceTimersByTime(1000 * 3); // Advance time by 3 seconds to ensure the timestamp of the log is different from deployment timestamp.

await logHeartbeat();

expect(logger.info).toHaveBeenCalledWith(expectedLogMessage);
expect(heartbeatLogger.info).toHaveBeenCalledWith(expectedLogMessage);
});

it('the heartbeat log can be parsed', () => {
Expand Down
9 changes: 5 additions & 4 deletions packages/pusher/src/heartbeat/heartbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { logger } from '../logger';
import { getState } from '../state';
import { loadRawConfig } from '../validation/config';

import { heartbeatLogger } from './logger';

export const initiateHeartbeat = () => {
logger.debug('Initiating heartbeat loop');
setInterval(async () => {
Expand Down Expand Up @@ -47,8 +49,7 @@ export const logHeartbeat = async () => {
const heartbeatSignature = await signHeartbeat(airnodeWallet, heartbeatPayload);
const heartbeatLog = [...heartbeatPayload, heartbeatSignature].join(' - ');

// We ensure in config validation that INFO level logs are not disabled. The logs are sent to API3 for validation
// (that the data provider deployed deployed the correct configuration) and monitoring purposes (whether the instance
// is running).
logger.info(heartbeatLog);
// The logs are sent to API3 for validation (that the data provider deployed deployed the correct configuration) and
// monitoring purposes (whether the instance is running).
heartbeatLogger.info(heartbeatLog);
};
15 changes: 15 additions & 0 deletions packages/pusher/src/heartbeat/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createLogger } from '@api3/commons';

import { loadEnv } from '../validation/env';

// We need to load the environment variables before we can use the logger. Because we want the logger to always be
// available, we load the environment variables as a side effect during the module import.
const env = loadEnv();

export const heartbeatLogger = createLogger({
colorize: env.LOG_COLORIZE,
format: env.LOG_FORMAT,
// We make sure the heartbeat logger is always enabled and logs all levels.
enabled: true,
minLevel: 'debug',
});

0 comments on commit 1a92b46

Please sign in to comment.