diff --git a/services/src/modules/apollo-server-plugins/metrics.ts b/services/src/modules/apollo-server-plugins/metrics.ts index 12960aae..a43b6e7e 100644 --- a/services/src/modules/apollo-server-plugins/metrics.ts +++ b/services/src/modules/apollo-server-plugins/metrics.ts @@ -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 | undefined; let resolverDurationHistogram: Histogram | undefined; +let requestParsingErrorCounter: Counter | undefined; +let requestValidationErrorCounter: Counter | undefined; export function createMetricsPlugin(fastifyInstance: Pick): ApolloServerPlugin { return { @@ -25,10 +29,34 @@ export function createMetricsPlugin(fastifyInstance: Pick { + if (err) { + requestParsingErrorCounter?.inc(1); + } + }; + }, + validationDidStart(): GraphQLRequestListenerValidationDidEnd { + return (err?: ReadonlyArray) => { + if (err && err?.length > 0) { + requestValidationErrorCounter?.inc(1); + } + }; + }, willSendResponse(willSendResponseContext: GraphQLRequestContextWillSendResponse) { const { response: { errors }, diff --git a/services/tests/e2e/basic/metrics.spec.ts b/services/tests/e2e/basic/metrics.spec.ts index bde705aa..56cf8bc4 100644 --- a/services/tests/e2e/basic/metrics.spec.ts +++ b/services/tests/e2e/basic/metrics.spec.ts @@ -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();