@envelop/prometheus
Advanced tools
Comparing version 0.1.0-alpha-066dedf.0 to 0.1.0-alpha-51566cc.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-066dedf.0", | ||
"version": "0.1.0-alpha-51566cc.0", | ||
"sideEffects": false, | ||
@@ -5,0 +5,0 @@ "peerDependencies": { |
@@ -7,4 +7,12 @@ ## `@envelop/prometheus` | ||
- | ||
- errors (categorized by `phase`) | ||
- resolvers tracing and runtime | ||
- deprecated fields usage | ||
- `parse` execution time | ||
- `validate` execution time | ||
- `contextBuilding` execution time | ||
- `execute` execution time | ||
> You can also customize each phase reporter, and add custom metadata and labels to the metrics. | ||
## Getting Started | ||
@@ -18,6 +26,78 @@ | ||
## Configuration | ||
```ts | ||
import { envelop } from '@envelop/core'; | ||
import { 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: 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 | ||
}), | ||
], | ||
}); | ||
``` | ||
### Custom registry | ||
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
68874
102