@distilled.cloud/core 1.0.0-rc.16 → 1.0.0-rc.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (306) hide show
  1. package/lib/_bundle/aws.js +2 -0
  2. package/lib/_bundle/aws.js.map +1 -0
  3. package/lib/_bundle/base.js +5 -0
  4. package/lib/_bundle/base.js.map +1 -0
  5. package/lib/_bundle/graphql.js +3 -0
  6. package/lib/_bundle/graphql.js.map +1 -0
  7. package/lib/_bundle/rest.js +2 -0
  8. package/lib/_bundle/rest.js.map +1 -0
  9. package/lib/_bundle/xml.js +4 -0
  10. package/lib/_bundle/xml.js.map +1 -0
  11. package/lib/annotations.d.ts +15 -0
  12. package/lib/annotations.d.ts.map +1 -0
  13. package/lib/annotations.js +2 -0
  14. package/lib/annotations.js.map +1 -0
  15. package/lib/api.d.ts +15 -0
  16. package/lib/api.d.ts.map +1 -1
  17. package/lib/api.js +1 -796
  18. package/lib/category.d.ts.map +1 -1
  19. package/lib/category.js +1 -278
  20. package/lib/chunks.js +1 -11
  21. package/lib/config.d.ts +54 -13
  22. package/lib/config.d.ts.map +1 -1
  23. package/lib/config.js +1 -161
  24. package/lib/crypto.js +1 -130
  25. package/lib/debug.d.ts.map +1 -1
  26. package/lib/debug.js +1 -8
  27. package/lib/error-category.d.ts.map +1 -1
  28. package/lib/error-category.js +1 -47
  29. package/lib/errors.d.ts +2 -13
  30. package/lib/errors.d.ts.map +1 -1
  31. package/lib/errors.js +1 -198
  32. package/lib/graphql.d.ts +14 -0
  33. package/lib/graphql.d.ts.map +1 -1
  34. package/lib/graphql.js +1 -1058
  35. package/lib/graphql.types.js +1 -6
  36. package/lib/index.d.ts +3 -2
  37. package/lib/index.d.ts.map +1 -1
  38. package/lib/index.js +1 -15
  39. package/lib/pagination.d.ts +34 -2
  40. package/lib/pagination.d.ts.map +1 -1
  41. package/lib/pagination.js +1 -271
  42. package/lib/plain-values.js +1 -39
  43. package/lib/plain-values.js.map +1 -1
  44. package/lib/protocol-http.d.ts +30 -6
  45. package/lib/protocol-http.d.ts.map +1 -1
  46. package/lib/protocol-http.js +1 -812
  47. package/lib/protocol-rest.d.ts +53 -39
  48. package/lib/protocol-rest.d.ts.map +1 -1
  49. package/lib/protocol-rest.js +1 -375
  50. package/lib/query.js +1 -27
  51. package/lib/response-validation-strict.js +1 -284
  52. package/lib/response-validation.d.ts +4 -3
  53. package/lib/response-validation.d.ts.map +1 -1
  54. package/lib/response-validation.js +1 -118
  55. package/lib/retry-after.js +1 -112
  56. package/lib/retry.js +1 -197
  57. package/lib/schema-lite-impl.js +1 -341
  58. package/lib/schema-lite-typed.js +1 -27
  59. package/lib/schema-lite.js +1 -31
  60. package/lib/schema.js +1 -220
  61. package/lib/telemetry-debug.d.ts +14 -0
  62. package/lib/telemetry-debug.d.ts.map +1 -0
  63. package/lib/telemetry-debug.js +2 -0
  64. package/lib/telemetry-debug.js.map +1 -0
  65. package/lib/telemetry-hooks.d.ts +32 -0
  66. package/lib/telemetry-hooks.d.ts.map +1 -0
  67. package/lib/telemetry-hooks.js +2 -0
  68. package/lib/telemetry-hooks.js.map +1 -0
  69. package/lib/telemetry-host-rules.d.ts +23 -0
  70. package/lib/telemetry-host-rules.d.ts.map +1 -0
  71. package/lib/telemetry-host-rules.js +2 -0
  72. package/lib/telemetry-host-rules.js.map +1 -0
  73. package/lib/telemetry-lambda-context.d.ts +17 -0
  74. package/lib/telemetry-lambda-context.d.ts.map +1 -0
  75. package/lib/telemetry-lambda-context.js +2 -0
  76. package/lib/telemetry-lambda-context.js.map +1 -0
  77. package/lib/telemetry-message-context.d.ts +52 -0
  78. package/lib/telemetry-message-context.d.ts.map +1 -0
  79. package/lib/telemetry-message-context.js +2 -0
  80. package/lib/telemetry-message-context.js.map +1 -0
  81. package/lib/telemetry-metrics.d.ts +6 -0
  82. package/lib/telemetry-metrics.d.ts.map +1 -0
  83. package/lib/telemetry-metrics.js +2 -0
  84. package/lib/telemetry-metrics.js.map +1 -0
  85. package/lib/telemetry-name-template.d.ts +13 -0
  86. package/lib/telemetry-name-template.d.ts.map +1 -0
  87. package/lib/telemetry-name-template.js +2 -0
  88. package/lib/telemetry-name-template.js.map +1 -0
  89. package/lib/telemetry-stream.d.ts +9 -0
  90. package/lib/telemetry-stream.d.ts.map +1 -0
  91. package/lib/telemetry-stream.js +2 -0
  92. package/lib/telemetry-stream.js.map +1 -0
  93. package/lib/telemetry-xray.d.ts +28 -0
  94. package/lib/telemetry-xray.d.ts.map +1 -0
  95. package/lib/telemetry-xray.js +2 -0
  96. package/lib/telemetry-xray.js.map +1 -0
  97. package/lib/telemetry.d.ts +269 -0
  98. package/lib/telemetry.d.ts.map +1 -0
  99. package/lib/telemetry.js +3 -0
  100. package/lib/telemetry.js.map +1 -0
  101. package/lib/trait.js +1 -131
  102. package/lib/xml.js +1 -323
  103. package/package.json +1 -3
  104. package/src/annotations.ts +37 -0
  105. package/src/api.ts +130 -30
  106. package/src/category.ts +21 -22
  107. package/src/config.ts +58 -29
  108. package/src/debug.ts +9 -6
  109. package/src/error-category.ts +12 -14
  110. package/src/errors.ts +7 -7
  111. package/src/graphql.ts +123 -18
  112. package/src/index.ts +3 -2
  113. package/src/pagination.ts +119 -6
  114. package/src/protocol-http.ts +63 -43
  115. package/src/protocol-rest.ts +201 -140
  116. package/src/response-validation.ts +4 -3
  117. package/src/telemetry-debug.ts +135 -0
  118. package/src/telemetry-hooks.ts +33 -0
  119. package/src/telemetry-host-rules.ts +39 -0
  120. package/src/telemetry-lambda-context.ts +46 -0
  121. package/src/telemetry-message-context.ts +170 -0
  122. package/src/telemetry-metrics.ts +69 -0
  123. package/src/telemetry-name-template.ts +26 -0
  124. package/src/telemetry-stream.ts +91 -0
  125. package/src/telemetry-xray.ts +66 -0
  126. package/src/telemetry.ts +768 -0
  127. package/lib/api.js.map +0 -1
  128. package/lib/category.js.map +0 -1
  129. package/lib/chunks.js.map +0 -1
  130. package/lib/codegen/boolean-string-enums.d.ts +0 -36
  131. package/lib/codegen/boolean-string-enums.d.ts.map +0 -1
  132. package/lib/codegen/boolean-string-enums.js +0 -147
  133. package/lib/codegen/boolean-string-enums.js.map +0 -1
  134. package/lib/codegen/chunk.d.ts +0 -7
  135. package/lib/codegen/chunk.d.ts.map +0 -1
  136. package/lib/codegen/chunk.js +0 -139
  137. package/lib/codegen/chunk.js.map +0 -1
  138. package/lib/codegen/cli.d.ts +0 -80
  139. package/lib/codegen/cli.d.ts.map +0 -1
  140. package/lib/codegen/cli.js +0 -166
  141. package/lib/codegen/cli.js.map +0 -1
  142. package/lib/codegen/datafy.d.ts +0 -14
  143. package/lib/codegen/datafy.d.ts.map +0 -1
  144. package/lib/codegen/datafy.js +0 -853
  145. package/lib/codegen/datafy.js.map +0 -1
  146. package/lib/codegen/dedupe.d.ts +0 -5
  147. package/lib/codegen/dedupe.d.ts.map +0 -1
  148. package/lib/codegen/dedupe.js +0 -153
  149. package/lib/codegen/dedupe.js.map +0 -1
  150. package/lib/codegen/emit.d.ts +0 -125
  151. package/lib/codegen/emit.d.ts.map +0 -1
  152. package/lib/codegen/emit.js +0 -99
  153. package/lib/codegen/emit.js.map +0 -1
  154. package/lib/codegen/format.d.ts +0 -11
  155. package/lib/codegen/format.d.ts.map +0 -1
  156. package/lib/codegen/format.js +0 -32
  157. package/lib/codegen/format.js.map +0 -1
  158. package/lib/codegen/generated-at.d.ts +0 -29
  159. package/lib/codegen/generated-at.d.ts.map +0 -1
  160. package/lib/codegen/generated-at.js +0 -78
  161. package/lib/codegen/generated-at.js.map +0 -1
  162. package/lib/codegen/generator.d.ts +0 -353
  163. package/lib/codegen/generator.d.ts.map +0 -1
  164. package/lib/codegen/generator.js +0 -810
  165. package/lib/codegen/generator.js.map +0 -1
  166. package/lib/codegen/graph.d.ts +0 -36
  167. package/lib/codegen/graph.d.ts.map +0 -1
  168. package/lib/codegen/graph.js +0 -136
  169. package/lib/codegen/graph.js.map +0 -1
  170. package/lib/codegen/graphql-client.d.ts +0 -70
  171. package/lib/codegen/graphql-client.d.ts.map +0 -1
  172. package/lib/codegen/graphql-client.js +0 -439
  173. package/lib/codegen/graphql-client.js.map +0 -1
  174. package/lib/codegen/graphql.d.ts +0 -207
  175. package/lib/codegen/graphql.d.ts.map +0 -1
  176. package/lib/codegen/graphql.js +0 -786
  177. package/lib/codegen/graphql.js.map +0 -1
  178. package/lib/codegen/latin1.d.ts +0 -3
  179. package/lib/codegen/latin1.d.ts.map +0 -1
  180. package/lib/codegen/latin1.js +0 -126
  181. package/lib/codegen/latin1.js.map +0 -1
  182. package/lib/codegen/members.d.ts +0 -32
  183. package/lib/codegen/members.d.ts.map +0 -1
  184. package/lib/codegen/members.js +0 -62
  185. package/lib/codegen/members.js.map +0 -1
  186. package/lib/codegen/model-diff.d.ts +0 -10
  187. package/lib/codegen/model-diff.d.ts.map +0 -1
  188. package/lib/codegen/model-diff.js +0 -67
  189. package/lib/codegen/model-diff.js.map +0 -1
  190. package/lib/codegen/naming.d.ts +0 -29
  191. package/lib/codegen/naming.d.ts.map +0 -1
  192. package/lib/codegen/naming.js +0 -74
  193. package/lib/codegen/naming.js.map +0 -1
  194. package/lib/codegen/openapi-cli.d.ts +0 -52
  195. package/lib/codegen/openapi-cli.d.ts.map +0 -1
  196. package/lib/codegen/openapi-cli.js +0 -84
  197. package/lib/codegen/openapi-cli.js.map +0 -1
  198. package/lib/codegen/openapi.d.ts +0 -209
  199. package/lib/codegen/openapi.d.ts.map +0 -1
  200. package/lib/codegen/openapi.js +0 -1548
  201. package/lib/codegen/openapi.js.map +0 -1
  202. package/lib/codegen/operations.d.ts +0 -24
  203. package/lib/codegen/operations.d.ts.map +0 -1
  204. package/lib/codegen/operations.js +0 -56
  205. package/lib/codegen/operations.js.map +0 -1
  206. package/lib/codegen/pagination.d.ts +0 -39
  207. package/lib/codegen/pagination.d.ts.map +0 -1
  208. package/lib/codegen/pagination.js +0 -31
  209. package/lib/codegen/pagination.js.map +0 -1
  210. package/lib/codegen/patch-audit.d.ts +0 -84
  211. package/lib/codegen/patch-audit.d.ts.map +0 -1
  212. package/lib/codegen/patch-audit.js +0 -366
  213. package/lib/codegen/patch-audit.js.map +0 -1
  214. package/lib/codegen/patches.d.ts +0 -129
  215. package/lib/codegen/patches.d.ts.map +0 -1
  216. package/lib/codegen/patches.js +0 -472
  217. package/lib/codegen/patches.js.map +0 -1
  218. package/lib/codegen/prelude.d.ts +0 -15
  219. package/lib/codegen/prelude.d.ts.map +0 -1
  220. package/lib/codegen/prelude.js +0 -60
  221. package/lib/codegen/prelude.js.map +0 -1
  222. package/lib/codegen/privatize.d.ts +0 -21
  223. package/lib/codegen/privatize.d.ts.map +0 -1
  224. package/lib/codegen/privatize.js +0 -176
  225. package/lib/codegen/privatize.js.map +0 -1
  226. package/lib/codegen/proto.d.ts +0 -121
  227. package/lib/codegen/proto.d.ts.map +0 -1
  228. package/lib/codegen/proto.js +0 -960
  229. package/lib/codegen/proto.js.map +0 -1
  230. package/lib/codegen/pure.d.ts +0 -6
  231. package/lib/codegen/pure.d.ts.map +0 -1
  232. package/lib/codegen/pure.js +0 -91
  233. package/lib/codegen/pure.js.map +0 -1
  234. package/lib/codegen/rewrite-operation-ids.d.ts +0 -154
  235. package/lib/codegen/rewrite-operation-ids.d.ts.map +0 -1
  236. package/lib/codegen/rewrite-operation-ids.js +0 -1117
  237. package/lib/codegen/rewrite-operation-ids.js.map +0 -1
  238. package/lib/codegen/spec-path.d.ts +0 -16
  239. package/lib/codegen/spec-path.d.ts.map +0 -1
  240. package/lib/codegen/spec-path.js +0 -101
  241. package/lib/codegen/spec-path.js.map +0 -1
  242. package/lib/codegen/table.d.ts +0 -5
  243. package/lib/codegen/table.d.ts.map +0 -1
  244. package/lib/codegen/table.js +0 -249
  245. package/lib/codegen/table.js.map +0 -1
  246. package/lib/config.js.map +0 -1
  247. package/lib/crypto.js.map +0 -1
  248. package/lib/debug.js.map +0 -1
  249. package/lib/error-category.js.map +0 -1
  250. package/lib/errors.js.map +0 -1
  251. package/lib/graphql.js.map +0 -1
  252. package/lib/graphql.types.js.map +0 -1
  253. package/lib/index.js.map +0 -1
  254. package/lib/json-patch.d.ts +0 -58
  255. package/lib/json-patch.d.ts.map +0 -1
  256. package/lib/json-patch.js +0 -227
  257. package/lib/json-patch.js.map +0 -1
  258. package/lib/pagination.js.map +0 -1
  259. package/lib/protocol-http.js.map +0 -1
  260. package/lib/protocol-rest.js.map +0 -1
  261. package/lib/query.js.map +0 -1
  262. package/lib/response-validation-strict.js.map +0 -1
  263. package/lib/response-validation.js.map +0 -1
  264. package/lib/retry-after.js.map +0 -1
  265. package/lib/retry.js.map +0 -1
  266. package/lib/schema-lite-impl.js.map +0 -1
  267. package/lib/schema-lite-typed.js.map +0 -1
  268. package/lib/schema-lite.js.map +0 -1
  269. package/lib/schema.js.map +0 -1
  270. package/lib/testing.d.ts +0 -40
  271. package/lib/testing.d.ts.map +0 -1
  272. package/lib/testing.js +0 -42
  273. package/lib/testing.js.map +0 -1
  274. package/lib/trait.js.map +0 -1
  275. package/lib/xml.js.map +0 -1
  276. package/src/codegen/boolean-string-enums.ts +0 -155
  277. package/src/codegen/chunk.ts +0 -147
  278. package/src/codegen/cli.ts +0 -293
  279. package/src/codegen/datafy.ts +0 -874
  280. package/src/codegen/dedupe.ts +0 -173
  281. package/src/codegen/emit.ts +0 -200
  282. package/src/codegen/format.ts +0 -49
  283. package/src/codegen/generated-at.ts +0 -88
  284. package/src/codegen/generator.ts +0 -1229
  285. package/src/codegen/graph.ts +0 -151
  286. package/src/codegen/graphql-client.ts +0 -545
  287. package/src/codegen/graphql.ts +0 -1145
  288. package/src/codegen/latin1.ts +0 -131
  289. package/src/codegen/members.ts +0 -79
  290. package/src/codegen/model-diff.ts +0 -67
  291. package/src/codegen/naming.ts +0 -85
  292. package/src/codegen/openapi-cli.ts +0 -141
  293. package/src/codegen/openapi.ts +0 -1845
  294. package/src/codegen/operations.ts +0 -73
  295. package/src/codegen/pagination.ts +0 -64
  296. package/src/codegen/patch-audit.ts +0 -480
  297. package/src/codegen/patches.ts +0 -581
  298. package/src/codegen/prelude.ts +0 -70
  299. package/src/codegen/privatize.ts +0 -200
  300. package/src/codegen/proto.ts +0 -1092
  301. package/src/codegen/pure.ts +0 -94
  302. package/src/codegen/rewrite-operation-ids.ts +0 -1219
  303. package/src/codegen/spec-path.ts +0 -112
  304. package/src/codegen/table.ts +0 -254
  305. package/src/json-patch.ts +0 -273
  306. package/src/testing.ts +0 -74
@@ -0,0 +1,768 @@
1
+ /**
2
+ * Telemetry shared by every distilled SDK.
3
+ *
4
+ * operation spans (always): one span per operation call, around every retry.
5
+ * Its name, kind and attributes come from the model's
6
+ * `com.distilled.telemetry` traits (see
7
+ * `packages/dev/src/codegen/telemetry-traits.md`), carried by the generated
8
+ * operation's `telemetry` and `span` config, plus the
9
+ * request's host and method and the response's status. It
10
+ * replaces Effect's HTTP span. Retries are attributes on it
11
+ * (attempts, errors, total backoff). A failure gets the
12
+ * user's call site as a stack frame, so `Cause.pretty`
13
+ * shows it.
14
+ * debug (opt in): spans for distilled's internals too, INTERNAL spans
15
+ * at trace level `Debug`: first-call setup, serialize, sign,
16
+ * deserialize, one `Attempt` per try, backoff sleeps, and the
17
+ * caller's `code.*` attributes. Its code lives in
18
+ * `./telemetry-debug.ts`, so only programs that import it
19
+ * bundle it:
20
+ *
21
+ * ```ts
22
+ * import { TelemetryDebug } from "@distilled.cloud/core/telemetry-debug";
23
+ * program.pipe(Effect.provide(TelemetryDebug.layer));
24
+ * ```
25
+ *
26
+ * No span is recorded unless the app installs a tracer (an exporter).
27
+ */
28
+ import * as Cause from "effect/Cause";
29
+ import * as Context from "effect/Context";
30
+ import * as Effect from "effect/Effect";
31
+ import type * as Exit from "effect/Exit";
32
+ import * as Fiber from "effect/Fiber";
33
+ import * as HttpClient from "effect/http/HttpClient";
34
+ import type * as HttpClientError from "effect/http/HttpClientError";
35
+ import * as HttpClientRequest from "effect/http/HttpClientRequest";
36
+ import type * as HttpClientResponse from "effect/http/HttpClientResponse";
37
+ import * as Redacted from "effect/Redacted";
38
+ import * as References from "effect/References";
39
+ import type * as AST from "effect/SchemaAST";
40
+ import type * as Stream from "effect/Stream";
41
+ import * as Tracer from "effect/Tracer";
42
+ import { getAnn, getProps } from "./annotations.ts";
43
+
44
+ /**
45
+ * Debug mode's implementation, provided by `TelemetryDebug.layer`
46
+ * (`./telemetry-debug.ts`); undefined, the default, is operation spans only.
47
+ * The per-call path only checks whether it is set.
48
+ */
49
+ export interface DebugHooks {
50
+ /** An INTERNAL span around one of distilled's steps. */
51
+ readonly phase: <A, E, R>(
52
+ name: string,
53
+ effect: Effect.Effect<A, E, R>,
54
+ attributes?: Record<string, unknown>,
55
+ ) => Effect.Effect<A, E, R>;
56
+ /** Try `n` (1-based) as its own `Attempt` span. */
57
+ readonly attempt: <A, E, R>(base: Effect.Effect<A, E, R>, n: number) => Effect.Effect<A, E, R>;
58
+ /** The caller's read of a streamed output member, as a span under `parent`. */
59
+ readonly readStream: <A, E, R>(
60
+ stream: Stream.Stream<A, E, R>,
61
+ parent: Tracer.Span,
62
+ member: string,
63
+ ) => Stream.Stream<A, E, R>;
64
+ /** Log each request and response line to stderr (`./debug.ts`). */
65
+ readonly logRequests: boolean;
66
+ /** OTel `code.*` attributes for the caller's line. */
67
+ readonly callSite: (
68
+ callSite: (() => string | undefined) | undefined,
69
+ ) => Record<string, unknown> | undefined;
70
+ }
71
+
72
+ export const Debug = /*@__PURE__*/ Context.Reference<DebugHooks | undefined>(
73
+ "@distilled.cloud/core/Telemetry/Debug",
74
+ { defaultValue: () => undefined },
75
+ );
76
+
77
+ /**
78
+ * The traced call running on this fiber: its operation span, the debug-mode
79
+ * attempt span of the current try, and the retry history so far. Protocols
80
+ * write through {@link annotate} synchronously, without an extra effect.
81
+ */
82
+ export interface Call {
83
+ readonly span: Tracer.Span;
84
+ readonly info: OperationInfo;
85
+ attempt: Tracer.Span | undefined;
86
+ readonly retryErrors: Array<string>;
87
+ retryDelayMs: number;
88
+ }
89
+
90
+ export const OperationSpan = /*@__PURE__*/ Context.Reference<Call | undefined>(
91
+ "@distilled.cloud/core/Telemetry/OperationSpan",
92
+ { defaultValue: () => undefined },
93
+ );
94
+
95
+ //#region traits
96
+
97
+ /**
98
+ * How a value becomes an attribute value (`com.distilled.telemetry`
99
+ * transforms; see `packages/dev/src/codegen/telemetry-traits.md`).
100
+ */
101
+ export type Transform =
102
+ | "list"
103
+ | "keys"
104
+ | "count"
105
+ | "lastPathSegment"
106
+ | "arnResource"
107
+ | "arn"
108
+ | "jsonList"
109
+ | "nonZero";
110
+
111
+ export interface AttributeRule {
112
+ readonly name: string;
113
+ /** Dotted path into the member's value, read before the transform. */
114
+ readonly field?: string;
115
+ readonly transform?: Transform;
116
+ }
117
+
118
+ /**
119
+ * Extra facts for a request, by host (`hostRules` in
120
+ * `./telemetry-host-rules.ts`): `set` records an attribute on the span.
121
+ */
122
+ export type RequestHook = (
123
+ host: string,
124
+ span: Tracer.Span,
125
+ set: (key: string, value: unknown) => void,
126
+ ) => void;
127
+
128
+ /** The `com.distilled.telemetry#service` trait, as generated code carries it. */
129
+ export interface ServiceTelemetry {
130
+ readonly system: string;
131
+ readonly attributes?: Readonly<Record<string, string | number | boolean>>;
132
+ readonly responseHeaders?: ReadonlyArray<AttributeRule & { readonly header: string }>;
133
+ /** From the trait's `hosts`. */
134
+ readonly onRequest?: RequestHook;
135
+ /**
136
+ * The rules every operation of this module follows, so its generated
137
+ * `span` holds only what differs (see {@link operationInfo}): span name
138
+ * `<prefix>.<op>`, `rpc.method` `<service>/<op>`, and each path label
139
+ * recorded as `<labels>.<label in snake_case>`.
140
+ */
141
+ readonly prefix?: string;
142
+ readonly service?: string;
143
+ readonly labels?: string;
144
+ /** Path labels named another way than `<labels>.<snake_case>`, by property. */
145
+ readonly labelNames?: Readonly<Record<string, string>>;
146
+ /** `false`: operations do not derive `url.template` (RPC protocols). */
147
+ readonly templates?: false;
148
+ /**
149
+ * How requests carry the span's trace context, from the trait's
150
+ * `propagation`: `"w3c"`, a `traceparent` header; or a function returning
151
+ * the header to set (X-Ray, `./telemetry-xray.ts`).
152
+ */
153
+ readonly propagate?: "w3c" | ((span: Tracer.AnySpan) => readonly [header: string, value: string]);
154
+ }
155
+
156
+ /** A span name for one call from its input attributes; undefined keeps the trait's `name`. */
157
+ export type SpanRename = (attributes: Readonly<Record<string, unknown>>) => string | undefined;
158
+
159
+ /**
160
+ * Runs an operation call in its span and keeps the span open after the call
161
+ * returns, until the caller has read a streamed output member (the trait's
162
+ * `stream`; built by `./telemetry-stream.ts`). Every other operation uses
163
+ * `Effect.useSpan`, which ends the span on return.
164
+ */
165
+ export type SpanHold = <A, E, R>(
166
+ fiber: Fiber.Fiber<unknown, unknown>,
167
+ name: string,
168
+ options: Tracer.SpanOptionsNoTrace,
169
+ debug: DebugHooks | undefined,
170
+ inSpan: (span: Tracer.Span) => Effect.Effect<A, E, R>,
171
+ ) => Effect.Effect<A, E, R>;
172
+
173
+ /** Read trace context back from an operation's messages (the trait's `extract`). */
174
+ export interface MessageExtract {
175
+ /** The input asking the service to return the trace context. */
176
+ readonly request: (input: unknown) => unknown;
177
+ /** Links to the spans the output's messages name. */
178
+ readonly links: (output: unknown) => ReadonlyArray<Tracer.SpanLink>;
179
+ }
180
+
181
+ /**
182
+ * The `com.distilled.telemetry#operation` trait plus its input members'
183
+ * rules. The trait's `nameTemplate`, `inject` and `extract` arrive as the
184
+ * functions `./telemetry-name-template.ts` and
185
+ * `./telemetry-message-context.ts` build from them.
186
+ */
187
+ export interface SpanConfig {
188
+ /**
189
+ * The operation's model name. With it, whatever this config leaves out
190
+ * follows the service's rules and the input schema's annotations.
191
+ */
192
+ readonly op?: string;
193
+ /** `false`: no `url.template`, whatever the service's rule. */
194
+ readonly template?: false;
195
+ readonly name?: string;
196
+ readonly method?: string;
197
+ readonly kind?: "client" | "producer" | "consumer";
198
+ /** Fixed attributes; `null` drops one the service sets. */
199
+ readonly attributes?: Readonly<Record<string, string | number | boolean | null>>;
200
+ /** `com.distilled.telemetry#attributes` of the input members, by property name. */
201
+ readonly input?: Readonly<Record<string, ReadonlyArray<AttributeRule>>>;
202
+ /**
203
+ * Attributes recorded under a second name once the request is built:
204
+ * `{ "cloud.region": "faas.invoked_region" }`.
205
+ */
206
+ readonly copy?: Readonly<Record<string, string>>;
207
+ /** The same, for output members: recorded when the call succeeds. */
208
+ readonly output?: Readonly<Record<string, ReadonlyArray<AttributeRule>>>;
209
+ readonly rename?: SpanRename;
210
+ /** Write the span's trace context into the request's messages. */
211
+ readonly inject?: (span: Tracer.Span, input: unknown) => unknown;
212
+ readonly extract?: MessageExtract;
213
+ /** Keep the span open until the caller has read a streamed output member. */
214
+ readonly hold?: SpanHold;
215
+ }
216
+
217
+ /** One operation's span facts, built once per operation. */
218
+ export interface OperationInfo {
219
+ /** Span name, e.g. `S3.GetObject`. */
220
+ readonly name: string;
221
+ readonly kind: "client" | "producer" | "consumer";
222
+ /** Fixed attributes: rpc.system.name, rpc.method, service and operation attributes. */
223
+ readonly attributes: Readonly<Record<string, unknown>>;
224
+ /** Input property and rule pairs, flattened. */
225
+ readonly input: ReadonlyArray<readonly [property: string, rule: AttributeRule]>;
226
+ readonly output: ReadonlyArray<readonly [property: string, rule: AttributeRule]>;
227
+ readonly responseHeaders: ReadonlyArray<AttributeRule & { readonly header: string }>;
228
+ readonly onRequest: RequestHook | undefined;
229
+ readonly propagate: ServiceTelemetry["propagate"];
230
+ readonly copy: SpanConfig["copy"];
231
+ readonly rename: SpanRename | undefined;
232
+ readonly inject: SpanConfig["inject"];
233
+ readonly extract: MessageExtract | undefined;
234
+ readonly hold: SpanHold | undefined;
235
+ }
236
+
237
+ const isScalar = (value: unknown): value is string | number | boolean =>
238
+ typeof value === "string" || typeof value === "number" || typeof value === "boolean";
239
+
240
+ const fieldOf = (value: unknown, path: string): unknown => {
241
+ let at = value;
242
+ for (const key of path.split(".")) {
243
+ if (at === null || typeof at !== "object") return undefined;
244
+ at = (at as Record<string, unknown>)[key];
245
+ }
246
+ return at;
247
+ };
248
+
249
+ const transform = (rule: AttributeRule, raw: unknown): unknown => {
250
+ const value = rule.field === undefined ? raw : fieldOf(raw, rule.field);
251
+ if (value === undefined || value === null || Redacted.isRedacted(value)) return undefined;
252
+ switch (rule.transform) {
253
+ case undefined:
254
+ return isScalar(value) || (Array.isArray(value) && value.length > 0 && value.every(isScalar))
255
+ ? value
256
+ : undefined;
257
+ case "nonZero":
258
+ return typeof value === "number" && value !== 0 ? value : undefined;
259
+ case "list":
260
+ return typeof value === "string" ? [value] : undefined;
261
+ case "keys":
262
+ return value !== null && typeof value === "object" ? Object.keys(value) : undefined;
263
+ case "count":
264
+ return Array.isArray(value) ? value.length : undefined;
265
+ case "lastPathSegment":
266
+ return typeof value === "string" ? value.slice(value.lastIndexOf("/") + 1) : undefined;
267
+ case "arnResource":
268
+ return typeof value === "string" ? value.slice(value.lastIndexOf(":") + 1) : undefined;
269
+ case "arn":
270
+ return typeof value === "string" && value.startsWith("arn:") ? value : undefined;
271
+ case "jsonList":
272
+ return Array.isArray(value)
273
+ ? value.map((v) => JSON.stringify(v))
274
+ : typeof value === "object"
275
+ ? [JSON.stringify(value)]
276
+ : undefined;
277
+ }
278
+ };
279
+
280
+ const HTTP = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http");
281
+ const LABEL = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http/label");
282
+
283
+ /** `zoneId` / `zone-id` / `ZoneID` -> `zone_id`, as the trait defaults spell labels. */
284
+ const snakeCase = (name: string): string =>
285
+ name
286
+ .replace(/([a-z0-9])([A-Z])/g, "$1_$2")
287
+ .replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2")
288
+ .replace(/[^A-Za-z0-9]+/g, "_")
289
+ .toLowerCase();
290
+
291
+ /**
292
+ * The span facts of an operation from its generated `telemetry` and `span`
293
+ * config. A `span` naming its `op` (a string is just the op) derives what it
294
+ * leaves out, from the service's rules and the input schema:
295
+ *
296
+ * name `<service.prefix>.<op>`
297
+ * rpc.method `<service.service>/<op>`
298
+ * url.template the path of the input's `T.Http` uri, unless the service
299
+ * or the span says `false`
300
+ * input each `T.Label` property as `<service.labels>.<snake_case>`;
301
+ * an explicit entry replaces it (`[]` records nothing)
302
+ *
303
+ * The generator emits only what differs from these rules, so the result is
304
+ * exactly the model's `com.distilled.telemetry` traits. An operation
305
+ * generated without traits gets its operation name, or
306
+ * `distilled.operation`.
307
+ */
308
+ export const operationInfo = (
309
+ service: ServiceTelemetry | undefined,
310
+ config: SpanConfig | string | undefined,
311
+ operationName: string | undefined,
312
+ inputAst?: AST.AST,
313
+ ): OperationInfo => {
314
+ const span: SpanConfig | undefined = typeof config === "string" ? { op: config } : config;
315
+ const op = span?.op;
316
+ let rules = span?.input;
317
+ let template: string | undefined;
318
+ if (op !== undefined && inputAst !== undefined) {
319
+ if (service?.templates !== false && span?.template !== false) {
320
+ template = (getAnn(inputAst, HTTP) as { readonly uri?: string } | undefined)?.uri?.split(
321
+ "?",
322
+ )[0];
323
+ }
324
+ if (service?.labels !== undefined) {
325
+ const derived: Record<string, ReadonlyArray<AttributeRule>> = {};
326
+ for (const prop of getProps(inputAst)) {
327
+ if (getAnn(prop.type, LABEL) === undefined) continue;
328
+ const property = String(prop.name);
329
+ derived[property] = [
330
+ { name: service.labelNames?.[property] ?? `${service.labels}.${snakeCase(property)}` },
331
+ ];
332
+ }
333
+ rules = { ...derived, ...rules };
334
+ }
335
+ }
336
+ const input = flatten(rules);
337
+ const attributes: Record<string, unknown> = {
338
+ ...(service ? { "rpc.system.name": service.system } : {}),
339
+ ...service?.attributes,
340
+ ...(template !== undefined ? { "url.template": template } : {}),
341
+ ...span?.attributes,
342
+ };
343
+ for (const key in span?.attributes) if (attributes[key] === null) delete attributes[key];
344
+ const method =
345
+ span?.method ??
346
+ (op !== undefined && service?.service !== undefined ? `${service.service}/${op}` : undefined);
347
+ return {
348
+ name:
349
+ span?.name ??
350
+ (op !== undefined && service?.prefix !== undefined ? `${service.prefix}.${op}` : undefined) ??
351
+ operationName ??
352
+ "distilled.operation",
353
+ kind: span?.kind ?? "client",
354
+ attributes: method === undefined ? attributes : { "rpc.method": method, ...attributes },
355
+ input,
356
+ output: flatten(span?.output),
357
+ responseHeaders: service?.responseHeaders ?? [],
358
+ onRequest: service?.onRequest,
359
+ propagate: service?.propagate,
360
+ copy: span?.copy,
361
+ rename: span?.rename,
362
+ inject: span?.inject,
363
+ extract: span?.extract,
364
+ hold: span?.hold,
365
+ };
366
+ };
367
+
368
+ const flatten = (
369
+ rules: Readonly<Record<string, ReadonlyArray<AttributeRule>>> | undefined,
370
+ ): Array<readonly [string, AttributeRule]> => {
371
+ const out: Array<readonly [string, AttributeRule]> = [];
372
+ for (const property in rules) {
373
+ for (const rule of rules[property]!) out.push([property, rule]);
374
+ }
375
+ return out;
376
+ };
377
+
378
+ /** Attributes the rules read from a value (the input or the output). Redacted values are skipped. */
379
+ const readRules = (
380
+ rules: OperationInfo["input"],
381
+ value: unknown,
382
+ set: (key: string, value: unknown) => void,
383
+ ): void => {
384
+ if (rules.length === 0 || value === null || typeof value !== "object") return;
385
+ for (const [property, rule] of rules) {
386
+ const read = transform(rule, (value as Record<string, unknown>)[property]);
387
+ if (read !== undefined) set(rule.name, read);
388
+ }
389
+ };
390
+
391
+ /**
392
+ * An opt-in observer of every operation call (`./telemetry-metrics.ts`):
393
+ * wraps the call, whether or not a span is recorded.
394
+ */
395
+ export type Measure = <A, E, R>(
396
+ info: OperationInfo,
397
+ effect: Effect.Effect<A, E, R>,
398
+ ) => Effect.Effect<A, E, R>;
399
+
400
+ export const Metrics = /*@__PURE__*/ Context.Reference<Measure | undefined>(
401
+ "@distilled.cloud/core/Telemetry/Metrics",
402
+ { defaultValue: () => undefined },
403
+ );
404
+
405
+ /** App callbacks on each recorded operation span (`./telemetry-hooks.ts`). */
406
+ export interface SpanHooks {
407
+ readonly onStart?: (
408
+ span: Tracer.Span,
409
+ call: { readonly name: string; readonly method: unknown; readonly input: unknown },
410
+ ) => void;
411
+ readonly onEnd?: (span: Tracer.Span, exit: Exit.Exit<unknown, unknown>) => void;
412
+ }
413
+
414
+ export const Hooks = /*@__PURE__*/ Context.Reference<SpanHooks | undefined>(
415
+ "@distilled.cloud/core/Telemetry/Hooks",
416
+ { defaultValue: () => undefined },
417
+ );
418
+
419
+ //#endregion
420
+
421
+ export const isDebug = (context: Context.Context<any>): boolean =>
422
+ Context.get(context, Debug) !== undefined;
423
+
424
+ /**
425
+ * Record a fact about the current try (status, request id, host): on the
426
+ * operation span, where the last try wins, and on the debug attempt span.
427
+ */
428
+ export const annotate = (context: Context.Context<any>, key: string, value: unknown): void => {
429
+ const call = Context.get(context, OperationSpan);
430
+ if (call === undefined) return;
431
+ call.span.attribute(key, value);
432
+ call.attempt?.attribute(key, value);
433
+ };
434
+
435
+ /**
436
+ * The request facts, recorded when it is sent (so a call that never gets a
437
+ * response still has them): method, host and port. Host only: the full URL
438
+ * can carry a presigned signature.
439
+ */
440
+ export const annotateRequest = (
441
+ context: Context.Context<any>,
442
+ method: string,
443
+ url: string,
444
+ ): void => {
445
+ if (Context.get(context, OperationSpan) === undefined) return;
446
+ const schemeEnd = url.indexOf("://");
447
+ const hostStart = schemeEnd + 3;
448
+ let hostEnd = hostStart;
449
+ while (hostEnd < url.length && !"/?#".includes(url[hostEnd]!)) hostEnd++;
450
+ const authority = url.slice(hostStart, hostEnd);
451
+ const colon = authority.lastIndexOf(":");
452
+ const hasPort = colon !== -1 && !authority.endsWith("]");
453
+ const host = hasPort ? authority.slice(0, colon) : authority;
454
+ annotate(context, "http.request.method", method);
455
+ annotate(context, "server.address", host);
456
+ annotate(
457
+ context,
458
+ "server.port",
459
+ hasPort ? Number(authority.slice(colon + 1)) : url.startsWith("https") ? 443 : 80,
460
+ );
461
+ const call = Context.get(context, OperationSpan)!;
462
+ call.info.onRequest?.(host, call.span, (key, value) => annotate(context, key, value));
463
+ for (const from in call.info.copy) {
464
+ const value = call.span.attributes.get(from);
465
+ if (value !== undefined) annotate(context, call.info.copy[from]!, value);
466
+ }
467
+ };
468
+
469
+ /**
470
+ * The response facts, for every protocol: status, and the response
471
+ * headers the service trait lists (request ids).
472
+ */
473
+ export const annotateResponse = (
474
+ context: Context.Context<any>,
475
+ status: number,
476
+ headers: Readonly<Record<string, string | undefined>>,
477
+ ): void => {
478
+ const call = Context.get(context, OperationSpan);
479
+ if (call === undefined) return;
480
+ annotate(context, "http.response.status_code", status);
481
+ for (const rule of call.info.responseHeaders) {
482
+ const value = headers[rule.header];
483
+ if (value !== undefined) annotate(context, rule.name, transform(rule, value));
484
+ }
485
+ };
486
+
487
+ /**
488
+ * `client.execute(request)` for the traced call on this fiber: the
489
+ * request facts before it is sent, the response facts when it returns.
490
+ * Untraced calls pay one context read.
491
+ */
492
+ export const execute = (
493
+ context: Context.Context<any>,
494
+ client: HttpClient.HttpClient,
495
+ request: HttpClientRequest.HttpClientRequest,
496
+ ): Effect.Effect<HttpClientResponse.HttpClientResponse, HttpClientError.HttpClientError> => {
497
+ const call = Context.get(context, OperationSpan);
498
+ if (call === undefined) return client.execute(request);
499
+ annotateRequest(context, request.method, request.url);
500
+ const propagate = call.info.propagate;
501
+ if (propagate !== undefined) {
502
+ const span = call.attempt ?? call.span;
503
+ request =
504
+ propagate === "w3c"
505
+ ? HttpClientRequest.setHeader(
506
+ request,
507
+ "traceparent",
508
+ `00-${span.traceId}-${span.spanId}-${span.sampled ? "01" : "00"}`,
509
+ )
510
+ : HttpClientRequest.setHeader(request, ...propagate(span));
511
+ }
512
+ return Effect.map(client.execute(request), (response) => {
513
+ annotateResponse(
514
+ context,
515
+ response.status,
516
+ response.headers as Readonly<Record<string, string | undefined>>,
517
+ );
518
+ return response;
519
+ });
520
+ };
521
+
522
+ //#region retries
523
+
524
+ export const tagOf = (error: unknown): string =>
525
+ (error as { _tag?: string } | null)?._tag ?? "_OTHER";
526
+
527
+ /**
528
+ * Try `n` failed with `error` and the call retries after `delayMs`. The
529
+ * operation span carries the history, so a slow call shows why:
530
+ *
531
+ * distilled.attempts 5 (tries so far, including the next one)
532
+ * distilled.retry.errors ["SlowDown", "SlowDown", "SlowDown", "SlowDown"]
533
+ * distilled.retry.delay_ms 4087 (total backoff)
534
+ *
535
+ * A call that succeeds first time has none of them.
536
+ */
537
+ export const retried = (
538
+ context: Context.Context<any>,
539
+ n: number,
540
+ error: unknown,
541
+ delayMs: number,
542
+ ): void => {
543
+ const call = Context.get(context, OperationSpan);
544
+ if (call === undefined) return;
545
+ call.retryErrors.push(tagOf(error));
546
+ call.retryDelayMs += delayMs;
547
+ call.span.attribute("distilled.attempts", n + 1);
548
+ call.span.attribute("distilled.retry.errors", call.retryErrors.slice());
549
+ call.span.attribute("distilled.retry.delay_ms", Math.round(call.retryDelayMs));
550
+ };
551
+
552
+ //#endregion
553
+
554
+ /** Debug-only internal span around `effect`; `effect` itself otherwise. */
555
+ export const phase = <A, E, R>(
556
+ context: Context.Context<any>,
557
+ name: string,
558
+ effect: Effect.Effect<A, E, R>,
559
+ attributes?: Record<string, unknown>,
560
+ ): Effect.Effect<A, E, R> => {
561
+ const debug = Context.get(context, Debug);
562
+ return debug === undefined ? effect : debug.phase(name, effect, attributes);
563
+ };
564
+
565
+ //#region call site
566
+
567
+ // Frames inside any distilled package (workspace sources or installed).
568
+ const CORE_FRAME =
569
+ /[\\/](?:packages[\\/][^\\/]+[\\/](?:src|lib)[\\/]|@distilled\.cloud[\\/])|node:internal/;
570
+
571
+ /**
572
+ * Capture the caller of an operation, cheaply: `Error.captureStackTrace`
573
+ * drops `entry` (the operation function the user called) and every frame
574
+ * above it, so 2 frames are enough. V8 records them now and formats them
575
+ * only if someone reads `.stack` (an error rendered with `Cause.pretty`, or
576
+ * a debug span exporting `code.*`). Skipped when `Error.stackTraceLimit`
577
+ * is 0, the rule Effect's own spans follow.
578
+ */
579
+ export const captureCallSite = (entry: Function): (() => string | undefined) | undefined => {
580
+ const limit = Error.stackTraceLimit;
581
+ if (limit === 0) return undefined;
582
+ const holder: { stack?: string } = {};
583
+ Error.stackTraceLimit = 2;
584
+ if (typeof Error.captureStackTrace === "function") Error.captureStackTrace(holder, entry);
585
+ else holder.stack = new Error().stack;
586
+ Error.stackTraceLimit = limit;
587
+ let cache: string | undefined | null = null;
588
+ return () => {
589
+ if (cache !== null) return cache;
590
+ const lines = holder.stack?.split("\n") ?? [];
591
+ // Skip the "Error" line and any frame still inside distilled.
592
+ cache = lines
593
+ .slice(1)
594
+ .find((line) => !CORE_FRAME.test(line))
595
+ ?.trim();
596
+ return cache;
597
+ };
598
+ };
599
+
600
+ /**
601
+ * Build typed errors without a JS stack: their own
602
+ * frames all point into distilled's response decoder (noise), and the
603
+ * failure frame supplies the caller's line instead. Saves the frame capture
604
+ * on every error. Defects keep their stacks. Use inline:
605
+ *
606
+ * const saved = suppressStack();
607
+ * try { error = new Cls(...) } finally { restoreStack(saved) }
608
+ */
609
+ export const suppressStack = (): number | undefined => {
610
+ const limit = Error.stackTraceLimit;
611
+ if (limit === 0) return undefined;
612
+ Error.stackTraceLimit = 0;
613
+ return limit;
614
+ };
615
+
616
+ export const restoreStack = (saved: number | undefined): void => {
617
+ if (saved !== undefined) Error.stackTraceLimit = saved;
618
+ };
619
+
620
+ //#endregion
621
+
622
+ //#region operation span
623
+
624
+ const always = () => true;
625
+
626
+ /** Whether an app installed a tracer (an exporter); the native tracer exports nothing. */
627
+ const exporting = (fiber: Fiber.Fiber<unknown, unknown>): boolean => {
628
+ const tracer = fiber.cache.tracer;
629
+ return tracer !== undefined && tracer !== Tracer.nativeTracer;
630
+ };
631
+
632
+ /**
633
+ * On failure only, add the operation as a stack frame at the caller's line,
634
+ * so `Cause.pretty` prints `at S3.GetObject (user.ts:12:5)`. Success pays
635
+ * one `catchCause`; no stack-frame context is provided per call.
636
+ */
637
+ const withFailureFrame = <A, E, R>(
638
+ body: Effect.Effect<A, E, R>,
639
+ name: string,
640
+ callSite: (() => string | undefined) | undefined,
641
+ ): Effect.Effect<A, E, R> =>
642
+ callSite === undefined
643
+ ? body
644
+ : Effect.catchCause(body, (cause) =>
645
+ Effect.withFiber((fiber) =>
646
+ Effect.failCause(
647
+ Cause.annotate(
648
+ cause,
649
+ Context.make(Cause.StackTrace, {
650
+ name,
651
+ stack: callSite,
652
+ parent:
653
+ Context.getOrUndefined(Cause.annotations(cause), Cause.StackTrace) ??
654
+ fiber.getRef(References.CurrentStackFrame),
655
+ }),
656
+ { overwrite: true },
657
+ ),
658
+ ),
659
+ ),
660
+ );
661
+
662
+ /**
663
+ * One operation call (every attempt), with or without debug hooks.
664
+ *
665
+ * no exporter: no span. Effect's HTTP span is switched off too (nobody
666
+ * would see it); the failure frame still names the call.
667
+ * operation: one CLIENT span. Effect's HTTP span is switched off and
668
+ * protocols copy its facts (status, host, request id) onto
669
+ * this span, so one span per call.
670
+ * debug: the span is the parent of Effect's HTTP span (one per
671
+ * attempt) and of distilled's internal spans.
672
+ *
673
+ * Failures set `error.type` to the error's tag; Effect sets the status.
674
+ * An operation whose trait names a streamed output member (`stream`:
675
+ * Bedrock ConverseStream) keeps the span open until the caller has read
676
+ * that stream (`./telemetry-stream.ts`); every other span ends when the
677
+ * call returns, after the response headers.
678
+ */
679
+ export const operationSpan = <A, E, R>(
680
+ fiber: Fiber.Fiber<unknown, unknown>,
681
+ debug: DebugHooks | undefined,
682
+ info: OperationInfo,
683
+ callSite: (() => string | undefined) | undefined,
684
+ input: unknown,
685
+ run: (input: unknown) => Effect.Effect<A, E, R>,
686
+ ): Effect.Effect<A, E, R> => {
687
+ const measure = fiber.getRef(Metrics);
688
+ const traced = traceCall(fiber, debug, info, callSite, input, run);
689
+ return measure === undefined ? traced : measure(info, traced);
690
+ };
691
+
692
+ const traceCall = <A, E, R>(
693
+ fiber: Fiber.Fiber<unknown, unknown>,
694
+ debug: DebugHooks | undefined,
695
+ info: OperationInfo,
696
+ callSite: (() => string | undefined) | undefined,
697
+ input: unknown,
698
+ run: (input: unknown) => Effect.Effect<A, E, R>,
699
+ ): Effect.Effect<A, E, R> => {
700
+ if (!exporting(fiber)) {
701
+ const body = run(input);
702
+ return withFailureFrame(
703
+ debug !== undefined
704
+ ? body
705
+ : Effect.provideService(body, HttpClient.TracerDisabledWhen, always),
706
+ info.name,
707
+ callSite,
708
+ );
709
+ }
710
+ const attributes: Record<string, unknown> =
711
+ debug !== undefined
712
+ ? { ...info.attributes, ...debug.callSite(callSite) }
713
+ : { ...info.attributes };
714
+ readRules(info.input, input, (key, value) => (attributes[key] = value));
715
+ const hooks = fiber.getRef(Hooks);
716
+ const inSpan = (span: Tracer.Span): Effect.Effect<A, E, R> => {
717
+ const extract = info.extract;
718
+ let sent = info.inject === undefined ? input : info.inject(span, input);
719
+ if (extract !== undefined) sent = extract.request(sent);
720
+ let body = Effect.tapError(run(sent), (error) =>
721
+ Effect.sync(() => span.attribute("error.type", tagOf(error))),
722
+ );
723
+ if (extract !== undefined || info.output.length > 0) {
724
+ body = Effect.tap(body, (output) =>
725
+ Effect.sync(() => {
726
+ readRules(info.output, output, (key, value) => span.attribute(key, value));
727
+ if (extract !== undefined) {
728
+ const links = extract.links(output);
729
+ if (links.length > 0) span.addLinks(links);
730
+ }
731
+ }),
732
+ );
733
+ }
734
+ if (hooks !== undefined) {
735
+ hooks.onStart?.(span, { name: info.name, method: info.attributes["rpc.method"], input });
736
+ const onEnd = hooks.onEnd;
737
+ if (onEnd !== undefined) {
738
+ body = Effect.onExit(body, (exit) => Effect.sync(() => onEnd(span, exit)));
739
+ }
740
+ }
741
+ const call: Call = {
742
+ span,
743
+ info,
744
+ attempt: undefined,
745
+ retryErrors: [],
746
+ retryDelayMs: 0,
747
+ };
748
+ if (debug !== undefined) {
749
+ // ParentSpan directly: withParentSpan would also push a name-only
750
+ // stack frame, printed twice next to the failure frame.
751
+ return Effect.updateContext(body, (context: Context.Context<any>) =>
752
+ Context.add(Context.add(context, OperationSpan, call), Tracer.ParentSpan, span),
753
+ );
754
+ }
755
+ return Effect.updateContext(body, (context: Context.Context<any>) =>
756
+ Context.add(Context.add(context, OperationSpan, call), HttpClient.TracerDisabledWhen, always),
757
+ );
758
+ };
759
+ const name = info.rename?.(attributes) ?? info.name;
760
+ const options = { kind: info.kind, attributes };
761
+ const traced =
762
+ info.hold === undefined
763
+ ? Effect.useSpan(name, options, inSpan)
764
+ : info.hold(fiber, name, options, debug, inSpan);
765
+ return withFailureFrame(traced, info.name, callSite);
766
+ };
767
+
768
+ //#endregion