Skip to content

Commit 3dc808b

Browse files
committed
fix(prom): remove unwanted labels completely from metrics
1 parent c0d1fe6 commit 3dc808b

File tree

3 files changed

+53
-34
lines changed

3 files changed

+53
-34
lines changed

.changeset/olive-squids-drum.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@envelop/prometheus': patch
3+
---
4+
5+
Remove unwanted labels from metrics

packages/plugins/prometheus/src/index.ts

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import {
2222
createSummary,
2323
extractDeprecatedFields,
2424
FillLabelsFnParams,
25+
filterFillParamsFnParams,
2526
getHistogramFromConfig,
27+
labelExists,
2628
shouldTraceFieldResolver,
2729
} from './utils.js';
2830

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

74-
function labelExists(label: string) {
75-
const labelFlag = config.labels?.[label];
76-
if (labelFlag == null) {
77-
return true;
78-
}
79-
return labelFlag;
80-
}
81-
82-
function filterFillParamsFnParams(params: Record<string, any>) {
83-
return Object.fromEntries(Object.entries(params).filter(([key]) => labelExists(key)));
84-
}
85-
8676
const resolversHistogram =
8777
typeof config.resolvers === 'object'
8878
? config.resolvers
@@ -97,11 +87,11 @@ export const usePrometheus = (config: PrometheusTracingPluginConfig = {}): Plugi
9787
'fieldName',
9888
'typeName',
9989
'returnType',
100-
].filter(labelExists),
90+
].filter(label => labelExists(config, label)),
10191
registers: [config.registry || defaultRegistry],
10292
}),
10393
fillLabelsFn: params =>
104-
filterFillParamsFnParams({
94+
filterFillParamsFnParams(config, {
10595
operationName: params.operationName!,
10696
operationType: params.operationType!,
10797
fieldName: params.info?.fieldName!,
@@ -119,11 +109,13 @@ export const usePrometheus = (config: PrometheusTracingPluginConfig = {}): Plugi
119109
histogram: new Histogram({
120110
name: 'graphql_envelop_request_duration',
121111
help: 'Time spent on running the GraphQL operation from parse to execute',
122-
labelNames: ['operationType', 'operationName'].filter(labelExists),
112+
labelNames: ['operationType', 'operationName'].filter(label =>
113+
labelExists(config, label),
114+
),
123115
registers: [config.registry || defaultRegistry],
124116
}),
125117
fillLabelsFn: params =>
126-
filterFillParamsFnParams({
118+
filterFillParamsFnParams(config, {
127119
operationName: params.operationName!,
128120
operationType: params.operationType!,
129121
}),
@@ -138,11 +130,13 @@ export const usePrometheus = (config: PrometheusTracingPluginConfig = {}): Plugi
138130
summary: new Summary({
139131
name: 'graphql_envelop_request_time_summary',
140132
help: 'Summary to measure the time to complete GraphQL operations',
141-
labelNames: ['operationType', 'operationName'].filter(labelExists),
133+
labelNames: ['operationType', 'operationName'].filter(label =>
134+
labelExists(config, label),
135+
),
142136
registers: [config.registry || defaultRegistry],
143137
}),
144138
fillLabelsFn: params =>
145-
filterFillParamsFnParams({
139+
filterFillParamsFnParams(config, {
146140
operationName: params.operationName!,
147141
operationType: params.operationType!,
148142
}),
@@ -157,11 +151,13 @@ export const usePrometheus = (config: PrometheusTracingPluginConfig = {}): Plugi
157151
counter: new Counter({
158152
name: 'graphql_envelop_error_result',
159153
help: 'Counts the amount of errors reported from all phases',
160-
labelNames: ['operationType', 'operationName', 'path', 'phase'].filter(labelExists),
154+
labelNames: ['operationType', 'operationName', 'path', 'phase'].filter(label =>
155+
labelExists(config, label),
156+
),
161157
registers: [config.registry || defaultRegistry],
162158
}),
163159
fillLabelsFn: params =>
164-
filterFillParamsFnParams({
160+
filterFillParamsFnParams(config, {
165161
operationName: params.operationName!,
166162
operationType: params.operationType!,
167163
path: params.error?.path?.join('.')!,
@@ -178,11 +174,13 @@ export const usePrometheus = (config: PrometheusTracingPluginConfig = {}): Plugi
178174
counter: new Counter({
179175
name: 'graphql_envelop_request',
180176
help: 'Counts the amount of GraphQL requests executed through Envelop',
181-
labelNames: ['operationType', 'operationName'].filter(labelExists),
177+
labelNames: ['operationType', 'operationName'].filter(label =>
178+
labelExists(config, label),
179+
),
182180
registers: [config.registry || defaultRegistry],
183181
}),
184182
fillLabelsFn: params =>
185-
filterFillParamsFnParams({
183+
filterFillParamsFnParams(config, {
186184
operationName: params.operationName!,
187185
operationType: params.operationType!,
188186
}),
@@ -197,13 +195,13 @@ export const usePrometheus = (config: PrometheusTracingPluginConfig = {}): Plugi
197195
counter: new Counter({
198196
name: 'graphql_envelop_deprecated_field',
199197
help: 'Counts the amount of deprecated fields used in selection sets',
200-
labelNames: ['operationType', 'operationName', 'fieldName', 'typeName'].filter(
201-
labelExists,
198+
labelNames: ['operationType', 'operationName', 'fieldName', 'typeName'].filter(label =>
199+
labelExists(config, label),
202200
),
203201
registers: [config.registry || defaultRegistry],
204202
}),
205203
fillLabelsFn: params =>
206-
filterFillParamsFnParams({
204+
filterFillParamsFnParams(config, {
207205
operationName: params.operationName!,
208206
operationType: params.operationType!,
209207
fieldName: params.deprecationInfo?.fieldName!,
@@ -237,10 +235,8 @@ export const usePrometheus = (config: PrometheusTracingPluginConfig = {}): Plugi
237235
const totalTime = (Date.now() - startTime) / 1000;
238236
let fillLabelsFnParams = fillLabelsFnParamsMap.get(params.result);
239237
if (!fillLabelsFnParams) {
240-
fillLabelsFnParams = createFillLabelFnParams(
241-
params.result,
242-
context,
243-
filterFillParamsFnParams,
238+
fillLabelsFnParams = createFillLabelFnParams(params.result, context, params =>
239+
filterFillParamsFnParams(config, params),
244240
);
245241
fillLabelsFnParamsMap.set(context, fillLabelsFnParams);
246242
}

packages/plugins/prometheus/src/utils.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,16 @@ export function getHistogramFromConfig(
104104
histogram: new Histogram({
105105
name,
106106
help,
107-
labelNames: ['operationType', 'operationName'] as const,
107+
labelNames: ['operationType', 'operationName'].filter(label =>
108+
labelExists(config, label),
109+
),
108110
registers: [config.registry || defaultRegistry],
109111
}),
110-
fillLabelsFn: params => ({
111-
operationName: params.operationName!,
112-
operationType: params.operationType!,
113-
}),
112+
fillLabelsFn: params =>
113+
filterFillParamsFnParams(config, {
114+
operationName: params.operationName!,
115+
operationType: params.operationType!,
116+
}),
114117
})
115118
: undefined;
116119
}
@@ -153,3 +156,18 @@ export function extractDeprecatedFields(node: ASTNode, typeInfo: TypeInfo): Depr
153156

154157
return found;
155158
}
159+
160+
export function labelExists(config: PrometheusTracingPluginConfig, label: string) {
161+
const labelFlag = config.labels?.[label];
162+
if (labelFlag == null) {
163+
return true;
164+
}
165+
return labelFlag;
166+
}
167+
168+
export function filterFillParamsFnParams(
169+
config: PrometheusTracingPluginConfig,
170+
params: Record<string, any>,
171+
) {
172+
return Object.fromEntries(Object.entries(params).filter(([key]) => labelExists(config, key)));
173+
}

0 commit comments

Comments
 (0)