Skip to content

Commit

Permalink
Add request parsing and validation metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
AleF83 committed Mar 9, 2021
1 parent 09db5d8 commit 62a97a7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions services/src/modules/apollo-server-plugins/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ export function createMetricsPlugin(fastifyInstance: Pick<FastifyInstance, 'metr
});

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

requestValidationErrorCounter = new promClient.Counter({
name: 'graphql_request_validation_errors',
name: 'graphql_request_validation_errors_count',
help: 'request query validation errors',
});
},
Expand Down
4 changes: 4 additions & 0 deletions services/tests/e2e/basic/__snapshots__/metrics.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ graphql_resolver_duration_seconds_bucket{le="X",parentType: MetricsFoo,fieldName
graphql_resolver_duration_seconds_sum{parentType: MetricsFoo,fieldName: baz,status: success} Y
graphql_resolver_duration_seconds_count{parentType: MetricsFoo,fieldName: baz,status: success} Y
`;

exports[`Metrics Parsing errors metric 1`] = `[Error: Syntax Error: Unexpected Name "wrong".: {"response":{"errors":[{"message":"Syntax Error: Unexpected Name \\"wrong\\".","locations":[{"line":1,"column":1}],"extensions":{"code":"GRAPHQL_PARSE_FAILED"}}],"status":400},"request":{"query":"wrong query"}}]`;

exports[`Metrics Validation errors metric 1`] = `[Error: Cannot query field "wrong" on type "Query".: {"response":{"errors":[{"message":"Cannot query field \\"wrong\\" on type \\"Query\\".","locations":[{"line":2,"column":3}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"status":400},"request":{"query":"{\\n wrong\\n}\\n"}}]`;
22 changes: 17 additions & 5 deletions services/tests/e2e/basic/metrics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,28 @@ describe('Metrics', () => {
});

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

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'))
);
expect(metrics.some(m => m === 'graphql_request_parsing_errors_count 1')).toBeTruthy();
});

test('Validation errors metric', async () => {
const query = print(gql`
query {
wrong
}
`);
await expect(gatewayClient.request(query)).rejects.toMatchSnapshot();

const response = await fetch(gatewayMetricsEndpoint);
expect(response.ok).toBeTruthy();
const body = await response.text();
const metrics = body.split('\n');
expect(metrics.some(m => m === 'graphql_request_validation_errors_count 1')).toBeTruthy();
});

test('Registry metrics', async () => {
Expand Down

0 comments on commit 62a97a7

Please sign in to comment.