This will generate a custom span for each GraphQL, you can read more about custom span here https://www.elastic.co/guide/en/apm/agent/nodejs/current/custom-spans.html
Created
April 7, 2020 11:55
-
-
Save sibelius/bd1d3f2baa659e21db33bd4a614cdc2c to your computer and use it in GitHub Desktop.
trace resolver to generate span for each GraphQL resolver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { addMiddleware } from 'graphql-add-middleware'; | |
addMiddleware(schema, traceResolveMiddleware); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ResponsePath, responsePathAsArray, GraphQLType } from 'graphql'; | |
import apm from 'elastic-apm-node'; | |
export const traceResolveMiddleware = async (source, args, context, info, next) => { | |
const timespanKey = responsePathAsArray(info.path).join('.'); | |
const span = apm.startSpan(timespanKey); | |
// willResolveField | |
const result = await next(); | |
if (span) { | |
span.end(); | |
} | |
return result; | |
}; |
Author
sibelius
commented
Dec 10, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment