@envelop/prometheus
Advanced tools
Comparing version 0.1.0-alpha-d822e9b.0 to 0.1.0
20
index.js
@@ -173,13 +173,13 @@ 'use strict'; | ||
? ({ context }) => { | ||
if (context[promPluginContext]) { | ||
const startTime = Date.now(); | ||
return () => { | ||
const totalTime = (Date.now() - startTime) / 1000; | ||
const labels = contextBuildingHistogram.fillLabelsFn | ||
? contextBuildingHistogram.fillLabelsFn(context[promPluginContext]) | ||
: {}; | ||
contextBuildingHistogram.histogram.observe(labels, totalTime); | ||
}; | ||
if (!context[promPluginContext]) { | ||
return undefined; | ||
} | ||
return undefined; | ||
const startTime = Date.now(); | ||
return () => { | ||
const totalTime = (Date.now() - startTime) / 1000; | ||
const labels = contextBuildingHistogram.fillLabelsFn | ||
? contextBuildingHistogram.fillLabelsFn(context[promPluginContext]) | ||
: {}; | ||
contextBuildingHistogram.histogram.observe(labels, totalTime); | ||
}; | ||
} | ||
@@ -186,0 +186,0 @@ : undefined; |
{ | ||
"name": "@envelop/prometheus", | ||
"version": "0.1.0-alpha-d822e9b.0", | ||
"version": "0.1.0", | ||
"sideEffects": false, | ||
"peerDependencies": { | ||
"graphql": "^14.0.0 || ^15.0.0" | ||
"graphql": "^14.0.0 || ^15.0.0", | ||
"prom-client": "^13" | ||
}, | ||
"dependencies": { | ||
"prom-client": "13.1.0" | ||
}, | ||
"repository": { | ||
@@ -12,0 +10,0 @@ "type": "git", |
@@ -1,10 +0,16 @@ | ||
## `@envelop/apollo-tracing` | ||
## `@envelop/prometheus` | ||
This plugin tracks execution and resolvers and reports it using [`apollo-tracing`](https://github.com/apollographql/apollo-server/tree/main/packages/apollo-tracing) format (based on GraphQL `extensions`). | ||
This plugin tracks the complete execution flow, and reports metrics using Prometheus tracing (based on `prom-client`). | ||
You can see the results of the collected metrics if you are using [GraphQL Playground](https://github.com/graphql/graphql-playground). | ||
You can opt-in to collect tracing from the following phases: | ||
This is how it looks like (note the `TRACING` section): | ||
- errors (categorized by `phase`) | ||
- resolvers tracing and runtime | ||
- deprecated fields usage | ||
- `parse` execution time | ||
- `validate` execution time | ||
- `contextBuilding` execution time | ||
- `execute` execution time | ||
![Example](./example.png) | ||
> You can also customize each phase reporter, and add custom metadata and labels to the metrics. | ||
@@ -14,3 +20,3 @@ ## Getting Started | ||
``` | ||
yarn add @envelop/apollo-tracing | ||
yarn add prom-client @envelop/prometheus | ||
``` | ||
@@ -22,3 +28,3 @@ | ||
import { envelop } from '@envelop/core'; | ||
import { useApolloTracing } from '@envelop/apollo-tracing'; | ||
import { usePrometheus } from '@envelop/prometheus'; | ||
@@ -28,3 +34,14 @@ const getEnveloped = envelop({ | ||
// ... other plugins ... | ||
useApolloTracing(), | ||
usePrometheus({ | ||
// all optional, and by default, all set to false, please opt-in to the metrics you wish to get | ||
parse: true, | ||
validate: true, | ||
contextBuilding: true, | ||
execute: true, | ||
errors: true, | ||
resolvers: true, // requires "execute" to be `true` as well | ||
resolversWhitelist: ['Mutation.*', 'Query.user'], // reports metrics als for these resolvers, leave `undefined` to report all fields | ||
deprecatedFields: true, // requires "execute" and "resolvers" to be `true` as well | ||
registry: myRegistry, // If you are using a custom prom-client registry, please set it here | ||
}), | ||
], | ||
@@ -34,4 +51,55 @@ }); | ||
## Notes | ||
### Custom registry | ||
It's recommended to keep this plugin active only while developing only, since it's mainly used for performance tracking while developing. | ||
You can customize the `prom-client` `Registry` object if you are using a custom one, by passing it along with the configuration object: | ||
```ts | ||
import { Registry } from 'prom-client'; | ||
const myRegistry = new Registry(); | ||
const getEnveloped = envelop({ | ||
plugins: [ | ||
// ... other plugins ... | ||
usePrometheus({ | ||
// ... config ... | ||
registry: myRegistry, | ||
}), | ||
], | ||
}); | ||
``` | ||
> Note: if you are using custom `prom-client` instances, you need to make sure to pass your registry there as well. | ||
### Custom `prom-client` instances | ||
Each tracing field supports custom `prom-client` objects, and custom `labels` a metadata, you can create a custom extraction function for every `Histogram` / `Summary` / `Counter`: | ||
```ts | ||
import { Histogram } from 'prom-client'; | ||
import { envelop } from '@envelop/core'; | ||
import { createHistogram, usePrometheus } from '@envelop/prometheus'; | ||
const getEnveloped = envelop({ | ||
plugins: [ | ||
// ... other plugins ... | ||
usePrometheus({ | ||
// all optional, and by default, all set to false, please opt-in to the metrics you wish to get | ||
parse: createHistogram({ | ||
histogram: new Histogram({ | ||
name: 'my_custom_name', | ||
help: 'HELP ME', | ||
labelNames: ['opText'] as const, | ||
registers: [registry], // make sure to add your custom registry, if you are not using the default one | ||
}), | ||
fillLabelsFn: params => { | ||
// if you wish to fill your `lables` with metadata, you can use the params in order to get access to things like DocumentNode, operationName, operationType, `error` (for error metrics) and `info` (for resolvers metrics) | ||
return { | ||
opText: print(params.document), | ||
}; | ||
}, | ||
}), | ||
}), | ||
], | ||
}); | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
68843
102
+ Addedprom-client@13.2.0(transitive)
- Removedprom-client@13.1.0
- Removedprom-client@13.1.0(transitive)