-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[otel] rely on context for parenting spans correctly
- Loading branch information
1 parent
89c2839
commit 6faa867
Showing
9 changed files
with
872 additions
and
385 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
.changeset/@graphql-mesh_plugin-opentelemetry-532-dependencies.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@graphql-mesh/plugin-opentelemetry': patch | ||
--- | ||
|
||
dependencies updates: | ||
|
||
- Added dependency [`@opentelemetry/context-async-hooks@^1.30.1` ↗︎](https://www.npmjs.com/package/@opentelemetry/context-async-hooks/v/1.30.1) (to `dependencies`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { trace, type Context } from '@opentelemetry/api'; | ||
|
||
type Node = { | ||
ctx: Context; | ||
previous?: Node; | ||
}; | ||
|
||
export class OtelContextStack { | ||
#root: Node; | ||
#current: Node; | ||
|
||
constructor(root: Context) { | ||
this.#root = { ctx: root }; | ||
this.#current = this.#root; | ||
} | ||
|
||
get current(): Context { | ||
return this.#current.ctx; | ||
} | ||
|
||
get root(): Context { | ||
return this.#root.ctx; | ||
} | ||
|
||
push = (ctx: Context) => { | ||
this.#current = { ctx, previous: this.#current }; | ||
}; | ||
|
||
pop = () => { | ||
this.#current = this.#current.previous ?? this.#root; | ||
}; | ||
|
||
toString() { | ||
let node: Node | undefined = this.#current; | ||
const names = []; | ||
while (node != undefined) { | ||
names.push((trace.getSpan(node.ctx) as unknown as { name: string }).name); | ||
node = node.previous; | ||
} | ||
return names.join(' -> '); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import type { ExecutionRequest } from '@graphql-tools/utils'; | ||
|
||
export function withState< | ||
P, | ||
GraphqlState = object, | ||
HttpState = object, | ||
SubExecState = object, | ||
>(plugin: WithState<P, HttpState, GraphqlState, SubExecState>): P { | ||
const states: { | ||
forRequest?: WeakMap<Request, Partial<HttpState>>; | ||
forOperation?: WeakMap<any, Partial<GraphqlState>>; | ||
forSubgraphExecution?: WeakMap<ExecutionRequest, Partial<SubExecState>>; | ||
} = {}; | ||
|
||
function getProp(scope: keyof typeof states, key: any): PropertyDescriptor { | ||
return { | ||
get() { | ||
if (!states[scope]) states[scope] = new WeakMap<any, any>(); | ||
let value = states[scope].get(key as any); | ||
if (!value) states[scope].set(key, (value = {})); | ||
return value; | ||
}, | ||
enumerable: true, | ||
}; | ||
} | ||
|
||
const pluginWithState: Record<string, (payload: any) => unknown> = {}; | ||
for (const [hookName, hook] of Object.entries(plugin) as any) { | ||
pluginWithState[hookName] = (payload) => | ||
hook({ | ||
...payload, | ||
get state() { | ||
let { executionRequest, context, request } = payload; | ||
|
||
const state = {}; | ||
const defineState = (scope: keyof typeof states, key: any) => | ||
Object.defineProperty(state, scope, getProp(scope, key)); | ||
|
||
if (executionRequest) { | ||
defineState('forSubgraphExecution', executionRequest); | ||
if (executionRequest.context) context = executionRequest.context; | ||
} | ||
if (context) { | ||
defineState('forOperation', context); | ||
if (context.request) request = context.request; | ||
} | ||
if (request) { | ||
defineState('forRequest', request); | ||
} | ||
return state; | ||
}, | ||
}); | ||
} | ||
|
||
return pluginWithState as P; | ||
} | ||
|
||
export type HttpState<T> = { | ||
forRequest: Partial<T>; | ||
}; | ||
|
||
export type GraphQLState<T> = { | ||
forOperation: Partial<T>; | ||
}; | ||
|
||
export type GatewayState<T> = { | ||
forSubgraphExecution: Partial<T>; | ||
}; | ||
|
||
export function getMostSpecificState<T>( | ||
state: Partial<HttpState<T> & GraphQLState<T> & GatewayState<T>> = {}, | ||
): Partial<T> | undefined { | ||
const { forOperation, forRequest, forSubgraphExecution } = state; | ||
return forSubgraphExecution ?? forOperation ?? forRequest; | ||
} | ||
|
||
// Brace yourself! TS Wizardry is coming! | ||
|
||
type PayloadWithState<T, Http, GraphQL, Gateway> = T extends { | ||
executionRequest: any; | ||
} | ||
? T & { | ||
state: Partial<HttpState<Http> & GraphQLState<GraphQL>> & | ||
GatewayState<Gateway>; | ||
} | ||
: T extends { | ||
executionRequest?: any; | ||
} | ||
? T & { | ||
state: Partial< | ||
HttpState<Http> & GraphQLState<GraphQL> & GatewayState<Gateway> | ||
>; | ||
} | ||
: T extends { context: any } | ||
? T & { state: HttpState<Http> & GraphQLState<GraphQL> } | ||
: T extends { request: any } | ||
? T & { state: HttpState<Http> } | ||
: T; | ||
|
||
type WithState<P, Http = object, GraphQL = object, Gateway = object> = { | ||
[K in keyof P]: P[K] extends ((payload: infer T) => infer R) | undefined | ||
? (payload: PayloadWithState<T, Http, GraphQL, Gateway>) => R | undefined | ||
: P[K]; | ||
}; |
Oops, something went wrong.