Skip to content

Commit bff65e1

Browse files
committed
feat(opentelemetry): do not trace prometheus requests by default
1 parent ecfd435 commit bff65e1

File tree

1 file changed

+101
-77
lines changed

1 file changed

+101
-77
lines changed

packages/plugins/opentelemetry/src/plugin.ts

Lines changed: 101 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -107,83 +107,85 @@ export type OpenTelemetryGatewayPluginOptions = {
107107
/**
108108
* Tracing configuration
109109
*/
110-
traces?:
111-
| boolean
112-
| {
113-
/**
114-
* Tracer instance to use for creating spans (default: a tracer with name 'gateway').
115-
*/
116-
tracer?: Tracer;
117-
/**
118-
* Options to control which spans to create.
119-
* By default, all spans are enabled.
120-
*
121-
* You may specify a boolean value to enable/disable all spans, or a function to dynamically enable/disable spans based on the input.
122-
*/
123-
spans?: {
124-
/**
125-
* Enable/disable HTTP request spans (default: true).
126-
*
127-
* Disabling the HTTP span will also disable all other child spans.
128-
*/
129-
http?: BooleanOrPredicate<{ request: Request }>;
130-
/**
131-
* Enable/disable GraphQL operation spans (default: true).
132-
*
133-
* Disabling the GraphQL operation spa will also disable all other child spans.
134-
*/
135-
graphql?: BooleanOrPredicate<{ context: unknown }>; // FIXME: better type for graphql context
136-
/**
137-
* Enable/disable GraphQL context building phase (default: true).
138-
*/
139-
graphqlContextBuilding?: BooleanOrPredicate<{ context: unknown }>; // FIXME: better type for graphql context
140-
/**
141-
* Enable/disable GraphQL parse spans (default: true).
142-
*/
143-
graphqlParse?: BooleanOrPredicate<{ context: unknown }>; // FIXME: better type for graphql context
144-
/**
145-
* Enable/disable GraphQL validate spans (default: true).
146-
*/
147-
graphqlValidate?: BooleanOrPredicate<{ context: unknown }>;
148-
/**
149-
* Enable/disable GraphQL execute spans (default: true).
150-
*
151-
* Disabling the GraphQL execute spans will also disable all other child spans.
152-
*/
153-
graphqlExecute?: BooleanOrPredicate<{ context: unknown }>;
154-
/**
155-
* Enable/disable subgraph execute spans (default: true).
156-
*
157-
* Disabling the subgraph execute spans will also disable all other child spans.
158-
*/
159-
subgraphExecute?: BooleanOrPredicate<{
160-
executionRequest: ExecutionRequest;
161-
subgraphName: string;
162-
}>;
163-
/**
164-
* Enable/disable upstream HTTP fetch calls spans (default: true).
165-
*/
166-
upstreamFetch?: BooleanOrPredicate<{
167-
executionRequest: ExecutionRequest | undefined;
168-
}>;
169-
/**
170-
* Enable/disable schema loading spans (default: true if context manager available).
171-
*
172-
* Note: This span requires an Async compatible context manager
173-
*/
174-
schema?: boolean;
175-
/**
176-
* Enable/disable initialization span (default: true).
177-
*/
178-
initialization?: boolean;
179-
};
180-
events?: {
181-
/**
182-
* Enable/Disable cache related span events (default: true).
183-
*/
184-
cache?: BooleanOrPredicate<{ key: string; action: 'read' | 'write' }>;
185-
};
186-
};
110+
traces?: boolean | TracesConfig;
111+
};
112+
113+
type TracesConfig = {
114+
/**
115+
* Tracer instance to use for creating spans (default: a tracer with name 'gateway').
116+
*/
117+
tracer?: Tracer;
118+
/**
119+
* Options to control which spans to create.
120+
* By default, all spans are enabled.
121+
*
122+
* You may specify a boolean value to enable/disable all spans, or a function to dynamically enable/disable spans based on the input.
123+
*/
124+
spans?: SpansConfig;
125+
events?: {
126+
/**
127+
* Enable/Disable cache related span events (default: true).
128+
*/
129+
cache?: BooleanOrPredicate<{ key: string; action: 'read' | 'write' }>;
130+
};
131+
};
132+
133+
type SpansConfig = {
134+
/**
135+
* Enable/disable HTTP request spans (default: true).
136+
*
137+
* Disabling the HTTP span will also disable all other child spans.
138+
*/
139+
http?: BooleanOrPredicate<{ request: Request }>;
140+
/**
141+
* Enable/disable GraphQL operation spans (default: true).
142+
*
143+
* Disabling the GraphQL operation spa will also disable all other child spans.
144+
*/
145+
graphql?: BooleanOrPredicate<{ context: unknown }>; // FIXME: better type for graphql context
146+
/**
147+
* Enable/disable GraphQL context building phase (default: true).
148+
*/
149+
graphqlContextBuilding?: BooleanOrPredicate<{ context: unknown }>; // FIXME: better type for graphql context
150+
/**
151+
* Enable/disable GraphQL parse spans (default: true).
152+
*/
153+
graphqlParse?: BooleanOrPredicate<{ context: unknown }>; // FIXME: better type for graphql context
154+
/**
155+
* Enable/disable GraphQL validate spans (default: true).
156+
*/
157+
graphqlValidate?: BooleanOrPredicate<{ context: unknown }>;
158+
/**
159+
* Enable/disable GraphQL execute spans (default: true).
160+
*
161+
* Disabling the GraphQL execute spans will also disable all other child spans.
162+
*/
163+
graphqlExecute?: BooleanOrPredicate<{ context: unknown }>;
164+
/**
165+
* Enable/disable subgraph execute spans (default: true).
166+
*
167+
* Disabling the subgraph execute spans will also disable all other child spans.
168+
*/
169+
subgraphExecute?: BooleanOrPredicate<{
170+
executionRequest: ExecutionRequest;
171+
subgraphName: string;
172+
}>;
173+
/**
174+
* Enable/disable upstream HTTP fetch calls spans (default: true).
175+
*/
176+
upstreamFetch?: BooleanOrPredicate<{
177+
executionRequest: ExecutionRequest | undefined;
178+
}>;
179+
/**
180+
* Enable/disable schema loading spans (default: true if context manager available).
181+
*
182+
* Note: This span requires an Async compatible context manager
183+
*/
184+
schema?: boolean;
185+
/**
186+
* Enable/disable initialization span (default: true).
187+
*/
188+
initialization?: boolean;
187189
};
188190

189191
export const otelCtxForRequestId = new Map<string, Context>();
@@ -255,6 +257,19 @@ export function useOpenTelemetry(
255257

256258
pluginLogger?.info('Enabled');
257259

260+
if (options.traces !== false) {
261+
const httpSpanConfig =
262+
options.traces === true ? null : options.traces?.spans?.http;
263+
264+
// Only override http filter if it's not disabled or already a function
265+
if (httpSpanConfig ?? true === true) {
266+
options.traces = {
267+
...(options.traces as object),
268+
spans: { http: defaultHttpFilter },
269+
};
270+
}
271+
}
272+
258273
function isParentEnabled(state: State): boolean {
259274
const parentState = getMostSpecificState(state);
260275
return !parentState || !!parentState.otel;
@@ -959,3 +974,12 @@ function getURL(request: Request) {
959974

960975
return new URL(request.url, 'http://localhost'); // to be iso with whatwg-node/server behavior
961976
}
977+
978+
export const defaultHttpFilter: SpansConfig['http'] = ({ request }) => {
979+
// Ignore Prometheus requests
980+
if (request.url.endsWith('/metrics')) {
981+
return false;
982+
}
983+
984+
return true;
985+
};

0 commit comments

Comments
 (0)