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

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,44 @@ 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
+ /** What Sentry calls an SDK call's span: its operation, and the call's own name. */
205
+ const sdkSpanAttributes = (name) => ({
206
+ "sentry.op": "sealant.sdk",
207
+ "sentry.description": name,
208
+ });
186
209
  /**
187
210
  * Run one contract call in its client span, and choose what trace header its request carries (see
188
211
  * the module comment).
189
212
  */
190
- export const traceCall = (name, attributes, effect) => Effect.gen(function* () {
213
+ export const traceCall = (name, attributes, effect, route) => Effect.gen(function* () {
191
214
  const parent = yield* Effect.serviceOption(Tracer.ParentSpan);
192
215
  const tracer = yield* Effect.tracer;
193
- const spanned = Effect.withSpan(effect, name, {
216
+ const templated = route === undefined || tracer === Tracer.nativeTracer
217
+ ? effect
218
+ : Effect.flatMap(Effect.currentSpan.pipe(Effect.option), (span) => Option.isNone(span)
219
+ ? effect
220
+ : Effect.withTracer(effect, routeTemplatingTracer(tracer, span.value.spanId, route)));
221
+ const spanned = Effect.withSpan(templated, name, {
194
222
  kind: "client",
195
- attributes,
223
+ attributes: { ...attributes, ...sdkSpanAttributes(name) },
196
224
  captureStackTrace: false,
197
225
  });
198
226
  if (tracer !== Tracer.nativeTracer)
@@ -216,33 +244,34 @@ const idAttributes = (request) => {
216
244
  return attributes;
217
245
  };
218
246
  /** An endpoint function wrapped in {@link traceCall}; any other value is kept as it is. */
219
- const instrumentValue = (name, value, holder) => {
247
+ const instrumentValue = (name, value, holder, route) => {
220
248
  if (typeof value !== "function")
221
249
  return value;
222
250
  return (...args) => {
223
251
  const result = Reflect.apply(value, holder, args);
224
- return Effect.isEffect(result) ? traceCall(name, idAttributes(args[0]), result) : result;
252
+ return Effect.isEffect(result) ? traceCall(name, idAttributes(args[0]), result, route) : result;
225
253
  };
226
254
  };
227
- const instrumentGroup = (prefix, group) => {
255
+ const instrumentGroup = (prefix, group, routes) => {
228
256
  const instrumented = {};
229
257
  for (const [key, value] of Object.entries(group)) {
230
- instrumented[key] = instrumentValue(`${prefix}.${snakeCase(key)}`, value, group);
258
+ instrumented[key] = instrumentValue(`${prefix}.${snakeCase(key)}`, value, group, routes?.get(key));
231
259
  }
232
260
  return instrumented;
233
261
  };
234
262
  /**
235
263
  * The derived control-plane client with every endpoint run through {@link traceCall}, named
236
- * `sealant.sdk.<group>.<endpoint>` from the contract's own identifiers.
264
+ * `sealant.sdk.<group>.<endpoint>` from the contract's own identifiers, its HTTP span described by
265
+ * the endpoint's route in `routes`.
237
266
  */
238
- export function instrumentClient(client) {
267
+ export function instrumentClient(client, routes = new Map()) {
239
268
  const instrumented = {};
240
269
  for (const [key, value] of Object.entries(client)) {
241
270
  const name = `sealant.sdk.${snakeCase(key)}`;
242
271
  instrumented[key] =
243
272
  typeof value !== "function" && isRecord(value)
244
- ? instrumentGroup(name, value)
245
- : instrumentValue(name, value, client);
273
+ ? instrumentGroup(name, value, routes.get(key))
274
+ : instrumentValue(name, value, client, undefined);
246
275
  }
247
276
  // SAFETY: the copy has the client's keys, and each endpoint function is replaced by one taking the
248
277
  // same arguments and returning the same effect type (`traceCall` preserves A, E and R). TypeScript
@@ -250,3 +279,11 @@ export function instrumentClient(client) {
250
279
  // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the mapping above keeps the client's type; see SAFETY.
251
280
  return instrumented;
252
281
  }
282
+ /** The route of every endpoint `api` declares, keyed as its derived client is. */
283
+ export const endpointRoutes = (api) => new Map(Object.entries(api.groups).map(([groupKey, group]) => [
284
+ groupKey,
285
+ new Map(Object.entries(group.endpoints).map(([endpointKey, endpoint]) => [
286
+ endpointKey,
287
+ { method: endpoint.method, path: endpoint.path },
288
+ ])),
289
+ ]));
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.949",
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.949"
30
30
  },
31
31
  "peerDependencies": {
32
32
  "effect": "^4.0.5"
33
33
  },
34
- "gitHead": "33a1ba1eb814bed84b63eb5835d2dd4bbaee43c8",
34
+ "gitHead": "6f35c172c6b2eecc56639358a795ce54cb42d8b3",
35
35
  "publishConfig": {
36
36
  "access": "public"
37
37
  }