The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Hex from "effect/encoding/Hex"; |
| 3 | import * as Option from "effect/Option"; |
| 4 | import * as Stream from "effect/Stream"; |
| 5 | import * as Tracer from "effect/Tracer"; |
| 6 | |
| 7 | /** |
| 8 | * The span a child process or RPC call runs under, so its records and spans |
| 9 | * join the caller's trace instead of starting their own. |
| 10 | */ |
| 11 | export interface TraceParent { |
| 12 | readonly traceId: string; |
| 13 | readonly spanId: string; |
| 14 | } |
| 15 | |
| 16 | /** Set on alchemy child processes to the trace they report under. */ |
| 17 | export const TRACE_PARENT_ENV = "ALCHEMY_TRACE_PARENT"; |
| 18 | |
| 19 | const fromEnv = (): TraceParent | undefined => { |
| 20 | const [traceId, spanId] = (process.env[TRACE_PARENT_ENV] ?? "").split("-"); |
| 21 | return traceId && spanId ? { traceId, spanId } : undefined; |
| 22 | }; |
| 23 | |
| 24 | /** |
| 25 | * This process's trace, for records logged outside any span (startup, |
| 26 | * shutdown, finalizers): the parent's when it passed one in |
| 27 | * {@link TRACE_PARENT_ENV}, otherwise a new one. It ties a process's |
| 28 | * untraced records together and to its parent's, without a span that would |
| 29 | * stay open for the life of the process. |
| 30 | */ |
| 31 | export const processTrace: TraceParent = fromEnv() ?? { |
| 32 | traceId: Hex.random(32), |
| 33 | spanId: Hex.random(16), |
| 34 | }; |
| 35 | |
| 36 | /** The caller's current span, if it is in one. */ |
| 37 | export const currentTraceParent: Effect.Effect<TraceParent | undefined> = Effect.map( |
| 38 | Effect.option(Effect.currentParentSpan), |
| 39 | Option.match({ |
| 40 | onNone: () => undefined, |
| 41 | onSome: (span) => ({ traceId: span.traceId, spanId: span.spanId }), |
| 42 | }), |
| 43 | ); |
| 44 | |
| 45 | /** |
| 46 | * {@link TRACE_PARENT_ENV} for a child process: the current span, or this |
| 47 | * process's trace outside one. |
| 48 | */ |
| 49 | export const traceParentEnv: Effect.Effect<Record<string, string>> = Effect.map( |
| 50 | currentTraceParent, |
| 51 | (parent = processTrace): Record<string, string> => ({ |
| 52 | [TRACE_PARENT_ENV]: `${parent.traceId}-${parent.spanId}`, |
| 53 | }), |
| 54 | ); |
| 55 | |
| 56 | const spanOf = (parent: TraceParent) => Tracer.externalSpan({ ...parent, sampled: true }); |
| 57 | |
| 58 | /** Run `effect` as a child of `parent`, when there is one. */ |
| 59 | export const withTraceParent = |
| 60 | (parent: TraceParent | undefined) => |
| 61 | <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> => |
| 62 | parent === undefined ? effect : Effect.withParentSpan(effect, spanOf(parent)); |
| 63 | |
| 64 | /** Run `stream` (its whole execution, not just its construction) as a child of `parent`. */ |
| 65 | export const withStreamTraceParent = |
| 66 | (parent: TraceParent | undefined) => |
| 67 | <A, E, R>(stream: Stream.Stream<A, E, R>): Stream.Stream<A, E, R> => |
| 68 | parent === undefined |
| 69 | ? stream |
| 70 | : Stream.provideService(stream, Tracer.ParentSpan, spanOf(parent)); |