Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ec4f0890dc | ||
|
|
e2b13e1848 | ||
|
|
2e6d3863ce | ||
|
|
a58caad17a |
No files matched your search
@@ -7,9 +7,12 @@ import * as Logger from "effect/Logger";
|
||||
import * as Option from "effect/Option";
|
||||
import * as PlatformError from "effect/PlatformError";
|
||||
import * as References from "effect/References";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import * as TraceDiagnostics from "./TraceDiagnostics.ts";
|
||||
|
||||
const encodeUnknownJsonString = Schema.encodeUnknownSync(Schema.UnknownFromJsonString);
|
||||
|
||||
function ns(ms: number): string {
|
||||
return String(BigInt(ms) * 1_000_000n);
|
||||
}
|
||||
@@ -23,7 +26,7 @@ function record(input: {
|
||||
readonly exit?: { readonly _tag: "Success" | "Failure" | "Interrupted"; readonly cause?: string };
|
||||
readonly events?: ReadonlyArray<unknown>;
|
||||
}) {
|
||||
return JSON.stringify({
|
||||
return encodeUnknownJsonString({
|
||||
type: "effect-span",
|
||||
name: input.name,
|
||||
traceId: input.traceId,
|
||||
@@ -151,6 +154,41 @@ describe("TraceDiagnostics", () => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("keeps valid spans without a start timestamp", () =>
|
||||
Effect.sync(() => {
|
||||
const diagnostics = TraceDiagnostics.aggregateTraceDiagnostics({
|
||||
traceFilePath: "/tmp/server.trace.ndjson",
|
||||
readAt: DateTime.makeUnsafe("2026-05-05T10:00:00.000Z"),
|
||||
files: [
|
||||
{
|
||||
path: "/tmp/server.trace.ndjson",
|
||||
text: encodeUnknownJsonString({
|
||||
name: "server.noStart",
|
||||
traceId: "trace-no-start",
|
||||
spanId: "span-no-start",
|
||||
endTimeUnixNano: ns(10_000),
|
||||
durationMs: 100,
|
||||
exit: { _tag: "Success" },
|
||||
events: [],
|
||||
}),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
assert.equal(diagnostics.recordCount, 1);
|
||||
assert.equal(diagnostics.parseErrorCount, 0);
|
||||
assert.isTrue(Option.isNone(diagnostics.firstSpanAt));
|
||||
assert.equal(
|
||||
Option.match(diagnostics.lastSpanAt, {
|
||||
onNone: () => null,
|
||||
onSome: DateTime.formatIso,
|
||||
}),
|
||||
"1970-01-01T00:00:10.000Z",
|
||||
);
|
||||
assert.equal(diagnostics.slowestSpans[0]?.traceId, "trace-no-start");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("preserves full failure causes and log messages", () =>
|
||||
Effect.sync(() => {
|
||||
const longCause = `VcsProcessSpawnError: ${"missing executable ".repeat(80)}`.trim();
|
||||
|
||||
@@ -94,33 +94,36 @@ function isRecordObject(value: unknown): value is TraceRecordLike {
|
||||
return typeof value === "object" && value !== null;
|
||||
}
|
||||
|
||||
function toStringValue(value: unknown): string | null {
|
||||
return typeof value === "string" && value.trim().length > 0 ? value : null;
|
||||
function toStringValue(value: unknown): Option.Option<string> {
|
||||
return typeof value === "string" && value.trim().length > 0 ? Option.some(value) : Option.none();
|
||||
}
|
||||
|
||||
function toNumberValue(value: unknown): number | null {
|
||||
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
||||
function toNumberValue(value: unknown): Option.Option<number> {
|
||||
return typeof value === "number" && Number.isFinite(value) ? Option.some(value) : Option.none();
|
||||
}
|
||||
|
||||
function unixNanoToDateTime(value: unknown): DateTime.Utc | null {
|
||||
function unixNanoToDateTime(value: unknown): Option.Option<DateTime.Utc> {
|
||||
const text = toStringValue(value);
|
||||
if (!text) return null;
|
||||
if (Option.isNone(text)) return Option.none();
|
||||
try {
|
||||
const millis = Number(BigInt(text) / 1_000_000n);
|
||||
return Option.getOrNull(DateTime.make(millis));
|
||||
const millis = Number(BigInt(text.value) / 1_000_000n);
|
||||
return DateTime.make(millis);
|
||||
} catch {
|
||||
return null;
|
||||
return Option.none();
|
||||
}
|
||||
}
|
||||
|
||||
function readExitTag(exit: unknown): string | null {
|
||||
if (!isRecordObject(exit) || !("_tag" in exit)) return null;
|
||||
function readExitTag(exit: unknown): Option.Option<string> {
|
||||
if (!isRecordObject(exit) || !("_tag" in exit)) return Option.none();
|
||||
return toStringValue(exit._tag);
|
||||
}
|
||||
|
||||
function readExitCause(exit: unknown): string {
|
||||
if (!isRecordObject(exit) || !("cause" in exit)) return "Failure";
|
||||
return toStringValue(exit.cause)?.trim() ?? "Failure";
|
||||
return Option.getOrElse(
|
||||
Option.map(toStringValue(exit.cause), (cause) => cause.trim()),
|
||||
() => "Failure",
|
||||
);
|
||||
}
|
||||
|
||||
function isTraceEvent(value: unknown): value is TraceEventLike {
|
||||
@@ -186,6 +189,27 @@ function insertBoundedSlowestSpan(
|
||||
}
|
||||
}
|
||||
|
||||
function earliestDateTime(
|
||||
current: Option.Option<DateTime.Utc>,
|
||||
candidate: DateTime.Utc,
|
||||
): Option.Option<DateTime.Utc> {
|
||||
return Option.match(current, {
|
||||
onNone: () => Option.some(candidate),
|
||||
onSome: (value) => (DateTime.isLessThan(candidate, value) ? Option.some(candidate) : current),
|
||||
});
|
||||
}
|
||||
|
||||
function latestDateTime(
|
||||
current: Option.Option<DateTime.Utc>,
|
||||
candidate: DateTime.Utc,
|
||||
): Option.Option<DateTime.Utc> {
|
||||
return Option.match(current, {
|
||||
onNone: () => Option.some(candidate),
|
||||
onSome: (value) =>
|
||||
DateTime.isGreaterThan(candidate, value) ? Option.some(candidate) : current,
|
||||
});
|
||||
}
|
||||
|
||||
export function aggregateTraceDiagnostics(
|
||||
input: TraceDiagnosticsInput,
|
||||
): ServerTraceDiagnosticsResult {
|
||||
@@ -211,8 +235,8 @@ export function aggregateTraceDiagnostics(
|
||||
let failureCount = 0;
|
||||
let interruptionCount = 0;
|
||||
let slowSpanCount = 0;
|
||||
let firstSpanAt: DateTime.Utc | null = null;
|
||||
let lastSpanAt: DateTime.Utc | null = null;
|
||||
let firstSpanAt: Option.Option<DateTime.Utc> = Option.none();
|
||||
let lastSpanAt: Option.Option<DateTime.Utc> = Option.none();
|
||||
|
||||
const spansByName = new Map<
|
||||
string,
|
||||
@@ -248,40 +272,52 @@ export function aggregateTraceDiagnostics(
|
||||
const durationMs = toNumberValue(parsed.durationMs);
|
||||
const endedAt = unixNanoToDateTime(parsed.endTimeUnixNano);
|
||||
const startedAt = unixNanoToDateTime(parsed.startTimeUnixNano);
|
||||
const spanFields = Option.all({ name, traceId, spanId, durationMs, endedAt });
|
||||
|
||||
if (!name || !traceId || !spanId || durationMs === null || !endedAt) {
|
||||
if (Option.isNone(spanFields)) {
|
||||
parseErrorCount += 1;
|
||||
continue;
|
||||
}
|
||||
const {
|
||||
name: spanName,
|
||||
traceId: spanTraceId,
|
||||
spanId: spanSpanId,
|
||||
durationMs: spanDurationMs,
|
||||
endedAt: spanEndedAt,
|
||||
} = spanFields.value;
|
||||
|
||||
recordCount += 1;
|
||||
firstSpanAt =
|
||||
startedAt && (firstSpanAt === null || DateTime.isLessThan(startedAt, firstSpanAt))
|
||||
? startedAt
|
||||
: firstSpanAt;
|
||||
lastSpanAt =
|
||||
lastSpanAt === null || DateTime.isGreaterThan(endedAt, lastSpanAt) ? endedAt : lastSpanAt;
|
||||
if (Option.isSome(startedAt)) {
|
||||
firstSpanAt = earliestDateTime(firstSpanAt, startedAt.value);
|
||||
}
|
||||
lastSpanAt = latestDateTime(lastSpanAt, spanEndedAt);
|
||||
|
||||
const exitTag = readExitTag(parsed.exit);
|
||||
const isFailure = exitTag === "Failure";
|
||||
const isInterrupted = exitTag === "Interrupted";
|
||||
const isFailure = Option.isSome(exitTag) && exitTag.value === "Failure";
|
||||
const isInterrupted = Option.isSome(exitTag) && exitTag.value === "Interrupted";
|
||||
if (isFailure) failureCount += 1;
|
||||
if (isInterrupted) interruptionCount += 1;
|
||||
|
||||
const spanSummary = spansByName.get(name) ?? {
|
||||
const spanSummary = spansByName.get(spanName) ?? {
|
||||
count: 0,
|
||||
failureCount: 0,
|
||||
totalDurationMs: 0,
|
||||
maxDurationMs: 0,
|
||||
};
|
||||
spanSummary.count += 1;
|
||||
spanSummary.totalDurationMs += durationMs;
|
||||
spanSummary.maxDurationMs = Math.max(spanSummary.maxDurationMs, durationMs);
|
||||
spanSummary.totalDurationMs += spanDurationMs;
|
||||
spanSummary.maxDurationMs = Math.max(spanSummary.maxDurationMs, spanDurationMs);
|
||||
if (isFailure) spanSummary.failureCount += 1;
|
||||
spansByName.set(name, spanSummary);
|
||||
spansByName.set(spanName, spanSummary);
|
||||
|
||||
const spanItem = { name, durationMs, endedAt, traceId, spanId };
|
||||
if (durationMs >= slowSpanThresholdMs) {
|
||||
const spanItem = {
|
||||
name: spanName,
|
||||
durationMs: spanDurationMs,
|
||||
endedAt: spanEndedAt,
|
||||
traceId: spanTraceId,
|
||||
spanId: spanSpanId,
|
||||
};
|
||||
if (spanDurationMs >= slowSpanThresholdMs) {
|
||||
slowSpanCount += 1;
|
||||
}
|
||||
insertBoundedSlowestSpan(slowestSpans, spanItem);
|
||||
@@ -290,16 +326,17 @@ export function aggregateTraceDiagnostics(
|
||||
const cause = readExitCause(parsed.exit);
|
||||
latestFailures.push({ ...spanItem, cause });
|
||||
|
||||
const failureKey = `${name}\0${cause}`;
|
||||
const failureKey = `${spanName}\0${cause}`;
|
||||
const existing = failuresByKey.get(failureKey);
|
||||
const isLatestFailure = !existing || DateTime.isGreaterThan(endedAt, existing.lastSeenAt);
|
||||
const isLatestFailure =
|
||||
!existing || DateTime.isGreaterThan(spanEndedAt, existing.lastSeenAt);
|
||||
failuresByKey.set(failureKey, {
|
||||
name,
|
||||
name: spanName,
|
||||
cause,
|
||||
count: (existing?.count ?? 0) + 1,
|
||||
lastSeenAt: isLatestFailure ? endedAt : existing!.lastSeenAt,
|
||||
traceId: isLatestFailure ? traceId : existing!.traceId,
|
||||
spanId: isLatestFailure ? spanId : existing!.spanId,
|
||||
lastSeenAt: isLatestFailure ? spanEndedAt : existing!.lastSeenAt,
|
||||
traceId: isLatestFailure ? spanTraceId : existing!.traceId,
|
||||
spanId: isLatestFailure ? spanSpanId : existing!.spanId,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -308,10 +345,10 @@ export function aggregateTraceDiagnostics(
|
||||
if (!isTraceEvent(rawEvent)) continue;
|
||||
const attributes = readEventAttributes(rawEvent);
|
||||
const level = toStringValue(attributes["effect.logLevel"]);
|
||||
if (!level) continue;
|
||||
if (Option.isNone(level)) continue;
|
||||
|
||||
logLevelCounts[level] = (logLevelCounts[level] ?? 0) + 1;
|
||||
const normalizedLevel = level.toLowerCase();
|
||||
logLevelCounts[level.value] = (logLevelCounts[level.value] ?? 0) + 1;
|
||||
const normalizedLevel = level.value.toLowerCase();
|
||||
if (
|
||||
normalizedLevel !== "warning" &&
|
||||
normalizedLevel !== "warn" &&
|
||||
@@ -321,15 +358,21 @@ export function aggregateTraceDiagnostics(
|
||||
continue;
|
||||
}
|
||||
|
||||
const seenAt = unixNanoToDateTime(rawEvent.timeUnixNano) ?? endedAt;
|
||||
const message = toStringValue(rawEvent.name)?.trim() ?? "Log event";
|
||||
const seenAt = Option.getOrElse(
|
||||
unixNanoToDateTime(rawEvent.timeUnixNano),
|
||||
() => spanEndedAt,
|
||||
);
|
||||
const message = Option.getOrElse(
|
||||
Option.map(toStringValue(rawEvent.name), (eventName) => eventName.trim()),
|
||||
() => "Log event",
|
||||
);
|
||||
latestWarningAndErrorLogs.push({
|
||||
spanName: name,
|
||||
level,
|
||||
spanName,
|
||||
level: level.value,
|
||||
message,
|
||||
seenAt,
|
||||
traceId,
|
||||
spanId,
|
||||
traceId: spanTraceId,
|
||||
spanId: spanSpanId,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -354,8 +397,8 @@ export function aggregateTraceDiagnostics(
|
||||
readAt,
|
||||
recordCount,
|
||||
parseErrorCount,
|
||||
firstSpanAt: Option.fromNullishOr(firstSpanAt),
|
||||
lastSpanAt: Option.fromNullishOr(lastSpanAt),
|
||||
firstSpanAt,
|
||||
lastSpanAt,
|
||||
failureCount,
|
||||
interruptionCount,
|
||||
slowSpanThresholdMs,
|
||||
|
||||
@@ -4,6 +4,7 @@ import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Path from "effect/Path";
|
||||
import * as PlatformError from "effect/PlatformError";
|
||||
|
||||
import * as ServerConfig from "../config.ts";
|
||||
import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
|
||||
@@ -185,8 +186,8 @@ it.layer(TestLayer, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
|
||||
operationPath: resolvedPath,
|
||||
operation: "realpath-target",
|
||||
});
|
||||
expect(error.cause).toBeInstanceOf(Error);
|
||||
expect((error.cause as NodeJS.ErrnoException).code).toBe("ENOENT");
|
||||
expect(error.cause).toBeInstanceOf(PlatformError.PlatformError);
|
||||
expect((error.cause as PlatformError.PlatformError).reason._tag).toBe("NotFound");
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -140,30 +140,32 @@ export const make = Effect.gen(function* () {
|
||||
relativePath: input.relativePath,
|
||||
});
|
||||
|
||||
const realWorkspaceRoot = yield* Effect.tryPromise({
|
||||
try: () => NodeFSP.realpath(input.cwd),
|
||||
catch: (cause) =>
|
||||
new WorkspaceFileSystemOperationError({
|
||||
workspaceRoot: input.cwd,
|
||||
relativePath: input.relativePath,
|
||||
resolvedPath: target.absolutePath,
|
||||
operationPath: input.cwd,
|
||||
operation: "realpath-workspace-root",
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
const realTargetPath = yield* Effect.tryPromise({
|
||||
try: () => NodeFSP.realpath(target.absolutePath),
|
||||
catch: (cause) =>
|
||||
new WorkspaceFileSystemOperationError({
|
||||
workspaceRoot: input.cwd,
|
||||
relativePath: input.relativePath,
|
||||
resolvedPath: target.absolutePath,
|
||||
operationPath: target.absolutePath,
|
||||
operation: "realpath-target",
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
const realWorkspaceRoot = yield* fileSystem.realPath(input.cwd).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new WorkspaceFileSystemOperationError({
|
||||
workspaceRoot: input.cwd,
|
||||
relativePath: input.relativePath,
|
||||
resolvedPath: target.absolutePath,
|
||||
operationPath: input.cwd,
|
||||
operation: "realpath-workspace-root",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const realTargetPath = yield* fileSystem.realPath(target.absolutePath).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new WorkspaceFileSystemOperationError({
|
||||
workspaceRoot: input.cwd,
|
||||
relativePath: input.relativePath,
|
||||
resolvedPath: target.absolutePath,
|
||||
operationPath: target.absolutePath,
|
||||
operation: "realpath-target",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const relativeRealPath = path.relative(realWorkspaceRoot, realTargetPath);
|
||||
if (
|
||||
relativeRealPath.startsWith(`..${path.sep}`) ||
|
||||
|
||||
Reference in new issue
Block a user