-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathgraphqlClient.ts
More file actions
270 lines (226 loc) · 8.53 KB
/
graphqlClient.ts
File metadata and controls
270 lines (226 loc) · 8.53 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import type { Client, IntegrationFn } from '@sentry/core';
import {
defineIntegration,
isString,
SEMANTIC_ATTRIBUTE_HTTP_REQUEST_METHOD,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_URL_FULL,
spanToJSON,
stringMatchesSomePattern,
} from '@sentry/core';
import type { FetchHint, XhrHint } from '@sentry-internal/browser-utils';
import { getBodyString, getFetchRequestArgBody, SENTRY_XHR_DATA_KEY } from '@sentry-internal/browser-utils';
interface GraphQLClientOptions {
endpoints: Array<string | RegExp>;
}
/** Standard graphql request shape: https://graphql.org/learn/serving-over-http/#post-request-and-body */
interface GraphQLStandardRequest {
query: string;
operationName?: string;
variables?: Record<string, unknown>;
extensions?: Record<string, unknown>;
}
/** Persisted operation request */
interface GraphQLPersistedRequest {
operationName: string;
variables?: Record<string, unknown>;
extensions: {
persistedQuery: {
version: number;
sha256Hash: string;
};
} & Record<string, unknown>;
}
type GraphQLRequestPayload = GraphQLStandardRequest | GraphQLPersistedRequest;
interface GraphQLOperation {
operationType?: string;
operationName?: string;
}
const INTEGRATION_NAME = 'GraphQLClient';
const _graphqlClientIntegration = ((options: GraphQLClientOptions) => {
return {
name: INTEGRATION_NAME,
setup(client: Client) {
_updateSpanWithGraphQLData(client, options);
_updateBreadcrumbWithGraphQLData(client, options);
},
};
}) satisfies IntegrationFn;
function _updateSpanWithGraphQLData(client: Client, options: GraphQLClientOptions): void {
client.on('beforeOutgoingRequestSpan', (span, hint) => {
const spanJSON = spanToJSON(span);
const spanAttributes = spanJSON.data || {};
const spanOp = spanAttributes[SEMANTIC_ATTRIBUTE_SENTRY_OP];
const isHttpClientSpan = spanOp === 'http.client';
if (!isHttpClientSpan) {
return;
}
// Fall back to `url` because fetch instrumentation only sets `http.url` for absolute URLs;
// relative URLs end up only in `url` (see `getFetchSpanAttributes` in packages/core/src/fetch.ts).
const httpUrl = spanAttributes[SEMANTIC_ATTRIBUTE_URL_FULL] || spanAttributes['http.url'] || spanAttributes['url'];
const httpMethod = spanAttributes[SEMANTIC_ATTRIBUTE_HTTP_REQUEST_METHOD] || spanAttributes['http.method'];
if (!isString(httpUrl) || !isString(httpMethod)) {
return;
}
const { endpoints } = options;
const isTracedGraphqlEndpoint = stringMatchesSomePattern(httpUrl, endpoints);
const payload = getRequestPayloadXhrOrFetch(hint as XhrHint | FetchHint);
if (isTracedGraphqlEndpoint && payload) {
const graphqlBody = getGraphQLRequestPayload(payload);
if (graphqlBody) {
const operationInfo = _getGraphQLOperation(graphqlBody);
span.updateName(`${httpMethod} ${httpUrl} (${operationInfo})`);
// Handle standard requests - always capture the query document
if (isStandardRequest(graphqlBody)) {
span.setAttribute('graphql.document', graphqlBody.query);
}
// Handle persisted operations - capture hash for debugging
if (isPersistedRequest(graphqlBody)) {
span.setAttribute('graphql.persisted_query.hash.sha256', graphqlBody.extensions.persistedQuery.sha256Hash);
span.setAttribute('graphql.persisted_query.version', graphqlBody.extensions.persistedQuery.version);
}
}
}
});
}
function _updateBreadcrumbWithGraphQLData(client: Client, options: GraphQLClientOptions): void {
client.on('beforeOutgoingRequestBreadcrumb', (breadcrumb, handlerData) => {
const { category, type, data } = breadcrumb;
const isFetch = category === 'fetch';
const isXhr = category === 'xhr';
const isHttpBreadcrumb = type === 'http';
if (isHttpBreadcrumb && (isFetch || isXhr)) {
const httpUrl = data?.url;
const { endpoints } = options;
const isTracedGraphqlEndpoint = stringMatchesSomePattern(httpUrl, endpoints);
const payload = getRequestPayloadXhrOrFetch(handlerData as XhrHint | FetchHint);
if (isTracedGraphqlEndpoint && data && payload) {
const graphqlBody = getGraphQLRequestPayload(payload);
if (!data.graphql && graphqlBody) {
const operationInfo = _getGraphQLOperation(graphqlBody);
data['graphql.operation'] = operationInfo;
if (isStandardRequest(graphqlBody)) {
data['graphql.document'] = graphqlBody.query;
}
if (isPersistedRequest(graphqlBody)) {
data['graphql.persisted_query.hash.sha256'] = graphqlBody.extensions.persistedQuery.sha256Hash;
data['graphql.persisted_query.version'] = graphqlBody.extensions.persistedQuery.version;
}
}
}
}
});
}
/**
* @param requestBody - GraphQL request
* @returns A formatted version of the request: 'TYPE NAME' or 'TYPE' or 'persisted NAME'
*/
export function _getGraphQLOperation(requestBody: GraphQLRequestPayload): string {
// Handle persisted operations
if (isPersistedRequest(requestBody)) {
return `persisted ${requestBody.operationName}`;
}
// Handle standard GraphQL requests
if (isStandardRequest(requestBody)) {
const { query: graphqlQuery, operationName: graphqlOperationName } = requestBody;
const { operationName = graphqlOperationName, operationType } = parseGraphQLQuery(graphqlQuery);
const operationInfo = operationName ? `${operationType} ${operationName}` : `${operationType}`;
return operationInfo;
}
// Fallback for unknown request types
return 'unknown';
}
/**
* Get the request body/payload based on the shape of the hint.
*
* Exported for tests only.
*/
export function getRequestPayloadXhrOrFetch(hint: XhrHint | FetchHint): string | undefined {
const isXhr = 'xhr' in hint;
let body: string | undefined;
if (isXhr) {
const sentryXhrData = hint.xhr[SENTRY_XHR_DATA_KEY];
body = sentryXhrData && getBodyString(sentryXhrData.body)[0];
} else {
const sentryFetchData = getFetchRequestArgBody(hint.input);
body = getBodyString(sentryFetchData)[0];
}
return body;
}
/**
* Extract the name and type of the operation from the GraphQL query.
*
* Exported for tests only.
*/
export function parseGraphQLQuery(query: string): GraphQLOperation {
const namedQueryRe = /^(?:\s*)(query|mutation|subscription)(?:\s*)(\w+)(?:\s*)[{(]/;
const unnamedQueryRe = /^(?:\s*)(query|mutation|subscription)(?:\s*)[{(]/;
const namedMatch = query.match(namedQueryRe);
if (namedMatch) {
return {
operationType: namedMatch[1],
operationName: namedMatch[2],
};
}
const unnamedMatch = query.match(unnamedQueryRe);
if (unnamedMatch) {
return {
operationType: unnamedMatch[1],
operationName: undefined,
};
}
return {
operationType: undefined,
operationName: undefined,
};
}
/**
* Helper to safely check if a value is a non-null object
*/
function isObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}
/**
* Type guard to check if a request is a standard GraphQL request
*/
function isStandardRequest(payload: unknown): payload is GraphQLStandardRequest {
return isObject(payload) && typeof payload.query === 'string';
}
/**
* Type guard to check if a request is a persisted operation request
*/
function isPersistedRequest(payload: unknown): payload is GraphQLPersistedRequest {
return (
isObject(payload) &&
typeof payload.operationName === 'string' &&
isObject(payload.extensions) &&
isObject(payload.extensions.persistedQuery) &&
typeof payload.extensions.persistedQuery.sha256Hash === 'string' &&
typeof payload.extensions.persistedQuery.version === 'number'
);
}
/**
* Extract the payload of a request if it's GraphQL.
* Exported for tests only.
* @param payload - A valid JSON string
* @returns A POJO or undefined
*/
export function getGraphQLRequestPayload(payload: string): GraphQLRequestPayload | undefined {
try {
const requestBody = JSON.parse(payload);
// Return any valid GraphQL request (standard, persisted, or APQ retry with both)
if (isStandardRequest(requestBody) || isPersistedRequest(requestBody)) {
return requestBody;
}
// Not a GraphQL request
return undefined;
} catch {
// Invalid JSON
return undefined;
}
}
/**
* This integration ensures that GraphQL requests made in the browser
* have their GraphQL-specific data captured and attached to spans and breadcrumbs.
*/
export const graphqlClientIntegration = defineIntegration(_graphqlClientIntegration);