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

[MEX-623] filtered pairs performance #1542

Draft
wants to merge 6 commits into
base: development
Choose a base branch
from
Draft
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
28 changes: 14 additions & 14 deletions src/modules/auth/gql.auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ export class GqlAuthGuard implements CanActivate {
const ctx = GqlExecutionContext.create(context);
const { req } = ctx.getContext();
const headers = req.headers;
this.logger.info('request header', [
{
requestIP: req.ip,
headers: {
host: headers['host'],
'x-request-id': headers['x-request-id'],
'x-real-ip': headers['x-real-ip'],
'x-forwarded-for': headers['x-forwarded-for'],
'x-forwarded-host': headers['x-forwarded-host'],
'x-forwarded-port': headers['x-forwarded-port'],
'user-agent': headers['user-agent'],
},
},
]);
// this.logger.info('request header', [
// {
// requestIP: req.ip,
// headers: {
// host: headers['host'],
// 'x-request-id': headers['x-request-id'],
// 'x-real-ip': headers['x-real-ip'],
// 'x-forwarded-for': headers['x-forwarded-for'],
// 'x-forwarded-host': headers['x-forwarded-host'],
// 'x-forwarded-port': headers['x-forwarded-port'],
// 'user-agent': headers['user-agent'],
// },
// },
// ]);

if (headers !== undefined) {
this.impersonateAddress = headers['impersonate-address'];
Expand Down
16 changes: 8 additions & 8 deletions src/modules/auto-router/services/auto-router.compute.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ export class AutoRouterComputeService {

const addressRoute = this.getAddressRoute(pairs, paths[pathIndex]);

this.logger.info(`Swap Type ${swapType}`, {
paths,
amounts,
bestPath: paths[pathIndex],
intermediaryAmounts: amounts[pathIndex],
addressRoute,
bestAmount,
});
// this.logger.info(`Swap Type ${swapType}`, {
// paths,
// amounts,
// bestPath: paths[pathIndex],
// intermediaryAmounts: amounts[pathIndex],
// addressRoute,
// bestAmount,
// });

return {
tokenRoute: paths[pathIndex],
Expand Down
10 changes: 10 additions & 0 deletions src/modules/pair/services/pair.compute.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ export class PairComputeService implements IPairComputeService {
this.pairService.getSecondToken(pairAddress),
]);

if (
firstToken === undefined ||
secondToken === undefined ||
firstToken.decimals === undefined ||
secondToken.decimals === undefined
) {
console.log(`First Token: ${firstToken}`);
console.log(`Second Token: ${secondToken}`);
}

const firstTokenPrice =
await this.pairService.getEquivalentForLiquidity(
pairAddress,
Expand Down
4 changes: 4 additions & 0 deletions src/services/multiversx-communication/mx.gateway.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ export class MXGatewayService {
name,
profiler.duration,
);

this.logger.info(`${resourceUrl} took ${profiler.duration}ms`, {
context: MXGatewayService.name,
});
}
}

Expand Down
75 changes: 67 additions & 8 deletions src/utils/query.metrics.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,35 @@ import { Plugin } from '@nestjs/apollo';
import { PerformanceProfiler } from './performance.profiler';
import { CpuProfiler } from '@multiversx/sdk-nestjs-monitoring';
import { MetricsCollector } from './metrics.collector';
import { Kind, OperationTypeNode } from 'graphql';
import { FieldNode, Kind, OperationTypeNode, SelectionNode } from 'graphql';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Inject } from '@nestjs/common';
import { Logger } from 'winston';

type QueryField = {
name: string;
subfields?: QueryField[];
};

@Plugin()
export class QueryMetricsPlugin implements ApolloServerPlugin {
constructor(
@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
) {}

async requestDidStart(): Promise<GraphQLRequestListener<any>> {
const logger = this.logger;
let profiler: PerformanceProfiler;
let cpuProfiler: CpuProfiler;
let operationName: string;
let origin: string;
let queryFields: string;

return {
async executionDidStart(
requestContext: GraphQLRequestContext<any>,
): Promise<void | GraphQLRequestExecutionListener<any>> {
operationName = deanonymizeQuery(requestContext);
[operationName, queryFields] = deanonymizeQuery(requestContext);
origin =
requestContext.request.http?.headers.get('origin') ??
'Unknown';
Expand All @@ -35,6 +49,12 @@ export class QueryMetricsPlugin implements ApolloServerPlugin {
profiler.stop(operationName);
const cpuTime = cpuProfiler.stop();

if (operationName === 'filteredPairs') {
logger.info(queryFields, {
context: QueryMetricsPlugin.name,
});
}

MetricsCollector.setQueryDuration(
operationName,
origin,
Expand All @@ -47,15 +67,19 @@ export class QueryMetricsPlugin implements ApolloServerPlugin {
}
}

function deanonymizeQuery(requestContext: GraphQLRequestContext<any>): string {
function deanonymizeQuery(
requestContext: GraphQLRequestContext<any>,
): [string, string] {
if (requestContext.operationName) {
return requestContext.operationName;
return [requestContext.operationName, ''];
}

if (!requestContext.document) {
return requestContext.queryHash;
return [requestContext.queryHash, ''];
}

let debugFields: string;

const queryNames = [];
const definitions = requestContext.document.definitions;
for (const definition of definitions) {
Expand All @@ -74,11 +98,46 @@ function deanonymizeQuery(requestContext: GraphQLRequestContext<any>): string {

const name = selection.name?.value ?? 'undefined';

if (name === 'filteredPairs') {
const fields = extractQueryFields(
selection.selectionSet?.selections || [],
);
debugFields = JSON.stringify(fields);
}

queryNames.push(name);
}
}

return queryNames.length > 0
? queryNames.join('|')
: requestContext.queryHash;
const deanonymizedQuery =
queryNames.length > 0 ? queryNames.join('|') : requestContext.queryHash;

return [deanonymizedQuery, debugFields];
}

function extractQueryFields(
selectionNodes: readonly SelectionNode[],
): QueryField[] {
const fields: QueryField[] = [];

selectionNodes
.filter((node): node is FieldNode => node.kind === Kind.FIELD)
.forEach((node) => {
if (node.selectionSet) {
const subfields = extractQueryFields(
node.selectionSet.selections,
);

fields.push({
name: node.name.value,
subfields: subfields,
});
} else {
fields.push({
name: node.name.value,
});
}
});

return fields;
}
Loading