feat: add native resource telemetry
Replace scheduled process shell probes with a persistent native collector, Electron power/process telemetry, Effect services, diagnostics UI, attribution, and release packaging. Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
79ad071184
commit
dbd8a8e09c
62 files changed
+7749
-1965
No files matched your search
@@ -8,6 +8,7 @@ import * as Layer from "effect/Layer";
|
||||
import * as Logger from "effect/Logger";
|
||||
import * as Order from "effect/Order";
|
||||
import * as Path from "effect/Path";
|
||||
import * as Ref from "effect/Ref";
|
||||
import * as References from "effect/References";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as Tracer from "effect/Tracer";
|
||||
@@ -18,6 +19,7 @@ import {
|
||||
makeLocalFileTracer,
|
||||
makeTraceSink,
|
||||
type TraceRecord,
|
||||
type TraceSinkFlushStats,
|
||||
} from "./observability.ts";
|
||||
|
||||
describe("causeErrorTag", () => {
|
||||
@@ -167,6 +169,34 @@ describe("observability", () => {
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("reports successful logical trace writes", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const tempDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-trace-sink-" });
|
||||
const tracePath = path.join(tempDir, "shared.trace.ndjson");
|
||||
const reported = yield* Ref.make<ReadonlyArray<TraceSinkFlushStats>>([]);
|
||||
|
||||
const sink = yield* makeTraceSink({
|
||||
filePath: tracePath,
|
||||
maxBytes: 1024,
|
||||
maxFiles: 2,
|
||||
batchWindowMs: 10_000,
|
||||
onFlush: (stats) => Ref.update(reported, (current) => [...current, stats]),
|
||||
});
|
||||
|
||||
sink.push(makeRecord("attributed"));
|
||||
yield* sink.flush;
|
||||
|
||||
const stats = yield* Ref.get(reported);
|
||||
assert.equal(stats.length, 1);
|
||||
assert.equal(stats[0]?.count, 1);
|
||||
assert.isAbove(stats[0]?.logicalWriteBytes ?? 0, 0);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("rotates the trace file when the configured max size is exceeded", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
|
||||
@@ -9,6 +9,7 @@ import { OtlpResource, OtlpTracer } from "effect/unstable/observability";
|
||||
import { RotatingFileSink } from "./logging.ts";
|
||||
|
||||
const FLUSH_BUFFER_THRESHOLD = 32;
|
||||
const textEncoder = new TextEncoder();
|
||||
|
||||
export type TraceAttributes = Readonly<Record<string, unknown>>;
|
||||
|
||||
@@ -94,6 +95,13 @@ export interface TraceSinkOptions {
|
||||
readonly maxBytes: number;
|
||||
readonly maxFiles: number;
|
||||
readonly batchWindowMs: number;
|
||||
readonly onFlush?: (stats: TraceSinkFlushStats) => Effect.Effect<void>;
|
||||
}
|
||||
|
||||
export interface TraceSinkFlushStats {
|
||||
readonly logicalWriteBytes: number;
|
||||
readonly count: number;
|
||||
readonly durationMs: number;
|
||||
}
|
||||
|
||||
export interface TraceSink {
|
||||
@@ -263,23 +271,50 @@ export const makeTraceSink = Effect.fn("makeTraceSink")(function* (options: Trac
|
||||
});
|
||||
|
||||
let buffer: Array<string> = [];
|
||||
let pendingFlushStats: TraceSinkFlushStats = {
|
||||
logicalWriteBytes: 0,
|
||||
count: 0,
|
||||
durationMs: 0,
|
||||
};
|
||||
|
||||
const flushUnsafe = () => {
|
||||
if (buffer.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const chunk = buffer.join("");
|
||||
const records = buffer;
|
||||
const chunk = records.join("");
|
||||
buffer = [];
|
||||
const startedAt = performance.now();
|
||||
|
||||
try {
|
||||
sink.write(chunk);
|
||||
pendingFlushStats = {
|
||||
logicalWriteBytes:
|
||||
pendingFlushStats.logicalWriteBytes + textEncoder.encode(chunk).byteLength,
|
||||
count: pendingFlushStats.count + records.length,
|
||||
durationMs: pendingFlushStats.durationMs + Math.max(0, performance.now() - startedAt),
|
||||
};
|
||||
} catch {
|
||||
buffer.unshift(chunk);
|
||||
buffer.unshift(...records);
|
||||
}
|
||||
};
|
||||
|
||||
const flush = Effect.sync(flushUnsafe).pipe(Effect.withTracerEnabled(false));
|
||||
const flush = Effect.sync(() => {
|
||||
flushUnsafe();
|
||||
const stats = pendingFlushStats;
|
||||
pendingFlushStats = {
|
||||
logicalWriteBytes: 0,
|
||||
count: 0,
|
||||
durationMs: 0,
|
||||
};
|
||||
return stats;
|
||||
}).pipe(
|
||||
Effect.flatMap((stats) =>
|
||||
stats.count > 0 && options.onFlush ? options.onFlush(stats).pipe(Effect.ignore) : Effect.void,
|
||||
),
|
||||
Effect.withTracerEnabled(false),
|
||||
);
|
||||
|
||||
yield* Effect.addFinalizer(() => flush.pipe(Effect.ignore));
|
||||
yield* Effect.forkScoped(
|
||||
|
||||
Reference in new issue
Block a user