@sincpro/client-framework 1.4.0 → 1.6.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/AGENTS.md +16 -2
- package/README.md +6 -1
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/infrastructure/process.d.ts +20 -0
- package/dist/core/infrastructure/process.js +12 -1
- package/dist/core/services/execution.js +34 -1
- package/dist/observability/adapters/console.js +9 -1
- package/dist/observability/adapters/opentelemetry.js +5 -4
- package/dist/observability/adapters/sentry.d.ts +3 -0
- package/dist/observability/adapters/sentry.js +27 -0
- package/dist/observability/domain/errors.d.ts +16 -0
- package/dist/observability/domain/identity.d.ts +58 -0
- package/dist/observability/domain/identity.js +73 -0
- package/dist/observability/domain/logging.d.ts +29 -0
- package/dist/observability/index.d.ts +4 -2
- package/dist/observability/index.js +2 -0
- package/dist/observability/infrastructure/registry.d.ts +3 -0
- package/dist/observability/infrastructure/registry.js +1 -0
- package/dist/observability/services/failure.d.ts +21 -0
- package/dist/observability/services/failure.js +73 -0
- package/dist/observability/services/logging.d.ts +2 -1
- package/dist/observability/services/logging.js +43 -20
- package/dist/observability/services/reporting.js +19 -1
- package/package.json +1 -1
package/AGENTS.md
CHANGED
|
@@ -204,8 +204,22 @@ options)`: no route, no verb, no RPC method. The contract's bindings (`rest`, `r
|
|
|
204
204
|
`loggerAdapter` are predefined; injected as a dependency they bind to the run (`BIND`), imported
|
|
205
205
|
at module level they do not. No logger is predefined for an application layer. Console is the
|
|
206
206
|
default engine; `configureProcess({ logger })` and `configureLogger(...)` take the same
|
|
207
|
-
`LoggerConfiguration`. The host chooses production explicitly. Never log DTOs, results
|
|
208
|
-
session context
|
|
207
|
+
`LoggerConfiguration`. The host chooses production explicitly. Never log DTOs, results or the
|
|
208
|
+
full session context automatically.
|
|
209
|
+
- **A failure is described once, as sincpro_framework's failure line** (`PRD_03 §6`,
|
|
210
|
+
`observability/failure.py`). The run where it was raised writes its type, kind, fault and —
|
|
211
|
+
when nobody expected it — its message, the use case's own line (`at`) and its stack; a run it
|
|
212
|
+
crosses says only `failedIn`. Its span records the exception; the spans it crosses take the
|
|
213
|
+
error status. An expected failure is information: type and kind, never its message (it may
|
|
214
|
+
carry the caller's data) nor a stack. `sincpro.outcome` is `ok` or the failure's kind, beside
|
|
215
|
+
`sincpro.fault` and `error.type`, on the span and in GlitchTip.
|
|
216
|
+
- **Who is running is on every signal** (`configureProcess({ identity })`, `PRD_03 §4.10`):
|
|
217
|
+
`release`, the run's `tenant` and `user_id`. Tenant and user are optional, and so are their
|
|
218
|
+
keys — `identity.keys` names the context keys an application uses (`company_id`, `tenant_ids`);
|
|
219
|
+
nothing is sent when its context says none. Nothing else of the context is copied.
|
|
220
|
+
- **Observability never fails a use case.** Every tracer, logger and reporter call is shielded:
|
|
221
|
+
a collector down, Loki unreachable or no network leave the run's own answer — or its own error —
|
|
222
|
+
untouched.
|
|
209
223
|
|
|
210
224
|
- **Only what is transversal comes here.** A value's wire form (a decimal as text, a day never
|
|
211
225
|
an instant, a money with its currency) is the backend's contract, the same on every platform,
|
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.
|
|
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.
|
package/dist/core/index.d.ts
CHANGED
|
@@ -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";
|
package/dist/core/index.js
CHANGED
|
@@ -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 IdentityKeys, 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,19 @@ 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) and its `user_id`; `service.name` and `service.version` are read
|
|
49
|
+
* out of the release. Tenant and user are optional, and so are their keys: `keys` names the
|
|
50
|
+
* context keys they are read from (`tenant`/`tenant_id` and `user_id` by default) — an
|
|
51
|
+
* application whose tenant is its company says `keys: { tenant: "company_id" }`.
|
|
52
|
+
*/
|
|
53
|
+
identity?: {
|
|
54
|
+
release: string;
|
|
55
|
+
tenant?: string;
|
|
56
|
+
keys?: IdentityKeys;
|
|
57
|
+
};
|
|
43
58
|
/** The platform's (OpenTelemetry in a browser, a queue on a phone, memory in a test). */
|
|
44
59
|
tracer?: Tracer;
|
|
45
60
|
/** The platform's (GlitchTip, Sentry, memory in a test). Only what nobody expected reaches it. */
|
|
@@ -52,5 +67,10 @@ export type ProcessOptions = {
|
|
|
52
67
|
outcomeOf?: (error: unknown) => Outcome;
|
|
53
68
|
};
|
|
54
69
|
export declare function configureProcess(options?: ProcessOptions): void;
|
|
70
|
+
/**
|
|
71
|
+
* Who the process is running as (`configureProcess({ identity })`): what a platform's tracer puts
|
|
72
|
+
* on its resource and its log shipping on its labels, so no adapter invents a key of its own.
|
|
73
|
+
*/
|
|
74
|
+
export declare function processIdentity(): ProcessIdentity | undefined;
|
|
55
75
|
/** Replace logging only; tracing, reporting and the context stay as they are. */
|
|
56
76
|
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, options.identity.keys)
|
|
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,8 +2,10 @@ 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, userOf } from "../../observability/domain/identity.js";
|
|
5
6
|
import { NO_TRACER, } from "../../observability/domain/tracing.js";
|
|
6
7
|
import { observabilityNow } from "../../observability/infrastructure/registry.js";
|
|
8
|
+
import { describeFailure } from "../../observability/services/failure.js";
|
|
7
9
|
import { executionLogger, logExecution, logHandled, } from "../../observability/services/logging.js";
|
|
8
10
|
import { firstReport } from "../../observability/services/reporting.js";
|
|
9
11
|
import { remoteSpan } from "../../observability/services/trace-context.js";
|
|
@@ -133,7 +135,14 @@ export async function executeService(target, command, options) {
|
|
|
133
135
|
const correlationId = above?.correlationId ?? options.cause?.correlationId ?? id;
|
|
134
136
|
const causationId = above?.id ?? options.cause?.id;
|
|
135
137
|
const layer = options.layer;
|
|
138
|
+
// Who is running, on the span as on its lines and its report.
|
|
139
|
+
const release = ports.identity?.release;
|
|
140
|
+
const tenant = tenantOf(context, ports.identity);
|
|
141
|
+
const userId = userOf(context, ports.identity);
|
|
136
142
|
const attributes = {
|
|
143
|
+
...(release ? { release } : {}),
|
|
144
|
+
...(tenant ? { tenant } : {}),
|
|
145
|
+
...(userId ? { user_id: userId } : {}),
|
|
137
146
|
"sincpro.use_case": useCase,
|
|
138
147
|
"sincpro.layer": options.layer,
|
|
139
148
|
"sincpro.context": options.boundedContext,
|
|
@@ -174,10 +183,21 @@ export async function executeService(target, command, options) {
|
|
|
174
183
|
? "refused"
|
|
175
184
|
: (failedAt(error.value)?.outcome ?? ports.outcomeOf(error.value)), "failed")
|
|
176
185
|
: "ok";
|
|
177
|
-
|
|
186
|
+
// A failure is described once, by the run where it was raised; a run it crosses on the way
|
|
187
|
+
// out says only where it failed, and its span takes the error status, not the exception.
|
|
188
|
+
const origin = error ? failedAt(error.value) : undefined;
|
|
189
|
+
const failure = error
|
|
190
|
+
? observe(() => describeFailure(error.value, outcome, origin?.execution.useCase), undefined)
|
|
191
|
+
: undefined;
|
|
192
|
+
observe(() => span.set({
|
|
193
|
+
"sincpro.outcome": failure ? failure.kind : "ok",
|
|
194
|
+
...(failure ? { "sincpro.fault": failure.fault, "error.type": failure.type } : {}),
|
|
195
|
+
}), undefined);
|
|
196
|
+
observe(() => span.end(outcome, origin ? undefined : error?.value), undefined);
|
|
178
197
|
logExecution(layer, execution, {
|
|
179
198
|
outcome,
|
|
180
199
|
durationMs: Math.max(0, Date.now() - startedAt),
|
|
200
|
+
...(failure ? { failure } : {}),
|
|
181
201
|
});
|
|
182
202
|
if (outcome === "failed" && error && ports.reporter && !reported.has(error.value)) {
|
|
183
203
|
reported.add(error.value);
|
|
@@ -188,6 +208,19 @@ export async function executeService(target, command, options) {
|
|
|
188
208
|
traceId: span.traceId,
|
|
189
209
|
executionId: id,
|
|
190
210
|
context,
|
|
211
|
+
...(release ? { release } : {}),
|
|
212
|
+
...(ports.identity
|
|
213
|
+
? {
|
|
214
|
+
serviceName: ports.identity.serviceName,
|
|
215
|
+
serviceVersion: ports.identity.serviceVersion,
|
|
216
|
+
}
|
|
217
|
+
: {}),
|
|
218
|
+
...(tenant ? { tenant } : {}),
|
|
219
|
+
...(userId ? { userId } : {}),
|
|
220
|
+
boundedContext: options.boundedContext,
|
|
221
|
+
layer,
|
|
222
|
+
...(span.spanId ? { spanId: span.spanId } : {}),
|
|
223
|
+
...(failure ? { errorType: failure.type, kind: failure.kind } : {}),
|
|
191
224
|
}), undefined);
|
|
192
225
|
}
|
|
193
226
|
if (error?.value !== null &&
|
|
@@ -29,7 +29,15 @@ export function consoleLogger(options) {
|
|
|
29
29
|
: record.level === "error"
|
|
30
30
|
? console.error
|
|
31
31
|
: console.log;
|
|
32
|
-
|
|
32
|
+
// An unexpected failure's stack prints as a stack, readable, after its line — not escaped
|
|
33
|
+
// inside the record.
|
|
34
|
+
const stack = record.failure?.stack;
|
|
35
|
+
if (!stack) {
|
|
36
|
+
method.call(console, prefix, record.message, record);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const { stack: _printedBelow, ...failure } = record.failure ?? {};
|
|
40
|
+
method.call(console, prefix, record.message, { ...record, failure }, `\n${stack}`);
|
|
33
41
|
},
|
|
34
42
|
};
|
|
35
43
|
}
|
|
@@ -48,12 +48,13 @@ export function openTelemetryTracer(tracer) {
|
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
50
|
/**
|
|
51
|
-
* How the use case ended, said the way OpenTelemetry reads it: `failed` is an error with its
|
|
52
|
-
* exception
|
|
53
|
-
* an error, the way the backend leaves a refusal out of its error
|
|
51
|
+
* How the use case ended, said the way OpenTelemetry reads it: `failed` is an error — with its
|
|
52
|
+
* exception on the span where it was raised, the only one handed it —; `refused` and `aborted`
|
|
53
|
+
* are expected answers, never an error, the way the backend leaves a refusal out of its error
|
|
54
|
+
* rate. `sincpro.outcome` (`ok` or the failure's kind), `sincpro.fault` and `error.type` are set
|
|
55
|
+
* by the run before it ends.
|
|
54
56
|
*/
|
|
55
57
|
function close(span, outcome, error) {
|
|
56
|
-
span.setAttribute("sincpro.outcome", outcome);
|
|
57
58
|
if (outcome === "failed") {
|
|
58
59
|
if (error instanceof Error)
|
|
59
60
|
span.recordException(error);
|
|
@@ -17,11 +17,38 @@ export function sentryReporter(client) {
|
|
|
17
17
|
report(error, about) {
|
|
18
18
|
client.captureException(error, {
|
|
19
19
|
captureContext: {
|
|
20
|
+
// sincpro_framework's keys (PRD_03 §5, §4.10): the same text as the log line and the span.
|
|
20
21
|
tags: {
|
|
21
22
|
"sincpro.use_case": about.useCase,
|
|
23
|
+
...(about.boundedContext ? { "sincpro.context": about.boundedContext } : {}),
|
|
24
|
+
...(about.layer ? { "sincpro.layer": about.layer } : {}),
|
|
25
|
+
...(about.kind
|
|
26
|
+
? { "sincpro.outcome": about.kind, "sincpro.kind": about.kind }
|
|
27
|
+
: {}),
|
|
28
|
+
...(about.errorType ? { "error.type": about.errorType } : {}),
|
|
29
|
+
...(about.release ? { release: about.release } : {}),
|
|
30
|
+
...(about.serviceName ? { service_name: about.serviceName } : {}),
|
|
31
|
+
...(about.serviceVersion
|
|
32
|
+
? {
|
|
33
|
+
service_version: about.serviceVersion,
|
|
34
|
+
"sincpro.version": about.serviceVersion,
|
|
35
|
+
}
|
|
36
|
+
: {}),
|
|
37
|
+
...(about.tenant ? { tenant: about.tenant } : {}),
|
|
38
|
+
...(about.userId ? { user_id: about.userId } : {}),
|
|
22
39
|
...(about.traceId === "" ? {} : { trace_id: about.traceId }),
|
|
23
40
|
},
|
|
41
|
+
// The issue opens its trace, and says how many users it hit.
|
|
42
|
+
...(about.userId ? { user: { id: about.userId } } : {}),
|
|
24
43
|
contexts: {
|
|
44
|
+
...(about.traceId === ""
|
|
45
|
+
? {}
|
|
46
|
+
: {
|
|
47
|
+
trace: {
|
|
48
|
+
trace_id: about.traceId,
|
|
49
|
+
...(about.spanId ? { span_id: about.spanId } : {}),
|
|
50
|
+
},
|
|
51
|
+
}),
|
|
25
52
|
sincpro: {
|
|
26
53
|
use_case: about.useCase,
|
|
27
54
|
execution_id: about.executionId,
|
|
@@ -16,6 +16,22 @@ 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;
|
|
23
|
+
/** The run's user — its context's `user_id` —: the event's user. */
|
|
24
|
+
userId?: string;
|
|
25
|
+
/** The name and version out of the release (`identityOf`). */
|
|
26
|
+
serviceName?: string;
|
|
27
|
+
serviceVersion?: string;
|
|
28
|
+
/** Where it ran: its bounded context, its layer and its span. */
|
|
29
|
+
boundedContext?: string;
|
|
30
|
+
layer?: string;
|
|
31
|
+
spanId?: string;
|
|
32
|
+
/** The class's name (`error.type`) and the backend's failure kind. */
|
|
33
|
+
errorType?: string;
|
|
34
|
+
kind?: string;
|
|
19
35
|
};
|
|
20
36
|
/** Reports what nobody expected. One per platform; none when nothing reports. */
|
|
21
37
|
export interface ErrorReporter {
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
/** The context keys a run's tenant is read from, first found wins: `tenant`, `tenant_id`. */
|
|
27
|
+
readonly tenantKeys: readonly string[];
|
|
28
|
+
/** The context keys a run's user is read from: `user_id`. */
|
|
29
|
+
readonly userKeys: readonly string[];
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Which keys of a run's context say its tenant and its user — optional both: an application
|
|
33
|
+
* that calls its tenant `company_id` names it here, one without tenants or users names nothing
|
|
34
|
+
* and nothing is sent.
|
|
35
|
+
*/
|
|
36
|
+
export type IdentityKeys = {
|
|
37
|
+
readonly tenant?: string | readonly string[];
|
|
38
|
+
readonly user?: string | readonly string[];
|
|
39
|
+
};
|
|
40
|
+
/** The key the release goes by on a span, a log line and a report. */
|
|
41
|
+
export declare const RELEASE = "release";
|
|
42
|
+
/** The key the tenant goes by on a span, a log line and a report. */
|
|
43
|
+
export declare const TENANT = "tenant";
|
|
44
|
+
/** The key the user goes by on a span, a log line and a report — GlitchTip's user. */
|
|
45
|
+
export declare const USER_ID = "user_id";
|
|
46
|
+
/**
|
|
47
|
+
* Who is running, out of its release — the deployment's tenant, when there is one, and the
|
|
48
|
+
* context keys its runs say their tenant and user under.
|
|
49
|
+
*/
|
|
50
|
+
export declare function identityOf(release: string, tenant?: string, keys?: IdentityKeys): ProcessIdentity;
|
|
51
|
+
/**
|
|
52
|
+
* The tenant of a run: its context's, under the keys the application named (`tenant`, else
|
|
53
|
+
* `tenant_id`, by default), else the deployment's. Undefined when neither says one — most phones
|
|
54
|
+
* serve whoever opens the app.
|
|
55
|
+
*/
|
|
56
|
+
export declare function tenantOf(context: Readonly<Record<string, unknown>>, identity: ProcessIdentity | undefined): string | undefined;
|
|
57
|
+
/** The user of a run: its context's, under the keys the application named (`user_id`). */
|
|
58
|
+
export declare function userOf(context: Readonly<Record<string, unknown>>, identity?: ProcessIdentity): string | undefined;
|
|
@@ -0,0 +1,73 @@
|
|
|
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
|
+
/** The key the user goes by on a span, a log line and a report — GlitchTip's user. */
|
|
6
|
+
export const USER_ID = "user_id";
|
|
7
|
+
/** Where a run's tenant and user are read from when the application names no keys. */
|
|
8
|
+
const DEFAULT_TENANT_KEYS = [TENANT, "tenant_id"];
|
|
9
|
+
const DEFAULT_USER_KEYS = [USER_ID];
|
|
10
|
+
const keysOf = (named, fallback) => (named === undefined ? fallback : typeof named === "string" ? [named] : [...named]);
|
|
11
|
+
/**
|
|
12
|
+
* Who is running, out of its release — the deployment's tenant, when there is one, and the
|
|
13
|
+
* context keys its runs say their tenant and user under.
|
|
14
|
+
*/
|
|
15
|
+
export function identityOf(release, tenant, keys = {}) {
|
|
16
|
+
const verbatim = release.trim();
|
|
17
|
+
const [artifact, version] = split(verbatim);
|
|
18
|
+
const deployed = tenant?.trim();
|
|
19
|
+
return {
|
|
20
|
+
release: verbatim,
|
|
21
|
+
serviceName: artifact.slice(artifact.lastIndexOf("/") + 1),
|
|
22
|
+
serviceVersion: version,
|
|
23
|
+
...(deployed ? { tenant: deployed } : {}),
|
|
24
|
+
tenantKeys: keysOf(keys.tenant, DEFAULT_TENANT_KEYS),
|
|
25
|
+
userKeys: keysOf(keys.user, DEFAULT_USER_KEYS),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The tenant of a run: its context's, under the keys the application named (`tenant`, else
|
|
30
|
+
* `tenant_id`, by default), else the deployment's. Undefined when neither says one — most phones
|
|
31
|
+
* serve whoever opens the app.
|
|
32
|
+
*/
|
|
33
|
+
export function tenantOf(context, identity) {
|
|
34
|
+
return valueOf(context, identity?.tenantKeys ?? DEFAULT_TENANT_KEYS) ?? identity?.tenant;
|
|
35
|
+
}
|
|
36
|
+
/** The user of a run: its context's, under the keys the application named (`user_id`). */
|
|
37
|
+
export function userOf(context, identity) {
|
|
38
|
+
return valueOf(context, identity?.userKeys ?? DEFAULT_USER_KEYS);
|
|
39
|
+
}
|
|
40
|
+
/** The first key the context says, as text; a list of them (`tenant_ids`) is said as one. */
|
|
41
|
+
function valueOf(context, keys) {
|
|
42
|
+
for (const key of keys) {
|
|
43
|
+
const value = context[key];
|
|
44
|
+
if (typeof value === "string" && value !== "")
|
|
45
|
+
return value;
|
|
46
|
+
if (typeof value === "number")
|
|
47
|
+
return String(value);
|
|
48
|
+
if (Array.isArray(value) && value.length > 0)
|
|
49
|
+
return value.map(String).join(",");
|
|
50
|
+
}
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Name and version out of a release, read as an image reference or `name@version`:
|
|
55
|
+
* 1. `@`: a digest (`@sha256:…`) is dropped; anything else after it is the version.
|
|
56
|
+
* 2. `:` after the last `/` is the tag; one before it is a registry's port.
|
|
57
|
+
* Final: no version when neither says one.
|
|
58
|
+
*/
|
|
59
|
+
function split(release) {
|
|
60
|
+
let artifact = release;
|
|
61
|
+
const at = artifact.lastIndexOf("@");
|
|
62
|
+
if (at >= 0) {
|
|
63
|
+
const after = artifact.slice(at + 1);
|
|
64
|
+
artifact = artifact.slice(0, at);
|
|
65
|
+
if (!after.includes(":"))
|
|
66
|
+
return [artifact, after];
|
|
67
|
+
}
|
|
68
|
+
const colon = artifact.lastIndexOf(":");
|
|
69
|
+
if (colon > artifact.lastIndexOf("/")) {
|
|
70
|
+
return [artifact.slice(0, colon), artifact.slice(colon + 1)];
|
|
71
|
+
}
|
|
72
|
+
return [artifact, ""];
|
|
73
|
+
}
|
|
@@ -14,6 +14,27 @@ export type LogLevel = "debug" | "info" | "warn" | "error";
|
|
|
14
14
|
/** The layer a line comes from. */
|
|
15
15
|
export type LogLayer = "infrastructure" | "adapter" | "bounded_context" | "event_handler" | "application_service" | "feature";
|
|
16
16
|
/** Structured logging never implicitly includes a DTO, response, error body or session context. */
|
|
17
|
+
/**
|
|
18
|
+
* What a failure says — sincpro_framework's failure line: its type (`error.type`), its kind, its
|
|
19
|
+
* fault and, when nobody expected it, its message, where it was raised and its stack. Told once,
|
|
20
|
+
* by the run where it was raised; a run it crosses says only where it failed (`failedIn`).
|
|
21
|
+
*/
|
|
22
|
+
export type FailureDescription = {
|
|
23
|
+
/** The class's name: `TypeError`, `TransportError`. */
|
|
24
|
+
readonly type: string;
|
|
25
|
+
/** The backend's failure kind (`invalid` … `internal`), or `aborted`. */
|
|
26
|
+
readonly kind: string;
|
|
27
|
+
/** The system's problem, not an answer to the caller. */
|
|
28
|
+
readonly fault: boolean;
|
|
29
|
+
/** An unexpected failure's message, cut at 500 characters. */
|
|
30
|
+
readonly message?: string;
|
|
31
|
+
/** The first frame outside the framework: the use case's own line. */
|
|
32
|
+
readonly at?: string;
|
|
33
|
+
/** An unexpected failure's stack — on the run where it was raised only. */
|
|
34
|
+
readonly stack?: string;
|
|
35
|
+
/** The use case it was raised in, when that is another run's. */
|
|
36
|
+
readonly failedIn?: string;
|
|
37
|
+
};
|
|
17
38
|
export type LogRecord = {
|
|
18
39
|
readonly timestamp: string;
|
|
19
40
|
readonly level: LogLevel;
|
|
@@ -30,6 +51,14 @@ export type LogRecord = {
|
|
|
30
51
|
readonly spanId?: string;
|
|
31
52
|
readonly outcome?: Outcome;
|
|
32
53
|
readonly durationMs?: number;
|
|
54
|
+
/** Who was running (`configureProcess({ identity })`), when the process says. */
|
|
55
|
+
readonly release?: string;
|
|
56
|
+
/** The run's tenant — its context's, else the deployment's —, when there is one. */
|
|
57
|
+
readonly tenant?: string;
|
|
58
|
+
/** The run's user — its context's `user_id` —, when there is one. */
|
|
59
|
+
readonly userId?: string;
|
|
60
|
+
/** How the run failed, on its `execution.finished` line. */
|
|
61
|
+
readonly failure?: FailureDescription;
|
|
33
62
|
};
|
|
34
63
|
/** A sink owns filtering and delivery. Logging is best-effort and never awaited by business work. */
|
|
35
64
|
export interface Logger {
|
|
@@ -2,15 +2,17 @@
|
|
|
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 type
|
|
12
|
+
export { type IdentityKeys, identityOf, type ProcessIdentity, RELEASE, TENANT, tenantOf, USER_ID, userOf, } from "./domain/identity.js";
|
|
13
|
+
export type { FailureDescription, 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";
|
|
15
|
+
export { describeFailure } from "./services/failure.js";
|
|
14
16
|
export { combineLoggers, logger, loggerAdapter, } from "./services/logging.js";
|
|
15
17
|
export { outcomeOf } from "./services/outcome.js";
|
|
16
18
|
export { reportError } from "./services/reporting.js";
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
export { identityOf, RELEASE, TENANT, tenantOf, USER_ID, userOf, } from "./domain/identity.js";
|
|
1
2
|
export { NO_TRACER, } from "./domain/tracing.js";
|
|
3
|
+
export { describeFailure } from "./services/failure.js";
|
|
2
4
|
export { combineLoggers, logger, loggerAdapter, } from "./services/logging.js";
|
|
3
5
|
export { outcomeOf } from "./services/outcome.js";
|
|
4
6
|
export { reportError } from "./services/reporting.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" }),
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { FailureDescription } from "../domain/logging.js";
|
|
2
|
+
import type { Outcome } from "../domain/tracing.js";
|
|
3
|
+
/**
|
|
4
|
+
* What a failure says on its log line, its span and its report — sincpro_framework's failure
|
|
5
|
+
* (`observability/failure.py`): its type, its kind, whether it is a fault, its message, where it
|
|
6
|
+
* was raised and, when nobody expected it, its stack.
|
|
7
|
+
*
|
|
8
|
+
* describeFailure(new TypeError("x is undefined"), "failed")
|
|
9
|
+
* → { type: "TypeError", kind: "internal", fault: true, message: "x is undefined",
|
|
10
|
+
* at: "SaveServer.execute (save_server.feature.ts:20:5)", stack: "TypeError: …" }
|
|
11
|
+
* describeFailure(new TransportError("/x", 409, null), "refused")
|
|
12
|
+
* → { type: "TransportError", kind: "conflict", fault: false } // expected: no message, no stack
|
|
13
|
+
* describeFailure(error, "failed", "odoo.SaveServer")
|
|
14
|
+
* → { type, kind, fault, message, failedIn: "odoo.SaveServer" } // told once, where it failed
|
|
15
|
+
*
|
|
16
|
+
* Context: a failure is described once — by the run where it was raised —; a run it crosses on
|
|
17
|
+
* the way out says only its type and where it failed (`failedIn`). An expected one (`refused`,
|
|
18
|
+
* `aborted`) is the business answering or the caller leaving: its type and kind, never its
|
|
19
|
+
* message, which may carry the caller's data. Never the DTO, the result or the context.
|
|
20
|
+
*/
|
|
21
|
+
export declare function describeFailure(error: unknown, outcome: Outcome, failedIn?: string): FailureDescription;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { failureOf } from "../../common/failures.js";
|
|
2
|
+
/** A message is cut here: a line is a description, not a dump. */
|
|
3
|
+
const MAX_MESSAGE = 500;
|
|
4
|
+
/**
|
|
5
|
+
* Frames of the framework itself — the installed package, or its own sources —: where a failure
|
|
6
|
+
* was raised is the first frame outside them.
|
|
7
|
+
*/
|
|
8
|
+
const FRAMEWORK_FRAME = /@sincpro\/client-framework\/|sincpro_client_framework\/src\/|node:internal/;
|
|
9
|
+
/**
|
|
10
|
+
* What a failure says on its log line, its span and its report — sincpro_framework's failure
|
|
11
|
+
* (`observability/failure.py`): its type, its kind, whether it is a fault, its message, where it
|
|
12
|
+
* was raised and, when nobody expected it, its stack.
|
|
13
|
+
*
|
|
14
|
+
* describeFailure(new TypeError("x is undefined"), "failed")
|
|
15
|
+
* → { type: "TypeError", kind: "internal", fault: true, message: "x is undefined",
|
|
16
|
+
* at: "SaveServer.execute (save_server.feature.ts:20:5)", stack: "TypeError: …" }
|
|
17
|
+
* describeFailure(new TransportError("/x", 409, null), "refused")
|
|
18
|
+
* → { type: "TransportError", kind: "conflict", fault: false } // expected: no message, no stack
|
|
19
|
+
* describeFailure(error, "failed", "odoo.SaveServer")
|
|
20
|
+
* → { type, kind, fault, message, failedIn: "odoo.SaveServer" } // told once, where it failed
|
|
21
|
+
*
|
|
22
|
+
* Context: a failure is described once — by the run where it was raised —; a run it crosses on
|
|
23
|
+
* the way out says only its type and where it failed (`failedIn`). An expected one (`refused`,
|
|
24
|
+
* `aborted`) is the business answering or the caller leaving: its type and kind, never its
|
|
25
|
+
* message, which may carry the caller's data. Never the DTO, the result or the context.
|
|
26
|
+
*/
|
|
27
|
+
export function describeFailure(error, outcome, failedIn) {
|
|
28
|
+
const failure = failureOf(error);
|
|
29
|
+
const type = typeOf(error);
|
|
30
|
+
const base = {
|
|
31
|
+
type,
|
|
32
|
+
kind: failure.aborted ? "aborted" : failure.kind,
|
|
33
|
+
fault: failure.fault,
|
|
34
|
+
};
|
|
35
|
+
if (outcome !== "failed")
|
|
36
|
+
return base;
|
|
37
|
+
const message = messageOf(error);
|
|
38
|
+
if (failedIn)
|
|
39
|
+
return { ...base, ...(message ? { message } : {}), failedIn };
|
|
40
|
+
const stack = error instanceof Error ? error.stack : undefined;
|
|
41
|
+
const at = stack ? raisedAt(stack) : undefined;
|
|
42
|
+
return {
|
|
43
|
+
...base,
|
|
44
|
+
...(message ? { message } : {}),
|
|
45
|
+
...(at ? { at } : {}),
|
|
46
|
+
...(stack ? { stack } : {}),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** The class's name, as every signal spells it (`error.type`). */
|
|
50
|
+
function typeOf(error) {
|
|
51
|
+
if (error instanceof Error) {
|
|
52
|
+
const named = error.constructor?.name;
|
|
53
|
+
return named && named !== "Object" ? named : error.name || "Error";
|
|
54
|
+
}
|
|
55
|
+
return error === null ? "null" : typeof error;
|
|
56
|
+
}
|
|
57
|
+
function messageOf(error) {
|
|
58
|
+
const text = error instanceof Error ? error.message : String(error);
|
|
59
|
+
return text.length > MAX_MESSAGE ? `${text.slice(0, MAX_MESSAGE)}…` : text;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Where it was raised: the first frame of the stack outside the framework — the use case's own
|
|
63
|
+
* code (`SaveServer.execute (save_server.feature.ts:20:5)`), V8's `at …` or JavaScriptCore's
|
|
64
|
+
* `name@file:line`.
|
|
65
|
+
*/
|
|
66
|
+
function raisedAt(stack) {
|
|
67
|
+
for (const line of stack.split("\n").slice(1)) {
|
|
68
|
+
const frame = line.trim().replace(/^at /, "");
|
|
69
|
+
if (frame && !FRAMEWORK_FRAME.test(frame))
|
|
70
|
+
return frame;
|
|
71
|
+
}
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Execution } from "../../context/domain/execution.js";
|
|
2
|
-
import type { LayerLogger, Logger, LogLayer } from "../domain/logging.js";
|
|
2
|
+
import type { FailureDescription, LayerLogger, Logger, LogLayer } from "../domain/logging.js";
|
|
3
3
|
import type { Outcome } from "../domain/tracing.js";
|
|
4
4
|
/** The infrastructure's logger — the global one: the code that starts and holds the application. */
|
|
5
5
|
export declare const logger: LayerLogger;
|
|
@@ -17,6 +17,7 @@ export declare function combineLoggers(...loggers: readonly Logger[]): Logger;
|
|
|
17
17
|
export declare function logExecution(layer: RunLogLayer, execution: Execution, completed?: {
|
|
18
18
|
outcome: Outcome;
|
|
19
19
|
durationMs: number;
|
|
20
|
+
failure?: FailureDescription;
|
|
20
21
|
}): void;
|
|
21
22
|
/**
|
|
22
23
|
* @internal — an error a handler of the bounded context answered instead of raising: said once,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { BIND } from "../../context/domain/execution.js";
|
|
2
|
+
import { tenantOf, userOf } 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,26 @@ 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
|
+
const userId = userOf(runContext ?? {}, identity);
|
|
27
|
+
return {
|
|
28
|
+
...fields,
|
|
29
|
+
...where,
|
|
30
|
+
...(identity ? { release: identity.release } : {}),
|
|
31
|
+
...(tenant ? { tenant } : {}),
|
|
32
|
+
...(userId ? { userId } : {}),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
17
35
|
function correlationOf(execution) {
|
|
18
36
|
return {
|
|
37
|
+
runContext: execution.context,
|
|
19
38
|
boundedContext: execution.boundedContext,
|
|
20
39
|
useCase: execution.useCase,
|
|
21
40
|
executionId: execution.id,
|
|
@@ -26,14 +45,13 @@ function correlationOf(execution) {
|
|
|
26
45
|
}
|
|
27
46
|
function layerLogger(layer, sink, correlation = {}) {
|
|
28
47
|
const write = (level, message, attributes) => {
|
|
29
|
-
deliver(sink(), {
|
|
48
|
+
deliver(sink(), recordOf({
|
|
30
49
|
timestamp: new Date().toISOString(),
|
|
31
50
|
level,
|
|
32
51
|
layer,
|
|
33
52
|
message,
|
|
34
53
|
...(attributes ? { attributes: { ...attributes } } : {}),
|
|
35
|
-
|
|
36
|
-
});
|
|
54
|
+
}, correlation));
|
|
37
55
|
};
|
|
38
56
|
const logger = {
|
|
39
57
|
debug: (message, attributes) => write("debug", message, attributes),
|
|
@@ -71,36 +89,41 @@ export function combineLoggers(...loggers) {
|
|
|
71
89
|
/** @internal — lifecycle records contain correlation and outcome, never business payloads. */
|
|
72
90
|
export function logExecution(layer, execution, completed) {
|
|
73
91
|
const outcome = completed?.outcome;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
? "info"
|
|
80
|
-
: "debug";
|
|
81
|
-
deliver(processSink(), {
|
|
92
|
+
// As sincpro_framework: an unexpected failure is an error; an expected one — a refusal, the
|
|
93
|
+
// caller leaving — is information, without a stack.
|
|
94
|
+
const level = outcome === "failed" ? "error" : completed ? "info" : "debug";
|
|
95
|
+
const failure = completed?.failure;
|
|
96
|
+
deliver(processSink(), recordOf({
|
|
82
97
|
timestamp: new Date().toISOString(),
|
|
83
98
|
level,
|
|
84
99
|
layer,
|
|
85
100
|
event: completed ? "execution.finished" : "execution.started",
|
|
86
|
-
message: completed
|
|
87
|
-
|
|
101
|
+
message: completed
|
|
102
|
+
? `Execution ${completed.outcome}${failure ? `: ${summaryOf(failure)}` : ""}`
|
|
103
|
+
: "Execution started",
|
|
88
104
|
...(completed ? completed : {}),
|
|
89
|
-
});
|
|
105
|
+
}, correlationOf(execution)));
|
|
106
|
+
}
|
|
107
|
+
/** `TypeError: x is undefined at SaveServer.execute (…)`, or where it failed when it was another run. */
|
|
108
|
+
function summaryOf(failure) {
|
|
109
|
+
const what = failure.message ? `${failure.type}: ${failure.message}` : failure.type;
|
|
110
|
+
if (failure.failedIn)
|
|
111
|
+
return `${what} (in ${failure.failedIn})`;
|
|
112
|
+
return failure.at ? `${what} at ${failure.at}` : what;
|
|
90
113
|
}
|
|
91
114
|
/**
|
|
92
115
|
* @internal — an error a handler of the bounded context answered instead of raising: said once,
|
|
93
116
|
* with where it was handled, never with the error itself.
|
|
94
117
|
*/
|
|
95
118
|
export function logHandled(layer, correlation) {
|
|
96
|
-
deliver(processSink(), {
|
|
119
|
+
deliver(processSink(), recordOf({
|
|
97
120
|
timestamp: new Date().toISOString(),
|
|
98
|
-
|
|
121
|
+
// As sincpro_framework: an error a handler answered is a warning.
|
|
122
|
+
level: "warn",
|
|
99
123
|
layer,
|
|
100
124
|
event: "error.handled",
|
|
101
125
|
message: "Error handled",
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
});
|
|
126
|
+
}, "execution" in correlation
|
|
127
|
+
? correlationOf(correlation.execution)
|
|
128
|
+
: { boundedContext: correlation.boundedContext }));
|
|
106
129
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { newEntityId } from "../../common/ids.js";
|
|
2
2
|
import { processContext } from "../../context/infrastructure/process.js";
|
|
3
|
+
import { tenantOf, userOf } from "../domain/identity.js";
|
|
3
4
|
import { observabilityNow } from "../infrastructure/registry.js";
|
|
5
|
+
import { describeFailure } from "./failure.js";
|
|
4
6
|
/**
|
|
5
7
|
* Whether this is the first time this error is reported. A Feature that fails inside an
|
|
6
8
|
* ApplicationService throws through it, and a screen that draws the failure may break on it:
|
|
@@ -41,11 +43,27 @@ export function reportError(error, where) {
|
|
|
41
43
|
if (!firstReport(error))
|
|
42
44
|
return;
|
|
43
45
|
try {
|
|
46
|
+
const context = processContext.values;
|
|
47
|
+
const identity = settings.identity;
|
|
48
|
+
const tenant = tenantOf(context, identity);
|
|
49
|
+
const userId = userOf(context, identity);
|
|
50
|
+
const failure = describeFailure(error, "failed");
|
|
44
51
|
settings.reporter?.report(error, {
|
|
45
52
|
useCase: where,
|
|
46
53
|
traceId: "",
|
|
47
54
|
executionId: newEntityId(),
|
|
48
|
-
context
|
|
55
|
+
context,
|
|
56
|
+
...(identity
|
|
57
|
+
? {
|
|
58
|
+
release: identity.release,
|
|
59
|
+
serviceName: identity.serviceName,
|
|
60
|
+
serviceVersion: identity.serviceVersion,
|
|
61
|
+
}
|
|
62
|
+
: {}),
|
|
63
|
+
...(tenant ? { tenant } : {}),
|
|
64
|
+
...(userId ? { userId } : {}),
|
|
65
|
+
errorType: failure.type,
|
|
66
|
+
kind: failure.kind,
|
|
49
67
|
});
|
|
50
68
|
}
|
|
51
69
|
catch {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sincpro/client-framework",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.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",
|