-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Apollo performance tracing not working with nestjs graphql #5808
Comments
Hey, thanks for writing in! This is super strange - |
Hi @AbhiPrasad, I've upgrade both to the newest version but that didn't helped. Do you have another idea?
|
@kaufmo, could you create a reproduction for us to debug this further? |
@onurtemizkan yes for sure i can create a simple repo for that, but we are using a selfhosted sentry instance which i cannot use here. Is it ok when you have to set dsn manually? |
That's totally fine @kaufmo, thanks. |
@onurtemizkan I've created the repo: https://github.com/kaufmo/nestjs-sentry-apollo-example. When you start the api with
|
Thanks for the reproduction @kaufmo! I was able to reproduce and debug the issue. Apollo integration only supports It seems there are two other ways to create an
So, I'm also opening another issue about adding support for |
Let's leave this issue open until we fix the NestJS specific problem, thanks for opening a PR about bug fix though @onurtemizkan! |
@onurtemizkan thank you very much that sounds good. Do you now when the other issue is fixed? It would be really nice when I could use this :) |
I have a custom implementation of tracing with NestJS Apollo or Mercurius + mongoose here, if anyone wants to use it for inspiration: https://gist.github.com/andreialecu/40cb13c01b0d88c8163bbec3de59c580 Works great, and properly traces async boundaries using |
Just to chime in here – I too am using NestJS with a code-first GraphQL schema, and I was skeptical about whether my resolvers were actually getting picked up (Sentry only ever logged GraphQLTransactions, no spans), so I printed out the {
Upload: GraphQLScalarType {
name: 'Upload',
description: 'The `Upload` scalar type represents a file upload.',
specifiedByURL: undefined,
serialize: [Function: serialize],
parseValue: [Function: parseValue],
parseLiteral: [Function: parseLiteral],
extensions: [Object: null prototype] {},
astNode: undefined,
extensionASTNodes: []
}
} I placed by Sentry init at the top of my main.ts' |
If anyone stumbled into this issue and you're using code first, try the plugin mentioned by @Nickersoft And also, here's an updated version of the article mentioned: import { ApolloServerPlugin } from "@apollo/server";
import { Transaction, startTransaction } from "@sentry/node";
// Move this to another file if desirable
const plugin: ApolloServerPlugin<{ transaction: Transaction }> = {
async requestDidStart(requestContext) {
if (requestContext.request.operationName) {
requestContext.contextValue.transaction.setName(
requestContext.request.operationName,
);
}
return {
async willSendResponse(requestContext) {
requestContext.contextValue.transaction.finish();
},
async executionDidStart() {
return {
willResolveField(requestContext) {
const span = requestContext.contextValue.transaction.startChild({
op: "resolver",
description: `${requestContext.info.parentType.name}.${requestContext.info.fieldName}`,
});
return () => {
span.finish();
};
},
};
},
};
},
};
@Module({
imports: [
GraphQLModule.forRoot({
plugins: [plugin],
context: () => ({
transaction: startTransaction({
op: "gql",
name: "GraphQLTransaction", // default name in case query doesn't contain a name
}),
}),
}),
],
})
export class Test {} |
@ThallesP you are using in the code depreciated methods. Have you updated your method so far? |
I still couldn't get it to work out of the box with the current Sentry NestJS package (8.42.0). This is my current approach that works with distributed tracing: import {
ApolloServerPlugin,
GraphQLRequestExecutionListener,
GraphQLRequestListener
} from '@apollo/server';
import {GraphQLContext} from '../graphql.context';
import * as Sentry from '@sentry/core';
export const sentryTracingPlugin: () => ApolloServerPlugin<GraphQLContext> = () => {
return {
async requestDidStart(requestContext) {
return Sentry.withScope(async (scope) => {
const propagationContext = Sentry.propagationContextFromHeaders(
requestContext.request.http?.headers.get('sentry-trace'),
requestContext.request.http?.headers.get('baggage')
);
scope.setPropagationContext(propagationContext);
const account = requestContext.contextValue.req?.user?.account;
if (account) {
scope.setUser({
id: account.id,
email: account.email
});
}
return Sentry.startSpanManual(
{
op: 'gql',
name: requestContext.request.operationName || 'GraphQLRequest',
scope
},
async (requestSpan) => {
return Promise.resolve<GraphQLRequestListener<GraphQLContext>>({
willSendResponse: async () => {
requestSpan.end();
return Promise.resolve();
},
async executionDidStart(_requestContext) {
return Promise.resolve<GraphQLRequestExecutionListener<GraphQLContext>>({
willResolveField({info}) {
if (!info.path.prev) {
return Sentry.startSpanManual(
{
op: 'resolver',
name: info.path.typename
? `${info.path.typename}.${info.fieldName}`
: info.fieldName,
parentSpan: requestSpan
},
(resolverSpan) => {
return () => {
resolverSpan.end();
};
}
);
}
}
});
}
});
}
);
});
}
};
}; I can imagine you don't have the same Usage in the NestJS Graphql Apollo module: const plugins: Array<ApolloServerPlugin<GraphQLContext>> = [
sentryTracingPlugin(),
]; Hopefully it helps someone 😄 |
Is there an existing issue for this?
How do you use Sentry?
Self-hosted/on-premise
Which package are you using?
@sentry/node
SDK Version
7.13.0
Framework Version
Link to Sentry event
No response
Steps to Reproduce
Im running a nestjs graphql api which uses apollo-server behind. I want to integrate the Apollo tracing: https://docs.sentry.io/platforms/node/performance/database/opt-in/#apollo-server-integration but I'm always getting an error.
It looks like the problem is that the integration cannot find the resolvers in constructSchema? See here my resolvers are empty https://github.com/getsentry/sentry-javascript/blob/master/packages/tracing/src/integrations/node/apollo.ts#L49
Can somebody help me how can i fix that?
Expected Result
Expected result would be getting tracing into my sentry instance 😅
Actual Result
Error on API startup
The text was updated successfully, but these errors were encountered: