@sealant/sdk-next 0.40.0-next.947 → 0.40.0-next.950

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.
@@ -13,9 +13,10 @@ import { Context, Effect, Layer } from "effect";
13
13
  import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/http";
14
14
  import { HttpApiClient } from "effect/http-api";
15
15
  import { resolveRequestPriority } from "../internal/config.js";
16
- import { CallerTraceState, DirectTraceparent, instrumentClient } from "./trace.js";
16
+ import { CallerTraceState, DirectTraceparent, endpointRoutes, instrumentClient } from "./trace.js";
17
17
  /** A per-operation priority; providing it never changes concurrent requests. */
18
18
  export const CurrentRequestPriority = Context.Reference("@sealant/sdk/CurrentRequestPriority", { defaultValue: () => undefined });
19
+ const CONTROL_PLANE_ROUTES = endpointRoutes(ControlPlaneAPI);
19
20
  /** Builds the contract-derived client. Requires an `HttpClient` in context (provided by the layer). */
20
21
  const buildControlPlaneClient = (config) => {
21
22
  return Effect.map(HttpApiClient.make(ControlPlaneAPI, {
@@ -35,7 +36,7 @@ const buildControlPlaneClient = (config) => {
35
36
  ? prioritized
36
37
  : HttpClientRequest.bearerToken(prioritized, config.apiKey);
37
38
  })),
38
- }), instrumentClient);
39
+ }), (client) => instrumentClient(client, CONTROL_PLANE_ROUTES));
39
40
  };
40
41
  export class SealantApiClient extends Context.Service()("@sealant/sdk/SealantApiClient") {
41
42
  }
@@ -54,13 +54,30 @@ export declare function captureCallerTrace(provider: (() => SealantTraceContext
54
54
  export declare const continueCallerTrace: (trace: CallerTrace | undefined) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
55
55
  /** The trace headers a raw `fetch` made for a captured caller trace carries. */
56
56
  export declare function traceHeaders(trace: CallerTrace | undefined): Readonly<Record<string, string>>;
57
+ /** An endpoint's route as the contract declares it: `GET` and `/v1/sessions/:sessionId/output`. */
58
+ export interface EndpointRoute {
59
+ readonly method: string;
60
+ readonly path: string;
61
+ }
57
62
  /**
58
63
  * Run one contract call in its client span, and choose what trace header its request carries (see
59
64
  * the module comment).
60
65
  */
61
- export declare const traceCall: <A, E, R>(name: string, attributes: Readonly<Record<string, string>>, effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
66
+ export declare const traceCall: <A, E, R>(name: string, attributes: Readonly<Record<string, string>>, effect: Effect.Effect<A, E, R>, route?: EndpointRoute) => Effect.Effect<A, E, R>;
67
+ /** The routes of a contract's endpoints, by group and endpoint key. */
68
+ export type EndpointRoutes = ReadonlyMap<string, ReadonlyMap<string, EndpointRoute>>;
62
69
  /**
63
70
  * The derived control-plane client with every endpoint run through {@link traceCall}, named
64
- * `sealant.sdk.<group>.<endpoint>` from the contract's own identifiers.
71
+ * `sealant.sdk.<group>.<endpoint>` from the contract's own identifiers, its HTTP span described by
72
+ * the endpoint's route in `routes`.
65
73
  */
66
- export declare function instrumentClient<Client extends object>(client: Client): Client;
74
+ export declare function instrumentClient<Client extends object>(client: Client, routes?: EndpointRoutes): Client;
75
+ /** The route of every endpoint `api` declares, keyed as its derived client is. */
76
+ export declare const endpointRoutes: (api: {
77
+ readonly groups: Readonly<Record<string, {
78
+ readonly endpoints: Readonly<Record<string, {
79
+ readonly method: string;
80
+ readonly path: string;
81
+ }>>;
82
+ }>>;
83
+ }) => EndpointRoutes;
@@ -183,16 +183,49 @@ export function traceHeaders(trace) {
183
183
  ? { traceparent }
184
184
  : { traceparent, tracestate: trace.tracestate };
185
185
  }
186
+ /**
187
+ * `tracer`, giving the one HTTP client span started directly beneath the span `parentSpanId` the
188
+ * endpoint's route template: Sentry then describes the request as `GET
189
+ * /v1/sessions/:sessionId/output`, never by its URL and the IDs in it.
190
+ */
191
+ const routeTemplatingTracer = (tracer, parentSpanId, route) => Tracer.make({
192
+ ...tracer,
193
+ span: (options) => {
194
+ const span = tracer.span(options);
195
+ const parent = Option.getOrUndefined(options.parent);
196
+ if (options.kind === "client" && parent?.spanId === parentSpanId) {
197
+ span.attribute("url.template", route.path);
198
+ span.attribute("sentry.op", "http.client");
199
+ span.attribute("sentry.description", `${route.method} ${route.path}`);
200
+ }
201
+ return span;
202
+ },
203
+ });
204
+ /**
205
+ * What Sentry calls an SDK call's span: its operation, and in words what the call does, from its
206
+ * endpoint (`sealant.sdk.sessions.get_session_output` reads `sealant.sdk — get session output`).
207
+ * The span's name stays in `code.function.name`, so a search for it still finds the call.
208
+ */
209
+ const sdkSpanAttributes = (name) => ({
210
+ "sentry.op": "sealant.sdk",
211
+ "sentry.description": (name.split(".").at(-1) ?? name).split("_").filter(Boolean).join(" "),
212
+ "code.function.name": name,
213
+ });
186
214
  /**
187
215
  * Run one contract call in its client span, and choose what trace header its request carries (see
188
216
  * the module comment).
189
217
  */
190
- export const traceCall = (name, attributes, effect) => Effect.gen(function* () {
218
+ export const traceCall = (name, attributes, effect, route) => Effect.gen(function* () {
191
219
  const parent = yield* Effect.serviceOption(Tracer.ParentSpan);
192
220
  const tracer = yield* Effect.tracer;
193
- const spanned = Effect.withSpan(effect, name, {
221
+ const templated = route === undefined || tracer === Tracer.nativeTracer
222
+ ? effect
223
+ : Effect.flatMap(Effect.currentSpan.pipe(Effect.option), (span) => Option.isNone(span)
224
+ ? effect
225
+ : Effect.withTracer(effect, routeTemplatingTracer(tracer, span.value.spanId, route)));
226
+ const spanned = Effect.withSpan(templated, name, {
194
227
  kind: "client",
195
- attributes,
228
+ attributes: { ...attributes, ...sdkSpanAttributes(name) },
196
229
  captureStackTrace: false,
197
230
  });
198
231
  if (tracer !== Tracer.nativeTracer)
@@ -216,33 +249,34 @@ const idAttributes = (request) => {
216
249
  return attributes;
217
250
  };
218
251
  /** An endpoint function wrapped in {@link traceCall}; any other value is kept as it is. */
219
- const instrumentValue = (name, value, holder) => {
252
+ const instrumentValue = (name, value, holder, route) => {
220
253
  if (typeof value !== "function")
221
254
  return value;
222
255
  return (...args) => {
223
256
  const result = Reflect.apply(value, holder, args);
224
- return Effect.isEffect(result) ? traceCall(name, idAttributes(args[0]), result) : result;
257
+ return Effect.isEffect(result) ? traceCall(name, idAttributes(args[0]), result, route) : result;
225
258
  };
226
259
  };
227
- const instrumentGroup = (prefix, group) => {
260
+ const instrumentGroup = (prefix, group, routes) => {
228
261
  const instrumented = {};
229
262
  for (const [key, value] of Object.entries(group)) {
230
- instrumented[key] = instrumentValue(`${prefix}.${snakeCase(key)}`, value, group);
263
+ instrumented[key] = instrumentValue(`${prefix}.${snakeCase(key)}`, value, group, routes?.get(key));
231
264
  }
232
265
  return instrumented;
233
266
  };
234
267
  /**
235
268
  * The derived control-plane client with every endpoint run through {@link traceCall}, named
236
- * `sealant.sdk.<group>.<endpoint>` from the contract's own identifiers.
269
+ * `sealant.sdk.<group>.<endpoint>` from the contract's own identifiers, its HTTP span described by
270
+ * the endpoint's route in `routes`.
237
271
  */
238
- export function instrumentClient(client) {
272
+ export function instrumentClient(client, routes = new Map()) {
239
273
  const instrumented = {};
240
274
  for (const [key, value] of Object.entries(client)) {
241
275
  const name = `sealant.sdk.${snakeCase(key)}`;
242
276
  instrumented[key] =
243
277
  typeof value !== "function" && isRecord(value)
244
- ? instrumentGroup(name, value)
245
- : instrumentValue(name, value, client);
278
+ ? instrumentGroup(name, value, routes.get(key))
279
+ : instrumentValue(name, value, client, undefined);
246
280
  }
247
281
  // SAFETY: the copy has the client's keys, and each endpoint function is replaced by one taking the
248
282
  // same arguments and returning the same effect type (`traceCall` preserves A, E and R). TypeScript
@@ -250,3 +284,11 @@ export function instrumentClient(client) {
250
284
  // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the mapping above keeps the client's type; see SAFETY.
251
285
  return instrumented;
252
286
  }
287
+ /** The route of every endpoint `api` declares, keyed as its derived client is. */
288
+ export const endpointRoutes = (api) => new Map(Object.entries(api.groups).map(([groupKey, group]) => [
289
+ groupKey,
290
+ new Map(Object.entries(group.endpoints).map(([endpointKey, endpoint]) => [
291
+ endpointKey,
292
+ { method: endpoint.method, path: endpoint.path },
293
+ ])),
294
+ ]));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealant/sdk-next",
3
- "version": "0.40.0-next.947",
3
+ "version": "0.40.0-next.950",
4
4
  "description": "The fluent public SDK for Sealant — create a workspace, run a harness, replay the record.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -26,12 +26,12 @@
26
26
  "node": ">=20"
27
27
  },
28
28
  "dependencies": {
29
- "@sealant/api-contracts": "npm:@sealant/api-contracts-next@0.40.0-next.947"
29
+ "@sealant/api-contracts": "npm:@sealant/api-contracts-next@0.40.0-next.950"
30
30
  },
31
31
  "peerDependencies": {
32
32
  "effect": "^4.0.5"
33
33
  },
34
- "gitHead": "33a1ba1eb814bed84b63eb5835d2dd4bbaee43c8",
34
+ "gitHead": "71099ee552fbf9bc07d6317289cab82c5d45a571",
35
35
  "publishConfig": {
36
36
  "access": "public"
37
37
  }