-
Notifications
You must be signed in to change notification settings - Fork 28
/
create-plugin.js
410 lines (327 loc) · 12.8 KB
/
create-plugin.js
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
const ErrorHelper = require('./error-helper.js')
const errorHelper = new ErrorHelper()
const cleanQuery = require('./query-utils')
const NOTICED_ERRORS = ErrorHelper.NOTICED_ERRORS
const ANON_PLACEHOLDER = '<anonymous>'
const UNKNOWN_OPERATION = '<unknown>'
const CATEGORY = 'GraphQL'
const FRAMEWORK = 'ApolloServer'
const OPERATION_PREFIX = CATEGORY + '/operation/' + FRAMEWORK
const RESOLVE_PREFIX = CATEGORY + '/resolve/' + FRAMEWORK
const BATCH_PREFIX = 'batch'
const FIELD_NAME_ATTR = 'graphql.field.name'
const RETURN_TYPE_ATTR = 'graphql.field.returnType'
const PARENT_TYPE_ATTR = 'graphql.field.parentType'
const FIELD_PATH_ATTR = 'graphql.field.path'
const FIELD_ARGS_ATTR = 'graphql.field.args'
const OPERATION_TYPE_ATTR = 'graphql.operation.type'
const OPERATION_NAME_ATTR = 'graphql.operation.name'
const OPERATION_PATH_ATTR = 'graphql.operation.deepestPath'
const OPERATION_QUERY_ATTR = 'graphql.operation.query'
const DESTINATIONS = {
NONE: 0x00
}
/**
* @typedef {object} PluginConfig
* @property {boolean} [captureScalars=false]
* Enable capture of timing of fields resolved with the `GraphQLScalarType` return type.
* This may be desired when performing time intensive calculations to return a scalar
* value.This is not recommended for queries that return a large number of pre-calculated
* scalar fields.
* NOTE: query/mutation resolvers will always be captured even if returning a scalar type.
*/
/**
* Creates an Apollo Server plugin for capturing timing data
* via the New Relic Node.js agent.
* @param {*} instrumentationApi New Relic instrumentation API
* @param {PluginConfig} [config]
*/
function createPlugin(instrumentationApi, config = {}) {
if (!instrumentationApi) {
return {}
}
const logger = instrumentationApi.logger.child({component: 'ApolloServerPlugin'})
logger.info('Apollo Server plugin created.')
config.captureScalars = config.captureScalars || false
logger.debug('Plugin configuration: ', config)
createModuleUsageMetric(instrumentationApi.agent)
return {
requestDidStart(requestContext) {
logger.trace('Begin requestDidStart')
const requestParent = instrumentationApi.getActiveSegment()
if (!requestParent) {
logger.trace('No active segment found at query start. Not recording.')
return null
}
// We do not set to active here as batched queries will hit this
// back to back and we'd prefer those not nest with each-other.
const operationSegment = instrumentationApi.createSegment(
UNKNOWN_OPERATION,
recordOperationSegment,
requestParent
)
if (!operationSegment) {
logger.trace('Operation segment was not created. Not recording.')
return null
}
operationSegment.start()
const deepestResolvedPath = {
depth: 0,
formatted: null
}
return {
didEncounterErrors(errorsRequestContext) {
// Since we don't set the operation segment as active, we want to apply the
// operation segment as active while setting the error to appropriately assign
// error attributes for any errors we've not noticed on field resolve.
instrumentationApi.applySegment(function addErrors() {
errorHelper.addErrorsFromApolloRequestContext(
instrumentationApi,
errorsRequestContext
)
}, operationSegment)
},
executionDidStart() {
return {
willResolveField({args, info}) {
const pathArray = flattenToArray(info.path)
const formattedPath = pathArray.reverse().join('.')
if (pathArray.length > deepestResolvedPath.depth) {
deepestResolvedPath.depth = pathArray.length
deepestResolvedPath.formatted = formattedPath
}
if (!config.captureScalars && !isTopLevelField(info) && isScalar(info)) {
return null
}
const currentSeg = instrumentationApi.getActiveSegment()
// Nest everything under operation as resolvers start/finish
// in order but instrumentation such as promise tracking might
// try to treat as nested.
const resolverSegment = instrumentationApi.createSegment(
`${RESOLVE_PREFIX}/${info.fieldName}`,
recordResolveSegment,
operationSegment
)
if (!resolverSegment) {
logger.trace(
'Resolver segment was not created (%s).',
formattedPath
)
return null
}
resolverSegment.start()
instrumentationApi.setActiveSegment(resolverSegment)
resolverSegment.name = `${RESOLVE_PREFIX}/${formattedPath}`
resolverSegment.addAttribute(FIELD_PATH_ATTR, formattedPath)
resolverSegment.addAttribute(FIELD_NAME_ATTR, info.fieldName)
resolverSegment.addAttribute(RETURN_TYPE_ATTR, info.returnType.toString())
resolverSegment.addAttribute(PARENT_TYPE_ATTR, info.parentType.toString())
for (const [key, value] of Object.entries(args)) {
// Require adding to attribute 'include' configuration
// so as not to accidentally send senstive info to New Relic.
resolverSegment.attributes.addAttribute(
DESTINATIONS.NONE,
`${FIELD_ARGS_ATTR}.${key}`,
value
)
}
return (error) => {
if (error) {
// This handler is invoked prior to didEncounterErrors
// which means we need to handle the error now to capture
// in context of the appropriate span.
errorHelper.noticeError(instrumentationApi, error)
requestContext[NOTICED_ERRORS] = requestContext[NOTICED_ERRORS] || []
requestContext[NOTICED_ERRORS].push(error)
}
resolverSegment.end()
instrumentationApi.setActiveSegment(currentSeg)
}
}
}
},
willSendResponse(responseContext) {
// default. only used if failed to parse
let transactionName = '*'
let segmentName = UNKNOWN_OPERATION
let query = responseContext.request.query
if (query) {
// attempt to extract arguments to strip from query
query = cleanQuery(query)
operationSegment.addAttribute(
OPERATION_QUERY_ATTR,
query)
}
const operationDetails
= getOperationDetails(responseContext, deepestResolvedPath.formatted)
if (operationDetails) {
const {operationName, operationType, deepestPath} = operationDetails
operationSegment.addAttribute(OPERATION_TYPE_ATTR, operationType)
if (operationName) {
operationSegment.addAttribute(OPERATION_NAME_ATTR, operationName)
}
const formattedName = operationName || ANON_PLACEHOLDER
let formattedOperation = `${operationType}/${formattedName}`
// Certain requests, such as introspection, won't hit any resolvers
if (deepestPath) {
operationSegment.addAttribute(OPERATION_PATH_ATTR, deepestPath)
formattedOperation += `/${deepestPath}`
}
segmentName = formattedOperation
transactionName = formattedOperation
}
setTransactionName(operationSegment.transaction, transactionName)
operationSegment.name = `${OPERATION_PREFIX}/${segmentName}`
operationSegment.end()
logger.trace('End willSendResponse')
}
}
}
}
}
function getOperationDetails(responseContext, deepestPath) {
if (responseContext.operation) {
// Resolved operation: parsed and validated, will execute resolvers
return {
operationName: responseContext.operationName,
operationType: responseContext.operation.operation,
deepestPath: deepestPath
}
} else if (responseContext.document) {
// Failed validation, fallback to document AST
return getDetailsFromDocument(responseContext.document)
}
return null
}
function isScalar(fieldInfo) {
const result =
isScalarType(fieldInfo.returnType) || isNonNullScalarType(fieldInfo.returnType)
return result
}
function isScalarType(typeInstance) {
const typeName = typeInstance.constructor.name
const result = typeName === 'GraphQLScalarType'
return result
}
function isNonNullScalarType(returnType) {
const returnTypeName = returnType.constructor.name
if (returnTypeName !== 'GraphQLNonNull' || !returnType.ofType) {
return false
}
const nestedType = returnType.ofType
const result = isScalarType(nestedType)
return result
}
function isTopLevelField(fieldInfo) {
const parentName = fieldInfo.parentType.name
const result = parentName === 'Query' || parentName === 'Mutation'
return result
}
function getDetailsFromDocument(document) {
const definition = document.definitions[0]
const deepestSelectionPath = getDeepestSelectionPath(definition)
const definitionName = definition.name && definition.name.value
return {
operationType: definition.operation,
operationName: definitionName,
deepestPath: deepestSelectionPath.join('.')
}
}
function flattenToArray(fieldPath) {
const pathArray = []
let thisPath = fieldPath
while (thisPath) {
if (typeof thisPath.key !== 'number') {
pathArray.push(thisPath.key)
}
thisPath = thisPath.prev
}
return pathArray
}
function setTransactionName(transaction, transactionName) {
const nameState = transaction.nameState
if (!nameState.graphql) {
// Override previously set path stack set thus far by web framework.
nameState.setName(
nameState.prefix,
nameState.verb,
nameState.delimiter,
transactionName
)
// Indicate we've set a name via graphql and future attempts to name
// are a part of a batch query request to apollo.
nameState.graphql = true
} else {
// If this is a batch query, add 'batch' indicator to the first part of the
// name unless we've already done so processing a prior query in the batch.
const firstPart = nameState.pathStack[0]
if (firstPart.path !== BATCH_PREFIX) {
nameState.pathStack.unshift({path: BATCH_PREFIX, params: null})
}
nameState.appendPath(transactionName)
}
}
/**
* Returns the deepest path as an array of parts
* from an apollo-server document definition.
*/
function getDeepestSelectionPath(definition) {
let deepestPath = null
definition.selectionSet.selections.forEach((selection) => {
searchSelection(selection)
})
return deepestPath
/**
* Search each selection path until no-more sub-selections
* exist. If the curent path is deeper than deepestPath,
* deepestPath is replaced.
*/
function searchSelection(selection, currentParts) {
const parts = currentParts ? [...currentParts] : []
parts.push(selection.name.value)
if (selection.selectionSet) {
selection.selectionSet.selections.forEach((innerSelection) => {
searchSelection(innerSelection, parts)
})
} else if (!deepestPath || parts.length > deepestPath.length) {
deepestPath = parts
}
}
}
function recordResolveSegment(segment, scope) {
const duration = segment.getDurationInMillis()
const exclusive = segment.getExclusiveDurationInMillis()
const transaction = segment.transaction
const attributes = segment.getAttributes()
const fieldName = attributes[FIELD_NAME_ATTR]
// The segment name uses the path to differentiate between duplicate
// names resolving across different types. Here we use the field name
// without the path to compare resolver across usage and transactions.
if (fieldName) {
const fieldNameMetric = `${RESOLVE_PREFIX}/${fieldName}`
createMetricPairs(transaction, fieldNameMetric, scope, duration, exclusive)
}
}
function recordOperationSegment(segment, scope) {
const duration = segment.getDurationInMillis()
const exclusive = segment.getExclusiveDurationInMillis()
const transaction = segment.transaction
createMetricPairs(transaction, segment.name, scope, duration, exclusive)
}
function createMetricPairs(transaction, name, scope, duration, exclusive) {
if (scope) {
transaction.measure(name, scope, duration, exclusive)
}
transaction.measure(name, null, duration, exclusive)
}
function createModuleUsageMetric(agent) {
agent.metrics.getOrCreateMetric(
'Supportability/ExternalModules/ApolloServerPlugin'
).incrementCallCount()
}
module.exports = createPlugin