-
Notifications
You must be signed in to change notification settings - Fork 575
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
fix(prom): graphql-ws integration #3651
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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-prometheus': patch | ||
--- | ||
|
||
`request` is missing when GraphQL WS is used as expected, and as we don't need HTTP/Yoga specific | ||
metrics, this should be skipped |
132 changes: 132 additions & 0 deletions
132
packages/plugins/prometheus/__integration-tests__/graphql-ws.test.ts
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,132 @@ | ||
import { createServer, type Server } from 'node:http'; | ||
import { AddressInfo } from 'node:net'; | ||
import { Client, createClient } from 'graphql-ws'; | ||
import { useServer } from 'graphql-ws/lib/use/ws'; | ||
import { createSchema, createYoga } from 'graphql-yoga'; | ||
import { register as registry } from 'prom-client'; | ||
import WebSocket from 'ws'; | ||
import { usePrometheus } from '../src'; | ||
|
||
describe('GraphQL WS & Prometheus integration', () => { | ||
const schema = createSchema({ | ||
typeDefs: /* GraphQL */ ` | ||
type Query { | ||
hello: String! | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
hello: () => 'Hello world!', | ||
}, | ||
}, | ||
}); | ||
let client: Client | undefined; | ||
let server: Server | undefined; | ||
afterEach(async () => { | ||
registry.clear(); | ||
if (client) { | ||
await client.dispose(); | ||
} | ||
if (server) { | ||
await new Promise<void>((resolve, reject) => { | ||
server?.close(err => { | ||
if (err) return reject(err); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
it('should have default configs for the plugin metrics', async () => { | ||
const yoga = createYoga({ | ||
schema, | ||
plugins: [ | ||
usePrometheus({ | ||
registry, | ||
}), | ||
], | ||
}); | ||
server = createServer(yoga); | ||
await new Promise<void>((resolve, reject) => { | ||
server?.listen(0, () => { | ||
resolve(); | ||
}); | ||
server?.once('error', reject); | ||
}); | ||
const wss = new WebSocket.WebSocketServer({ | ||
server, | ||
path: yoga.graphqlEndpoint, | ||
}); | ||
|
||
useServer( | ||
{ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
execute: (args: any) => args.execute(args), | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
subscribe: (args: any) => args.subscribe(args), | ||
onSubscribe: async (ctx, msg) => { | ||
const { schema, execute, subscribe, contextFactory, parse, validate } = yoga.getEnveloped( | ||
{ | ||
...ctx, | ||
req: ctx.extra.request, | ||
socket: ctx.extra.socket, | ||
params: msg.payload, | ||
}, | ||
); | ||
|
||
const args = { | ||
schema, | ||
operationName: msg.payload.operationName, | ||
document: parse(msg.payload.query), | ||
variableValues: msg.payload.variables, | ||
contextValue: await contextFactory(), | ||
execute, | ||
subscribe, | ||
}; | ||
|
||
const errors = validate(args.schema, args.document); | ||
if (errors.length) return errors; | ||
return args; | ||
}, | ||
}, | ||
wss, | ||
); | ||
|
||
const port = (server.address() as AddressInfo).port; | ||
|
||
const client = createClient({ | ||
url: `ws://localhost:${port}${yoga.graphqlEndpoint}`, | ||
webSocketImpl: WebSocket, | ||
}); | ||
|
||
const iterable = client.iterate({ | ||
query: /* GraphQL */ ` | ||
query Test { | ||
hello | ||
} | ||
`, | ||
}); | ||
|
||
for await (const result of iterable) { | ||
expect(result.data).toEqual({ hello: 'Hello world!' }); | ||
} | ||
|
||
const metrics = await registry.metrics(); | ||
|
||
// enabled by default | ||
expect(metrics).toContain('# TYPE graphql_envelop_phase_parse histogram'); | ||
expect(metrics).toContain('# TYPE graphql_envelop_phase_validate histogram'); | ||
expect(metrics).toContain('# TYPE graphql_envelop_phase_context histogram'); | ||
expect(metrics).toContain('# TYPE graphql_envelop_phase_execute histogram'); | ||
expect(metrics).toContain('# TYPE graphql_envelop_phase_subscribe histogram'); | ||
expect(metrics).toContain('# TYPE graphql_envelop_request_duration histogram'); | ||
expect(metrics).toContain('# TYPE graphql_envelop_request_time_summary summary'); | ||
expect(metrics).toContain('# TYPE graphql_envelop_error_result counter'); | ||
expect(metrics).toContain('# TYPE graphql_envelop_request counter'); | ||
expect(metrics).toContain('# TYPE graphql_envelop_deprecated_field counter'); | ||
expect(metrics).toContain('# TYPE graphql_envelop_schema_change counter'); | ||
|
||
// disabled by default | ||
expect(metrics).not.toContain('graphql_envelop_execute_resolver'); | ||
}); | ||
}); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure to understand why we don't want to also track subscriptions ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Subscriptions are not the case here. Yoga subscriptions(SSE) are still tracked.
The issue is when GraphQL WS uses the Envelop instance of Yoga, this plugin fails because GraphQL WS doesn't have
request
object in the context.It is ok to skip this because GraphQL WS is not HTTP at all.
This doesn't skip tracking entirely, it just skips the metric starts on
onRequest
and ends inonResponse
hooks. Those hooks are not triggered by GraphQL WS at all.