Skip to content

Commit

Permalink
Configurable request and resolver duration histogram metrics (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
AleF83 authored Jun 20, 2021
1 parent a7eef97 commit 6ea33e3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
10 changes: 7 additions & 3 deletions services/src/modules/apollo-server-plugins/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
GraphQLRequestListenerValidationDidEnd,
} from 'apollo-server-plugin-base';
import * as promClient from 'prom-client';
import { knownApolloDirectives } from '../config';
import {
knownApolloDirectives,
requestDurationPromHistogramBuckets,
resolverDurationPromHistogramBuckets,
} from '../config';

let requestDurationHistogram: promClient.Histogram<string> | undefined;
let resolverDurationHistogram: promClient.Histogram<string> | undefined;
Expand All @@ -18,14 +22,14 @@ export function initializeMetrics(pClient: typeof promClient) {
name: 'graphql_request_duration_seconds',
help: 'request duration in seconds',
labelNames: ['status', 'operationName'],
buckets: [0.02, 0.1, 0.5, 2, 10],
buckets: requestDurationPromHistogramBuckets,
});

resolverDurationHistogram = new pClient.Histogram({
name: 'graphql_resolver_duration_seconds',
help: 'resolver duration in seconds',
labelNames: ['parentType', 'fieldName', 'status'],
buckets: [0.02, 0.1, 0.5, 2, 10],
buckets: resolverDurationPromHistogramBuckets,
});

requestParsingErrorCounter = new pClient.Counter({
Expand Down
10 changes: 10 additions & 0 deletions services/src/modules/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,13 @@ export const knownApolloDirectives = envVarExt
.asSet();

export const corsConfiguration = envVar.get('CORS_CONFIGURATION').default({}).asJsonObject() as CorsConfiguration;

export const requestDurationPromHistogramBuckets: number[] = envVarExt
.get('REQUEST_DURATION_PROM_HISTOGRAM_BUCKETS')
.default('[0.02, 0.1, 0.5, 2, 10]')
.asJsonArray();

export const resolverDurationPromHistogramBuckets: number[] = envVarExt
.get('RESOLVER_DURATION_PROM_HISTOGRAM_BUCKETS')
.default('[0.02, 0.1, 0.5, 2, 10]')
.asJsonArray();
3 changes: 0 additions & 3 deletions services/tests/e2e/basic/__snapshots__/metrics.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Array [
"0.1",
"0.5",
"2",
"10",
"+Inf",
]
`;
Expand All @@ -25,8 +24,6 @@ Array [
"0.02",
"0.1",
"0.5",
"2",
"10",
"+Inf",
]
`;
Expand Down
2 changes: 2 additions & 0 deletions services/tests/e2e/basic/metrics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ describe('Metrics', () => {
);
const bucketRegex = /le="(.+)",parentType=/;
const buckets = bucketMetrics.map(m => m.match(bucketRegex)?.[1]);
expect(buckets).toHaveLength(4);
expect(buckets).toMatchSnapshot();
});

Expand All @@ -117,6 +118,7 @@ describe('Metrics', () => {
);
const bucketRegex = /le="(.+)",operationName=/;
const buckets = bucketMetrics.map(m => m.match(bucketRegex)?.[1]);
expect(buckets).toHaveLength(5);
expect(buckets).toMatchSnapshot();
});

Expand Down
4 changes: 4 additions & 0 deletions services/tests/e2e/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ services:
"publicPaths": ["/metrics", "/.well-known/apollo/server-health", "/graphql"]
}
}
REQUEST_DURATION_PROM_HISTOGRAM_BUCKETS: >
[0.02, 0.1, 0.5, 2]
RESOLVER_DURATION_PROM_HISTOGRAM_BUCKETS: >
[0.02, 0.1, 0.5]
volumes:
- ./config/plugins:/plugins:ro
- ./config/scripts:/scripts:ro
Expand Down

0 comments on commit 6ea33e3

Please sign in to comment.