Skip to content

Commit 49fbd81

Browse files
authored
Test Prometheus fetch metrics when response caching (#1621)
1 parent c449385 commit 49fbd81

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

packages/plugins/prometheus/tests/prometheus.spec.ts

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import { createGatewayRuntime } from '@graphql-hive/gateway-runtime';
1+
import {
2+
createGatewayRuntime,
3+
useCustomFetch,
4+
} from '@graphql-hive/gateway-runtime';
5+
import InMemoryLRUCache from '@graphql-mesh/cache-inmemory-lru';
26
import { getUnifiedGraphGracefully } from '@graphql-mesh/fusion-composition';
37
import { createDefaultExecutor } from '@graphql-mesh/transport-common';
48
import { isDebug } from '@internal/testing';
5-
import { createSchema } from 'graphql-yoga';
9+
import { createSchema, createYoga } from 'graphql-yoga';
610
import { Registry, register as registry } from 'prom-client';
711
import { beforeEach, describe, expect, it } from 'vitest';
812
import usePrometheus, {
@@ -274,4 +278,67 @@ describe('Prometheus', () => {
274278
expect(metrics).toContain('subgraphName="TEST_SUBGRAPH"');
275279
expect(metrics).toContain('operationType="query"');
276280
});
281+
282+
it('should not increment fetch count on cached responses', async () => {
283+
const registry = new Registry();
284+
const subgraph = createYoga({ schema: subgraphSchema });
285+
await using cache = new InMemoryLRUCache();
286+
const gateway = createGatewayRuntime({
287+
supergraph: getUnifiedGraphGracefully([
288+
{
289+
name: 'TEST_SUBGRAPH',
290+
schema: subgraphSchema,
291+
url: 'http://subgraph/graphql',
292+
},
293+
]),
294+
cache,
295+
responseCaching: {
296+
session: () => null,
297+
},
298+
plugins: (ctx) => [
299+
// @ts-expect-error
300+
useCustomFetch(subgraph.fetch),
301+
usePrometheus({
302+
...ctx,
303+
registry,
304+
metrics: {
305+
graphql_gateway_subgraph_execute_duration: true,
306+
graphql_gateway_subgraph_execute_errors: true,
307+
graphql_gateway_fetch_duration: true,
308+
},
309+
}),
310+
],
311+
});
312+
313+
for (let i = 0; i < 3; i++) {
314+
const res = await gateway.fetch('http://gateway/graphql', {
315+
method: 'POST',
316+
headers: {
317+
'Content-Type': 'application/json',
318+
},
319+
body: JSON.stringify({
320+
query: /* GraphQL */ `
321+
{
322+
hello
323+
}
324+
`,
325+
}),
326+
});
327+
await expect(res.json()).resolves.toEqual({
328+
data: { hello: 'Hello world!' },
329+
});
330+
}
331+
332+
const metric = await registry
333+
.getSingleMetric('graphql_gateway_fetch_duration')
334+
?.get();
335+
336+
const count = metric?.values.find(
337+
(v) =>
338+
// @ts-expect-error metricName does exist
339+
v.metricName === 'graphql_gateway_fetch_duration_count',
340+
)?.value;
341+
342+
expect(count).toBe(1);
343+
});
277344
});

0 commit comments

Comments
 (0)