Skip to content

Commit

Permalink
Metrics for parsing and validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AleF83 committed Mar 9, 2021
1 parent df9af3f commit 09db5d8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
30 changes: 29 additions & 1 deletion services/src/modules/apollo-server-plugins/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import {
ApolloServerPlugin,
GraphQLRequestContextWillSendResponse,
GraphQLFieldResolverParams,
GraphQLRequestListenerParsingDidEnd,
GraphQLRequestListenerValidationDidEnd,
} from 'apollo-server-plugin-base';
import { FastifyInstance } from 'fastify';
import { Histogram } from 'prom-client';
import { Histogram, Counter } from 'prom-client';
import { knownApolloDirectives } from '../config';

let requestDurationHistogram: Histogram<string> | undefined;
let resolverDurationHistogram: Histogram<string> | undefined;
let requestParsingErrorCounter: Counter<string> | undefined;
let requestValidationErrorCounter: Counter<string> | undefined;

export function createMetricsPlugin(fastifyInstance: Pick<FastifyInstance, 'metrics'>): ApolloServerPlugin {
return {
Expand All @@ -25,10 +29,34 @@ export function createMetricsPlugin(fastifyInstance: Pick<FastifyInstance, 'metr
help: 'resolver duration in seconds',
labelNames: ['parentType', 'fieldName', 'status'],
});

requestParsingErrorCounter = new promClient.Counter({
name: 'graphql_request_parsing_errors',
help: 'request query parsing errors',
});

requestValidationErrorCounter = new promClient.Counter({
name: 'graphql_request_validation_errors',
help: 'request query validation errors',
});
},
requestDidStart() {
const endResponseTimer = requestDurationHistogram?.startTimer();
return {
parsingDidStart(): GraphQLRequestListenerParsingDidEnd {
return (err?: Error) => {
if (err) {
requestParsingErrorCounter?.inc(1);
}
};
},
validationDidStart(): GraphQLRequestListenerValidationDidEnd {
return (err?: ReadonlyArray<Error>) => {
if (err && err?.length > 0) {
requestValidationErrorCounter?.inc(1);
}
};
},
willSendResponse(willSendResponseContext: GraphQLRequestContextWillSendResponse<unknown>) {
const {
response: { errors },
Expand Down
13 changes: 13 additions & 0 deletions services/tests/e2e/basic/metrics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ describe('Metrics', () => {
).toBeTruthy();
});

test('Parsing errors metric', async () => {
await gatewayClient.request('wrong query');

const response = await fetch(gatewayMetricsEndpoint);
expect(response.ok).toBeTruthy();
const body = await response.text();
const metrics = body.split('\n');
console.log(
'=====',
metrics.some(m => m.startsWith('graphql_request_parsing_errors'))
);
});

test('Registry metrics', async () => {
const response = await fetch(registryMetricsEndpoint);
expect(response.ok).toBeTruthy();
Expand Down

0 comments on commit 09db5d8

Please sign in to comment.