Skip to content

Commit

Permalink
fix(prom): remove unwanted labels completely from metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jan 24, 2024
1 parent c0d1fe6 commit 3dc808b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-squids-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@envelop/prometheus': patch
---

Remove unwanted labels from metrics
54 changes: 25 additions & 29 deletions packages/plugins/prometheus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import {
createSummary,
extractDeprecatedFields,
FillLabelsFnParams,
filterFillParamsFnParams,
getHistogramFromConfig,
labelExists,
shouldTraceFieldResolver,
} from './utils.js';

Expand Down Expand Up @@ -71,18 +73,6 @@ export const usePrometheus = (config: PrometheusTracingPluginConfig = {}): Plugi
'Time spent on running the GraphQL "subscribe" function',
);

function labelExists(label: string) {
const labelFlag = config.labels?.[label];
if (labelFlag == null) {
return true;
}
return labelFlag;
}

function filterFillParamsFnParams(params: Record<string, any>) {
return Object.fromEntries(Object.entries(params).filter(([key]) => labelExists(key)));
}

const resolversHistogram =
typeof config.resolvers === 'object'
? config.resolvers
Expand All @@ -97,11 +87,11 @@ export const usePrometheus = (config: PrometheusTracingPluginConfig = {}): Plugi
'fieldName',
'typeName',
'returnType',
].filter(labelExists),
].filter(label => labelExists(config, label)),
registers: [config.registry || defaultRegistry],
}),
fillLabelsFn: params =>
filterFillParamsFnParams({
filterFillParamsFnParams(config, {
operationName: params.operationName!,
operationType: params.operationType!,
fieldName: params.info?.fieldName!,
Expand All @@ -119,11 +109,13 @@ export const usePrometheus = (config: PrometheusTracingPluginConfig = {}): Plugi
histogram: new Histogram({
name: 'graphql_envelop_request_duration',
help: 'Time spent on running the GraphQL operation from parse to execute',
labelNames: ['operationType', 'operationName'].filter(labelExists),
labelNames: ['operationType', 'operationName'].filter(label =>
labelExists(config, label),
),
registers: [config.registry || defaultRegistry],
}),
fillLabelsFn: params =>
filterFillParamsFnParams({
filterFillParamsFnParams(config, {
operationName: params.operationName!,
operationType: params.operationType!,
}),
Expand All @@ -138,11 +130,13 @@ export const usePrometheus = (config: PrometheusTracingPluginConfig = {}): Plugi
summary: new Summary({
name: 'graphql_envelop_request_time_summary',
help: 'Summary to measure the time to complete GraphQL operations',
labelNames: ['operationType', 'operationName'].filter(labelExists),
labelNames: ['operationType', 'operationName'].filter(label =>
labelExists(config, label),
),
registers: [config.registry || defaultRegistry],
}),
fillLabelsFn: params =>
filterFillParamsFnParams({
filterFillParamsFnParams(config, {
operationName: params.operationName!,
operationType: params.operationType!,
}),
Expand All @@ -157,11 +151,13 @@ export const usePrometheus = (config: PrometheusTracingPluginConfig = {}): Plugi
counter: new Counter({
name: 'graphql_envelop_error_result',
help: 'Counts the amount of errors reported from all phases',
labelNames: ['operationType', 'operationName', 'path', 'phase'].filter(labelExists),
labelNames: ['operationType', 'operationName', 'path', 'phase'].filter(label =>
labelExists(config, label),
),
registers: [config.registry || defaultRegistry],
}),
fillLabelsFn: params =>
filterFillParamsFnParams({
filterFillParamsFnParams(config, {
operationName: params.operationName!,
operationType: params.operationType!,
path: params.error?.path?.join('.')!,
Expand All @@ -178,11 +174,13 @@ export const usePrometheus = (config: PrometheusTracingPluginConfig = {}): Plugi
counter: new Counter({
name: 'graphql_envelop_request',
help: 'Counts the amount of GraphQL requests executed through Envelop',
labelNames: ['operationType', 'operationName'].filter(labelExists),
labelNames: ['operationType', 'operationName'].filter(label =>
labelExists(config, label),
),
registers: [config.registry || defaultRegistry],
}),
fillLabelsFn: params =>
filterFillParamsFnParams({
filterFillParamsFnParams(config, {
operationName: params.operationName!,
operationType: params.operationType!,
}),
Expand All @@ -197,13 +195,13 @@ export const usePrometheus = (config: PrometheusTracingPluginConfig = {}): Plugi
counter: new Counter({
name: 'graphql_envelop_deprecated_field',
help: 'Counts the amount of deprecated fields used in selection sets',
labelNames: ['operationType', 'operationName', 'fieldName', 'typeName'].filter(
labelExists,
labelNames: ['operationType', 'operationName', 'fieldName', 'typeName'].filter(label =>
labelExists(config, label),
),
registers: [config.registry || defaultRegistry],
}),
fillLabelsFn: params =>
filterFillParamsFnParams({
filterFillParamsFnParams(config, {
operationName: params.operationName!,
operationType: params.operationType!,
fieldName: params.deprecationInfo?.fieldName!,
Expand Down Expand Up @@ -237,10 +235,8 @@ export const usePrometheus = (config: PrometheusTracingPluginConfig = {}): Plugi
const totalTime = (Date.now() - startTime) / 1000;
let fillLabelsFnParams = fillLabelsFnParamsMap.get(params.result);
if (!fillLabelsFnParams) {
fillLabelsFnParams = createFillLabelFnParams(
params.result,
context,
filterFillParamsFnParams,
fillLabelsFnParams = createFillLabelFnParams(params.result, context, params =>
filterFillParamsFnParams(config, params),
);
fillLabelsFnParamsMap.set(context, fillLabelsFnParams);
}
Expand Down
28 changes: 23 additions & 5 deletions packages/plugins/prometheus/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,16 @@ export function getHistogramFromConfig(
histogram: new Histogram({
name,
help,
labelNames: ['operationType', 'operationName'] as const,
labelNames: ['operationType', 'operationName'].filter(label =>
labelExists(config, label),
),
registers: [config.registry || defaultRegistry],
}),
fillLabelsFn: params => ({
operationName: params.operationName!,
operationType: params.operationType!,
}),
fillLabelsFn: params =>
filterFillParamsFnParams(config, {
operationName: params.operationName!,
operationType: params.operationType!,
}),
})
: undefined;
}
Expand Down Expand Up @@ -153,3 +156,18 @@ export function extractDeprecatedFields(node: ASTNode, typeInfo: TypeInfo): Depr

return found;
}

export function labelExists(config: PrometheusTracingPluginConfig, label: string) {
const labelFlag = config.labels?.[label];
if (labelFlag == null) {
return true;
}
return labelFlag;
}

export function filterFillParamsFnParams(
config: PrometheusTracingPluginConfig,
params: Record<string, any>,
) {
return Object.fromEntries(Object.entries(params).filter(([key]) => labelExists(config, key)));
}

0 comments on commit 3dc808b

Please sign in to comment.