@sincpro/web-framework 3.9.0 → 3.10.0

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.
package/README.md CHANGED
@@ -500,7 +500,7 @@ export async function register() {
500
500
  if (process.env.NEXT_RUNTIME !== "nodejs") return;
501
501
  const { registerServerTracing } = await import("@sincpro/web-framework/telemetry/server");
502
502
  const endpoint = process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT; // none: tracing stays off
503
- await registerServerTracing({ endpoint, release, environment });
503
+ await registerServerTracing({ endpoint, release, environment, oneTreePerRequest: true });
504
504
  }
505
505
 
506
506
  // where the server runs use cases — once per realm; in a browser it does nothing
@@ -508,6 +508,13 @@ import { configureServerProcess } from "@sincpro/web-framework/telemetry/server"
508
508
  configureServerProcess({ identity: { release } });
509
509
  ```
510
510
 
511
+ With `oneTreePerRequest`, each request is one trace: the request the SSR framework traced, its
512
+ renders, the use cases its page read and what the API did with them. It registers Node's
513
+ `AsyncLocalStorage` context manager (the optional peer `@opentelemetry/context-async-hooks`),
514
+ unless the platform registered one first, and a use case with no parent of its own starts under
515
+ the active span (client-framework 2.5's `adoptActiveSpan`). Without it — the default — each use
516
+ case's span starts a trace of its own, as before.
517
+
511
518
  A page that broke while drawing shows the route's error screen instead of the framework's generic
512
519
  one: plain words, the code the server logged it with (Next's `digest`), «try again» and «home»,
513
520
  reported once through the process:
@@ -29,6 +29,17 @@ export type Trail = {
29
29
  /** How many there are in all, when the server counted them. */
30
30
  total: number | null;
31
31
  };
32
+ /**
33
+ * The key a list's trail is kept by — and the `from` the records opened from it carry in their
34
+ * link: the list's React id (`useId()`), marked with this page load.
35
+ *
36
+ * Context: React's ids start over on every load, and a workspace restores its links from the
37
+ * URL. Unmarked, a record restored after a reload could name the trail of whichever new list
38
+ * happened to get the same id, and show its «3 / 64» and walk its rows. Marked, an old link
39
+ * names no trail of this load, and the record simply shows none. The key never reaches the
40
+ * HTML (a row opens its record on a click), so it cannot differ between server and browser.
41
+ */
42
+ export declare function trailKeyOf(listId: string): string;
32
43
  /** Where a record stands in its trail: «3 of 64», 1-based. */
33
44
  export declare function placeOf(trail: Trail, id: string): {
34
45
  index: number;
@@ -1,5 +1,20 @@
1
1
  import {} from "@sincpro/client-framework";
2
2
  import { resumingFrom } from "@sincpro/criteria";
3
+ /** This page load's mark: drawn once per load of this module, never sent anywhere. */
4
+ const PAGE_LOAD = Math.random().toString(36).slice(2, 8);
5
+ /**
6
+ * The key a list's trail is kept by — and the `from` the records opened from it carry in their
7
+ * link: the list's React id (`useId()`), marked with this page load.
8
+ *
9
+ * Context: React's ids start over on every load, and a workspace restores its links from the
10
+ * URL. Unmarked, a record restored after a reload could name the trail of whichever new list
11
+ * happened to get the same id, and show its «3 / 64» and walk its rows. Marked, an old link
12
+ * names no trail of this load, and the record simply shows none. The key never reaches the
13
+ * HTML (a row opens its record on a click), so it cannot differ between server and browser.
14
+ */
15
+ export function trailKeyOf(listId) {
16
+ return `trail:${PAGE_LOAD}${listId}`;
17
+ }
3
18
  /** Where a record stands in its trail: «3 of 64», 1-based. */
4
19
  export function placeOf(trail, id) {
5
20
  const at = trail.ids.indexOf(id);
@@ -9,7 +9,7 @@ import { type Attributes, type ProcessOptions, type Tracer } from "@sincpro/clie
9
9
  * if (process.env.NEXT_RUNTIME !== "nodejs") return;
10
10
  * const { registerServerTracing } = await import("./server.js");
11
11
  * const endpoint = process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT; // from its settings
12
- * await registerServerTracing({ endpoint, release, environment });
12
+ * await registerServerTracing({ endpoint, release, environment, oneTreePerRequest: true });
13
13
  * }
14
14
  *
15
15
  * // once per realm, where the server runs use cases — a module that reads on the server
@@ -26,7 +26,8 @@ import { type Attributes, type ProcessOptions, type Tracer } from "@sincpro/clie
26
26
  *
27
27
  * Optional on every count, like `./telemetry`: an entry of its own that nothing else imports,
28
28
  * the same optional peers (`@opentelemetry/api`, `@opentelemetry/sdk-trace`,
29
- * `@opentelemetry/exporter-trace-otlp-http`, `@opentelemetry/resources`) loaded with `import()`,
29
+ * `@opentelemetry/exporter-trace-otlp-http`, `@opentelemetry/resources`, and
30
+ * `@opentelemetry/context-async-hooks` for `oneTreePerRequest`) loaded with `import()`,
30
31
  * and nothing throws: without an endpoint nothing loads and nothing is registered; if the SDK
31
32
  * is not installed or fails, that is said once on the console and use cases run untraced,
32
33
  * sending no `traceparent`. Node only — an edge runtime skips it (its exporter speaks `http`).
@@ -49,6 +50,18 @@ export type ServerTracingOptions = {
49
50
  sampling?: number;
50
51
  /** More resource attributes (`tenant.group`, …). */
51
52
  attributes?: Attributes;
53
+ /**
54
+ * Each request one tree — the request the SSR framework traces, its renders, the use cases a
55
+ * page reads and what the backend did with them. Node's AsyncLocalStorage carries the active
56
+ * span through each request's awaits (registered here, unless the platform registered a
57
+ * context manager of its own first), and a use case run with no parent of its own starts
58
+ * under it (client-framework's `adoptActiveSpan`, 2.5+).
59
+ *
60
+ * Default `false`, as before: a use case's span is the root of its trace. Turning it on loads
61
+ * one more optional peer, `@opentelemetry/context-async-hooks`; if it does not load, that is
62
+ * said once and the rest works as without it.
63
+ */
64
+ oneTreePerRequest?: boolean;
52
65
  };
53
66
  /** The process's tracing, once registered. */
54
67
  export type ServerTracing = {
@@ -19,9 +19,38 @@ export function registerServerTracing(options) {
19
19
  if (!endpoint)
20
20
  return Promise.resolve(undefined);
21
21
  const shared = held();
22
- shared.registered ??= started({ ...options, endpoint });
22
+ if (shared.registered === undefined) {
23
+ // Before the first page: every realm's tracer reads it when it is first asked for.
24
+ shared.adoptActiveSpan = options.oneTreePerRequest === true;
25
+ shared.registered = started({ ...options, endpoint });
26
+ }
23
27
  return shared.registered;
24
28
  }
29
+ /** A tracer of client-framework's over OpenTelemetry's, as this process's registration asked. */
30
+ function tracerOver(trace, overOpenTelemetry) {
31
+ return (name) => overOpenTelemetry(trace.getTracer(name), {
32
+ adoptActiveSpan: held().adoptActiveSpan === true,
33
+ });
34
+ }
35
+ /**
36
+ * What is active follows each request through its awaits: Node's AsyncLocalStorage, registered
37
+ * with OpenTelemetry's API — unless the platform registered a context manager first, which then
38
+ * carries it.
39
+ */
40
+ async function followEachRequest() {
41
+ try {
42
+ const [{ context }, { AsyncLocalStorageContextManager }] = await Promise.all([
43
+ import("@opentelemetry/api"),
44
+ import("@opentelemetry/context-async-hooks"),
45
+ ]);
46
+ const manager = new AsyncLocalStorageContextManager().enable();
47
+ if (!context.setGlobalContextManager(manager))
48
+ manager.disable();
49
+ }
50
+ catch (error) {
51
+ console.warn(`${WHO}: a request is not one tree — @opentelemetry/context-async-hooks did not load`, error);
52
+ }
53
+ }
25
54
  async function started(options) {
26
55
  try {
27
56
  const [{ trace }, { TracerProvider, BatchSpanProcessor, TraceIdRatioBasedSampler, ParentBasedSampler }, { OTLPTraceExporter }, { resourceFromAttributes }, { openTelemetryTracer },] = await Promise.all([
@@ -31,6 +60,8 @@ async function started(options) {
31
60
  import("@opentelemetry/resources"),
32
61
  import("@sincpro/client-framework/opentelemetry"),
33
62
  ]);
63
+ if (options.oneTreePerRequest === true)
64
+ await followEachRequest();
34
65
  const exporter = new OTLPTraceExporter({
35
66
  url: options.endpoint,
36
67
  ...(options.headers ? { headers: { ...options.headers } } : {}),
@@ -42,7 +73,7 @@ async function started(options) {
42
73
  }),
43
74
  spanProcessors: [new BatchSpanProcessor({ exporter })],
44
75
  });
45
- held().tracerOf ??= (name) => openTelemetryTracer(trace.getTracer(name));
76
+ held().tracerOf ??= tracerOver(trace, openTelemetryTracer);
46
77
  if (!trace.setGlobalTracerProvider(provider)) {
47
78
  await provider.shutdown();
48
79
  console.warn(`${WHO}: another provider was registered first — use cases trace on it`);
@@ -64,7 +95,7 @@ function loadApi() {
64
95
  import("@opentelemetry/api"),
65
96
  import("@sincpro/client-framework/opentelemetry"),
66
97
  ]);
67
- held().tracerOf ??= (name) => openTelemetryTracer(trace.getTracer(name));
98
+ held().tracerOf ??= tracerOver(trace, openTelemetryTracer);
68
99
  }
69
100
  catch (error) {
70
101
  console.warn(`${WHO}: tracing is off — @opentelemetry/api did not load`, error);
@@ -9,6 +9,7 @@ import { explain } from "../../core/explain.js";
9
9
  import { choosesMany, } from "../../core/list.js";
10
10
  import { textOf, viewOf } from "../../core/present.js";
11
11
  import { summaryFieldsOf } from "../../core/summary.js";
12
+ import { trailKeyOf } from "../../core/trail.js";
12
13
  import { useFramework } from "../../react/context.js";
13
14
  import { useGroups } from "../../react/useGroups.js";
14
15
  import { usePivot } from "../../react/usePivot.js";
@@ -81,7 +82,7 @@ export function EntityList({ listing, api, columns: askedColumns, cells, yesNo,
81
82
  const ticking = selectable || choosingMany || selectionActions !== undefined;
82
83
  // The record opened from a row knows the page it came from: «3 / 64» and its neighbours.
83
84
  const { trails, dateStyle } = useFramework();
84
- const trailKey = `trail:${useId()}`;
85
+ const trailKey = trailKeyOf(useId());
85
86
  // Kept current with the page on screen: the row and its name (`RecordLink`) open the same
86
87
  // reference, and whichever opens it, the record finds where it came from.
87
88
  useEffect(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sincpro/web-framework",
3
- "version": "3.9.0",
3
+ "version": "3.10.0",
4
4
  "description": "The web client of sincpro_framework: entities, lists and records bound to the metadata the server publishes, drawn with @sincpro/web-ui and arranged with @sincpro/web-composer.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -40,6 +40,7 @@
40
40
  },
41
41
  "peerDependencies": {
42
42
  "@opentelemetry/api": "^1.10.0",
43
+ "@opentelemetry/context-async-hooks": "^3.1.0",
43
44
  "@opentelemetry/exporter-trace-otlp-http": "^0.301.0",
44
45
  "@opentelemetry/resources": "^3.1.0",
45
46
  "@opentelemetry/sdk-trace": "^3.1.0",
@@ -56,6 +57,9 @@
56
57
  "@opentelemetry/api": {
57
58
  "optional": true
58
59
  },
60
+ "@opentelemetry/context-async-hooks": {
61
+ "optional": true
62
+ },
59
63
  "@opentelemetry/exporter-trace-otlp-http": {
60
64
  "optional": true
61
65
  },
@@ -72,11 +76,12 @@
72
76
  "devDependencies": {
73
77
  "@eslint/js": "^10.0.1",
74
78
  "@opentelemetry/api": "1.10.0",
79
+ "@opentelemetry/context-async-hooks": "3.1.1",
75
80
  "@opentelemetry/exporter-trace-otlp-http": "0.301.0",
76
81
  "@opentelemetry/resources": "3.1.0",
77
82
  "@opentelemetry/sdk-trace": "3.1.0",
78
83
  "@sentry/browser": "11.11.0",
79
- "@sincpro/client-framework": "^2.3.0",
84
+ "@sincpro/client-framework": "^2.5.0",
80
85
  "@sincpro/criteria": "^2.4.0",
81
86
  "@sincpro/criteria-react": "^2.4.0",
82
87
  "@sincpro/web-composer": "^1.8.0",