Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Chains retrieval and cache deletion debug logs #1786

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/config/entities/__tests__/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export default (): ReturnType<typeof configuration> => ({
swapsDecoding: true,
twapsDecoding: true,
debugLogs: false,
configHooksDebugLogs: false,
imitationMapping: false,
auth: false,
confirmationView: false,
Expand Down
2 changes: 2 additions & 0 deletions src/config/entities/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ export default () => ({
swapsDecoding: process.env.FF_SWAPS_DECODING?.toLowerCase() === 'true',
twapsDecoding: process.env.FF_TWAPS_DECODING?.toLowerCase() === 'true',
debugLogs: process.env.FF_DEBUG_LOGS?.toLowerCase() === 'true',
configHooksDebugLogs:
process.env.FF_CONFIG_HOOKS_DEBUG_LOGS?.toLowerCase() === 'true',
imitationMapping:
process.env.FF_IMITATION_MAPPING?.toLowerCase() === 'true',
auth: process.env.FF_AUTH?.toLowerCase() === 'true',
Expand Down
1 change: 1 addition & 0 deletions src/datasources/cache/cache.first.data.source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('CacheFirstDataSource', () => {
fakeCacheService = new FakeCacheService();
fakeConfigurationService = new FakeConfigurationService();
fakeConfigurationService.set('features.debugLogs', true);
fakeConfigurationService.set('features.configHooksDebugLogs', false);
cacheFirstDataSource = new CacheFirstDataSource(
fakeCacheService,
mockNetworkService,
Expand Down
32 changes: 32 additions & 0 deletions src/datasources/cache/cache.first.data.source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { Safe } from '@/domain/safe/entities/safe.entity';
@Injectable()
export class CacheFirstDataSource {
private readonly areDebugLogsEnabled: boolean;
private readonly areConfigHooksDebugLogsEnabled: boolean;

constructor(
@Inject(CacheService) private readonly cacheService: ICacheService,
Expand All @@ -45,6 +46,10 @@ export class CacheFirstDataSource {
) {
this.areDebugLogsEnabled =
this.configurationService.getOrThrow<boolean>('features.debugLogs');
this.areConfigHooksDebugLogsEnabled =
this.configurationService.getOrThrow<boolean>(
'features.configHooksDebugLogs',
);
}

/**
Expand Down Expand Up @@ -149,6 +154,13 @@ export class CacheFirstDataSource {
data as Safe,
);
}

if (
this.areConfigHooksDebugLogsEnabled &&
args.cacheDir.key.includes('chain')
) {
this.logChainUpdateCacheWrite(startTimeMs, args.cacheDir, data);
}
}
return data;
}
Expand Down Expand Up @@ -267,4 +279,24 @@ export class CacheFirstDataSource {
safe,
});
}

/**
* Logs the chain/chains retrieved.
* NOTE: this is a debugging-only function.
* TODO: remove this function after debugging.
*/
private logChainUpdateCacheWrite(
requestStartTime: number,
cacheDir: CacheDir,
data: unknown,
): void {
this.loggingService.info({
type: 'cache_write',
cacheKey: cacheDir.key,
cacheField: cacheDir.field,
cacheWriteTime: new Date(),
requestStartTime: new Date(requestStartTime),
data,
});
}
}
8 changes: 8 additions & 0 deletions src/datasources/config-api/config-api.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { HttpErrorFactory } from '@/datasources/errors/http-error-factory';
import { chainBuilder } from '@/domain/chains/entities/__tests__/chain.builder';
import { DataSourceError } from '@/domain/errors/data-source.error';
import { safeAppBuilder } from '@/domain/safe-apps/entities/__tests__/safe-app.builder';
import { ILoggingService } from '@/logging/logging.interface';
import { faker } from '@faker-js/faker';

const dataSource = {
Expand All @@ -25,6 +26,10 @@ const httpErrorFactory = {
} as jest.MockedObjectDeep<HttpErrorFactory>;
const mockHttpErrorFactory = jest.mocked(httpErrorFactory);

const mockLoggingService = {
info: jest.fn(),
} as jest.MockedObjectDeep<ILoggingService>;

describe('ConfigApi', () => {
const baseUri = faker.internet.url({ appendSlash: false });
const expirationTimeInSeconds = faker.number.int();
Expand All @@ -43,6 +48,7 @@ describe('ConfigApi', () => {
'expirationTimeInSeconds.notFound.default',
notFoundExpirationTimeInSeconds,
);
fakeConfigurationService.set('features.configHooksDebugLogs', false);
});

beforeEach(() => {
Expand All @@ -52,6 +58,7 @@ describe('ConfigApi', () => {
mockCacheService,
fakeConfigurationService,
mockHttpErrorFactory,
mockLoggingService,
);
});

Expand All @@ -65,6 +72,7 @@ describe('ConfigApi', () => {
mockCacheService,
fakeConfigurationService,
mockHttpErrorFactory,
mockLoggingService,
),
).toThrow();
});
Expand Down
13 changes: 12 additions & 1 deletion src/datasources/config-api/config-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Inject, Injectable } from '@nestjs/common';
import { IConfigurationService } from '@/config/configuration.service.interface';
import { CacheFirstDataSource } from '@/datasources/cache/cache.first.data.source';
import { CacheRouter } from '@/datasources/cache/cache.router';
Expand All @@ -11,19 +10,23 @@ import { Chain } from '@/domain/chains/entities/chain.entity';
import { Page } from '@/domain/entities/page.entity';
import { IConfigApi } from '@/domain/interfaces/config-api.interface';
import { SafeApp } from '@/domain/safe-apps/entities/safe-app.entity';
import { ILoggingService, LoggingService } from '@/logging/logging.interface';
import { Inject, Injectable } from '@nestjs/common';

@Injectable()
export class ConfigApi implements IConfigApi {
private readonly baseUri: string;
private readonly defaultExpirationTimeInSeconds: number;
private readonly defaultNotFoundExpirationTimeSeconds: number;
private readonly areConfigHooksDebugLogsEnabled: boolean;

constructor(
private readonly dataSource: CacheFirstDataSource,
@Inject(CacheService) private readonly cacheService: ICacheService,
@Inject(IConfigurationService)
private readonly configurationService: IConfigurationService,
private readonly httpErrorFactory: HttpErrorFactory,
@Inject(LoggingService) private readonly loggingService: ILoggingService,
) {
this.baseUri =
this.configurationService.getOrThrow<string>('safeConfig.baseUri');
Expand All @@ -35,6 +38,10 @@ export class ConfigApi implements IConfigApi {
this.configurationService.getOrThrow<number>(
'expirationTimeInSeconds.notFound.default',
);
this.areConfigHooksDebugLogsEnabled =
this.configurationService.getOrThrow<boolean>(
'features.configHooksDebugLogs',
);
}

async getChains(args: {
Expand Down Expand Up @@ -76,6 +83,10 @@ export class ConfigApi implements IConfigApi {
async clearChain(chainId: string): Promise<void> {
const chainCacheKey = CacheRouter.getChainCacheKey(chainId);
const chainsCacheKey = CacheRouter.getChainsCacheKey();
if (this.areConfigHooksDebugLogsEnabled) {
this.loggingService.info(`Clearing chain ${chainId}: ${chainCacheKey}`);
this.loggingService.info(`Clearing chains: ${chainsCacheKey}`);
}
await Promise.all([
this.cacheService.deleteByKey(chainCacheKey),
this.cacheService.deleteByKey(chainsCacheKey),
Expand Down
Loading