/** * Telemetry shared by every distilled SDK. * * operation spans (always): one span per operation call, around every retry. * Its name, kind and attributes come from the model's * `com.distilled.telemetry` traits (see * `packages/dev/src/codegen/telemetry-traits.md`), carried by the generated * operation's `telemetry` and `span` config, plus the * request's host and method and the response's status. It * replaces Effect's HTTP span. Retries are attributes on it * (attempts, errors, total backoff). A failure gets the * user's call site as a stack frame, so `Cause.pretty` * shows it. * debug (opt in): spans for distilled's internals too, INTERNAL spans * at trace level `Debug`: first-call setup, serialize, sign, * deserialize, one `Attempt` per try, backoff sleeps, and the * caller's `code.*` attributes; and `Effect.logDebug` lines * for each call and HTTP exchange, the same for every SDK, * with or without an exporter. Its code lives in * `./telemetry-debug.ts`, so only programs that import it * bundle it: * * ```ts * import { TelemetryDebug } from "@distilled.cloud/core/telemetry-debug"; * program.pipe(Effect.provide(TelemetryDebug.layer)); * ``` * * No span is recorded unless the app installs a tracer (an exporter). */ import * as Cause from "effect/Cause"; import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import type * as Exit from "effect/Exit"; import * as Fiber from "effect/Fiber"; import * as HttpClient from "effect/http/HttpClient"; import type * as HttpClientError from "effect/http/HttpClientError"; import * as HttpClientRequest from "effect/http/HttpClientRequest"; import type * as HttpClientResponse from "effect/http/HttpClientResponse"; import * as Redacted from "effect/Redacted"; import * as References from "effect/References"; import type * as AST from "effect/SchemaAST"; import type * as Stream from "effect/Stream"; import * as Tracer from "effect/Tracer"; import { getAnn, getProps } from "./annotations.ts"; /** * Debug mode's implementation, provided by `TelemetryDebug.layer` * (`./telemetry-debug.ts`); undefined, the default, is operation spans only. * The per-call path only checks whether it is set. */ export interface DebugHooks { /** An INTERNAL span around one of distilled's steps. */ readonly phase: ( name: string, effect: Effect.Effect, attributes?: Record, ) => Effect.Effect; /** Try `n` (1-based) as its own `Attempt` span. */ readonly attempt: (base: Effect.Effect, n: number) => Effect.Effect; /** The caller's read of a streamed output member, as a span under `parent`. */ readonly readStream: ( stream: Stream.Stream, parent: Tracer.Span, member: string, ) => Stream.Stream; /** * The operation call with its debug log lines: `Effect.logDebug` at the * start (with the input) and on exit (with the output), every line inside * annotated with the operation. With `schemas`, input and output are * logged with their sensitive values `Redacted` (`./sensitive.ts`); * without, they are left out. */ readonly call: ( info: OperationInfo, input: unknown, schemas: OperationSchemas | undefined, effect: Effect.Effect, ) => Effect.Effect; /** One HTTP exchange (`sent`) with its debug log lines. */ readonly http: ( request: HttpClientRequest.HttpClientRequest, sent: Effect.Effect, ) => Effect.Effect; /** OTel `code.*` attributes for the caller's line. */ readonly callSite: ( callSite: (() => string | undefined) | undefined, ) => Record | undefined; } /** An operation's input and output schemas, for the debug log lines. */ export interface OperationSchemas { readonly input: AST.AST; readonly output: AST.AST; } export const Debug = /*@__PURE__*/ Context.Reference( "@distilled.cloud/core/Telemetry/Debug", { defaultValue: () => undefined }, ); /** * The traced call running on this fiber: its operation span, the debug-mode * attempt span of the current try, and the retry history so far. Protocols * write through {@link annotate} synchronously, without an extra effect. */ export interface Call { readonly span: Tracer.Span; readonly info: OperationInfo; attempt: Tracer.Span | undefined; readonly retryErrors: Array; retryDelayMs: number; } export const OperationSpan = /*@__PURE__*/ Context.Reference( "@distilled.cloud/core/Telemetry/OperationSpan", { defaultValue: () => undefined }, ); /** * The untraced call's propagation (no exporter, so no operation span): the * caller's parent span and the service's header format. Requests carry the * parent's own trace context, as an OTel no-op tracer forwards it. */ interface Forward { readonly span: Tracer.AnySpan; readonly propagate: NonNullable; } const Forward = /*@__PURE__*/ Context.Reference( "@distilled.cloud/core/Telemetry/Forward", { defaultValue: () => undefined }, ); //#region traits /** * How a value becomes an attribute value (`com.distilled.telemetry` * transforms; see `packages/dev/src/codegen/telemetry-traits.md`). */ export type Transform = | "list" | "keys" | "count" | "lastPathSegment" | "arnResource" | "arn" | "jsonList" | "nonZero"; export interface AttributeRule { readonly name: string; /** Dotted path into the member's value, read before the transform. */ readonly field?: string; readonly transform?: Transform; } /** * Extra facts for a request, by host (`hostRules` in * `./telemetry-host-rules.ts`): `set` records an attribute on the span. */ export type RequestHook = ( host: string, span: Tracer.Span, set: (key: string, value: unknown) => void, ) => void; /** The `com.distilled.telemetry#service` trait, as generated code carries it. */ export interface ServiceTelemetry { readonly system: string; readonly attributes?: Readonly>; readonly responseHeaders?: ReadonlyArray; /** From the trait's `hosts`. */ readonly onRequest?: RequestHook; /** * The rules every operation of this module follows, so its generated * `span` holds only what differs (see {@link operationInfo}): span name * `.`, `rpc.method` `/`, and each path label * recorded as `.