@orthograph/prove 0.0.0-stage → 0.1.0-next.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 +202 -0
- package/README.md +2 -2
- package/dist/index.d.ts +32 -0
- package/dist/index.js +32 -0
- package/dist/modules/build/internal/manifest.d.ts +15 -0
- package/dist/modules/build/internal/manifest.js +38 -0
- package/dist/modules/build/service.d.ts +2 -0
- package/dist/modules/build/service.js +74 -0
- package/dist/modules/build/types.d.ts +39 -0
- package/dist/modules/build/types.js +4 -0
- package/dist/modules/environments/internal/collector.d.ts +2 -0
- package/dist/modules/environments/internal/collector.js +5 -0
- package/dist/modules/environments/internal/config.d.ts +32 -0
- package/dist/modules/environments/internal/config.js +42 -0
- package/dist/modules/environments/internal/identity.d.ts +3 -0
- package/dist/modules/environments/internal/identity.js +17 -0
- package/dist/modules/environments/internal/logbook.d.ts +10 -0
- package/dist/modules/environments/internal/logbook.js +23 -0
- package/dist/modules/environments/internal/process.d.ts +41 -0
- package/dist/modules/environments/internal/process.js +103 -0
- package/dist/modules/environments/internal/secrets.d.ts +9 -0
- package/dist/modules/environments/internal/secrets.js +23 -0
- package/dist/modules/environments/internal/status.d.ts +7 -0
- package/dist/modules/environments/internal/status.js +31 -0
- package/dist/modules/environments/service.d.ts +2 -0
- package/dist/modules/environments/service.js +97 -0
- package/dist/modules/environments/types.d.ts +80 -0
- package/dist/modules/environments/types.js +4 -0
- package/dist/modules/evals/internal/calls.d.ts +19 -0
- package/dist/modules/evals/internal/calls.js +168 -0
- package/dist/modules/evals/internal/evidence.d.ts +13 -0
- package/dist/modules/evals/internal/evidence.js +29 -0
- package/dist/modules/evals/internal/run.d.ts +12 -0
- package/dist/modules/evals/internal/run.js +85 -0
- package/dist/modules/evals/internal/secrets.d.ts +5 -0
- package/dist/modules/evals/internal/secrets.js +72 -0
- package/dist/modules/evals/service.d.ts +2 -0
- package/dist/modules/evals/service.js +29 -0
- package/dist/modules/evals/testing.d.ts +12 -0
- package/dist/modules/evals/testing.js +15 -0
- package/dist/modules/evals/types.d.ts +43 -0
- package/dist/modules/evals/types.js +1 -0
- package/dist/modules/impact/internal/changed.d.ts +6 -0
- package/dist/modules/impact/internal/changed.js +34 -0
- package/dist/modules/impact/internal/closure.d.ts +12 -0
- package/dist/modules/impact/internal/closure.js +69 -0
- package/dist/modules/impact/internal/digest.d.ts +1 -0
- package/dist/modules/impact/internal/digest.js +2 -0
- package/dist/modules/impact/internal/files.d.ts +11 -0
- package/dist/modules/impact/internal/files.js +48 -0
- package/dist/modules/impact/internal/ledger.d.ts +5 -0
- package/dist/modules/impact/internal/ledger.js +25 -0
- package/dist/modules/impact/internal/linked.d.ts +4 -0
- package/dist/modules/impact/internal/linked.js +56 -0
- package/dist/modules/impact/internal/scan.d.ts +12 -0
- package/dist/modules/impact/internal/scan.js +66 -0
- package/dist/modules/impact/service.d.ts +2 -0
- package/dist/modules/impact/service.js +88 -0
- package/dist/modules/impact/types.d.ts +109 -0
- package/dist/modules/impact/types.js +1 -0
- package/dist/modules/journeys/internal/checks.d.ts +25 -0
- package/dist/modules/journeys/internal/checks.js +155 -0
- package/dist/modules/journeys/internal/d1-facts.d.ts +10 -0
- package/dist/modules/journeys/internal/d1-facts.js +53 -0
- package/dist/modules/journeys/internal/database-facts.d.ts +2 -0
- package/dist/modules/journeys/internal/database-facts.js +41 -0
- package/dist/modules/journeys/internal/device.d.ts +16 -0
- package/dist/modules/journeys/internal/device.js +68 -0
- package/dist/modules/journeys/internal/keepers.d.ts +6 -0
- package/dist/modules/journeys/internal/keepers.js +6 -0
- package/dist/modules/journeys/internal/load.d.ts +4 -0
- package/dist/modules/journeys/internal/load.js +94 -0
- package/dist/modules/journeys/internal/stand-call.d.ts +10 -0
- package/dist/modules/journeys/internal/stand-call.js +22 -0
- package/dist/modules/journeys/internal/subject.d.ts +8 -0
- package/dist/modules/journeys/internal/subject.js +15 -0
- package/dist/modules/journeys/internal/world.d.ts +70 -0
- package/dist/modules/journeys/internal/world.js +133 -0
- package/dist/modules/journeys/service.d.ts +2 -0
- package/dist/modules/journeys/service.js +231 -0
- package/dist/modules/journeys/types.d.ts +132 -0
- package/dist/modules/journeys/types.js +5 -0
- package/dist/modules/portal-feed/internal/listen.d.ts +10 -0
- package/dist/modules/portal-feed/internal/listen.js +32 -0
- package/dist/modules/portal-feed/service.d.ts +2 -0
- package/dist/modules/portal-feed/service.js +6 -0
- package/dist/modules/portal-feed/types.d.ts +30 -0
- package/dist/modules/portal-feed/types.js +5 -0
- package/dist/modules/promote/internal/environment.d.ts +6 -0
- package/dist/modules/promote/internal/environment.js +10 -0
- package/dist/modules/promote/internal/gate.d.ts +9 -0
- package/dist/modules/promote/internal/gate.js +37 -0
- package/dist/modules/promote/internal/publishing.d.ts +23 -0
- package/dist/modules/promote/internal/publishing.js +21 -0
- package/dist/modules/promote/service.d.ts +2 -0
- package/dist/modules/promote/service.js +112 -0
- package/dist/modules/promote/types.d.ts +54 -0
- package/dist/modules/promote/types.js +8 -0
- package/dist/modules/prove/internal/changed-run.d.ts +9 -0
- package/dist/modules/prove/internal/changed-run.js +14 -0
- package/dist/modules/prove/internal/device-host.d.ts +16 -0
- package/dist/modules/prove/internal/device-host.js +60 -0
- package/dist/modules/prove/internal/empty-world.d.ts +5 -0
- package/dist/modules/prove/internal/empty-world.js +26 -0
- package/dist/modules/prove/internal/exercise.d.ts +35 -0
- package/dist/modules/prove/internal/exercise.js +47 -0
- package/dist/modules/prove/internal/proof-verdicts.d.ts +5 -0
- package/dist/modules/prove/internal/proof-verdicts.js +37 -0
- package/dist/modules/prove/internal/report.d.ts +35 -0
- package/dist/modules/prove/internal/report.js +169 -0
- package/dist/modules/prove/internal/staged.d.ts +9 -0
- package/dist/modules/prove/internal/staged.js +30 -0
- package/dist/modules/prove/internal/stand.d.ts +40 -0
- package/dist/modules/prove/internal/stand.js +155 -0
- package/dist/modules/prove/internal/structured.d.ts +78 -0
- package/dist/modules/prove/internal/structured.js +55 -0
- package/dist/modules/prove/internal/test-run.d.ts +7 -0
- package/dist/modules/prove/internal/test-run.js +63 -0
- package/dist/modules/prove/service.d.ts +2 -0
- package/dist/modules/prove/service.js +98 -0
- package/dist/modules/prove/types.d.ts +120 -0
- package/dist/modules/prove/types.js +5 -0
- package/dist/modules/publish/internal/bundle.d.ts +24 -0
- package/dist/modules/publish/internal/bundle.js +46 -0
- package/dist/modules/publish/internal/env.d.ts +3 -0
- package/dist/modules/publish/internal/env.js +2 -0
- package/dist/modules/publish/internal/plan.d.ts +28 -0
- package/dist/modules/publish/internal/plan.js +28 -0
- package/dist/modules/publish/internal/registry.d.ts +24 -0
- package/dist/modules/publish/internal/registry.js +55 -0
- package/dist/modules/publish/internal/sea.d.ts +20 -0
- package/dist/modules/publish/internal/sea.js +77 -0
- package/dist/modules/publish/internal/source.d.ts +4 -0
- package/dist/modules/publish/internal/source.js +41 -0
- package/dist/modules/publish/internal/stage.d.ts +14 -0
- package/dist/modules/publish/internal/stage.js +95 -0
- package/dist/modules/publish/internal/tap.d.ts +22 -0
- package/dist/modules/publish/internal/tap.js +61 -0
- package/dist/modules/publish/service.d.ts +2 -0
- package/dist/modules/publish/service.js +99 -0
- package/dist/modules/publish/types.d.ts +80 -0
- package/dist/modules/publish/types.js +4 -0
- package/dist/modules/releases/internal/http.d.ts +12 -0
- package/dist/modules/releases/internal/http.js +14 -0
- package/dist/modules/releases/internal/stale.d.ts +13 -0
- package/dist/modules/releases/internal/stale.js +43 -0
- package/dist/modules/releases/service.d.ts +2 -0
- package/dist/modules/releases/service.js +59 -0
- package/dist/modules/releases/types.d.ts +81 -0
- package/dist/modules/releases/types.js +6 -0
- package/dist/modules/secrets/internal/file.d.ts +5 -0
- package/dist/modules/secrets/internal/file.js +25 -0
- package/dist/modules/secrets/service.d.ts +2 -0
- package/dist/modules/secrets/service.js +38 -0
- package/dist/modules/secrets/types.d.ts +44 -0
- package/dist/modules/secrets/types.js +7 -0
- package/dist/modules/server-config/internal/jsonc.d.ts +2 -0
- package/dist/modules/server-config/internal/jsonc.js +29 -0
- package/dist/modules/server-config/service.d.ts +2 -0
- package/dist/modules/server-config/service.js +21 -0
- package/dist/modules/server-config/types.d.ts +22 -0
- package/dist/modules/server-config/types.js +3 -0
- package/dist/modules/sinks/internal/axiom.d.ts +29 -0
- package/dist/modules/sinks/internal/axiom.js +25 -0
- package/dist/modules/sinks/internal/keychain.d.ts +2 -0
- package/dist/modules/sinks/internal/keychain.js +13 -0
- package/dist/modules/sinks/service.d.ts +2 -0
- package/dist/modules/sinks/service.js +26 -0
- package/dist/modules/sinks/types.d.ts +37 -0
- package/dist/modules/sinks/types.js +5 -0
- package/dist/modules/telemetry/internal/axiom-source.d.ts +2 -0
- package/dist/modules/telemetry/internal/axiom-source.js +108 -0
- package/dist/modules/telemetry/internal/binary.d.ts +11 -0
- package/dist/modules/telemetry/internal/binary.js +66 -0
- package/dist/modules/telemetry/internal/config.d.ts +13 -0
- package/dist/modules/telemetry/internal/config.js +22 -0
- package/dist/modules/telemetry/internal/file-metrics.d.ts +2 -0
- package/dist/modules/telemetry/internal/file-metrics.js +60 -0
- package/dist/modules/telemetry/internal/file-source.d.ts +2 -0
- package/dist/modules/telemetry/internal/file-source.js +38 -0
- package/dist/modules/telemetry/internal/journey-root.d.ts +44 -0
- package/dist/modules/telemetry/internal/journey-root.js +36 -0
- package/dist/modules/telemetry/internal/local.d.ts +11 -0
- package/dist/modules/telemetry/internal/local.js +57 -0
- package/dist/modules/telemetry/internal/otlp.d.ts +15 -0
- package/dist/modules/telemetry/internal/otlp.js +53 -0
- package/dist/modules/telemetry/internal/private-keys.d.ts +7 -0
- package/dist/modules/telemetry/internal/private-keys.js +28 -0
- package/dist/modules/telemetry/internal/process.d.ts +27 -0
- package/dist/modules/telemetry/internal/process.js +96 -0
- package/dist/modules/telemetry/internal/runs.d.ts +17 -0
- package/dist/modules/telemetry/internal/runs.js +48 -0
- package/dist/modules/telemetry/service.d.ts +9 -0
- package/dist/modules/telemetry/service.js +48 -0
- package/dist/modules/telemetry/types.d.ts +138 -0
- package/dist/modules/telemetry/types.js +13 -0
- package/dist/modules/test-worlds/internal/lifetime.d.ts +3 -0
- package/dist/modules/test-worlds/internal/lifetime.js +19 -0
- package/dist/modules/test-worlds/service.d.ts +2 -0
- package/dist/modules/test-worlds/service.js +35 -0
- package/dist/modules/test-worlds/types.d.ts +41 -0
- package/dist/modules/test-worlds/types.js +4 -0
- package/dist/modules/trace-trees/service.d.ts +2 -0
- package/dist/modules/trace-trees/service.js +40 -0
- package/dist/modules/trace-trees/types.d.ts +9 -0
- package/dist/modules/trace-trees/types.js +1 -0
- package/dist/modules/verdicts/internal/proof.d.ts +24 -0
- package/dist/modules/verdicts/internal/proof.js +59 -0
- package/dist/modules/verdicts/service.d.ts +16 -0
- package/dist/modules/verdicts/service.js +71 -0
- package/dist/modules/verdicts/types.d.ts +80 -0
- package/dist/modules/verdicts/types.js +4 -0
- package/dist/modules/warm/internal/source.d.ts +7 -0
- package/dist/modules/warm/internal/source.js +27 -0
- package/dist/modules/warm/service.d.ts +2 -0
- package/dist/modules/warm/service.js +76 -0
- package/dist/modules/warm/types.d.ts +43 -0
- package/dist/modules/warm/types.js +4 -0
- package/dist/modules/watch/internal/choose.d.ts +20 -0
- package/dist/modules/watch/internal/choose.js +24 -0
- package/dist/modules/watch/internal/evidence.d.ts +9 -0
- package/dist/modules/watch/internal/evidence.js +26 -0
- package/dist/modules/watch/internal/report.d.ts +18 -0
- package/dist/modules/watch/internal/report.js +59 -0
- package/dist/modules/watch/internal/tasks.d.ts +5 -0
- package/dist/modules/watch/internal/tasks.js +35 -0
- package/dist/modules/watch/service.d.ts +2 -0
- package/dist/modules/watch/service.js +38 -0
- package/dist/modules/watch/types.d.ts +44 -0
- package/dist/modules/watch/types.js +4 -0
- package/dist/modules/world/internal/ports.d.ts +8 -0
- package/dist/modules/world/internal/ports.js +30 -0
- package/dist/modules/world/internal/wrangler.d.ts +46 -0
- package/dist/modules/world/internal/wrangler.js +112 -0
- package/dist/modules/world/service.d.ts +2 -0
- package/dist/modules/world/service.js +46 -0
- package/dist/modules/world/types.d.ts +97 -0
- package/dist/modules/world/types.js +4 -0
- package/dist/modules/world-clock/internal/door.d.ts +8 -0
- package/dist/modules/world-clock/internal/door.js +9 -0
- package/dist/modules/world-clock/service.d.ts +2 -0
- package/dist/modules/world-clock/service.js +9 -0
- package/dist/modules/world-clock/types.d.ts +34 -0
- package/dist/modules/world-clock/types.js +1 -0
- package/package.json +38 -3
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// The metrics reader over the collector's files (portal.md hole 2): what `ortho.calls` counted in a window. It reads `metrics.jsonl`, OTLP
|
|
2
|
+
// JSON lines, and answers each series' growth over the window, whichever temporality the exporter used: a delta point is what was counted
|
|
3
|
+
// in its interval, a cumulative one is a running total, so its growth is its last total in the window less the total just before it.
|
|
4
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
5
|
+
import { Effect } from "effect";
|
|
6
|
+
import { SourceFailed } from "../types.js";
|
|
7
|
+
const DELTA = new Set([1, "AGGREGATION_TEMPORALITY_DELTA"]);
|
|
8
|
+
/** Every counter data point in the files, in the order exported. */
|
|
9
|
+
const samplesIn = (dirs) => dirs.flatMap((dir) => {
|
|
10
|
+
const file = `${dir}/metrics.jsonl`;
|
|
11
|
+
if (!existsSync(file))
|
|
12
|
+
return [];
|
|
13
|
+
// Whole lines only: the exporter may be mid-line, and the next read sees it complete.
|
|
14
|
+
return readFileSync(file, "utf8").split("\n").slice(0, -1).filter((l) => l.trim() !== "").flatMap((line) => {
|
|
15
|
+
const doc = JSON.parse(line);
|
|
16
|
+
return (doc.resourceMetrics ?? []).flatMap((r) => (r.scopeMetrics ?? []).flatMap((s) => (s.metrics ?? []).map((metric) => ({ metric, producer: `${dir}\u0000${JSON.stringify(r.resource?.attributes ?? [])}\u0000${JSON.stringify(s.scope ?? null)}` })))).flatMap(({ metric, producer }) => (metric.sum?.dataPoints ?? []).map((point) => ({
|
|
17
|
+
producer, name: metric.name,
|
|
18
|
+
labels: Object.fromEntries((point.attributes ?? []).map(({ key, value }) => [key, value.stringValue ?? ""])),
|
|
19
|
+
at: Number(BigInt(point.timeUnixNano ?? "0") / 1000000n),
|
|
20
|
+
value: Number(point.asInt ?? point.asDouble ?? 0),
|
|
21
|
+
delta: DELTA.has(metric.sum?.aggregationTemporality),
|
|
22
|
+
})));
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
/** A series is one producer's counter: its resource, its scope and its source folder tell two processes that count the same name and labels apart. */
|
|
26
|
+
const keyOf = (s) => `${s.producer}\u0000${s.name}\u0000${JSON.stringify(Object.entries(s.labels).sort(([a], [b]) => (a < b ? -1 : 1)))}`;
|
|
27
|
+
/** One series' growth in `(from, now]`: its samples walked in order, each adding what it grew by, or its whole total after a restart (a total that fell). */
|
|
28
|
+
const growth = (series, from, now) => {
|
|
29
|
+
const sorted = [...series].filter((s) => s.at <= now).sort((a, b) => a.at - b.at);
|
|
30
|
+
if (sorted.length === 0)
|
|
31
|
+
return 0;
|
|
32
|
+
if (sorted[0].delta)
|
|
33
|
+
return sorted.filter((s) => s.at > from).reduce((n, s) => n + s.value, 0);
|
|
34
|
+
// With no sample before the window the series is counted from zero. That is exact in a prove world: its collector is made fresh and its
|
|
35
|
+
// counters start with it, so nothing was counted before its first sample. An older series would need history, which is M7.14's.
|
|
36
|
+
let previous = [...sorted].reverse().find((s) => s.at <= from)?.value ?? 0;
|
|
37
|
+
let total = 0;
|
|
38
|
+
for (const sample of sorted.filter((s) => s.at > from)) {
|
|
39
|
+
total += sample.value >= previous ? sample.value - previous : sample.value;
|
|
40
|
+
previous = sample.value;
|
|
41
|
+
}
|
|
42
|
+
return total;
|
|
43
|
+
};
|
|
44
|
+
const labelsKey = (labels) => JSON.stringify(Object.entries(labels).sort(([a], [b]) => (a < b ? -1 : 1)));
|
|
45
|
+
export const fileMetrics = (dirs) => ({
|
|
46
|
+
window: (since, now) => Effect.try({ try: () => samplesIn(dirs), catch: () => new SourceFailed({ message: "the collector's metrics could not be read" }) }).pipe(Effect.map((samples) => {
|
|
47
|
+
const series = new Map();
|
|
48
|
+
for (const s of samples)
|
|
49
|
+
series.set(keyOf(s), [...(series.get(keyOf(s)) ?? []), s]);
|
|
50
|
+
// Each producer's growth is its own; what the portal reads is the sum over producers of the same name and labels.
|
|
51
|
+
const summed = new Map();
|
|
52
|
+
for (const all of series.values()) {
|
|
53
|
+
const grew = growth(all, now - since, now);
|
|
54
|
+
const key = `${all[0].name}\u0000${labelsKey(all[0].labels)}`;
|
|
55
|
+
const held = summed.get(key);
|
|
56
|
+
summed.set(key, { name: all[0].name, labels: all[0].labels, value: (held?.value ?? 0) + grew });
|
|
57
|
+
}
|
|
58
|
+
return [...summed.values()].filter((p) => p.value > 0);
|
|
59
|
+
})),
|
|
60
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// The TraceSource over the collector's files: what prove reads, and what `ortho telemetry trace` reads.
|
|
2
|
+
import { Clock, Effect, Schedule } from "effect";
|
|
3
|
+
import { arrived, readAll } from "./otlp.js";
|
|
4
|
+
/** Nothing new for this long after every trace looks complete: the spike's rule C. */
|
|
5
|
+
const QUIET_MS = 250;
|
|
6
|
+
const POLL_MS = 25;
|
|
7
|
+
const LIMIT_MS = 10_000;
|
|
8
|
+
const listOf = (dirs) => (typeof dirs === "string" ? [dirs] : dirs);
|
|
9
|
+
/** What the collector has written to `dirs`, read back by trace id once each wanted trace is complete. */
|
|
10
|
+
const traces = (dirs) => (wanted) => Effect.gen(function* () {
|
|
11
|
+
let last = readAll(dirs);
|
|
12
|
+
let quietMs = 0;
|
|
13
|
+
const settled = Effect.sync(() => {
|
|
14
|
+
const now = readAll(dirs);
|
|
15
|
+
quietMs = now.size === last.size ? quietMs + POLL_MS : 0;
|
|
16
|
+
last = now;
|
|
17
|
+
return quietMs >= QUIET_MS && wanted.every((w) => arrived(now.byTrace.get(w.trace), w));
|
|
18
|
+
});
|
|
19
|
+
yield* settled.pipe(Effect.repeat({ schedule: Schedule.spaced(`${POLL_MS} millis`), until: (done) => done }), Effect.timeoutOption(`${LIMIT_MS} millis`));
|
|
20
|
+
return new Map(wanted.map((w) => [w.trace, last.byTrace.get(w.trace) ?? { spans: [], logs: [] }]));
|
|
21
|
+
});
|
|
22
|
+
export const FileSource = {
|
|
23
|
+
open: (given) => {
|
|
24
|
+
const dirs = listOf(given);
|
|
25
|
+
return {
|
|
26
|
+
traces: traces(dirs),
|
|
27
|
+
find: (trace) => Effect.sync(() => {
|
|
28
|
+
const found = readAll(dirs).byTrace.get(trace);
|
|
29
|
+
return found && (found.spans.length > 0 || found.logs.length > 0) ? found : undefined;
|
|
30
|
+
}),
|
|
31
|
+
window: (since, given) => Effect.gen(function* () {
|
|
32
|
+
const now = given ?? (yield* Clock.currentTimeMillis);
|
|
33
|
+
const all = readAll(dirs).byTrace;
|
|
34
|
+
return new Map([...all].filter(([, t]) => t.spans.some((s) => s.start >= now - since)));
|
|
35
|
+
}),
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { Trace } from "../types.ts";
|
|
3
|
+
export interface JourneyRoot {
|
|
4
|
+
readonly trace: string;
|
|
5
|
+
/** The span id the call's `traceparent` carried: the id this span is exported under. */
|
|
6
|
+
readonly span: string;
|
|
7
|
+
readonly journey: string;
|
|
8
|
+
readonly step: string;
|
|
9
|
+
readonly operation: string;
|
|
10
|
+
/** What the call answered with (HTTP status): a 5xx is a failed span, anything else is not. */
|
|
11
|
+
readonly status: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The span for one call, covering exactly the spans the server recorded for it (so it never starts after, or ends
|
|
15
|
+
* before, what it contains, whatever the two clocks say). Nothing when the server recorded nothing.
|
|
16
|
+
*/
|
|
17
|
+
export declare const rootOf: (call: JourneyRoot, trace: Trace | undefined) => {
|
|
18
|
+
traceId: string;
|
|
19
|
+
spanId: string;
|
|
20
|
+
name: string;
|
|
21
|
+
kind: number;
|
|
22
|
+
startTimeUnixNano: string;
|
|
23
|
+
endTimeUnixNano: string;
|
|
24
|
+
attributes: ({
|
|
25
|
+
key: string;
|
|
26
|
+
value: {
|
|
27
|
+
stringValue: string;
|
|
28
|
+
};
|
|
29
|
+
} | {
|
|
30
|
+
key: string;
|
|
31
|
+
value: {
|
|
32
|
+
intValue: string;
|
|
33
|
+
};
|
|
34
|
+
})[];
|
|
35
|
+
status: {
|
|
36
|
+
code: number;
|
|
37
|
+
message: string;
|
|
38
|
+
} | {
|
|
39
|
+
message?: never;
|
|
40
|
+
code: number;
|
|
41
|
+
};
|
|
42
|
+
} | undefined;
|
|
43
|
+
/** Send the spans to the collector at `url`, once. A collector that doesn't take them leaves the evals unchanged. */
|
|
44
|
+
export declare const exportRoots: (url: string, spans: ReadonlyArray<NonNullable<ReturnType<typeof rootOf>>>) => Effect.Effect<void, never, never>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// The journey's own span. A call's `traceparent` names a span prove made up, so every trace prove makes looks headless
|
|
2
|
+
// in any viewer ("root span not yet received"). Prove exports that span, named `journey.<name>`, so the viewer shows
|
|
3
|
+
// the journey as each trace's root (O1).
|
|
4
|
+
import { Effect } from "effect";
|
|
5
|
+
const nanos = (ms) => String(BigInt(Math.round(ms * 1000)) * 1000n);
|
|
6
|
+
/**
|
|
7
|
+
* The span for one call, covering exactly the spans the server recorded for it (so it never starts after, or ends
|
|
8
|
+
* before, what it contains, whatever the two clocks say). Nothing when the server recorded nothing.
|
|
9
|
+
*/
|
|
10
|
+
export const rootOf = (call, trace) => {
|
|
11
|
+
const mine = (trace?.spans ?? []).filter((s) => s.spanId !== call.span);
|
|
12
|
+
if (mine.length === 0)
|
|
13
|
+
return undefined;
|
|
14
|
+
const start = Math.min(...mine.map((s) => s.start));
|
|
15
|
+
const end = Math.max(...mine.map((s) => s.start + s.ms));
|
|
16
|
+
const text = (key, value) => ({ key, value: { stringValue: value } });
|
|
17
|
+
return {
|
|
18
|
+
traceId: call.trace, spanId: call.span, name: `journey.${call.journey}`, kind: 1,
|
|
19
|
+
startTimeUnixNano: nanos(start), endTimeUnixNano: nanos(end),
|
|
20
|
+
attributes: [text("journey", call.journey), text("journey.step", call.step), text("operation", call.operation), { key: "http.status_code", value: { intValue: String(call.status) } }],
|
|
21
|
+
status: call.status >= 500 ? { code: 2, message: `the call answered ${call.status}` } : { code: 1 },
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/** Send the spans to the collector at `url`, once. A collector that doesn't take them leaves the evals unchanged. */
|
|
25
|
+
export const exportRoots = (url, spans) => spans.length === 0
|
|
26
|
+
? Effect.void
|
|
27
|
+
: Effect.promise(() => fetch(`${url}/v1/traces`, {
|
|
28
|
+
method: "POST",
|
|
29
|
+
headers: { "content-type": "application/json" },
|
|
30
|
+
body: JSON.stringify({
|
|
31
|
+
resourceSpans: [{
|
|
32
|
+
resource: { attributes: [{ key: "service.name", value: { stringValue: "ortho-prove" } }, { key: "deployment.environment", value: { stringValue: "local" } }] },
|
|
33
|
+
scopeSpans: [{ scope: { name: "ortho.prove" }, spans }],
|
|
34
|
+
}],
|
|
35
|
+
}),
|
|
36
|
+
}).then(() => undefined, () => undefined));
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Server } from "node:http";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { CollectorFailed } from "../types.ts";
|
|
4
|
+
/** The same file format and privacy guard as the collector, on a loopback port picked by the OS. */
|
|
5
|
+
export declare const listen: (dir: string, strip: ReadonlyArray<string>) => Effect.Effect<{
|
|
6
|
+
readonly server: Server;
|
|
7
|
+
readonly url: string;
|
|
8
|
+
}, CollectorFailed, never>;
|
|
9
|
+
export declare const close: ({ server }: {
|
|
10
|
+
readonly server: Server;
|
|
11
|
+
}) => Effect.Effect<void, never, never>;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { appendFileSync } from "node:fs";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { Effect } from "effect";
|
|
5
|
+
import { CollectorFailed } from "../types.js";
|
|
6
|
+
import { logsOf, spansOf } from "./otlp.js";
|
|
7
|
+
/** The same file format and privacy guard as the collector, on a loopback port picked by the OS. */
|
|
8
|
+
export const listen = (dir, strip) => Effect.callback((resume) => {
|
|
9
|
+
const privateKeys = new Set(strip);
|
|
10
|
+
const server = createServer((request, response) => {
|
|
11
|
+
const signal = request.url === "/v1/traces" ? "traces"
|
|
12
|
+
: request.url === "/v1/logs" ? "logs"
|
|
13
|
+
: request.url === "/v1/metrics" ? "metrics" : undefined;
|
|
14
|
+
if (request.method !== "POST" || signal === undefined) {
|
|
15
|
+
response.writeHead(404).end();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
let body = "";
|
|
19
|
+
let bytes = 0;
|
|
20
|
+
const limit = 16 * 1024 * 1024;
|
|
21
|
+
request.setEncoding("utf8").on("data", (chunk) => {
|
|
22
|
+
bytes += Buffer.byteLength(chunk);
|
|
23
|
+
if (bytes <= limit)
|
|
24
|
+
body += chunk;
|
|
25
|
+
}).on("end", () => {
|
|
26
|
+
if (bytes > limit) {
|
|
27
|
+
response.writeHead(413).end();
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const parsed = Effect.runSync(Effect.result(Effect.try(() => {
|
|
31
|
+
const doc = JSON.parse(body);
|
|
32
|
+
if (signal === "traces")
|
|
33
|
+
spansOf(doc);
|
|
34
|
+
if (signal === "logs")
|
|
35
|
+
logsOf(doc);
|
|
36
|
+
return JSON.stringify(doc, (key, value) => key === "attributes" && Array.isArray(value)
|
|
37
|
+
? value.filter((attr) => typeof attr?.key === "string" && !privateKeys.has(attr.key) && !/^(user\.|organization\.id$)/.test(attr.key))
|
|
38
|
+
: value);
|
|
39
|
+
})));
|
|
40
|
+
if (parsed._tag === "Failure") {
|
|
41
|
+
response.writeHead(400).end();
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const written = Effect.runSync(Effect.result(Effect.try(() => appendFileSync(join(dir, `${signal}.jsonl`), `${parsed.success}\n`))));
|
|
45
|
+
response.writeHead(written._tag === "Success" ? 200 : 500, { "content-type": "application/json" }).end("{}");
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
server.once("error", (error) => resume(Effect.fail(new CollectorFailed({ message: `the local collector couldn't listen: ${error.message}` }))));
|
|
49
|
+
server.listen(0, "127.0.0.1", () => {
|
|
50
|
+
const { port } = server.address();
|
|
51
|
+
resume(Effect.succeed({ server, url: `http://127.0.0.1:${port}` }));
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
export const close = ({ server }) => Effect.callback((resume) => {
|
|
55
|
+
server.close(() => resume(Effect.void));
|
|
56
|
+
server.closeAllConnections();
|
|
57
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Log, Span, Trace, Wanted } from "../types.ts";
|
|
2
|
+
export declare const spansOf: (doc: any) => Array<Span>;
|
|
3
|
+
export declare const logsOf: (doc: any) => Array<Log>;
|
|
4
|
+
/** Everything the collector has written so far to these folders, by trace id, and how many bytes that was. */
|
|
5
|
+
export declare const readAll: (dirs: ReadonlyArray<string>) => {
|
|
6
|
+
readonly byTrace: Map<string, Trace>;
|
|
7
|
+
readonly size: number;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Root present (the span whose parent is the caller's), and no span pointing at a parent that isn't there. A span
|
|
11
|
+
* with no parent at all is a root of its own: the journey's span, when prove exported it. `rooted` waits for it.
|
|
12
|
+
*/
|
|
13
|
+
export declare const looksComplete: (trace: Trace | undefined, parent: string, rooted?: boolean) => boolean;
|
|
14
|
+
/** Whether what a source holds of a wanted trace is all that will come: complete, or empty when it may be. */
|
|
15
|
+
export declare const arrived: (trace: Trace | undefined, wanted: Wanted) => boolean;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Reading the collector's file exporter: OTLP JSON, one export per line.
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
const value = (v) => v === undefined ? undefined
|
|
4
|
+
: "stringValue" in v ? v.stringValue
|
|
5
|
+
: "intValue" in v ? Number(v.intValue)
|
|
6
|
+
: "boolValue" in v ? v.boolValue
|
|
7
|
+
: "doubleValue" in v ? v.doubleValue
|
|
8
|
+
: "arrayValue" in v ? (v.arrayValue.values ?? []).map(value)
|
|
9
|
+
: JSON.stringify(v);
|
|
10
|
+
const attrs = (kvs = []) => Object.fromEntries(kvs.map((kv) => [kv.key, value(kv.value)]));
|
|
11
|
+
export const spansOf = (doc) => (doc.resourceSpans ?? []).flatMap((rs) => (rs.scopeSpans ?? []).flatMap((ss) => (ss.spans ?? []).map((s) => ({
|
|
12
|
+
traceId: s.traceId, spanId: s.spanId, parentSpanId: s.parentSpanId ?? "", name: s.name, attrs: attrs(s.attributes),
|
|
13
|
+
status: s.status?.code ?? 0, ms: Number(BigInt(s.endTimeUnixNano) - BigInt(s.startTimeUnixNano)) / 1e6,
|
|
14
|
+
start: Number(BigInt(s.startTimeUnixNano) / 1000n) / 1e3,
|
|
15
|
+
}))));
|
|
16
|
+
export const logsOf = (doc) => (doc.resourceLogs ?? []).flatMap((rl) => (rl.scopeLogs ?? []).flatMap((sl) => (sl.logRecords ?? []).map((l) => {
|
|
17
|
+
const body = value(l.body);
|
|
18
|
+
return { traceId: l.traceId ?? "", spanId: l.spanId ?? "", body: Array.isArray(body) ? body.join(" ") : String(body), attrs: attrs(l.attributes) };
|
|
19
|
+
})));
|
|
20
|
+
const text = (path) => (existsSync(path) ? readFileSync(path, "utf8") : "");
|
|
21
|
+
/** Whole lines only: the exporter may be mid-line, and the next read sees that line complete. */
|
|
22
|
+
const docs = (content) => content.split("\n").slice(0, -1).filter((l) => l.trim() !== "").map((l) => JSON.parse(l));
|
|
23
|
+
/** Everything the collector has written so far to these folders, by trace id, and how many bytes that was. */
|
|
24
|
+
export const readAll = (dirs) => {
|
|
25
|
+
const byTrace = new Map();
|
|
26
|
+
const at = (id) => byTrace.get(id) ?? byTrace.set(id, { spans: [], logs: [] }).get(id);
|
|
27
|
+
let size = 0;
|
|
28
|
+
for (const dir of dirs) {
|
|
29
|
+
const traces = text(`${dir}/traces.jsonl`);
|
|
30
|
+
const logs = text(`${dir}/logs.jsonl`);
|
|
31
|
+
size += traces.length + logs.length;
|
|
32
|
+
for (const d of docs(traces))
|
|
33
|
+
for (const s of spansOf(d))
|
|
34
|
+
at(s.traceId).spans.push(s);
|
|
35
|
+
for (const d of docs(logs))
|
|
36
|
+
for (const l of logsOf(d))
|
|
37
|
+
at(l.traceId).logs.push(l);
|
|
38
|
+
}
|
|
39
|
+
return { byTrace, size };
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Root present (the span whose parent is the caller's), and no span pointing at a parent that isn't there. A span
|
|
43
|
+
* with no parent at all is a root of its own: the journey's span, when prove exported it. `rooted` waits for it.
|
|
44
|
+
*/
|
|
45
|
+
export const looksComplete = (trace, parent, rooted = false) => {
|
|
46
|
+
if (!trace)
|
|
47
|
+
return false;
|
|
48
|
+
const ids = new Set(trace.spans.map((s) => s.spanId));
|
|
49
|
+
const linked = trace.spans.some((s) => s.parentSpanId === parent) && trace.spans.every((s) => s.parentSpanId === "" || s.parentSpanId === parent || ids.has(s.parentSpanId));
|
|
50
|
+
return linked && (!rooted || ids.has(parent));
|
|
51
|
+
};
|
|
52
|
+
/** Whether what a source holds of a wanted trace is all that will come: complete, or empty when it may be. */
|
|
53
|
+
export const arrived = (trace, wanted) => (wanted.mayBeEmpty === true && (trace === undefined || trace.spans.length === 0)) || looksComplete(trace, wanted.parent, wanted.rooted);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Model } from "@orthograph/compiler";
|
|
2
|
+
/**
|
|
3
|
+
* `payload.<field>` and `answer.<field>` for every secret or private field of every operation, and the bare field name
|
|
4
|
+
* too (a log line annotated with it). A name that is public in another operation is deleted there as well: the guard
|
|
5
|
+
* leans towards deleting.
|
|
6
|
+
*/
|
|
7
|
+
export declare const privateKeys: (model: Model) => ReadonlyArray<string>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const nonPublic = (fields) => Object.entries(fields).filter(([, f]) => f.visibility !== undefined).map(([k]) => k);
|
|
2
|
+
/**
|
|
3
|
+
* `payload.<field>` and `answer.<field>` for every secret or private field of every operation, and the bare field name
|
|
4
|
+
* too (a log line annotated with it). A name that is public in another operation is deleted there as well: the guard
|
|
5
|
+
* leans towards deleting.
|
|
6
|
+
*/
|
|
7
|
+
export const privateKeys = (model) => {
|
|
8
|
+
const keys = new Set();
|
|
9
|
+
for (const op of Object.values(model.operations)) {
|
|
10
|
+
for (const k of nonPublic(op.payload))
|
|
11
|
+
for (const key of [`payload.${k}`, k])
|
|
12
|
+
keys.add(key);
|
|
13
|
+
for (const k of nonPublic(op.answer))
|
|
14
|
+
for (const key of [`answer.${k}`, k])
|
|
15
|
+
keys.add(key);
|
|
16
|
+
}
|
|
17
|
+
for (const t of Object.values(model.things)) {
|
|
18
|
+
for (const k of nonPublic(t.fields))
|
|
19
|
+
keys.add(k);
|
|
20
|
+
for (const e of Object.values(t.errors))
|
|
21
|
+
for (const k of nonPublic(e.fields))
|
|
22
|
+
keys.add(k);
|
|
23
|
+
for (const e of Object.values(t.events))
|
|
24
|
+
for (const k of nonPublic(e))
|
|
25
|
+
keys.add(k);
|
|
26
|
+
}
|
|
27
|
+
return [...keys].sort();
|
|
28
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { CollectorFailed } from "../types.ts";
|
|
3
|
+
import { type CollectorOptions } from "./config.ts";
|
|
4
|
+
import { type Marker } from "./runs.ts";
|
|
5
|
+
export interface Started {
|
|
6
|
+
readonly url: string;
|
|
7
|
+
readonly marker: Marker;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Start `otelcol-contrib` on a port the system chose, writing to `dir`. `owned` ties it to this process (`sweepOrphans` finds
|
|
11
|
+
* it if this process is killed first); otherwise it is detached and an environment's `down` stops it.
|
|
12
|
+
*/
|
|
13
|
+
export declare const runCollector: (dir: string, options: Pick<CollectorOptions, "strip">, owned: boolean) => Effect.Effect<{
|
|
14
|
+
url: string;
|
|
15
|
+
marker: Marker;
|
|
16
|
+
}, CollectorFailed, never>;
|
|
17
|
+
/** The last of what the collector wrote to its log: why it didn't come up. */
|
|
18
|
+
export declare const logTail: (dir: string) => string;
|
|
19
|
+
/** Stop the process a marker names, and only if it still runs that command: ask, wait, then insist. Nothing to stop is not an error. */
|
|
20
|
+
export declare const terminate: (marker: Marker) => Effect.Effect<void, never, never>;
|
|
21
|
+
/** Stop the collector an environment launched in `dir`, by its marker. */
|
|
22
|
+
export declare const stopCollector: (dir: string) => Effect.Effect<void, never, never>;
|
|
23
|
+
/**
|
|
24
|
+
* Stop every collector whose owner process is gone: a prove killed before it could clean up leaves its collector running. Collectors of a
|
|
25
|
+
* live process, and an environment's own (stopped by `env down`), are never touched. Answers the PIDs it stopped.
|
|
26
|
+
*/
|
|
27
|
+
export declare const sweepOrphans: () => Effect.Effect<ReadonlyArray<number>>;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// The Collector as a process: started from its pinned binary on a loopback port, owned by PID, stopped by PID.
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { closeSync, openSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { createServer } from "node:net";
|
|
5
|
+
import { Effect, Schedule } from "effect";
|
|
6
|
+
import { CollectorFailed } from "../types.js";
|
|
7
|
+
import { collectorBinary } from "./binary.js";
|
|
8
|
+
import { collectorConfig } from "./config.js";
|
|
9
|
+
import { alive, forget, markers, record, stillTheSame } from "./runs.js";
|
|
10
|
+
/** A port the system says is free. */
|
|
11
|
+
const freePort = Effect.callback((resume) => {
|
|
12
|
+
const server = createServer();
|
|
13
|
+
server.once("error", () => resume(Effect.fail(new CollectorFailed({ message: "no loopback port was free for the collector" }))));
|
|
14
|
+
server.listen(0, "127.0.0.1", () => {
|
|
15
|
+
const { port } = server.address();
|
|
16
|
+
server.close(() => resume(Effect.succeed(port)));
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
/** Where a collector keeps its own marker, so whoever stops it by folder can find it. */
|
|
20
|
+
const markerFile = (dir) => `${dir}/collector.json`;
|
|
21
|
+
/** The collector gets no environment but what it needs to find itself: nothing of the shell's keys reaches it. */
|
|
22
|
+
const KEPT = /^(PATH|HOME|TMPDIR|TZ|LANG|LC_[A-Z]+)$/;
|
|
23
|
+
/**
|
|
24
|
+
* Start `otelcol-contrib` on a port the system chose, writing to `dir`. `owned` ties it to this process (`sweepOrphans` finds
|
|
25
|
+
* it if this process is killed first); otherwise it is detached and an environment's `down` stops it.
|
|
26
|
+
*/
|
|
27
|
+
export const runCollector = (dir, options, owned) => Effect.gen(function* () {
|
|
28
|
+
const binary = yield* collectorBinary;
|
|
29
|
+
const port = yield* freePort;
|
|
30
|
+
const config = `${dir}/collector.yaml`;
|
|
31
|
+
writeFileSync(config, collectorConfig({ ...options, port, dir }));
|
|
32
|
+
const log = openSync(`${dir}/collector.log`, "a");
|
|
33
|
+
const child = spawn(binary, ["--config", `file:${config}`], {
|
|
34
|
+
stdio: ["ignore", log, log], detached: !owned, env: Object.fromEntries(Object.entries(process.env).filter(([name]) => KEPT.test(name))),
|
|
35
|
+
});
|
|
36
|
+
closeSync(log);
|
|
37
|
+
child.once("error", () => undefined);
|
|
38
|
+
if (child.pid === undefined)
|
|
39
|
+
return yield* new CollectorFailed({ message: `the collector at ${binary} could not start` });
|
|
40
|
+
if (!owned)
|
|
41
|
+
child.unref();
|
|
42
|
+
const marker = { kind: owned ? "collector" : "collector-env", pid: child.pid, owner: owned ? process.pid : 0, command: `${binary} --config file:${config}` };
|
|
43
|
+
record(marker);
|
|
44
|
+
if (!owned)
|
|
45
|
+
writeFileSync(markerFile(dir), JSON.stringify(marker));
|
|
46
|
+
return { url: `http://127.0.0.1:${port}`, marker };
|
|
47
|
+
});
|
|
48
|
+
const gone = (pid) => Effect.suspend(() => (alive(pid) ? Effect.fail("alive") : Effect.void));
|
|
49
|
+
/** The last of what the collector wrote to its log: why it didn't come up. */
|
|
50
|
+
export const logTail = (dir) => {
|
|
51
|
+
try {
|
|
52
|
+
return readFileSync(`${dir}/collector.log`, "utf8").trim().split("\n").slice(-5).join(" | ");
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return "";
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
/** Stop the process a marker names, and only if it still runs that command: ask, wait, then insist. Nothing to stop is not an error. */
|
|
59
|
+
export const terminate = (marker) => Effect.gen(function* () {
|
|
60
|
+
if (alive(marker.pid) && stillTheSame(marker)) {
|
|
61
|
+
const signal = (name) => Effect.sync(() => { try {
|
|
62
|
+
process.kill(marker.pid, name);
|
|
63
|
+
}
|
|
64
|
+
catch { /* gone already */ } });
|
|
65
|
+
yield* signal("SIGTERM");
|
|
66
|
+
const stopped = yield* gone(marker.pid).pipe(Effect.retry({ schedule: Schedule.spaced("50 millis"), times: 100 }), Effect.option);
|
|
67
|
+
if (stopped._tag === "None") {
|
|
68
|
+
yield* signal("SIGKILL");
|
|
69
|
+
yield* gone(marker.pid).pipe(Effect.retry({ schedule: Schedule.spaced("50 millis"), times: 100 }), Effect.ignore);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
forget(marker.kind, marker.pid);
|
|
73
|
+
});
|
|
74
|
+
/** Stop the collector an environment launched in `dir`, by its marker. */
|
|
75
|
+
export const stopCollector = (dir) => Effect.gen(function* () {
|
|
76
|
+
let marker;
|
|
77
|
+
try {
|
|
78
|
+
marker = JSON.parse(readFileSync(markerFile(dir), "utf8"));
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
marker = undefined;
|
|
82
|
+
}
|
|
83
|
+
if (marker !== undefined)
|
|
84
|
+
yield* terminate(marker);
|
|
85
|
+
rmSync(markerFile(dir), { force: true });
|
|
86
|
+
});
|
|
87
|
+
/**
|
|
88
|
+
* Stop every collector whose owner process is gone: a prove killed before it could clean up leaves its collector running. Collectors of a
|
|
89
|
+
* live process, and an environment's own (stopped by `env down`), are never touched. Answers the PIDs it stopped.
|
|
90
|
+
*/
|
|
91
|
+
export const sweepOrphans = () => Effect.gen(function* () {
|
|
92
|
+
const orphans = markers("collector").filter((m) => !alive(m.owner));
|
|
93
|
+
for (const m of orphans)
|
|
94
|
+
yield* terminate(m);
|
|
95
|
+
return orphans.map((m) => m.pid);
|
|
96
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface Marker {
|
|
2
|
+
readonly kind: string;
|
|
3
|
+
readonly pid: number;
|
|
4
|
+
/** The process that owns it and must stop it; 0 when something else (an environment's `down`) does. */
|
|
5
|
+
readonly owner: number;
|
|
6
|
+
readonly command: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const runDir: () => string;
|
|
9
|
+
export declare const record: (marker: Marker) => void;
|
|
10
|
+
export declare const forget: (kind: string, pid: number) => void;
|
|
11
|
+
/** The full command line `ps` shows for a PID, or undefined when there is no such process. */
|
|
12
|
+
export declare const commandOf: (pid: number) => string | undefined;
|
|
13
|
+
/** Whether the PID still runs the command it was marked with. */
|
|
14
|
+
export declare const stillTheSame: (marker: Marker) => boolean;
|
|
15
|
+
/** Whether a process is there. Not being allowed to signal it means it is: someone else's, and alive. */
|
|
16
|
+
export declare const alive: (pid: number) => boolean;
|
|
17
|
+
export declare const markers: (kind: string) => ReadonlyArray<Marker>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Owned processes, by PID (K10 section 8, M1.33). A process Orthograph starts leaves a marker `<kind>-<pid>.json` in the run
|
|
2
|
+
// folder: who owns it (a process that must outlive it, an environment's, has owner 0) and the command it was started with.
|
|
3
|
+
// A marker is a claim, never proof: the process is only ever signalled when `ps` still shows that command for the PID, so a
|
|
4
|
+
// recycled PID is left alone. `ORTHO_RUN_DIR` moves the folder: a gate points it at a folder of its own, and what is still
|
|
5
|
+
// running there when it ends is a leak.
|
|
6
|
+
import { spawnSync } from "node:child_process";
|
|
7
|
+
import { mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { cacheRoot } from "./binary.js";
|
|
10
|
+
export const runDir = () => process.env["ORTHO_RUN_DIR"] ?? join(cacheRoot(), "run");
|
|
11
|
+
const fileOf = (kind, pid) => join(runDir(), `${kind}-${pid}.json`);
|
|
12
|
+
export const record = (marker) => {
|
|
13
|
+
mkdirSync(runDir(), { recursive: true });
|
|
14
|
+
writeFileSync(fileOf(marker.kind, marker.pid), JSON.stringify(marker));
|
|
15
|
+
};
|
|
16
|
+
export const forget = (kind, pid) => rmSync(fileOf(kind, pid), { force: true });
|
|
17
|
+
/** The full command line `ps` shows for a PID, or undefined when there is no such process. */
|
|
18
|
+
export const commandOf = (pid) => {
|
|
19
|
+
const ps = spawnSync("ps", ["-ww", "-p", String(pid), "-o", "command="], { encoding: "utf8" });
|
|
20
|
+
return ps.status === 0 && ps.stdout.trim() !== "" ? ps.stdout.trim() : undefined;
|
|
21
|
+
};
|
|
22
|
+
/** Whether the PID still runs the command it was marked with. */
|
|
23
|
+
export const stillTheSame = (marker) => commandOf(marker.pid)?.startsWith(marker.command) === true;
|
|
24
|
+
/** Whether a process is there. Not being allowed to signal it means it is: someone else's, and alive. */
|
|
25
|
+
export const alive = (pid) => {
|
|
26
|
+
try {
|
|
27
|
+
process.kill(pid, 0);
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
return e.code === "EPERM";
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
export const markers = (kind) => (() => { try {
|
|
35
|
+
return readdirSync(runDir());
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return [];
|
|
39
|
+
} })()
|
|
40
|
+
.filter((name) => name.startsWith(`${kind}-`) && name.endsWith(".json"))
|
|
41
|
+
.flatMap((name) => {
|
|
42
|
+
try {
|
|
43
|
+
return [JSON.parse(readFileSync(join(runDir(), name), "utf8"))];
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type CollectorsApi, type FileMetricsApi } from "./types.ts";
|
|
2
|
+
export { FileSource } from "./internal/file-source.ts";
|
|
3
|
+
/** The metrics reader over a collector's files: what `ortho.calls` counted in a window. */
|
|
4
|
+
export declare const FileMetrics: FileMetricsApi;
|
|
5
|
+
export { AxiomSource } from "./internal/axiom-source.ts";
|
|
6
|
+
export { PROVE_SPAN_PREFIX } from "./types.ts";
|
|
7
|
+
export { exportRoots, rootOf, type JourneyRoot } from "./internal/journey-root.ts";
|
|
8
|
+
export { privateKeys } from "./internal/private-keys.ts";
|
|
9
|
+
export declare const Collectors: CollectorsApi;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { mkdirSync, readdirSync, rmSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { Effect, Schedule } from "effect";
|
|
4
|
+
import { CollectorFailed } from "./types.js";
|
|
5
|
+
import { logTail, runCollector, stopCollector, sweepOrphans, terminate } from "./internal/process.js";
|
|
6
|
+
import { FileSource } from "./internal/file-source.js";
|
|
7
|
+
import { fileMetrics } from "./internal/file-metrics.js";
|
|
8
|
+
import { close, listen } from "./internal/local.js";
|
|
9
|
+
export { FileSource } from "./internal/file-source.js";
|
|
10
|
+
/** The metrics reader over a collector's files: what `ortho.calls` counted in a window. */
|
|
11
|
+
export const FileMetrics = { open: (dirs) => fileMetrics(typeof dirs === "string" ? [dirs] : dirs) };
|
|
12
|
+
export { AxiomSource } from "./internal/axiom-source.js";
|
|
13
|
+
export { PROVE_SPAN_PREFIX } from "./types.js";
|
|
14
|
+
export { exportRoots, rootOf } from "./internal/journey-root.js";
|
|
15
|
+
export { privateKeys } from "./internal/private-keys.js";
|
|
16
|
+
/** An empty export answers `{"partialSuccess":{}}` once the receiver is up: the spike's readiness check. */
|
|
17
|
+
const ready = (url) => Effect.tryPromise({
|
|
18
|
+
try: () => fetch(`${url}/v1/traces`, { method: "POST", headers: { "content-type": "application/json" }, body: "{}" }).then((r) => r.ok),
|
|
19
|
+
catch: () => false,
|
|
20
|
+
}).pipe(Effect.flatMap((ok) => (ok ? Effect.void : Effect.fail(false))));
|
|
21
|
+
/** Wait until the collector at `url` takes an export. */
|
|
22
|
+
const waitReady = (url, dir) => ready(url).pipe(Effect.retry({ schedule: Schedule.spaced("100 millis"), times: 100 }), Effect.mapError(() => new CollectorFailed({ message: `the collector didn't answer at ${url} within 10 s: ${logTail(dir)}` })));
|
|
23
|
+
/** An empty folder for a collector to write into. */
|
|
24
|
+
const emptied = (dir) => {
|
|
25
|
+
mkdirSync(dir, { recursive: true });
|
|
26
|
+
for (const entry of readdirSync(dir))
|
|
27
|
+
rmSync(join(dir, entry), { recursive: true, force: true });
|
|
28
|
+
};
|
|
29
|
+
export const Collectors = {
|
|
30
|
+
local: (dir, strip) => Effect.try({ try: () => emptied(dir), catch: () => new CollectorFailed({ message: `couldn't prepare the collector folder ${dir}` }) }).pipe(Effect.andThen(Effect.acquireRelease(listen(dir, strip), close)), Effect.map(({ url }) => ({ url, ...FileSource.open(dir) }))),
|
|
31
|
+
start: (dir, options) => Effect.gen(function* () {
|
|
32
|
+
yield* sweepOrphans();
|
|
33
|
+
emptied(dir);
|
|
34
|
+
const started = yield* Effect.acquireRelease(runCollector(dir, { strip: options?.strip ?? [] }, true), ({ marker }) => terminate(marker));
|
|
35
|
+
yield* waitReady(started.url, dir);
|
|
36
|
+
return { url: started.url, ...FileSource.open(dir) };
|
|
37
|
+
}),
|
|
38
|
+
launch: ({ dir, strip }) => Effect.gen(function* () {
|
|
39
|
+
yield* sweepOrphans();
|
|
40
|
+
yield* stopCollector(dir);
|
|
41
|
+
emptied(dir);
|
|
42
|
+
const started = yield* runCollector(dir, { strip: strip ?? [] }, false);
|
|
43
|
+
yield* waitReady(started.url, dir).pipe(Effect.tapError(() => terminate(started.marker)));
|
|
44
|
+
return started.url;
|
|
45
|
+
}),
|
|
46
|
+
stop: stopCollector,
|
|
47
|
+
read: (dir) => FileSource.open(dir),
|
|
48
|
+
};
|