-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgraphql.ts
More file actions
109 lines (96 loc) · 3.77 KB
/
graphql.ts
File metadata and controls
109 lines (96 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import client from 'prom-client';
import { ApolloServerPlugin, GraphQLRequestContext, GraphQLRequestListener } from 'apollo-server-plugin-base';
import { GraphQLError } from 'graphql';
import HawkCatcher from '@hawk.so/nodejs';
/**
* GraphQL operation duration histogram
* Tracks GraphQL operation duration by operation name and type
*/
export const gqlOperationDuration = new client.Histogram({
name: 'hawk_gql_operation_duration_seconds',
help: 'Histogram of total GraphQL operation duration by operation name and type',
labelNames: ['operation_name', 'operation_type'],
buckets: [0.01, 0.05, 0.1, 0.5, 1, 5, 10],
});
/**
* GraphQL operation errors counter
* Tracks failed GraphQL operations grouped by operation name and error class
*/
export const gqlOperationErrors = new client.Counter({
name: 'hawk_gql_operation_errors_total',
help: 'Counter of failed GraphQL operations grouped by operation name and error class',
labelNames: ['operation_name', 'error_type'],
});
/**
* GraphQL resolver duration histogram
* Tracks resolver execution time per type, field, and operation
*/
export const gqlResolverDuration = new client.Histogram({
name: 'hawk_gql_resolver_duration_seconds',
help: 'Histogram of resolver execution time per type, field, and operation',
labelNames: ['type_name', 'field_name', 'operation_name'],
buckets: [0.01, 0.05, 0.1, 0.5, 1, 5],
});
/**
* Apollo Server plugin to track GraphQL metrics
*/
export const graphqlMetricsPlugin: ApolloServerPlugin = {
async requestDidStart(_requestContext: GraphQLRequestContext): Promise<GraphQLRequestListener> {
const startTime = Date.now();
let operationName = 'unknown';
let operationType = 'unknown';
return {
async didResolveOperation(ctx: GraphQLRequestContext): Promise<void> {
operationName = ctx.operationName || 'anonymous';
operationType = ctx.operation?.operation || 'unknown';
},
async executionDidStart(): Promise<GraphQLRequestListener> {
return {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
willResolveField({ info }: any): () => void {
const fieldStartTime = Date.now();
return (): void => {
const duration = (Date.now() - fieldStartTime) / 1000;
gqlResolverDuration
.labels(
info.parentType.name,
info.fieldName,
operationName
)
.observe(duration);
};
},
};
},
async willSendResponse(ctx: GraphQLRequestContext): Promise<void> {
const durationMs = Date.now() - startTime;
const duration = durationMs / 1000;
gqlOperationDuration
.labels(operationName, operationType)
.observe(duration);
const hasErrors = ctx.errors && ctx.errors.length > 0;
HawkCatcher.breadcrumbs.add({
type: 'request',
category: 'GraphQL Operation',
message: `${operationType} ${operationName} ${durationMs}ms${hasErrors ? ` [${ctx.errors!.length} error(s)]` : ''}`,
level: hasErrors ? 'error' : 'debug',
data: {
operationName: { value: operationName },
operationType: { value: operationType },
durationMs: { value: durationMs },
...(hasErrors && { errors: { value: ctx.errors!.map((e: GraphQLError) => e.message).join('; ') } }),
},
});
// Track errors if any
if (hasErrors) {
ctx.errors!.forEach((error: GraphQLError) => {
const errorType = error.extensions?.code || error.name || 'unknown';
gqlOperationErrors
.labels(operationName, errorType as string)
.inc();
});
}
},
};
},
};