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 Headers from "effect/http/Headers"; |
| 3 | import * as HttpTraceContext from "effect/http/HttpTraceContext"; |
| 4 | import * as Option from "effect/Option"; |
| 5 | import type * as Tracer from "effect/Tracer"; |
| 6 | |
| 7 | /** |
| 8 | * Link a producer context carried in a W3C `traceparent` (e.g. an SQS |
| 9 | * message attribute) to the batch's consumer span. |
| 10 | */ |
| 11 | export const traceparentLink = (traceparent: string | undefined): Option.Option<Tracer.SpanLink> => |
| 12 | traceparent === undefined |
| 13 | ? Option.none() |
| 14 | : HttpTraceContext.fromHeaders(Headers.fromInput({ traceparent })).pipe( |
| 15 | Option.map((span) => ({ span, attributes: {} })), |
| 16 | ); |
| 17 | |
| 18 | /** |
| 19 | * Wrap one delivered event-source batch in a `process <destination>` |
| 20 | * CONSUMER span (OTEL messaging semconv) linked to each record's producer |
| 21 | * context, and log the cause when the handler fails so a whole-batch retry |
| 22 | * is never silent. |
| 23 | */ |
| 24 | export const processBatch = |
| 25 | (options: { |
| 26 | readonly system: string; |
| 27 | readonly destination: string; |
| 28 | readonly count: number; |
| 29 | readonly links?: ReadonlyArray<Tracer.SpanLink>; |
| 30 | }) => |
| 31 | <A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> => |
| 32 | self.pipe( |
| 33 | Effect.tapCause((cause) => |
| 34 | Effect.logError( |
| 35 | `${options.system} batch of ${options.count} from ${options.destination} failed; the whole batch will be retried`, |
| 36 | cause, |
| 37 | ), |
| 38 | ), |
| 39 | Effect.withSpan(`process ${options.destination}`, { |
| 40 | kind: "consumer", |
| 41 | links: options.links, |
| 42 | attributes: { |
| 43 | "messaging.system": options.system, |
| 44 | "messaging.operation.type": "process", |
| 45 | "messaging.operation.name": "process", |
| 46 | "messaging.destination.name": options.destination, |
| 47 | "messaging.batch.message_count": options.count, |
| 48 | }, |
| 49 | }), |
| 50 | ); |