@sincpro/client-framework 1.4.0 → 1.5.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
@@ -493,6 +493,7 @@ import { openTelemetryTracer } from "@sincpro/client-framework/opentelemetry";
493
493
  import { sentryReporter } from "@sincpro/client-framework/sentry";
494
494
 
495
495
  configureProcess({
496
+ identity: { release: "forge-web:1.4.0" }, // + tenant, when the deployment serves one
496
497
  tracer: openTelemetryTracer(provider.getTracer("forge-web")), // the app's provider
497
498
  reporter: sentryReporter(client), // the app's Sentry or GlitchTip client
498
499
  logger: { mode: production ? "production" : "development" },
@@ -501,7 +502,11 @@ configureProcess({
501
502
  ```
502
503
 
503
504
  Every execution opens a span `<bounded context>/<Service>`; an ApplicationService's Features
504
- are its children, on the same trace. A dependency that is a `Transport` or a generated client is
505
+ are its children, on the same trace. Each span, log record and report says who was running, with
506
+ sincpro_framework's keys: `release` (`<name>:<version>`, the backend's `APP_RELEASE`) and, when
507
+ there is one, `tenant` — the run's context's, else the deployment's. `processIdentity()` gives
508
+ `release`, `serviceName` and `serviceVersion` (read out of the release with the backend's rules)
509
+ to a platform's tracer resource and log labels; nothing else of the context is copied. A dependency that is a `Transport` or a generated client is
505
510
  bound to the run, so each request carries `traceparent` (and `tracestate`) and
506
511
  `sincpro-context` — the backend continues the trace. A tracer, reporter or logger that fails is
507
512
  ignored: telling about a use case never changes its answer.
@@ -18,5 +18,5 @@ export { ApplicationService, type ApplicationServiceWith, type BoundedContextHan
18
18
  export { BoundedContext, type Registrar, type RegistrationOptions, } from "./entrypoint/bounded-context.js";
19
19
  export { screensTransport } from "./entrypoint/screens.js";
20
20
  export { processListeners } from "./infrastructure/listeners.js";
21
- export { configureLogger, configureProcess, type LoggerConfiguration, type ProcessOptions, } from "./infrastructure/process.js";
21
+ export { configureLogger, configureProcess, type LoggerConfiguration, processIdentity, type ProcessOptions, } from "./infrastructure/process.js";
22
22
  export type { HandlerRegistrar } from "./services/event-handlers.js";
@@ -15,4 +15,4 @@ export { ApplicationService, EventHandler, Feature, handlersWith, servicesWith,
15
15
  export { BoundedContext, } from "./entrypoint/bounded-context.js";
16
16
  export { screensTransport } from "./entrypoint/screens.js";
17
17
  export { processListeners } from "./infrastructure/listeners.js";
18
- export { configureLogger, configureProcess, } from "./infrastructure/process.js";
18
+ export { configureLogger, configureProcess, processIdentity, } from "./infrastructure/process.js";
@@ -1,6 +1,7 @@
1
1
  import type { ContextStore } from "../../context/domain/store.js";
2
2
  import { type ConsoleLoggerOptions } from "../../observability/adapters/console.js";
3
3
  import type { ErrorReporter } from "../../observability/domain/errors.js";
4
+ import { type ProcessIdentity } from "../../observability/domain/identity.js";
4
5
  import type { Logger } from "../../observability/domain/logging.js";
5
6
  import { type Outcome, type Tracer } from "../../observability/domain/tracing.js";
6
7
  /**
@@ -24,6 +25,7 @@ export type LoggerConfiguration = Partial<ConsoleLoggerOptions> & {
24
25
  *
25
26
  * // entrypoints/main.tsx — the platform's pieces, built from the application's settings
26
27
  * configureProcess({
28
+ * identity: { release: "forge-web:1.4.0" }, // + tenant, when the deployment serves one
27
29
  * tracer: browserTracer({ endpoint: settings.otlp, service: "forge-web" }),
28
30
  * reporter: glitchtipReporter({ dsn: settings.glitchtip }),
29
31
  * logger: { mode: settings.production ? "production" : "development" },
@@ -40,6 +42,15 @@ export type LoggerConfiguration = Partial<ConsoleLoggerOptions> & {
40
42
  * with its own tracer and starts from a clean context.
41
43
  */
42
44
  export type ProcessOptions = {
45
+ /**
46
+ * Who is running — the backend's `APP_RELEASE` (`<name>:<version>`) and `TENANT`. Every span,
47
+ * log line and report says its `release` and, when there is one, its `tenant` (the run's
48
+ * context's, else this one); `service.name` and `service.version` are read out of the release.
49
+ */
50
+ identity?: {
51
+ release: string;
52
+ tenant?: string;
53
+ };
43
54
  /** The platform's (OpenTelemetry in a browser, a queue on a phone, memory in a test). */
44
55
  tracer?: Tracer;
45
56
  /** The platform's (GlitchTip, Sentry, memory in a test). Only what nobody expected reaches it. */
@@ -52,5 +63,10 @@ export type ProcessOptions = {
52
63
  outcomeOf?: (error: unknown) => Outcome;
53
64
  };
54
65
  export declare function configureProcess(options?: ProcessOptions): void;
66
+ /**
67
+ * Who the process is running as (`configureProcess({ identity })`): what a platform's tracer puts
68
+ * on its resource and its log shipping on its labels, so no adapter invents a key of its own.
69
+ */
70
+ export declare function processIdentity(): ProcessIdentity | undefined;
55
71
  /** Replace logging only; tracing, reporting and the context stay as they are. */
56
72
  export declare function configureLogger(options?: LoggerConfiguration): void;
@@ -1,8 +1,9 @@
1
1
  import { memoryContexts } from "../../context/adapters/memory.js";
2
2
  import { replaceProcessStore } from "../../context/infrastructure/process.js";
3
3
  import { consoleLogger, } from "../../observability/adapters/console.js";
4
+ import { identityOf, } from "../../observability/domain/identity.js";
4
5
  import { NO_TRACER, } from "../../observability/domain/tracing.js";
5
- import { replaceObservability } from "../../observability/infrastructure/registry.js";
6
+ import { observabilityNow, replaceObservability, } from "../../observability/infrastructure/registry.js";
6
7
  import { outcomeOf } from "../../observability/services/outcome.js";
7
8
  function loggingEngine(options) {
8
9
  if (options.engine === false)
@@ -22,6 +23,9 @@ function loggingEngine(options) {
22
23
  export function configureProcess(options = {}) {
23
24
  const logger = loggingEngine(options.logger ?? {});
24
25
  replaceObservability({
26
+ identity: options.identity
27
+ ? identityOf(options.identity.release, options.identity.tenant)
28
+ : undefined,
25
29
  tracer: options.tracer ?? NO_TRACER,
26
30
  reporter: options.reporter,
27
31
  logger,
@@ -29,6 +33,13 @@ export function configureProcess(options = {}) {
29
33
  });
30
34
  replaceProcessStore(options.context ?? memoryContexts());
31
35
  }
36
+ /**
37
+ * Who the process is running as (`configureProcess({ identity })`): what a platform's tracer puts
38
+ * on its resource and its log shipping on its labels, so no adapter invents a key of its own.
39
+ */
40
+ export function processIdentity() {
41
+ return observabilityNow().identity;
42
+ }
32
43
  /** Replace logging only; tracing, reporting and the context stay as they are. */
33
44
  export function configureLogger(options = {}) {
34
45
  replaceObservability({ logger: loggingEngine(options) });
@@ -2,6 +2,7 @@ import { newEntityId } from "../../common/ids.js";
2
2
  import { bindDependency } from "./binding.js";
3
3
  import { handled } from "./error-handlers.js";
4
4
  import { runAround } from "./interceptors.js";
5
+ import { tenantOf } from "../../observability/domain/identity.js";
5
6
  import { NO_TRACER, } from "../../observability/domain/tracing.js";
6
7
  import { observabilityNow } from "../../observability/infrastructure/registry.js";
7
8
  import { executionLogger, logExecution, logHandled, } from "../../observability/services/logging.js";
@@ -133,7 +134,12 @@ export async function executeService(target, command, options) {
133
134
  const correlationId = above?.correlationId ?? options.cause?.correlationId ?? id;
134
135
  const causationId = above?.id ?? options.cause?.id;
135
136
  const layer = options.layer;
137
+ // Who is running, on the span as on its lines and its report.
138
+ const release = ports.identity?.release;
139
+ const tenant = tenantOf(context, ports.identity);
136
140
  const attributes = {
141
+ ...(release ? { release } : {}),
142
+ ...(tenant ? { tenant } : {}),
137
143
  "sincpro.use_case": useCase,
138
144
  "sincpro.layer": options.layer,
139
145
  "sincpro.context": options.boundedContext,
@@ -188,6 +194,8 @@ export async function executeService(target, command, options) {
188
194
  traceId: span.traceId,
189
195
  executionId: id,
190
196
  context,
197
+ ...(release ? { release } : {}),
198
+ ...(tenant ? { tenant } : {}),
191
199
  }), undefined);
192
200
  }
193
201
  if (error?.value !== null &&
@@ -19,6 +19,8 @@ export function sentryReporter(client) {
19
19
  captureContext: {
20
20
  tags: {
21
21
  "sincpro.use_case": about.useCase,
22
+ ...(about.release ? { release: about.release } : {}),
23
+ ...(about.tenant ? { tenant: about.tenant } : {}),
22
24
  ...(about.traceId === "" ? {} : { trace_id: about.traceId }),
23
25
  },
24
26
  contexts: {
@@ -16,6 +16,10 @@ export type ErrorAbout = {
16
16
  executionId: string;
17
17
  /** What the use case ran with: the process's context, its bounded context's, its scope's. */
18
18
  context: Readonly<Record<string, unknown>>;
19
+ /** Who was running (`configureProcess({ identity })`), when the process says. */
20
+ release?: string;
21
+ /** The run's tenant — its context's, else the deployment's —, when there is one. */
22
+ tenant?: string;
19
23
  };
20
24
  /** Reports what nobody expected. One per platform; none when nothing reports. */
21
25
  export interface ErrorReporter {
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Who is running — sincpro_framework's identity, the same text on every signal: a span, a log
3
+ * line, a report.
4
+ *
5
+ * identityOf("sincpro-mobile-tickets:0.3.0")
6
+ * → { release: "sincpro-mobile-tickets:0.3.0",
7
+ * serviceName: "sincpro-mobile-tickets", serviceVersion: "0.3.0" }
8
+ * identityOf("registry.io/team/app:1.2@sha256:ab…") → serviceName "app", serviceVersion "1.2"
9
+ * identityOf("app@1.2.3") → "app", "1.2.3"
10
+ * identityOf("registry.local:5000/team/app") → "app", "" (a port, no tag)
11
+ *
12
+ * Context: the release is the backend's `APP_RELEASE`, used verbatim; the name and the version
13
+ * are read out of it with the backend's rules (`ObservabilityIdentity._split`), so GlitchTip's
14
+ * release, Tempo's `service.name` and the logs' `service_name` never drift apart. The tenant is
15
+ * the deployment's, when it serves one; a run's own comes from its context (`tenantOf`).
16
+ */
17
+ export type ProcessIdentity = {
18
+ /** `<name>:<version>` as deployed — GlitchTip's release and every signal's `release`. */
19
+ readonly release: string;
20
+ /** The name, without its registry or its version: `service.name`. */
21
+ readonly serviceName: string;
22
+ /** The version out of the release; empty when it names none. */
23
+ readonly serviceVersion: string;
24
+ /** The tenant the deployment serves, when it serves one. */
25
+ readonly tenant?: string;
26
+ };
27
+ /** The key the release goes by on a span, a log line and a report. */
28
+ export declare const RELEASE = "release";
29
+ /** The key the tenant goes by on a span, a log line and a report. */
30
+ export declare const TENANT = "tenant";
31
+ /** Who is running, out of its release — and the deployment's tenant, when there is one. */
32
+ export declare function identityOf(release: string, tenant?: string): ProcessIdentity;
33
+ /**
34
+ * The tenant of a run: its context's (`tenant`, else `tenant_id`), else the deployment's.
35
+ * Undefined when neither says one — most phones serve whoever opens the app.
36
+ */
37
+ export declare function tenantOf(context: Readonly<Record<string, unknown>>, identity: ProcessIdentity | undefined): string | undefined;
@@ -0,0 +1,51 @@
1
+ /** The key the release goes by on a span, a log line and a report. */
2
+ export const RELEASE = "release";
3
+ /** The key the tenant goes by on a span, a log line and a report. */
4
+ export const TENANT = "tenant";
5
+ /** Who is running, out of its release — and the deployment's tenant, when there is one. */
6
+ export function identityOf(release, tenant) {
7
+ const verbatim = release.trim();
8
+ const [artifact, version] = split(verbatim);
9
+ const deployed = tenant?.trim();
10
+ return {
11
+ release: verbatim,
12
+ serviceName: artifact.slice(artifact.lastIndexOf("/") + 1),
13
+ serviceVersion: version,
14
+ ...(deployed ? { tenant: deployed } : {}),
15
+ };
16
+ }
17
+ /**
18
+ * The tenant of a run: its context's (`tenant`, else `tenant_id`), else the deployment's.
19
+ * Undefined when neither says one — most phones serve whoever opens the app.
20
+ */
21
+ export function tenantOf(context, identity) {
22
+ for (const key of [TENANT, "tenant_id"]) {
23
+ const value = context[key];
24
+ if (typeof value === "string" && value !== "")
25
+ return value;
26
+ if (typeof value === "number")
27
+ return String(value);
28
+ }
29
+ return identity?.tenant;
30
+ }
31
+ /**
32
+ * Name and version out of a release, read as an image reference or `name@version`:
33
+ * 1. `@`: a digest (`@sha256:…`) is dropped; anything else after it is the version.
34
+ * 2. `:` after the last `/` is the tag; one before it is a registry's port.
35
+ * Final: no version when neither says one.
36
+ */
37
+ function split(release) {
38
+ let artifact = release;
39
+ const at = artifact.lastIndexOf("@");
40
+ if (at >= 0) {
41
+ const after = artifact.slice(at + 1);
42
+ artifact = artifact.slice(0, at);
43
+ if (!after.includes(":"))
44
+ return [artifact, after];
45
+ }
46
+ const colon = artifact.lastIndexOf(":");
47
+ if (colon > artifact.lastIndexOf("/")) {
48
+ return [artifact.slice(0, colon), artifact.slice(colon + 1)];
49
+ }
50
+ return [artifact, ""];
51
+ }
@@ -30,6 +30,10 @@ export type LogRecord = {
30
30
  readonly spanId?: string;
31
31
  readonly outcome?: Outcome;
32
32
  readonly durationMs?: number;
33
+ /** Who was running (`configureProcess({ identity })`), when the process says. */
34
+ readonly release?: string;
35
+ /** The run's tenant — its context's, else the deployment's —, when there is one. */
36
+ readonly tenant?: string;
33
37
  };
34
38
  /** A sink owns filtering and delivery. Logging is best-effort and never awaited by business work. */
35
39
  export interface Logger {
@@ -2,13 +2,14 @@
2
2
  * Observability: the ports a platform plugs (`Tracer`, `ErrorReporter`, `Logger`), the console
3
3
  * engine logs go to by default, and how code writes and reports.
4
4
  *
5
- * domain/ the ports: tracing, errors, logging
5
+ * domain/ the ports: tracing, errors, logging; who is running (`identity`)
6
6
  * adapters/ console (default), memory (`./testing`), OpenTelemetry and Sentry (optional
7
7
  * entries `./opentelemetry`, `./sentry`)
8
8
  * infrastructure/ the ports this process runs with (`configureProcess` plugs them)
9
9
  * services/ the loggers, `reportError`, the default `outcomeOf`
10
10
  */
11
11
  export type { ErrorAbout, ErrorReporter, } from "./domain/errors.js";
12
+ export { identityOf, type ProcessIdentity, RELEASE, TENANT, tenantOf, } from "./domain/identity.js";
12
13
  export type { LayerLogger, Logger, LogLayer, LogLevel, LogRecord, } from "./domain/logging.js";
13
14
  export { type Attributes, NO_TRACER, type Outcome, type Span, type Tracer, } from "./domain/tracing.js";
14
15
  export { combineLoggers, logger, loggerAdapter, } from "./services/logging.js";
@@ -1,3 +1,4 @@
1
+ export { identityOf, RELEASE, TENANT, tenantOf, } from "./domain/identity.js";
1
2
  export { NO_TRACER, } from "./domain/tracing.js";
2
3
  export { combineLoggers, logger, loggerAdapter, } from "./services/logging.js";
3
4
  export { outcomeOf } from "./services/outcome.js";
@@ -1,4 +1,5 @@
1
1
  import type { ErrorReporter } from "../domain/errors.js";
2
+ import type { ProcessIdentity } from "../domain/identity.js";
2
3
  import type { Logger } from "../domain/logging.js";
3
4
  import { type Outcome, type Tracer } from "../domain/tracing.js";
4
5
  /**
@@ -10,6 +11,8 @@ import { type Outcome, type Tracer } from "../domain/tracing.js";
10
11
  * nothing reports, and logs go to the console in development mode.
11
12
  */
12
13
  export type ObservabilityPorts = {
14
+ /** Who is running: its release and the deployment's tenant, on every signal. */
15
+ readonly identity: ProcessIdentity | undefined;
13
16
  readonly tracer: Tracer;
14
17
  readonly reporter: ErrorReporter | undefined;
15
18
  readonly logger: Logger | undefined;
@@ -2,6 +2,7 @@ import { consoleLogger } from "../adapters/console.js";
2
2
  import { NO_TRACER, } from "../domain/tracing.js";
3
3
  import { outcomeOf } from "../services/outcome.js";
4
4
  const DEFAULTS = {
5
+ identity: undefined,
5
6
  tracer: NO_TRACER,
6
7
  reporter: undefined,
7
8
  logger: consoleLogger({ mode: "development" }),
@@ -1,4 +1,5 @@
1
1
  import { BIND } from "../../context/domain/execution.js";
2
+ import { tenantOf } from "../domain/identity.js";
2
3
  import { observabilityNow } from "../infrastructure/registry.js";
3
4
  const processSink = () => observabilityNow().logger;
4
5
  /** Sink failures cannot replace a business outcome or cause unhandled async rejections. */
@@ -14,8 +15,24 @@ function deliver(logger, record) {
14
15
  // Logging adapters are best-effort, as are tracing and error-reporting adapters.
15
16
  }
16
17
  }
18
+ /**
19
+ * A line as it is delivered: where it was written, and who was running — the process's release
20
+ * and the tenant (the run's context's, else the deployment's), when there is one.
21
+ */
22
+ function recordOf(fields, correlation) {
23
+ const { runContext, ...where } = correlation;
24
+ const identity = observabilityNow().identity;
25
+ const tenant = tenantOf(runContext ?? {}, identity);
26
+ return {
27
+ ...fields,
28
+ ...where,
29
+ ...(identity ? { release: identity.release } : {}),
30
+ ...(tenant ? { tenant } : {}),
31
+ };
32
+ }
17
33
  function correlationOf(execution) {
18
34
  return {
35
+ runContext: execution.context,
19
36
  boundedContext: execution.boundedContext,
20
37
  useCase: execution.useCase,
21
38
  executionId: execution.id,
@@ -26,14 +43,13 @@ function correlationOf(execution) {
26
43
  }
27
44
  function layerLogger(layer, sink, correlation = {}) {
28
45
  const write = (level, message, attributes) => {
29
- deliver(sink(), {
46
+ deliver(sink(), recordOf({
30
47
  timestamp: new Date().toISOString(),
31
48
  level,
32
49
  layer,
33
50
  message,
34
51
  ...(attributes ? { attributes: { ...attributes } } : {}),
35
- ...correlation,
36
- });
52
+ }, correlation));
37
53
  };
38
54
  const logger = {
39
55
  debug: (message, attributes) => write("debug", message, attributes),
@@ -78,29 +94,27 @@ export function logExecution(layer, execution, completed) {
78
94
  : completed
79
95
  ? "info"
80
96
  : "debug";
81
- deliver(processSink(), {
97
+ deliver(processSink(), recordOf({
82
98
  timestamp: new Date().toISOString(),
83
99
  level,
84
100
  layer,
85
101
  event: completed ? "execution.finished" : "execution.started",
86
102
  message: completed ? `Execution ${completed.outcome}` : "Execution started",
87
- ...correlationOf(execution),
88
103
  ...(completed ? completed : {}),
89
- });
104
+ }, correlationOf(execution)));
90
105
  }
91
106
  /**
92
107
  * @internal — an error a handler of the bounded context answered instead of raising: said once,
93
108
  * with where it was handled, never with the error itself.
94
109
  */
95
110
  export function logHandled(layer, correlation) {
96
- deliver(processSink(), {
111
+ deliver(processSink(), recordOf({
97
112
  timestamp: new Date().toISOString(),
98
113
  level: "info",
99
114
  layer,
100
115
  event: "error.handled",
101
116
  message: "Error handled",
102
- ...("execution" in correlation
103
- ? correlationOf(correlation.execution)
104
- : { boundedContext: correlation.boundedContext }),
105
- });
117
+ }, "execution" in correlation
118
+ ? correlationOf(correlation.execution)
119
+ : { boundedContext: correlation.boundedContext }));
106
120
  }
@@ -1,5 +1,6 @@
1
1
  import { newEntityId } from "../../common/ids.js";
2
2
  import { processContext } from "../../context/infrastructure/process.js";
3
+ import { tenantOf } from "../domain/identity.js";
3
4
  import { observabilityNow } from "../infrastructure/registry.js";
4
5
  /**
5
6
  * Whether this is the first time this error is reported. A Feature that fails inside an
@@ -41,11 +42,15 @@ export function reportError(error, where) {
41
42
  if (!firstReport(error))
42
43
  return;
43
44
  try {
45
+ const context = processContext.values;
46
+ const tenant = tenantOf(context, settings.identity);
44
47
  settings.reporter?.report(error, {
45
48
  useCase: where,
46
49
  traceId: "",
47
50
  executionId: newEntityId(),
48
- context: processContext.values,
51
+ context,
52
+ ...(settings.identity ? { release: settings.identity.release } : {}),
53
+ ...(tenant ? { tenant } : {}),
49
54
  });
50
55
  }
51
56
  catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sincpro/client-framework",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "The client side of sincpro_framework, for every platform: Feature and ApplicationService traced and reported by default, applications made of domains, and the typed client generated from a service's contract. No React, no DOM.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",