feat(server): improve gql measure (#10706)
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
|||||||
GraphQLRequestListener,
|
GraphQLRequestListener,
|
||||||
} from '@apollo/server';
|
} from '@apollo/server';
|
||||||
import { Plugin } from '@nestjs/apollo';
|
import { Plugin } from '@nestjs/apollo';
|
||||||
|
import { Logger } from '@nestjs/common';
|
||||||
import { Response } from 'express';
|
import { Response } from 'express';
|
||||||
|
|
||||||
import { metrics } from '../metrics/metrics';
|
import { metrics } from '../metrics/metrics';
|
||||||
@@ -17,13 +18,31 @@ export interface RequestContext {
|
|||||||
|
|
||||||
@Plugin()
|
@Plugin()
|
||||||
export class GQLLoggerPlugin implements ApolloServerPlugin {
|
export class GQLLoggerPlugin implements ApolloServerPlugin {
|
||||||
|
private readonly logger = new Logger(GQLLoggerPlugin.name);
|
||||||
|
|
||||||
requestDidStart(
|
requestDidStart(
|
||||||
ctx: GraphQLRequestContext<RequestContext>
|
ctx: GraphQLRequestContext<RequestContext>
|
||||||
): Promise<GraphQLRequestListener<GraphQLRequestContext<RequestContext>>> {
|
): Promise<GraphQLRequestListener<GraphQLRequestContext<RequestContext>>> {
|
||||||
const res = ctx.contextValue.req.res as Response;
|
const res = ctx.contextValue.req.res as Response;
|
||||||
const operation = ctx.request.operationName;
|
const headers = ctx.request.http?.headers;
|
||||||
|
|
||||||
metrics.gql.counter('query_counter').add(1, { operation });
|
const info = {
|
||||||
|
operation: ctx.request.operationName,
|
||||||
|
clientVersion: headers?.get('x-affine-version'),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!info.operation) {
|
||||||
|
this.logger.warn(
|
||||||
|
`GraphQL operation name is not provided (${JSON.stringify({
|
||||||
|
userAgent: headers?.get('user-agent'),
|
||||||
|
clientVersion: info.clientVersion,
|
||||||
|
rayId: headers?.get('cf-ray'),
|
||||||
|
country: headers?.get('cf-ipcountry'),
|
||||||
|
})})`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
metrics.gql.counter('query_counter').add(1, info);
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
function endTimer() {
|
function endTimer() {
|
||||||
return Date.now() - start;
|
return Date.now() - start;
|
||||||
@@ -33,7 +52,7 @@ export class GQLLoggerPlugin implements ApolloServerPlugin {
|
|||||||
willSendResponse: () => {
|
willSendResponse: () => {
|
||||||
const time = endTimer();
|
const time = endTimer();
|
||||||
res.setHeader('Server-Timing', `gql;dur=${time};desc="GraphQL"`);
|
res.setHeader('Server-Timing', `gql;dur=${time};desc="GraphQL"`);
|
||||||
metrics.gql.histogram('query_duration').record(time, { operation });
|
metrics.gql.histogram('query_duration').record(time, info);
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
},
|
},
|
||||||
didEncounterErrors: ctx => {
|
didEncounterErrors: ctx => {
|
||||||
@@ -42,7 +61,7 @@ export class GQLLoggerPlugin implements ApolloServerPlugin {
|
|||||||
error.log('GraphQL');
|
error.log('GraphQL');
|
||||||
|
|
||||||
metrics.gql.counter('query_error_counter').add(1, {
|
metrics.gql.counter('query_error_counter').add(1, {
|
||||||
operation,
|
...info,
|
||||||
code: error.status,
|
code: error.status,
|
||||||
type: error.type,
|
type: error.type,
|
||||||
error: error.name,
|
error: error.name,
|
||||||
|
|||||||
Reference in New Issue
Block a user