n1ru4l / envelop

Envelop is a lightweight library allowing developers to easily develop, share, collaborate and extend their GraphQL execution layer. Envelop is the missing GraphQL plugin system.
https://envelop.dev
MIT License
788 stars 128 forks source link

Make it possible to get the active span in the GraphQL resolver #2322

Open Karibash opened 2 days ago

Karibash commented 2 days ago

Is your feature request related to a problem? Please describe.

The getActiveSpan() function always returns undefined when used in a GraphQL resolver. This issue arises because @envelop/sentry does not execute the GraphQL resolver within the callback of startSpan.

This means that it's not possible to generate child spans for the spans created by @envelop/sentry. For example, @pothos/tracing-sentry uses getActiveSpan(), but since it can't retrieve an active span, it fails to perform tracing properly.

Describe the solution you'd like

For the sake of simplification, some implementations will be omitted, but it is necessary to execute the GraphQL resolver using setExecuteFn within the callback of startSpan as shown below.

onExecute({ args, executeFn, setExecuteFn }) {
  setExecuteFn(args => {
    return Sentry.startSpan(..., span => {
      return executeFn(args);
    });
  });
}

This is the correct way to handle startSpan as described in Sentry's documentation.