-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: [plugins/prometheus] fixes for phases/shouldObserve for Yoga (#2326
) * fix: [plugins/prometheus] fixes for phases/shouldObserve implementation in Yoga * make new API non breaking * chore(dependencies): updated changesets for modified dependencies * refactor to conditionally observe events based on phase * 🚧 WIP * chore(dependencies): updated changesets for modified dependencies * missing TS error * satisfies * add all test cases * fix fillLabelsFn and shouldObserve param types * don't use OperationType enum from graphql for compatibility with 15 * update the changeset * chore(dependencies): updated changesets for modified dependencies --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: enisdenjo <denis@denelop.com>
- Loading branch information
1 parent
4d82b34
commit 443fc15
Showing
5 changed files
with
1,553 additions
and
802 deletions.
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,96 @@ | ||
--- | ||
'@envelop/prometheus': minor | ||
--- | ||
|
||
Allow to explicitly control which events and timing should be observe. | ||
|
||
Each metric can now be configured to observe events and timings only for certain GraphQL pipeline | ||
phases, or depending on the request context. | ||
|
||
## Example: trace only execution and subscription errors | ||
|
||
```ts | ||
import { execute, parse, specifiedRules, subscribe, validate } from 'graphql' | ||
import { envelop, useEngine } from '@envelop/core' | ||
import { usePrometheus } from '@envelop/prometheus' | ||
|
||
const TRACKED_OPERATION_NAMES = [ | ||
// make a list of operation that you want to monitor | ||
] | ||
|
||
const getEnveloped = envelop({ | ||
plugins: [ | ||
useEngine({ parse, validate, specifiedRules, execute, subscribe }), | ||
usePrometheus({ | ||
metrics: { | ||
// Here, an array of phases can be provided to enable the metric only on certain phases. | ||
// In this example, only error happening during the execute and subscribe phases will tracked | ||
graphql_envelop_phase_error: ['execute', 'subscribe'] | ||
} | ||
}), | ||
], | ||
}) | ||
``` | ||
|
||
## Example: Monitor timing only of a set of operations by name | ||
|
||
```ts | ||
import { execute, parse, specifiedRules, subscribe, validate } from 'graphql' | ||
import { envelop, useEngine } from '@envelop/core' | ||
import { usePrometheus } from '@envelop/prometheus' | ||
|
||
const TRACKED_OPERATION_NAMES = [ | ||
// make a list of operation that you want to monitor | ||
] | ||
|
||
const getEnveloped = envelop({ | ||
plugins: [ | ||
useEngine({ parse, validate, specifiedRules, execute, subscribe }), | ||
usePrometheus({ | ||
metrics: { | ||
graphql_yoga_http_duration: createHistogram({ | ||
registry, | ||
histogram: { | ||
name: 'graphql_envelop_request_duration', | ||
help: 'Time spent on HTTP connection', | ||
labelNames: ['operationName'] | ||
}, | ||
fillLabelsFn: ({ operationName }, _rawContext) => ({ operationName, }), | ||
phases: ['execute', 'subscribe'], | ||
|
||
// Here `shouldObserve` control if the request timing should be observed, based on context | ||
shouldObserve: ({ operationName }) => TRACKED_OPERATIONS.includes(operationName), | ||
}) | ||
}, | ||
}) | ||
] | ||
}) | ||
``` | ||
|
||
## Default Behavior Change | ||
|
||
A metric is enabled using `true` value in metrics options will observe in every | ||
phases available. | ||
|
||
Previously, which phase was observe was depending on which other metric were enabled. For example, | ||
this config would only trace validation error: | ||
|
||
```ts | ||
usePrometheus({ | ||
metrics: { | ||
graphql_envelop_phase_error: true, | ||
graphql_envelop_phase_validate: true, | ||
}, | ||
}) | ||
``` | ||
|
||
This is no longer the case. If you were relying on this behavior, please use an array of string to | ||
restrict observed phases. | ||
|
||
```ts | ||
usePrometheus({ | ||
metrics: { | ||
graphql_envelop_phase_error: ['validate'], | ||
}, | ||
}) | ||
``` |
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.