Skip to content

Commit 63b78d5

Browse files
EmrysMyrddingithub-actions[bot]ardatan
authored
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>
1 parent 5a677d6 commit 63b78d5

36 files changed

+1045
-727
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@graphql-yoga/nestjs-federation": patch
3+
---
4+
dependencies updates:
5+
- 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`)
6+
- Updated dependency [`@envelop/core@^5.2.1` ↗︎](https://www.npmjs.com/package/@envelop/core/v/5.2.1) (from `^5.0.0`, in `dependencies`)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphql-yoga/plugin-apollo-inline-trace": patch
3+
---
4+
dependencies updates:
5+
- 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`)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphql-yoga/plugin-disable-introspection": patch
3+
---
4+
dependencies updates:
5+
- Added dependency [`@whatwg-node/promise-helpers@^1.2.4` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.2.4) (to `dependencies`)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphql-yoga/plugin-prometheus": patch
3+
---
4+
dependencies updates:
5+
- Updated dependency [`@envelop/prometheus@^12.1.1` ↗︎](https://www.npmjs.com/package/@envelop/prometheus/v/12.1.1) (from `^12.0.0`, in `dependencies`)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@graphql-yoga/plugin-response-cache": patch
3+
---
4+
dependencies updates:
5+
- Updated dependency [`@envelop/core@^5.2.1` ↗︎](https://www.npmjs.com/package/@envelop/core/v/5.2.1) (from `^5.0.2`, in `dependencies`)
6+
- 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`)

.changeset/afraid-jars-burn.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
'graphql-yoga': minor
3+
---
4+
5+
Add new Instruments API
6+
7+
Introduction of a new API allowing to instrument the graphql pipeline.
8+
9+
This new API differs from already existing Hooks by not having access to input/output of phases. The
10+
goal of `Instruments` is to run allow running code before, after or around the **whole process of a
11+
phase**, including plugins hooks executions.
12+
13+
The main use case of this new API is observability (monitoring, tracing, etc...).
14+
15+
### Basic usage
16+
17+
```ts
18+
import { createYoga } from 'graphql-yoga'
19+
import Sentry from '@sentry/node'
20+
import schema from './schema'
21+
22+
const server = createYoga({
23+
schema,
24+
plugins: [
25+
{
26+
instruments: {
27+
request: ({ request }, wrapped) =>
28+
Sentry.startSpan({ name: 'Graphql Operation' }, async () => {
29+
try {
30+
await wrapped()
31+
} catch (err) {
32+
Sentry.captureException(err)
33+
}
34+
})
35+
}
36+
}
37+
]
38+
})
39+
```
40+
41+
### Multiple instruments plugins
42+
43+
It is possible to have multiple instruments plugins (Prometheus and Sentry for example), they will
44+
be automatically composed by envelop in the same order than the plugin array (first is outermost,
45+
last is inner most).
46+
47+
```ts
48+
import { createYoga } from 'graphql-yoga'
49+
import schema from './schema'
50+
51+
const server = createYoga({
52+
schema,
53+
plugins: [useSentry(), useOpentelemetry()]
54+
})
55+
```
56+
57+
```mermaid
58+
sequenceDiagram
59+
Sentry->>Opentelemetry: ;
60+
Opentelemetry->>Server Adapter: ;
61+
Server Adapter->>Opentelemetry: ;
62+
Opentelemetry->>Sentry: ;
63+
```
64+
65+
### Custom instruments ordering
66+
67+
If the default composition ordering doesn't suite your need, you can manually compose instruments.
68+
This allows to have a different execution order of hooks and instruments.
69+
70+
```ts
71+
import { composeInstruments, createYoga } from 'graphql-yoga'
72+
import schema from './schema'
73+
74+
const { instruments: sentryInstruments, ...sentryPlugin } = useSentry()
75+
const { instruments: otelInstruments, ...otelPlugin } = useOpentelemetry()
76+
const instruments = composeInstruments([otelInstruments, sentryInstruments])
77+
78+
const server = createYoga({
79+
schema,
80+
plugins: [{ instruments }, useSentry(), useOpentelemetry()]
81+
})
82+
```
83+
84+
```mermaid
85+
sequenceDiagram
86+
Opentelemetry->>Sentry: ;
87+
Sentry->>Server Adapter: ;
88+
Server Adapter->>Sentry: ;
89+
Sentry->>Opentelemetry: ;
90+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"graphql-yoga": patch
3+
---
4+
dependencies updates:
5+
- Updated dependency [`@envelop/core@^5.2.1` ↗︎](https://www.npmjs.com/package/@envelop/core/v/5.2.1) (from `^5.0.2`, in `dependencies`)
6+
- Added dependency [`@envelop/instruments@^1.0.0` ↗︎](https://www.npmjs.com/package/@envelop/instruments/v/1.0.0) (to `dependencies`)
7+
- Added dependency [`@whatwg-node/promise-helpers@^1.2.4` ↗︎](https://www.npmjs.com/package/@whatwg-node/promise-helpers/v/1.2.4) (to `dependencies`)

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ packages/graphql-yoga/src/landing-page-html.ts
1717
packages/graphql-yoga/src/graphiql-html.ts
1818
run/
1919
website/public/_pagefind/
20+
.helix/languages.toml

examples/error-handling/src/yoga.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ export const yoga = createYoga({
3333
Query: {
3434
greeting: async () => {
3535
// This service does not exist
36-
const greeting = await fetch('http://localhost:9999/greeting').then(res => res.text());
36+
const greeting = await fetch('http://0.0.0.0:9999/greeting', {
37+
signal: AbortSignal.timeout(1000),
38+
}).then(res => res.text());
3739

3840
return greeting;
3941
},

examples/sveltekit/__integration-tests__/sveltekit.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('SvelteKit integration', () => {
5050
});
5151

5252
afterAll(async () => {
53-
await browser.close();
53+
await browser?.close();
5454
sveltekitProcess.kill();
5555
});
5656

0 commit comments

Comments
 (0)