-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add new Instruments API (#3793)
* feat(core): Add new Tracer API * changeset * fix root exports * use latest envelope snapshot * rename to instruments and use instruments utils * chore(dependencies): updated changesets for modified dependencies * refactor request parser to use promise utils * add tests * chore(dependencies): updated changesets for modified dependencies * fix `requestParse` and `resultProcess` instruments can be async * remove request from operation instruments payload * add documentation * use @envelop/instruments released version * chore(dependencies): updated changesets for modified dependencies * changeset * re-export instruments utils * update whatwg-node alpha * More refactor * chore(dependencies): updated changesets for modified dependencies * Lets go * Bump versions * Bump versions * Bump versions * Deduped lockfile * chore(dependencies): updated changesets for modified dependencies * Fix tests * Fix * Deduped lockfile * chore(dependencies): updated changesets for modified dependencies --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
- Loading branch information
1 parent
5a677d6
commit 63b78d5
Showing
36 changed files
with
1,045 additions
and
727 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
.changeset/@graphql-yoga_nestjs-federation-3793-dependencies.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@graphql-yoga/nestjs-federation": patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`@envelop/apollo-federation@^6.1.1` ↗︎](https://www.npmjs.com/package/@envelop/apollo-federation/v/6.1.1) (from `^6.0.0`, in `dependencies`) | ||
- Updated dependency [`@envelop/core@^5.2.1` ↗︎](https://www.npmjs.com/package/@envelop/core/v/5.2.1) (from `^5.0.0`, in `dependencies`) |
5 changes: 5 additions & 0 deletions
5
.changeset/@graphql-yoga_plugin-apollo-inline-trace-3793-dependencies.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@graphql-yoga/plugin-apollo-inline-trace": patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`@envelop/on-resolve@^5.1.1` ↗︎](https://www.npmjs.com/package/@envelop/on-resolve/v/5.1.1) (from `^5.0.0`, in `dependencies`) |
5 changes: 5 additions & 0 deletions
5
.changeset/@graphql-yoga_plugin-disable-introspection-3793-dependencies.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@graphql-yoga/plugin-disable-introspection": patch | ||
--- | ||
dependencies updates: | ||
- Added dependency [`@whatwg-node/promise-helpers@^1.2.4` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.2.4) (to `dependencies`) |
5 changes: 5 additions & 0 deletions
5
.changeset/@graphql-yoga_plugin-prometheus-3793-dependencies.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@graphql-yoga/plugin-prometheus": patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`@envelop/prometheus@^12.1.1` ↗︎](https://www.npmjs.com/package/@envelop/prometheus/v/12.1.1) (from `^12.0.0`, in `dependencies`) |
6 changes: 6 additions & 0 deletions
6
.changeset/@graphql-yoga_plugin-response-cache-3793-dependencies.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@graphql-yoga/plugin-response-cache": patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`@envelop/core@^5.2.1` ↗︎](https://www.npmjs.com/package/@envelop/core/v/5.2.1) (from `^5.0.2`, in `dependencies`) | ||
- Updated dependency [`@envelop/response-cache@^7.1.1` ↗︎](https://www.npmjs.com/package/@envelop/response-cache/v/7.1.1) (from `^7.0.0`, in `dependencies`) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
--- | ||
'graphql-yoga': minor | ||
--- | ||
|
||
Add new Instruments API | ||
|
||
Introduction of a new API allowing to instrument the graphql pipeline. | ||
|
||
This new API differs from already existing Hooks by not having access to input/output of phases. The | ||
goal of `Instruments` is to run allow running code before, after or around the **whole process of a | ||
phase**, including plugins hooks executions. | ||
|
||
The main use case of this new API is observability (monitoring, tracing, etc...). | ||
|
||
### Basic usage | ||
|
||
```ts | ||
import { createYoga } from 'graphql-yoga' | ||
import Sentry from '@sentry/node' | ||
import schema from './schema' | ||
|
||
const server = createYoga({ | ||
schema, | ||
plugins: [ | ||
{ | ||
instruments: { | ||
request: ({ request }, wrapped) => | ||
Sentry.startSpan({ name: 'Graphql Operation' }, async () => { | ||
try { | ||
await wrapped() | ||
} catch (err) { | ||
Sentry.captureException(err) | ||
} | ||
}) | ||
} | ||
} | ||
] | ||
}) | ||
``` | ||
|
||
### Multiple instruments plugins | ||
|
||
It is possible to have multiple instruments plugins (Prometheus and Sentry for example), they will | ||
be automatically composed by envelop in the same order than the plugin array (first is outermost, | ||
last is inner most). | ||
|
||
```ts | ||
import { createYoga } from 'graphql-yoga' | ||
import schema from './schema' | ||
|
||
const server = createYoga({ | ||
schema, | ||
plugins: [useSentry(), useOpentelemetry()] | ||
}) | ||
``` | ||
|
||
```mermaid | ||
sequenceDiagram | ||
Sentry->>Opentelemetry: ; | ||
Opentelemetry->>Server Adapter: ; | ||
Server Adapter->>Opentelemetry: ; | ||
Opentelemetry->>Sentry: ; | ||
``` | ||
|
||
### Custom instruments ordering | ||
|
||
If the default composition ordering doesn't suite your need, you can manually compose instruments. | ||
This allows to have a different execution order of hooks and instruments. | ||
|
||
```ts | ||
import { composeInstruments, createYoga } from 'graphql-yoga' | ||
import schema from './schema' | ||
|
||
const { instruments: sentryInstruments, ...sentryPlugin } = useSentry() | ||
const { instruments: otelInstruments, ...otelPlugin } = useOpentelemetry() | ||
const instruments = composeInstruments([otelInstruments, sentryInstruments]) | ||
|
||
const server = createYoga({ | ||
schema, | ||
plugins: [{ instruments }, useSentry(), useOpentelemetry()] | ||
}) | ||
``` | ||
|
||
```mermaid | ||
sequenceDiagram | ||
Opentelemetry->>Sentry: ; | ||
Sentry->>Server Adapter: ; | ||
Server Adapter->>Sentry: ; | ||
Sentry->>Opentelemetry: ; | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"graphql-yoga": patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`@envelop/core@^5.2.1` ↗︎](https://www.npmjs.com/package/@envelop/core/v/5.2.1) (from `^5.0.2`, in `dependencies`) | ||
- Added dependency [`@envelop/instruments@^1.0.0` ↗︎](https://www.npmjs.com/package/@envelop/instruments/v/1.0.0) (to `dependencies`) | ||
- Added dependency [`@whatwg-node/promise-helpers@^1.2.4` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.2.4) (to `dependencies`) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { createSchema, createYoga, Plugin } from '../src'; | ||
|
||
describe('instruments', () => { | ||
const schema = createSchema({ | ||
typeDefs: /* GraphQL */ ` | ||
type Query { | ||
hello: String! | ||
} | ||
type Subscription { | ||
greetings: String! | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
hello: () => 'world', | ||
}, | ||
Subscription: { | ||
greetings: { | ||
async *subscribe() { | ||
yield { greetings: 'Hi' }; | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
it('should wrap all the phases with the default composition order', async () => { | ||
const result: string[] = []; | ||
const make = (name: string): Plugin => ({ | ||
instruments: { | ||
context: (_, w) => { | ||
result.push(`pre-context-${name}`); | ||
w(); | ||
result.push(`post-context-${name}`); | ||
}, | ||
execute: async (_, w) => { | ||
result.push(`pre-execute-${name}`); | ||
await w(); | ||
result.push(`post-execute-${name}`); | ||
}, | ||
init: (_, w) => { | ||
result.push(`pre-init-${name}`); | ||
w(); | ||
result.push(`post-init-${name}`); | ||
}, | ||
parse: (_, w) => { | ||
result.push(`pre-parse-${name}`); | ||
w(); | ||
result.push(`post-parse-${name}`); | ||
}, | ||
request: async (_, w) => { | ||
result.push(`pre-request-${name}`); | ||
await w(); | ||
result.push(`post-request-${name}`); | ||
}, | ||
subscribe: async (_, w) => { | ||
result.push(`pre-subscribe-${name}`); | ||
await w(); | ||
result.push('post-subscribe-${name}'); | ||
}, | ||
validate: (_, w) => { | ||
result.push(`pre-validate-${name}`); | ||
w(); | ||
result.push(`post-validate-${name}`); | ||
}, | ||
operation: async (_, w) => { | ||
result.push(`pre-operation-${name}`); | ||
await w(); | ||
result.push(`post-operation-${name}`); | ||
}, | ||
requestParse: async (_, w) => { | ||
result.push(`pre-request-parse-${name}`); | ||
await w(); | ||
result.push(`post-request-parse-${name}`); | ||
}, | ||
resultProcess: async (_, w) => { | ||
result.push(`pre-result-process-${name}`); | ||
await w(); | ||
result.push(`post-result-process-${name}`); | ||
}, | ||
}, | ||
}); | ||
|
||
const yoga = createYoga({ | ||
schema, | ||
plugins: [make('1'), make('2'), make('3')], | ||
}); | ||
|
||
await yoga.fetch('http://yoga/graphql?query={hello}'); | ||
|
||
const withPrefix = (prefix: string) => [`${prefix}-1`, `${prefix}-2`, `${prefix}-3`]; | ||
expect(result).toEqual([ | ||
...withPrefix('pre-request'), | ||
...withPrefix('pre-request-parse'), | ||
...withPrefix('post-request-parse').reverse(), | ||
...withPrefix('pre-operation'), | ||
...withPrefix('pre-init'), | ||
...withPrefix('post-init').reverse(), | ||
...withPrefix('pre-parse'), | ||
...withPrefix('post-parse').reverse(), | ||
...withPrefix('pre-validate'), | ||
...withPrefix('post-validate').reverse(), | ||
...withPrefix('pre-context'), | ||
...withPrefix('post-context').reverse(), | ||
...withPrefix('pre-execute'), | ||
...withPrefix('post-execute').reverse(), | ||
...withPrefix('post-operation').reverse(), | ||
...withPrefix('pre-result-process'), | ||
...withPrefix('post-result-process').reverse(), | ||
...withPrefix('post-request').reverse(), | ||
]); | ||
}); | ||
}); |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.