@intellectual-frontiers/telemetry 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +8 -2
- package/dist/host/console.d.ts +80 -0
- package/dist/host/console.js +188 -0
- package/dist/host/mod.d.ts +15 -0
- package/dist/host/mod.js +16 -0
- package/dist/host/otel.d.ts +280 -0
- package/dist/host/otel.js +848 -0
- package/dist/mod.d.ts +17 -0
- package/dist/mod.js +18 -0
- package/dist/otel.d.ts +365 -0
- package/dist/otel.js +686 -0
- package/dist/otlp.d.ts +83 -0
- package/dist/otlp.js +634 -0
- package/dist/render.d.ts +111 -0
- package/dist/render.js +368 -0
- package/dist/signals.d.ts +371 -0
- package/dist/signals.js +800 -0
- package/package.json +76 -4
- package/src/host/console.ts +253 -0
- package/src/host/mod.ts +16 -0
- package/src/host/otel.ts +1068 -0
- package/src/mod.ts +18 -0
- package/src/otel.ts +919 -0
- package/src/otlp.ts +721 -0
- package/src/render.ts +489 -0
- package/src/signals.ts +1175 -0
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { type Attributes, type LogRecord, type SpanData, type SpanEvent } from "./otel.js";
|
|
2
|
+
import { type Capabilities, type MarkKind } from "@intellectual-frontiers/terminal/palette";
|
|
3
|
+
import { type Declarations, type Registry, type Source, type SpanStart } from "./signals.js";
|
|
4
|
+
/** How the console writes: see the module. */
|
|
5
|
+
export type Mode = "attended" | "plain" | "quiet" | "silent" | "json";
|
|
6
|
+
/** Every mode, in a fixed order. */
|
|
7
|
+
export declare const MODES: readonly Mode[];
|
|
8
|
+
/** A mode by its name; undefined for another text. */
|
|
9
|
+
export declare function parseMode(text: string): Mode | undefined;
|
|
10
|
+
/** The mode to use: the one asked for where it names one, else `attended` at a terminal and `plain` elsewhere (FR-026). */
|
|
11
|
+
export declare function chooseMode(facts: {
|
|
12
|
+
readonly requested?: string | undefined;
|
|
13
|
+
readonly tty: boolean;
|
|
14
|
+
}): Mode;
|
|
15
|
+
/** What the renderer is given: the processor's signals, a failed export, a tick of the clock (to redraw elapsed times), the end of the run. */
|
|
16
|
+
export type Input = {
|
|
17
|
+
readonly kind: "span-start";
|
|
18
|
+
readonly span: SpanStart;
|
|
19
|
+
readonly source: Source;
|
|
20
|
+
} | {
|
|
21
|
+
readonly kind: "span-event";
|
|
22
|
+
readonly span: SpanStart;
|
|
23
|
+
readonly event: SpanEvent;
|
|
24
|
+
readonly source: Source;
|
|
25
|
+
} | {
|
|
26
|
+
readonly kind: "span-end";
|
|
27
|
+
readonly span: SpanData;
|
|
28
|
+
readonly source: Source;
|
|
29
|
+
} | {
|
|
30
|
+
readonly kind: "log";
|
|
31
|
+
readonly record: LogRecord;
|
|
32
|
+
readonly source: Source;
|
|
33
|
+
} | {
|
|
34
|
+
readonly kind: "export-warning";
|
|
35
|
+
readonly message: string;
|
|
36
|
+
} | {
|
|
37
|
+
readonly kind: "tick";
|
|
38
|
+
} | {
|
|
39
|
+
readonly kind: "finish";
|
|
40
|
+
};
|
|
41
|
+
/** What a renderer is made with. */
|
|
42
|
+
export type RenderConfig = {
|
|
43
|
+
readonly mode: Mode;
|
|
44
|
+
/** What the terminal shows: colour, emoji, its width (a `Look` of `system/tty` is one). */
|
|
45
|
+
readonly look: Capabilities;
|
|
46
|
+
/** The registry that gives each kind its presentation role. */
|
|
47
|
+
readonly registry: Registry<Declarations>;
|
|
48
|
+
/** A signal's text from its kind and attributes; `defaultText` where not given (a log record's string body comes first). */
|
|
49
|
+
readonly text?: (name: string, attributes: Attributes) => string;
|
|
50
|
+
/** The most lines the live region takes (10 by default, at least 2); the spans beyond are counted on its last line. */
|
|
51
|
+
readonly maxLive?: number;
|
|
52
|
+
};
|
|
53
|
+
type Known = {
|
|
54
|
+
readonly depth: number;
|
|
55
|
+
readonly shown: boolean;
|
|
56
|
+
readonly anchor: string | undefined;
|
|
57
|
+
};
|
|
58
|
+
type Progress = {
|
|
59
|
+
readonly done: number;
|
|
60
|
+
readonly total: number | undefined;
|
|
61
|
+
};
|
|
62
|
+
type Live = {
|
|
63
|
+
readonly key: string;
|
|
64
|
+
readonly anchor: string | undefined;
|
|
65
|
+
readonly depth: number;
|
|
66
|
+
readonly text: string;
|
|
67
|
+
readonly start: bigint;
|
|
68
|
+
readonly progress: Progress | undefined;
|
|
69
|
+
};
|
|
70
|
+
/** The renderer's state: what is open, what the screen shows of the live region, the warnings kept for the end. */
|
|
71
|
+
export type State = {
|
|
72
|
+
readonly config: RenderConfig;
|
|
73
|
+
/** Every span started and not ended, shown or not, by trace and span id. */
|
|
74
|
+
readonly spans: ReadonlyMap<string, Known>;
|
|
75
|
+
/** The shown spans in progress, in the order they started. */
|
|
76
|
+
readonly live: readonly Live[];
|
|
77
|
+
/** The lines of the live region on the screen (attended). */
|
|
78
|
+
readonly drawn: number;
|
|
79
|
+
/** The export warnings, each once, said at the end. */
|
|
80
|
+
readonly warnings: readonly string[];
|
|
81
|
+
/** The last source seen, for the records `json` writes of its own. */
|
|
82
|
+
readonly source: Source | undefined;
|
|
83
|
+
/** Whether the run has finished; nothing is written after. */
|
|
84
|
+
readonly finished: boolean;
|
|
85
|
+
};
|
|
86
|
+
/** A reducer's step: the next state and the frames to write (empty for none). */
|
|
87
|
+
export type Step = {
|
|
88
|
+
readonly state: State;
|
|
89
|
+
readonly frames: string;
|
|
90
|
+
};
|
|
91
|
+
/** The state a renderer starts in. */
|
|
92
|
+
export declare function initial(config: RenderConfig): State;
|
|
93
|
+
/** A text with every control character but the line feed replaced (a tab by a space, the rest by `?`), so it never moves the cursor. */
|
|
94
|
+
export declare function clean(text: string): string;
|
|
95
|
+
/**
|
|
96
|
+
* The text of a signal by default: a span's is its kind and its attributes' values, in their order, separated by spaces (`build site`); a
|
|
97
|
+
* log record's is its attributes' values alone, or its kind where it has none (a log record with a string body shows the body).
|
|
98
|
+
*/
|
|
99
|
+
export declare function defaultText(name: string, attributes: Attributes, signal?: "span" | "log"): string;
|
|
100
|
+
/** A duration in nanoseconds as a person reads it: `420ms`, `1.2s`, `3m04s`, `2h05m` (cut, never rounded up; a negative one is `0ms`). */
|
|
101
|
+
export declare function duration(ns: bigint): string;
|
|
102
|
+
/** How far a span has come: `done/total` and the percentage where there is a positive total, else `done`. */
|
|
103
|
+
export declare function progressText(done: number, total?: number): string;
|
|
104
|
+
/** How a span ended, as a mark: a `skipped` or `cancelled` event first, then its status. */
|
|
105
|
+
export declare function outcomeOf(span: SpanData): MarkKind;
|
|
106
|
+
/**
|
|
107
|
+
* The next state and the frames to write for an input at the time `now` (nanoseconds since the Unix epoch). Pure: the same state, input
|
|
108
|
+
* and time give the same step. After `finish`, every input gives no frame.
|
|
109
|
+
*/
|
|
110
|
+
export declare function render(state: State, input: Input, now: bigint): Step;
|
|
111
|
+
export {};
|
package/dist/render.js
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
// Spec: 0001-telemetry FR-021
|
|
2
|
+
/**
|
|
3
|
+
* The console as a rendering of OpenTelemetry signals (commons 0001-telemetry FR-021 to FR-023): a pure reducer from a state, an input (a
|
|
4
|
+
* span that started, recorded an event or ended, a log record, an export that failed, a tick of the clock, the end of the run) and the
|
|
5
|
+
* time, to the next state and the frames to write. The runtime's `console.ts` feeds it and writes what it returns; a test feeds it and
|
|
6
|
+
* reads the frames through `vt.ts`'s screen.
|
|
7
|
+
*
|
|
8
|
+
* Five modes (FR-021):
|
|
9
|
+
* - `attended`: a live region of the spans in progress, nested by parent, each with its elapsed time and its progress (FR-023), redrawn in
|
|
10
|
+
* place (cursor up, erase in display); a line that is final (a span that ended, a log record) is written above the region and stays.
|
|
11
|
+
* Every line is cut to the terminal's width by display width, so nothing wraps.
|
|
12
|
+
* - `plain`: the final lines only, appended, with no escape sequence at all (no colour, no cursor movement), for a log or a pipe; a line
|
|
13
|
+
* is not cut.
|
|
14
|
+
* - `quiet`: the final lines that are warnings, errors (a span that failed, a log record of the role `warning` or `error`, or of severity
|
|
15
|
+
* WARN or more), actions (the role `action`: a step that is the person's to take, FR-027) or results (the role `result`), appended;
|
|
16
|
+
* coloured where the look allows it.
|
|
17
|
+
* - `silent`: nothing.
|
|
18
|
+
* - `json`: one OTLP JSON document per line, each an `ExportLogsServiceRequest` (`otlp.ts`'s `encodeLogs`) holding one log record: a log
|
|
19
|
+
* record as it is; a span that ended as a log record whose `eventName` is the span's name, whose time is its end, whose severity is
|
|
20
|
+
* ERROR where it failed and INFO otherwise, whose body is its line as `plain` writes it (without indentation, with ASCII marks), with
|
|
21
|
+
* its attributes and its trace and span ids; the export warning as a WARN record named `ifc.console.export`.
|
|
22
|
+
*
|
|
23
|
+
* Only a signal whose kind has a presentation role (`signals.ts`'s `roleOf`) other than `none` is shown; an undeclared kind is not. A span
|
|
24
|
+
* that is not shown still nests: its children are shown at its depth. How a span ended: ⏭️ where it recorded an event named `skipped`,
|
|
25
|
+
* ⏹️ where it recorded one named `cancelled` (and for a span still open at the end of the run), ❌ where its status is an error, ✅
|
|
26
|
+
* otherwise; marks and colours come from `palette.ts`, with its ASCII fallbacks and colour only where the look allows (FR-022).
|
|
27
|
+
*
|
|
28
|
+
* An export that failed is said once, dimmed, at the end of the run, and nowhere else (FR-024). Text from a signal is cleaned of control
|
|
29
|
+
* characters, so a signal never moves the cursor. Time comes only from the `now` given (nanoseconds since the Unix epoch, as signals' times).
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { initial, render } from "./render.ts";
|
|
34
|
+
* import { defineSignals } from "./signals.ts";
|
|
35
|
+
* const registry = defineSignals({ "build.step": { signal: "span", role: "step", attributes: { target: "string" } } });
|
|
36
|
+
* let state = initial({ mode: "plain", look: { colour: false, emoji: true, columns: 80 }, registry });
|
|
37
|
+
* // ...for each signal: ({ state, frames } = render(state, { kind: "span-end", span, source }, now)); write(frames);
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @module
|
|
41
|
+
*/
|
|
42
|
+
import { absurd } from "@intellectual-frontiers/core";
|
|
43
|
+
import { SEVERITY, severityText, } from "./otel.js";
|
|
44
|
+
import { encodeLogs } from "./otlp.js";
|
|
45
|
+
import { label, mark, paint } from "@intellectual-frontiers/terminal/palette";
|
|
46
|
+
import { roleOf } from "./signals.js";
|
|
47
|
+
import { cutToWidth } from "@intellectual-frontiers/terminal/width";
|
|
48
|
+
/** Every mode, in a fixed order. */
|
|
49
|
+
export const MODES = ["attended", "plain", "quiet", "silent", "json"];
|
|
50
|
+
/** A mode by its name; undefined for another text. */
|
|
51
|
+
export function parseMode(text) {
|
|
52
|
+
return MODES.find((m) => m === text.trim().toLowerCase());
|
|
53
|
+
}
|
|
54
|
+
/** The mode to use: the one asked for where it names one, else `attended` at a terminal and `plain` elsewhere (FR-026). */
|
|
55
|
+
export function chooseMode(facts) {
|
|
56
|
+
const asked = facts.requested === undefined ? undefined : parseMode(facts.requested);
|
|
57
|
+
return asked ?? (facts.tty ? "attended" : "plain");
|
|
58
|
+
}
|
|
59
|
+
/** The state a renderer starts in. */
|
|
60
|
+
export function initial(config) {
|
|
61
|
+
return { config, spans: new Map(), live: [], drawn: 0, warnings: [], source: undefined, finished: false };
|
|
62
|
+
}
|
|
63
|
+
const ESC = String.fromCharCode(0x1b);
|
|
64
|
+
const CONTROL = new RegExp(`[${String.fromCharCode(0)}-${String.fromCharCode(0x1f)}${String.fromCharCode(0x7f)}-${String.fromCharCode(0x9f)}]`, "g");
|
|
65
|
+
const NS_PER_MS = 1000000n;
|
|
66
|
+
/** A text with every control character but the line feed replaced (a tab by a space, the rest by `?`), so it never moves the cursor. */
|
|
67
|
+
export function clean(text) {
|
|
68
|
+
return text
|
|
69
|
+
.split("\n")
|
|
70
|
+
.map((l) => l.replace(CONTROL, (c) => (c === "\t" ? " " : "?")))
|
|
71
|
+
.join("\n");
|
|
72
|
+
}
|
|
73
|
+
function valueText(v) {
|
|
74
|
+
if (v instanceof Uint8Array)
|
|
75
|
+
return `${v.length} bytes`;
|
|
76
|
+
if (Array.isArray(v))
|
|
77
|
+
return v.map(String).join(",");
|
|
78
|
+
return String(v);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The text of a signal by default: a span's is its kind and its attributes' values, in their order, separated by spaces (`build site`); a
|
|
82
|
+
* log record's is its attributes' values alone, or its kind where it has none (a log record with a string body shows the body).
|
|
83
|
+
*/
|
|
84
|
+
export function defaultText(name, attributes, signal = "span") {
|
|
85
|
+
const values = Object.values(attributes).map(valueText);
|
|
86
|
+
return signal === "log" && values.length > 0 ? values.join(" ") : [name, ...values].join(" ");
|
|
87
|
+
}
|
|
88
|
+
/** A duration in nanoseconds as a person reads it: `420ms`, `1.2s`, `3m04s`, `2h05m` (cut, never rounded up; a negative one is `0ms`). */
|
|
89
|
+
export function duration(ns) {
|
|
90
|
+
const ms = ns < 0n ? 0 : Number(ns / NS_PER_MS);
|
|
91
|
+
if (ms < 1000)
|
|
92
|
+
return `${ms}ms`;
|
|
93
|
+
if (ms < 60_000)
|
|
94
|
+
return `${(Math.floor(ms / 100) / 10).toFixed(1)}s`;
|
|
95
|
+
const s = Math.floor(ms / 1000);
|
|
96
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
97
|
+
if (s < 3600)
|
|
98
|
+
return `${Math.floor(s / 60)}m${pad(s % 60)}s`;
|
|
99
|
+
return `${Math.floor(s / 3600)}h${pad(Math.floor(s / 60) % 60)}m`;
|
|
100
|
+
}
|
|
101
|
+
/** How far a span has come: `done/total` and the percentage where there is a positive total, else `done`. */
|
|
102
|
+
export function progressText(done, total) {
|
|
103
|
+
if (total === undefined || !(total > 0))
|
|
104
|
+
return `${done}`;
|
|
105
|
+
const pct = Math.max(0, Math.min(100, Math.floor((done / total) * 100)));
|
|
106
|
+
return `${done}/${total} ${pct}%`;
|
|
107
|
+
}
|
|
108
|
+
const keyOf = (traceId, spanId) => `${traceId}:${spanId}`;
|
|
109
|
+
const shownRole = (role) => role !== undefined && role !== "none";
|
|
110
|
+
const numberOf = (v) => typeof v === "number" ? v : typeof v === "bigint" ? Number(v) : undefined;
|
|
111
|
+
/** How a span ended, as a mark: a `skipped` or `cancelled` event first, then its status. */
|
|
112
|
+
export function outcomeOf(span) {
|
|
113
|
+
if (span.events.some((e) => e.name === "skipped"))
|
|
114
|
+
return "skipped";
|
|
115
|
+
if (span.events.some((e) => e.name === "cancelled"))
|
|
116
|
+
return "cancelled";
|
|
117
|
+
return span.status.kind === "error" ? "failed" : "done";
|
|
118
|
+
}
|
|
119
|
+
/** What colour and emoji a mode may use: `plain` none of the colour, whatever the look. */
|
|
120
|
+
function capsOf(config) {
|
|
121
|
+
const colour = (config.mode === "attended" || config.mode === "quiet") && config.look.colour;
|
|
122
|
+
return { colour, emoji: config.look.emoji };
|
|
123
|
+
}
|
|
124
|
+
const indent = (depth) => " ".repeat(depth);
|
|
125
|
+
/** Lines of a text that may hold line feeds: the first after `head`, the rest under it, two columns further in. */
|
|
126
|
+
function block(depth, head, text) {
|
|
127
|
+
const [first = "", ...rest] = clean(text).split("\n");
|
|
128
|
+
return [indent(depth) + head(first), ...rest.map((l) => indent(depth + 1) + l)];
|
|
129
|
+
}
|
|
130
|
+
function endedLines(config, depth, text, kind, message, took) {
|
|
131
|
+
const caps = capsOf(config);
|
|
132
|
+
const after = (message === undefined || message === "" ? "" : `: ${paint("failure", clean(message).split("\n")[0] ?? "", caps)}`) +
|
|
133
|
+
` ${paint("dim", duration(took), caps)}`;
|
|
134
|
+
const lines = [...block(depth, (first) => label(kind, first, caps), text)];
|
|
135
|
+
const last = lines.length - 1;
|
|
136
|
+
lines[last] = `${lines[last] ?? ""}${after}`;
|
|
137
|
+
return lines;
|
|
138
|
+
}
|
|
139
|
+
function logLines(config, depth, role, severity, text) {
|
|
140
|
+
const caps = capsOf(config);
|
|
141
|
+
const marked = (kind) => (first) => label(kind, first, caps);
|
|
142
|
+
switch (role) {
|
|
143
|
+
case "hint":
|
|
144
|
+
return block(depth, marked("hint"), text);
|
|
145
|
+
case "warning":
|
|
146
|
+
return block(depth, marked("warning"), text);
|
|
147
|
+
case "pending":
|
|
148
|
+
return block(depth, marked("pending"), text);
|
|
149
|
+
case "action":
|
|
150
|
+
return block(depth, marked("action"), text);
|
|
151
|
+
case "error":
|
|
152
|
+
return block(depth, marked("failed"), text);
|
|
153
|
+
case "detail":
|
|
154
|
+
return block(depth, (first) => paint("dim", first, caps), text);
|
|
155
|
+
case "step":
|
|
156
|
+
case "progress":
|
|
157
|
+
case "result":
|
|
158
|
+
return severity >= SEVERITY.ERROR
|
|
159
|
+
? block(depth, marked("failed"), text)
|
|
160
|
+
: severity >= SEVERITY.WARN
|
|
161
|
+
? block(depth, marked("warning"), text)
|
|
162
|
+
: block(depth, (first) => first, text);
|
|
163
|
+
default:
|
|
164
|
+
return absurd(role);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function bodyText(body) {
|
|
168
|
+
return typeof body === "string" ? body : undefined;
|
|
169
|
+
}
|
|
170
|
+
function textOf(config, name, attributes, signal = "span") {
|
|
171
|
+
return config.text === undefined ? defaultText(name, attributes, signal) : config.text(name, attributes);
|
|
172
|
+
}
|
|
173
|
+
/** The live region's lines, nested by parent: each span under the nearest shown ancestor still in progress. */
|
|
174
|
+
function regionLines(state, now) {
|
|
175
|
+
const { config, live } = state;
|
|
176
|
+
const caps = capsOf(config);
|
|
177
|
+
const liveKeys = new Set(live.map((l) => l.key));
|
|
178
|
+
const ordered = [];
|
|
179
|
+
const visit = (l) => {
|
|
180
|
+
ordered.push(l);
|
|
181
|
+
for (const child of live)
|
|
182
|
+
if (child.anchor === l.key)
|
|
183
|
+
visit(child);
|
|
184
|
+
};
|
|
185
|
+
for (const l of live)
|
|
186
|
+
if (l.anchor === undefined || !liveKeys.has(l.anchor))
|
|
187
|
+
visit(l);
|
|
188
|
+
const lineOf = (l) => {
|
|
189
|
+
const first = clean(l.text).split("\n")[0] ?? "";
|
|
190
|
+
const progress = l.progress === undefined ? "" : ` ${paint("hint", progressText(l.progress.done, l.progress.total), caps)}`;
|
|
191
|
+
return `${indent(l.depth)}${paint("accent", `${first}…`, caps)} ${paint("dim", duration(now - l.start), caps)}${progress}`;
|
|
192
|
+
};
|
|
193
|
+
const max = Math.max(2, config.maxLive ?? 10);
|
|
194
|
+
if (ordered.length <= max)
|
|
195
|
+
return ordered.map(lineOf);
|
|
196
|
+
const kept = ordered.slice(0, max - 1).map(lineOf);
|
|
197
|
+
return [...kept, paint("dim", `… ${ordered.length - kept.length} more`, caps)];
|
|
198
|
+
}
|
|
199
|
+
/** The frames that erase the live region, write the final lines above it, and draw it again (attended), with the next state. */
|
|
200
|
+
function redraw(state, permanent, now, region = true) {
|
|
201
|
+
const cols = Math.max(1, state.config.look.columns);
|
|
202
|
+
const erase = state.drawn > 0 ? `\r${ESC}[${state.drawn}A${ESC}[J` : "";
|
|
203
|
+
const lines = region ? regionLines(state, now) : [];
|
|
204
|
+
const out = [...permanent, ...lines].map((l) => `${cutToWidth(l, cols)}\n`).join("");
|
|
205
|
+
return { state: { ...state, drawn: lines.length }, frames: erase + out };
|
|
206
|
+
}
|
|
207
|
+
/** The frames of final lines in the state's mode (not `json`). */
|
|
208
|
+
function emit(state, lines, now) {
|
|
209
|
+
const mode = state.config.mode;
|
|
210
|
+
switch (mode) {
|
|
211
|
+
case "attended":
|
|
212
|
+
return lines.length === 0
|
|
213
|
+
? { state, frames: "" }
|
|
214
|
+
: redraw(state, lines.map((l) => l.text), now);
|
|
215
|
+
case "plain":
|
|
216
|
+
return { state, frames: lines.map((l) => `${l.text}\n`).join("") };
|
|
217
|
+
case "quiet":
|
|
218
|
+
return { state, frames: lines.flatMap((l) => (l.quiet ? [`${l.text}\n`] : [])).join("") };
|
|
219
|
+
case "silent":
|
|
220
|
+
case "json":
|
|
221
|
+
return { state, frames: "" };
|
|
222
|
+
default:
|
|
223
|
+
return absurd(mode);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
function jsonLine(source, record) {
|
|
227
|
+
const s = source ?? { resource: { attributes: {} }, scope: { name: "ifc.console" } };
|
|
228
|
+
return `${JSON.stringify(encodeLogs([{ resource: s.resource, scope: s.scope, records: [record] }]))}\n`;
|
|
229
|
+
}
|
|
230
|
+
const isLoud = (role, severity) => role === "result" || role === "warning" || role === "error" || role === "action" || severity >= SEVERITY.WARN;
|
|
231
|
+
/** The line `plain` writes for a span still open at the end of the run: cancelled. */
|
|
232
|
+
function leftoverLines(state, now) {
|
|
233
|
+
return state.live
|
|
234
|
+
.flatMap((l) => endedLines(state.config, l.depth, l.text, "cancelled", undefined, now - l.start))
|
|
235
|
+
.map((text) => ({ text, quiet: false }));
|
|
236
|
+
}
|
|
237
|
+
function warningLines(state) {
|
|
238
|
+
const caps = capsOf(state.config);
|
|
239
|
+
return state.warnings.map((w) => ({
|
|
240
|
+
text: paint("dim", `${mark("warning", caps)} ${clean(w).split("\n").join(" ")}`, caps),
|
|
241
|
+
quiet: true,
|
|
242
|
+
}));
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* The next state and the frames to write for an input at the time `now` (nanoseconds since the Unix epoch). Pure: the same state, input
|
|
246
|
+
* and time give the same step. After `finish`, every input gives no frame.
|
|
247
|
+
*/
|
|
248
|
+
export function render(state, input, now) {
|
|
249
|
+
if (state.finished)
|
|
250
|
+
return { state, frames: "" };
|
|
251
|
+
const { config } = state;
|
|
252
|
+
switch (input.kind) {
|
|
253
|
+
case "span-start": {
|
|
254
|
+
const s = input.span;
|
|
255
|
+
const key = keyOf(s.context.traceId, s.context.spanId);
|
|
256
|
+
const pk = s.parentSpanId === undefined ? undefined : keyOf(s.context.traceId, s.parentSpanId);
|
|
257
|
+
const parent = pk === undefined ? undefined : state.spans.get(pk);
|
|
258
|
+
const shown = shownRole(roleOf(config.registry, s.name));
|
|
259
|
+
const depth = parent === undefined ? 0 : parent.shown ? parent.depth + 1 : parent.depth;
|
|
260
|
+
const anchor = parent === undefined ? undefined : parent.shown ? pk : parent.anchor;
|
|
261
|
+
const spans = new Map(state.spans).set(key, { depth, shown, anchor });
|
|
262
|
+
const live = shown
|
|
263
|
+
? [...state.live, { key, anchor, depth, text: textOf(config, s.name, s.attributes), start: s.startTime, progress: undefined }]
|
|
264
|
+
: state.live;
|
|
265
|
+
const next = { ...state, spans, live, source: input.source };
|
|
266
|
+
return shown && config.mode === "attended" ? redraw(next, [], now) : { state: next, frames: "" };
|
|
267
|
+
}
|
|
268
|
+
case "span-event": {
|
|
269
|
+
if (input.event.name !== "progress")
|
|
270
|
+
return { state, frames: "" };
|
|
271
|
+
const key = keyOf(input.span.context.traceId, input.span.context.spanId);
|
|
272
|
+
const done = numberOf(input.event.attributes["progress.done"]);
|
|
273
|
+
if (done === undefined || !state.live.some((l) => l.key === key))
|
|
274
|
+
return { state, frames: "" };
|
|
275
|
+
const progress = { done, total: numberOf(input.event.attributes["progress.total"]) };
|
|
276
|
+
const next = { ...state, live: state.live.map((l) => (l.key === key ? { ...l, progress } : l)) };
|
|
277
|
+
return config.mode === "attended" ? redraw(next, [], now) : { state: next, frames: "" };
|
|
278
|
+
}
|
|
279
|
+
case "span-end": {
|
|
280
|
+
const s = input.span;
|
|
281
|
+
const key = keyOf(s.context.traceId, s.context.spanId);
|
|
282
|
+
const known = state.spans.get(key);
|
|
283
|
+
const spans = new Map(state.spans);
|
|
284
|
+
spans.delete(key);
|
|
285
|
+
const next = { ...state, spans, live: state.live.filter((l) => l.key !== key), source: input.source };
|
|
286
|
+
const role = roleOf(config.registry, s.name);
|
|
287
|
+
if (!shownRole(role))
|
|
288
|
+
return { state: next, frames: "" };
|
|
289
|
+
const kind = outcomeOf(s);
|
|
290
|
+
const message = s.status.kind === "error" ? s.status.message : undefined;
|
|
291
|
+
const text = textOf(config, s.name, s.attributes);
|
|
292
|
+
const took = s.endTime - s.startTime;
|
|
293
|
+
if (config.mode === "json") {
|
|
294
|
+
const body = endedLines({ ...config, look: { ...config.look, colour: false, emoji: false } }, 0, text, kind, message, took).join("\n");
|
|
295
|
+
const severity = kind === "failed" ? SEVERITY.ERROR : SEVERITY.INFO;
|
|
296
|
+
const record = {
|
|
297
|
+
time: s.endTime,
|
|
298
|
+
observedTime: s.endTime,
|
|
299
|
+
severityNumber: severity,
|
|
300
|
+
severityText: severityText(severity),
|
|
301
|
+
body,
|
|
302
|
+
attributes: s.attributes,
|
|
303
|
+
droppedAttributesCount: 0,
|
|
304
|
+
eventName: s.name,
|
|
305
|
+
traceId: s.context.traceId,
|
|
306
|
+
spanId: s.context.spanId,
|
|
307
|
+
traceFlags: s.context.traceFlags,
|
|
308
|
+
};
|
|
309
|
+
return { state: next, frames: jsonLine(input.source, record) };
|
|
310
|
+
}
|
|
311
|
+
const quiet = kind === "failed" || role === "result" || role === "warning" || role === "error";
|
|
312
|
+
const lines = endedLines(config, known?.depth ?? 0, text, kind, message, took).map((t) => ({ text: t, quiet }));
|
|
313
|
+
return emit(next, lines, now);
|
|
314
|
+
}
|
|
315
|
+
case "log": {
|
|
316
|
+
const r = input.record;
|
|
317
|
+
const next = { ...state, source: input.source };
|
|
318
|
+
const role = r.eventName === undefined ? undefined : roleOf(config.registry, r.eventName);
|
|
319
|
+
if (!shownRole(role))
|
|
320
|
+
return { state: next, frames: "" };
|
|
321
|
+
if (config.mode === "json")
|
|
322
|
+
return { state: next, frames: jsonLine(input.source, r) };
|
|
323
|
+
const known = r.traceId === undefined || r.spanId === undefined ? undefined : state.spans.get(keyOf(r.traceId, r.spanId));
|
|
324
|
+
const depth = known === undefined ? 0 : known.shown ? known.depth + 1 : known.depth;
|
|
325
|
+
const text = bodyText(r.body) ?? textOf(config, r.eventName ?? r.severityText, r.attributes, "log");
|
|
326
|
+
const quiet = isLoud(role, r.severityNumber);
|
|
327
|
+
return emit(next, logLines(config, depth, role, r.severityNumber, text).map((t) => ({ text: t, quiet })), now);
|
|
328
|
+
}
|
|
329
|
+
case "export-warning":
|
|
330
|
+
return {
|
|
331
|
+
state: state.warnings.includes(input.message) ? state : { ...state, warnings: [...state.warnings, input.message] },
|
|
332
|
+
frames: "",
|
|
333
|
+
};
|
|
334
|
+
case "tick":
|
|
335
|
+
return config.mode === "attended" && state.live.length > 0 ? redraw(state, [], now) : { state, frames: "" };
|
|
336
|
+
case "finish": {
|
|
337
|
+
const done = { ...state, live: [], spans: new Map(), finished: true };
|
|
338
|
+
switch (config.mode) {
|
|
339
|
+
case "attended":
|
|
340
|
+
return redraw(done, [...leftoverLines(state, now), ...warningLines(state)].map((l) => l.text), now, false);
|
|
341
|
+
case "plain":
|
|
342
|
+
case "quiet":
|
|
343
|
+
return emit(done, [...leftoverLines(state, now), ...warningLines(state)], now);
|
|
344
|
+
case "silent":
|
|
345
|
+
return { state: done, frames: "" };
|
|
346
|
+
case "json": {
|
|
347
|
+
const frames = state.warnings
|
|
348
|
+
.map((w) => jsonLine(state.source, {
|
|
349
|
+
time: now,
|
|
350
|
+
observedTime: now,
|
|
351
|
+
severityNumber: SEVERITY.WARN,
|
|
352
|
+
severityText: severityText(SEVERITY.WARN),
|
|
353
|
+
body: w,
|
|
354
|
+
attributes: {},
|
|
355
|
+
droppedAttributesCount: 0,
|
|
356
|
+
eventName: "ifc.console.export",
|
|
357
|
+
}))
|
|
358
|
+
.join("");
|
|
359
|
+
return { state: done, frames };
|
|
360
|
+
}
|
|
361
|
+
default:
|
|
362
|
+
return absurd(config.mode);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
default:
|
|
366
|
+
return absurd(input);
|
|
367
|
+
}
|
|
368
|
+
}
|