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

[SERVICES-2155] context tracker #1296

Merged
merged 6 commits into from
Mar 5, 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
10 changes: 10 additions & 0 deletions src/helpers/api.config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ export class ApiConfigService {
return tracerFlag === 'true';
}

isDeephistoryActive(): boolean {
const deepHistoryFlag = this.configService.get<string>(
'ENABLE_DEEP_HISTORY',
);
if (!deepHistoryFlag) {
return false;
}
return deepHistoryFlag === 'true';
}

getRedisUrl(): string {
const redisUrl = this.configService.get<string>('REDIS_URL');
if (!redisUrl) {
Expand Down
9 changes: 8 additions & 1 deletion src/helpers/decorators/caching.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Inject } from '@nestjs/common';
import { CacheService } from '@multiversx/sdk-nestjs-cache';
import { generateCacheKeyFromParams } from 'src/utils/generate-cache-key';
import { ContextTracker } from '@multiversx/sdk-nestjs-common';

export interface ICachingOptions {
baseKey: string;
Expand All @@ -20,12 +21,18 @@ export function GetOrSetCache(cachingOptions: ICachingOptions) {
const originalMethod = descriptor.value;

descriptor.value = async function (...args: any[]) {
const cacheKey = generateCacheKeyFromParams(
const context = ContextTracker.get();

let cacheKey = generateCacheKeyFromParams(
cachingOptions.baseKey,
propertyKey,
...args,
);

if (context && context.deepHistoryTimestamp) {
cacheKey = `${cacheKey}.${context.deepHistoryTimestamp}`;
}

const cachingService: CacheService = this.cachingService;

return await cachingService.getOrSet(
Expand Down
26 changes: 26 additions & 0 deletions src/helpers/proxy.network.provider.profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ import {
import { PerformanceProfiler } from '../utils/performance.profiler';
import { MetricsCollector } from '../utils/metrics.collector';
import { IContractQuery } from '@multiversx/sdk-network-providers/out/interface';
import { ContextTracker } from '@multiversx/sdk-nestjs-common';
import { AxiosRequestConfig } from 'axios';
import { ApiConfigService } from './api.config.service';

export class ProxyNetworkProviderProfiler extends ProxyNetworkProvider {
constructor(
private readonly apiConfigService: ApiConfigService,
url: string,
config?: AxiosRequestConfig,
) {
super(url, config);
}

async queryContract(query: IContractQuery): Promise<ContractQueryResponse> {
const profiler = new PerformanceProfiler();

Expand All @@ -22,4 +33,19 @@ export class ProxyNetworkProviderProfiler extends ProxyNetworkProvider {

return result;
}

async doPostGeneric(resourceUrl: string, payload: any): Promise<any> {
const context = ContextTracker.get();
if (
this.apiConfigService.isDeephistoryActive() &&
context &&
context.deepHistoryTimestamp
) {
resourceUrl = resourceUrl.includes('?')
? `${resourceUrl}&timestamp=${context.deepHistoryTimestamp}`
: `${resourceUrl}?timestamp=${context.deepHistoryTimestamp}`;
}
const response = await super.doPostGeneric(resourceUrl, payload);
return response;
}
}
12 changes: 12 additions & 0 deletions src/services/multiversx-communication/mx.api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from 'src/utils/token.type.compare';
import { PendingExecutor } from 'src/utils/pending.executor';
import { MXProxyService } from './mx.proxy.service';
import { ContextTracker } from '@multiversx/sdk-nestjs-common';

type GenericGetArgs = {
methodName: string;
Expand Down Expand Up @@ -82,6 +83,17 @@ export class MXApiService {
): Promise<T> {
const profiler = new PerformanceProfiler(`${name} ${resourceUrl}`);
try {
const context = ContextTracker.get();
if (
this.apiConfigService.isDeephistoryActive() &&
context &&
context.deepHistoryTimestamp
) {
resourceUrl = resourceUrl.includes('?')
? `${resourceUrl}&timestamp=${context.deepHistoryTimestamp}`
: `${resourceUrl}?timestamp=${context.deepHistoryTimestamp}`;
}

const response = await this.getService().doGetGeneric(resourceUrl);
profiler.stop();
MetricsCollector.setApiCall(
Expand Down
1 change: 1 addition & 0 deletions src/services/multiversx-communication/mx.proxy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class MXProxyService {
const httpsAgent = new HttpsAgent(keepAliveOptions);

this.proxy = new ProxyNetworkProviderProfiler(
this.apiConfigService,
this.apiConfigService.getApiUrl(),
{
timeout: mxConfig.proxyTimeout,
Expand Down
6 changes: 6 additions & 0 deletions src/utils/logging.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { tap } from 'rxjs/operators';
import { CpuProfiler } from '@multiversx/sdk-nestjs-monitoring';
import { MetricsCollector } from './metrics.collector';
import { PerformanceProfiler } from './performance.profiler';
import { ContextTracker } from '@multiversx/sdk-nestjs-common';

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
Expand All @@ -23,8 +24,13 @@ export class LoggingInterceptor implements NestInterceptor {
const { req } = gqlContext.getContext();

let origin = 'Unknown';
let timestamp: number = undefined;
if (req !== undefined) {
origin = req?.headers?.['origin'] ?? 'Unknown';
timestamp = req?.headers?.['timestamp'];
ContextTracker.assign({
deepHistoryTimestamp: timestamp,
});
}

const profiler = new PerformanceProfiler();
Expand Down
Loading