Skip to content

Instantly share code, notes, and snippets.

@nhusher
Last active May 28, 2019 15:13
Show Gist options
  • Save nhusher/28760bbeb22e49a357e8bcdf679027e9 to your computer and use it in GitHub Desktop.
Save nhusher/28760bbeb22e49a357e8bcdf679027e9 to your computer and use it in GitHub Desktop.
/**
The MIT License (MIT)
Copyright (c) 2019 Faraday Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
import { GraphQLExtension } from 'graphql-extensions'
import { GraphQLResolveInfo, ResponsePath } from 'graphql'
function generatePath (path: ResponsePath) {
if (!path.prev) return [path.key]
if (typeof path.key === 'number') {
// skip numeric keys
return generatePath(path.prev)
} else {
return generatePath(path.prev).concat(path.key)
}
}
export class InstrumentationApolloServerExtension extends GraphQLExtension<any> {
private instrumentation: {
[key: string]: {
path: string,
durations: number[],
arguments: any
}
} = {}
requestDidStart(o): ((...errors: Error[]) => void) {
const start = Date.now()
return (...errors: Error[]) => {
const end = Date.now()
const instrumentation = this.instrumentation
let args = {}
const timings = Object.values(instrumentation).map((i) => {
const analyzed = {
...i,
count: i.durations.length,
avgDuration: i.durations.reduce((a, b) => a + b) / i.durations.length,
minDuration: Math.min(...i.durations),
maxDuration: Math.max(...i.durations),
medianDuration: i.durations[Math.floor(i.durations.length / 2)],
}
delete analyzed.durations
args = i.arguments
delete analyzed.arguments
return analyzed
})
console.log(JSON.stringify({
operationName: o.operationName,
arguments: args,
timings,
start,
end,
}))
}
}
willResolveField(source: any, args: { [p: string]: any }, context: Context, info: GraphQLResolveInfo) {
const start = Date.now()
return () => {
const end = Date.now()
const path = generatePath(info.path)
const pathString = path.join('.')
const duration = end - start
if (!(pathString in this.instrumentation)) {
this.instrumentation[pathString] = {
path: path.join('.'),
arguments: info.variableValues,
durations: [duration]
}
} else {
// If it already exists: merge
const existing = this.instrumentation[pathString]
this.instrumentation[pathString] = {
...existing,
durations: existing.durations.concat(duration)
}
}
}
}
}
const apollo = new ApolloServer({
typeDefs,
resolvers,
// ... whatever else
extensions: [() => new InstrumentationApolloServerExtension()],
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment