| ... | @@ -16,6 +16,7 @@ import * as Option from "effect/Option"; | ... | @@ -16,6 +16,7 @@ import * as Option from "effect/Option"; |
| 16 | import * as PlatformError from "effect/PlatformError"; | 16 | import * as PlatformError from "effect/PlatformError"; |
| 17 | import * as Result from "effect/Result"; | 17 | import * as Result from "effect/Result"; |
| 18 | import * as Schema from "effect/Schema"; | 18 | import * as Schema from "effect/Schema"; |
| | 19 | import * as Stream from "effect/Stream"; |
| 19 | | 20 | |
| 20 | interface TraceRecordLike { | 21 | interface TraceRecordLike { |
| 21 | readonly name?: unknown; | 22 | readonly name?: unknown; |
| ... | @@ -63,16 +64,6 @@ export class TraceDiagnostics extends Context.Service< | ... | @@ -63,16 +64,6 @@ export class TraceDiagnostics extends Context.Service< |
| 63 | } | 64 | } |
| 64 | >()("t3/diagnostics/TraceDiagnostics") {} | 65 | >()("t3/diagnostics/TraceDiagnostics") {} |
| 65 | | 66 | |
| 66 | interface TraceDiagnosticsInput { | | |
| 67 | readonly traceFilePath: string; | | |
| 68 | readonly files: ReadonlyArray<{ readonly path: string; readonly text: string }>; | | |
| 69 | readonly scannedFilePaths?: ReadonlyArray<string>; | | |
| 70 | readonly slowSpanThresholdMs?: number; | | |
| 71 | readonly readAt: DateTime.Utc; | | |
| 72 | readonly error?: TraceDiagnosticsErrorSummary; | | |
| 73 | readonly partialFailure?: boolean; | | |
| 74 | } | | |
| 75 | | | |
| 76 | interface TraceDiagnosticsErrorSummary { | 67 | interface TraceDiagnosticsErrorSummary { |
| 77 | readonly kind: ServerTraceDiagnosticsErrorKind; | 68 | readonly kind: ServerTraceDiagnosticsErrorKind; |
| 78 | readonly message: string; | 69 | readonly message: string; |
| ... | @@ -173,44 +164,50 @@ function isNotFoundError(error: PlatformError.PlatformError): boolean { | ... | @@ -173,44 +164,50 @@ function isNotFoundError(error: PlatformError.PlatformError): boolean { |
| 173 | return error.reason._tag === "NotFound"; | 164 | return error.reason._tag === "NotFound"; |
| 174 | } | 165 | } |
| 175 | | 166 | |
| 176 | function insertBoundedSlowestSpan( | 167 | /** |
| 177 | slowestSpans: ServerTraceDiagnosticsSpanOccurrence[], | 168 | * Adds `item` to `items`, which stays sorted by `order` and holds at most |
| 178 | span: ServerTraceDiagnosticsSpanOccurrence, | 169 | * `limit` entries. Same result as a stable sort and slice over every item, but |
| | 170 | * memory stays bounded however many items stream in. |
| | 171 | */ |
| | 172 | function insertBounded<A>( |
| | 173 | items: A[], |
| | 174 | item: A, |
| | 175 | limit: number, |
| | 176 | order: (left: A, right: A) => number, |
| 179 | ): void { | 177 | ): void { |
| 180 | if ( | 178 | if (items.length >= limit && order(item, items[items.length - 1]!) >= 0) { |
| 181 | slowestSpans.length >= TOP_LIMIT && | | |
| 182 | span.durationMs <= slowestSpans[slowestSpans.length - 1]!.durationMs | | |
| 183 | ) { | | |
| 184 | return; | 179 | return; |
| 185 | } | 180 | } |
| 186 | | 181 | |
| 187 | slowestSpans.push(span); | 182 | items.push(item); |
| 188 | slowestSpans.sort((left, right) => right.durationMs - left.durationMs); | 183 | items.sort(order); |
| 189 | if (slowestSpans.length > TOP_LIMIT) { | 184 | if (items.length > limit) { |
| 190 | slowestSpans.length = TOP_LIMIT; | 185 | items.length = limit; |
| 191 | } | 186 | } |
| 192 | } | 187 | } |
| 193 | | 188 | |
| 194 | export function aggregateTraceDiagnostics( | 189 | const slowestFirst = ( |
| 195 | input: TraceDiagnosticsInput, | 190 | left: ServerTraceDiagnosticsSpanOccurrence, |
| 196 | ): ServerTraceDiagnosticsResult { | 191 | right: ServerTraceDiagnosticsSpanOccurrence, |
| 197 | const readAt = input.readAt; | 192 | ) => right.durationMs - left.durationMs; |
| 198 | const slowSpanThresholdMs = input.slowSpanThresholdMs ?? DEFAULT_SLOW_SPAN_THRESHOLD_MS; | 193 | |
| 199 | const scannedFilePaths = input.scannedFilePaths ?? input.files.map((file) => file.path); | 194 | const latestEndedFirst = ( |
| 200 | if (input.files.length === 0) { | 195 | left: ServerTraceDiagnosticsRecentFailure, |
| 201 | return makeEmptyDiagnostics({ | 196 | right: ServerTraceDiagnosticsRecentFailure, |
| 202 | traceFilePath: input.traceFilePath, | 197 | ) => DateTime.toEpochMillis(right.endedAt) - DateTime.toEpochMillis(left.endedAt); |
| 203 | scannedFilePaths, | 198 | |
| 204 | readAt, | 199 | const latestSeenFirst = ( |
| 205 | slowSpanThresholdMs, | 200 | left: ServerTraceDiagnosticsLogEvent, |
| 206 | error: input.error ?? { | 201 | right: ServerTraceDiagnosticsLogEvent, |
| 207 | kind: "trace-file-not-found", | 202 | ) => DateTime.toEpochMillis(right.seenAt) - DateTime.toEpochMillis(left.seenAt); |
| 208 | message: "No local trace files were found.", | 203 | |
| 209 | }, | 204 | /** |
| 210 | ...(input.partialFailure ? { partialFailure: true } : {}), | 205 | * Folds trace NDJSON into diagnostics. Call `addLine` once per line as the |
| 211 | }); | 206 | * rotated files stream in, then `finish` for the result. |
| 212 | } | 207 | */ |
| 213 | | 208 | export function makeTraceDiagnosticsAggregator( |
| | 209 | slowSpanThresholdMs = DEFAULT_SLOW_SPAN_THRESHOLD_MS, |
| | 210 | ) { |
| 214 | let parseErrorCount = 0; | 211 | let parseErrorCount = 0; |
| 215 | let recordCount = 0; | 212 | let recordCount = 0; |
| 216 | let failureCount = 0; | 213 | let failureCount = 0; |
| ... | @@ -229,182 +226,184 @@ export function aggregateTraceDiagnostics( | ... | @@ -229,182 +226,184 @@ export function aggregateTraceDiagnostics( |
| 229 | const latestWarningAndErrorLogs: ServerTraceDiagnosticsLogEvent[] = []; | 226 | const latestWarningAndErrorLogs: ServerTraceDiagnosticsLogEvent[] = []; |
| 230 | const logLevelCounts: Record<string, number> = {}; | 227 | const logLevelCounts: Record<string, number> = {}; |
| 231 | | 228 | |
| 232 | for (const file of input.files) { | 229 | const addLine = (line: string) => { |
| 233 | const lines = file.text.split(/\r?\n/); | 230 | if (line.trim().length === 0) return; |
| 234 | for (const line of lines) { | | |
| 235 | if (line.trim().length === 0) continue; | | |
| 236 | | | |
| 237 | let parsed: unknown; | | |
| 238 | try { | | |
| 239 | parsed = JSON.parse(line); | | |
| 240 | } catch { | | |
| 241 | parseErrorCount += 1; | | |
| 242 | continue; | | |
| 243 | } | | |
| 244 | | 231 | |
| 245 | if (!isRecordObject(parsed)) { | 232 | let parsed: unknown; |
| 246 | parseErrorCount += 1; | 233 | try { |
| 247 | continue; | 234 | parsed = JSON.parse(line); |
| 248 | } | 235 | } catch { |
| | 236 | parseErrorCount += 1; |
| | 237 | return; |
| | 238 | } |
| 249 | | 239 | |
| 250 | const name = toStringValue(parsed.name); | 240 | if (!isRecordObject(parsed)) { |
| 251 | const traceId = toStringValue(parsed.traceId); | 241 | parseErrorCount += 1; |
| 252 | const spanId = toStringValue(parsed.spanId); | 242 | return; |
| 253 | const durationMs = toNumberValue(parsed.durationMs); | 243 | } |
| 254 | const endedAt = unixNanoToDateTime(parsed.endTimeUnixNano); | | |
| 255 | const startedAt = unixNanoToDateTime(parsed.startTimeUnixNano); | | |
| 256 | | 244 | |
| 257 | if (!name || !traceId || !spanId || durationMs === null || !endedAt) { | 245 | const name = toStringValue(parsed.name); |
| 258 | parseErrorCount += 1; | 246 | const traceId = toStringValue(parsed.traceId); |
| 259 | continue; | 247 | const spanId = toStringValue(parsed.spanId); |
| 260 | } | 248 | const durationMs = toNumberValue(parsed.durationMs); |
| | 249 | const endedAt = unixNanoToDateTime(parsed.endTimeUnixNano); |
| | 250 | const startedAt = unixNanoToDateTime(parsed.startTimeUnixNano); |
| 261 | | 251 | |
| 262 | recordCount += 1; | 252 | if (!name || !traceId || !spanId || durationMs === null || !endedAt) { |
| 263 | firstSpanAt = | 253 | parseErrorCount += 1; |
| 264 | startedAt && (firstSpanAt === null || DateTime.isLessThan(startedAt, firstSpanAt)) | 254 | return; |
| 265 | ? startedAt | 255 | } |
| 266 | : firstSpanAt; | | |
| 267 | lastSpanAt = | | |
| 268 | lastSpanAt === null || DateTime.isGreaterThan(endedAt, lastSpanAt) ? endedAt : lastSpanAt; | | |
| 269 | | | |
| 270 | const exitTag = readExitTag(parsed.exit); | | |
| 271 | const isFailure = exitTag === "Failure"; | | |
| 272 | const isInterrupted = exitTag === "Interrupted"; | | |
| 273 | if (isFailure) failureCount += 1; | | |
| 274 | if (isInterrupted) interruptionCount += 1; | | |
| 275 | | | |
| 276 | const spanSummary = spansByName.get(name) ?? { | | |
| 277 | count: 0, | | |
| 278 | failureCount: 0, | | |
| 279 | totalDurationMs: 0, | | |
| 280 | maxDurationMs: 0, | | |
| 281 | }; | | |
| 282 | spanSummary.count += 1; | | |
| 283 | spanSummary.totalDurationMs += durationMs; | | |
| 284 | spanSummary.maxDurationMs = Math.max(spanSummary.maxDurationMs, durationMs); | | |
| 285 | if (isFailure) spanSummary.failureCount += 1; | | |
| 286 | spansByName.set(name, spanSummary); | | |
| 287 | | | |
| 288 | const spanItem = { name, durationMs, endedAt, traceId, spanId }; | | |
| 289 | if (durationMs >= slowSpanThresholdMs) { | | |
| 290 | slowSpanCount += 1; | | |
| 291 | } | | |
| 292 | insertBoundedSlowestSpan(slowestSpans, spanItem); | | |
| 293 | | | |
| 294 | if (isFailure) { | | |
| 295 | const cause = readExitCause(parsed.exit); | | |
| 296 | latestFailures.push({ ...spanItem, cause }); | | |
| 297 | | | |
| 298 | const failureKey = `${name}\0${cause}`; | | |
| 299 | const existing = failuresByKey.get(failureKey); | | |
| 300 | const isLatestFailure = !existing || DateTime.isGreaterThan(endedAt, existing.lastSeenAt); | | |
| 301 | failuresByKey.set(failureKey, { | | |
| 302 | name, | | |
| 303 | cause, | | |
| 304 | count: (existing?.count ?? 0) + 1, | | |
| 305 | lastSeenAt: isLatestFailure ? endedAt : existing!.lastSeenAt, | | |
| 306 | traceId: isLatestFailure ? traceId : existing!.traceId, | | |
| 307 | spanId: isLatestFailure ? spanId : existing!.spanId, | | |
| 308 | }); | | |
| 309 | } | | |
| 310 | | 256 | |
| 311 | if (Array.isArray(parsed.events)) { | 257 | recordCount += 1; |
| 312 | for (const rawEvent of parsed.events) { | 258 | firstSpanAt = |
| 313 | if (!isTraceEvent(rawEvent)) continue; | 259 | startedAt && (firstSpanAt === null || DateTime.isLessThan(startedAt, firstSpanAt)) |
| 314 | const attributes = readEventAttributes(rawEvent); | 260 | ? startedAt |
| 315 | const level = toStringValue(attributes["effect.logLevel"]); | 261 | : firstSpanAt; |
| 316 | if (!level) continue; | 262 | lastSpanAt = |
| 317 | | 263 | lastSpanAt === null || DateTime.isGreaterThan(endedAt, lastSpanAt) ? endedAt : lastSpanAt; |
| 318 | logLevelCounts[level] = (logLevelCounts[level] ?? 0) + 1; | 264 | |
| 319 | const normalizedLevel = level.toLowerCase(); | 265 | const exitTag = readExitTag(parsed.exit); |
| 320 | if ( | 266 | const isFailure = exitTag === "Failure"; |
| 321 | normalizedLevel !== "warning" && | 267 | const isInterrupted = exitTag === "Interrupted"; |
| 322 | normalizedLevel !== "warn" && | 268 | if (isFailure) failureCount += 1; |
| 323 | normalizedLevel !== "error" && | 269 | if (isInterrupted) interruptionCount += 1; |
| 324 | normalizedLevel !== "fatal" | 270 | |
| 325 | ) { | 271 | const spanSummary = spansByName.get(name) ?? { |
| 326 | continue; | 272 | count: 0, |
| 327 | } | 273 | failureCount: 0, |
| 328 | | 274 | totalDurationMs: 0, |
| 329 | const seenAt = unixNanoToDateTime(rawEvent.timeUnixNano) ?? endedAt; | 275 | maxDurationMs: 0, |
| 330 | const message = toStringValue(rawEvent.name)?.trim() ?? "Log event"; | 276 | }; |
| 331 | latestWarningAndErrorLogs.push({ | 277 | spanSummary.count += 1; |
| 332 | spanName: name, | 278 | spanSummary.totalDurationMs += durationMs; |
| 333 | level, | 279 | spanSummary.maxDurationMs = Math.max(spanSummary.maxDurationMs, durationMs); |
| 334 | message, | 280 | if (isFailure) spanSummary.failureCount += 1; |
| 335 | seenAt, | 281 | spansByName.set(name, spanSummary); |
| 336 | traceId, | 282 | |
| 337 | spanId, | 283 | const spanItem = { name, durationMs, endedAt, traceId, spanId }; |
| 338 | }); | 284 | if (durationMs >= slowSpanThresholdMs) { |
| | 285 | slowSpanCount += 1; |
| | 286 | } |
| | 287 | insertBounded(slowestSpans, spanItem, TOP_LIMIT, slowestFirst); |
| | 288 | |
| | 289 | if (isFailure) { |
| | 290 | const cause = readExitCause(parsed.exit); |
| | 291 | insertBounded(latestFailures, { ...spanItem, cause }, RECENT_LIMIT, latestEndedFirst); |
| | 292 | |
| | 293 | const failureKey = `${name}\0${cause}`; |
| | 294 | const existing = failuresByKey.get(failureKey); |
| | 295 | const isLatestFailure = !existing || DateTime.isGreaterThan(endedAt, existing.lastSeenAt); |
| | 296 | failuresByKey.set(failureKey, { |
| | 297 | name, |
| | 298 | cause, |
| | 299 | count: (existing?.count ?? 0) + 1, |
| | 300 | lastSeenAt: isLatestFailure ? endedAt : existing!.lastSeenAt, |
| | 301 | traceId: isLatestFailure ? traceId : existing!.traceId, |
| | 302 | spanId: isLatestFailure ? spanId : existing!.spanId, |
| | 303 | }); |
| | 304 | } |
| | 305 | |
| | 306 | if (Array.isArray(parsed.events)) { |
| | 307 | for (const rawEvent of parsed.events) { |
| | 308 | if (!isTraceEvent(rawEvent)) continue; |
| | 309 | const attributes = readEventAttributes(rawEvent); |
| | 310 | const level = toStringValue(attributes["effect.logLevel"]); |
| | 311 | if (!level) continue; |
| | 312 | |
| | 313 | logLevelCounts[level] = (logLevelCounts[level] ?? 0) + 1; |
| | 314 | const normalizedLevel = level.toLowerCase(); |
| | 315 | if ( |
| | 316 | normalizedLevel !== "warning" && |
| | 317 | normalizedLevel !== "warn" && |
| | 318 | normalizedLevel !== "error" && |
| | 319 | normalizedLevel !== "fatal" |
| | 320 | ) { |
| | 321 | continue; |
| 339 | } | 322 | } |
| | 323 | |
| | 324 | const seenAt = unixNanoToDateTime(rawEvent.timeUnixNano) ?? endedAt; |
| | 325 | const message = toStringValue(rawEvent.name)?.trim() ?? "Log event"; |
| | 326 | insertBounded( |
| | 327 | latestWarningAndErrorLogs, |
| | 328 | { spanName: name, level, message, seenAt, traceId, spanId }, |
| | 329 | RECENT_LIMIT, |
| | 330 | latestSeenFirst, |
| | 331 | ); |
| 340 | } | 332 | } |
| 341 | } | 333 | } |
| 342 | } | 334 | }; |
| 343 | | | |
| 344 | const topSpansByCount: ServerTraceDiagnosticsSpanSummary[] = [...spansByName.entries()] | | |
| 345 | .map(([name, span]) => ({ | | |
| 346 | name, | | |
| 347 | count: span.count, | | |
| 348 | failureCount: span.failureCount, | | |
| 349 | totalDurationMs: span.totalDurationMs, | | |
| 350 | averageDurationMs: span.count > 0 ? span.totalDurationMs / span.count : 0, | | |
| 351 | maxDurationMs: span.maxDurationMs, | | |
| 352 | })) | | |
| 353 | .toSorted((left, right) => right.count - left.count || right.maxDurationMs - left.maxDurationMs) | | |
| 354 | .slice(0, TOP_LIMIT); | | |
| 355 | | 335 | |
| 356 | return { | 336 | const finish = (input: { |
| 357 | traceFilePath: input.traceFilePath, | 337 | readonly traceFilePath: string; |
| 358 | scannedFilePaths, | 338 | readonly scannedFilePaths: ReadonlyArray<string>; |
| 359 | readAt, | 339 | readonly readAt: DateTime.Utc; |
| 360 | recordCount, | 340 | readonly error?: TraceDiagnosticsErrorSummary; |
| 361 | parseErrorCount, | 341 | readonly partialFailure?: boolean; |
| 362 | firstSpanAt: Option.fromNullishOr(firstSpanAt), | 342 | }): ServerTraceDiagnosticsResult => { |
| 363 | lastSpanAt: Option.fromNullishOr(lastSpanAt), | 343 | const topSpansByCount: ServerTraceDiagnosticsSpanSummary[] = [...spansByName.entries()] |
| 364 | failureCount, | 344 | .map(([name, span]) => ({ |
| 365 | interruptionCount, | 345 | name, |
| 366 | slowSpanThresholdMs, | 346 | count: span.count, |
| 367 | slowSpanCount, | 347 | failureCount: span.failureCount, |
| 368 | logLevelCounts, | 348 | totalDurationMs: span.totalDurationMs, |
| 369 | topSpansByCount, | 349 | averageDurationMs: span.count > 0 ? span.totalDurationMs / span.count : 0, |
| 370 | slowestSpans, | 350 | maxDurationMs: span.maxDurationMs, |
| 371 | commonFailures: [...failuresByKey.values()] | 351 | })) |
| 372 | .toSorted( | | |
| 373 | (left, right) => | | |
| 374 | right.count - left.count || | | |
| 375 | DateTime.toEpochMillis(right.lastSeenAt) - DateTime.toEpochMillis(left.lastSeenAt), | | |
| 376 | ) | | |
| 377 | .slice(0, TOP_LIMIT), | | |
| 378 | latestFailures: latestFailures | | |
| 379 | .toSorted( | 352 | .toSorted( |
| 380 | (left, right) => | 353 | (left, right) => right.count - left.count || right.maxDurationMs - left.maxDurationMs, |
| 381 | DateTime.toEpochMillis(right.endedAt) - DateTime.toEpochMillis(left.endedAt), | | |
| 382 | ) | 354 | ) |
| 383 | .slice(0, RECENT_LIMIT), | 355 | .slice(0, TOP_LIMIT); |
| 384 | latestWarningAndErrorLogs: latestWarningAndErrorLogs | 356 | |
| 385 | .toSorted( | 357 | return { |
| 386 | (left, right) => DateTime.toEpochMillis(right.seenAt) - DateTime.toEpochMillis(left.seenAt), | 358 | traceFilePath: input.traceFilePath, |
| 387 | ) | 359 | scannedFilePaths: input.scannedFilePaths, |
| 388 | .slice(0, RECENT_LIMIT), | 360 | readAt: input.readAt, |
| 389 | partialFailure: input.partialFailure ? Option.some(true) : Option.none(), | 361 | recordCount, |
| 390 | error: Option.fromNullishOr(input.error), | 362 | parseErrorCount, |
| | 363 | firstSpanAt: Option.fromNullishOr(firstSpanAt), |
| | 364 | lastSpanAt: Option.fromNullishOr(lastSpanAt), |
| | 365 | failureCount, |
| | 366 | interruptionCount, |
| | 367 | slowSpanThresholdMs, |
| | 368 | slowSpanCount, |
| | 369 | logLevelCounts, |
| | 370 | topSpansByCount, |
| | 371 | slowestSpans, |
| | 372 | commonFailures: [...failuresByKey.values()] |
| | 373 | .toSorted( |
| | 374 | (left, right) => |
| | 375 | right.count - left.count || |
| | 376 | DateTime.toEpochMillis(right.lastSeenAt) - DateTime.toEpochMillis(left.lastSeenAt), |
| | 377 | ) |
| | 378 | .slice(0, TOP_LIMIT), |
| | 379 | latestFailures, |
| | 380 | latestWarningAndErrorLogs, |
| | 381 | partialFailure: input.partialFailure ? Option.some(true) : Option.none(), |
| | 382 | error: Option.fromNullishOr(input.error), |
| | 383 | }; |
| 391 | }; | 384 | }; |
| 392 | } | | |
| 393 | | 385 | |
| 394 | type TraceFileReadResult = | 386 | return { addLine, finish }; |
| 395 | | { readonly _tag: "Loaded"; readonly path: string; readonly text: string } | 387 | } |
| 396 | | { readonly _tag: "Missing"; readonly path: string }; | | |
| 397 | | 388 | |
| 398 | function readTraceFile( | 389 | /** |
| | 390 | * Feeds each line of one trace file to `onLine`, streaming so only one chunk of |
| | 391 | * text is in memory at a time. Succeeds with false when the file does not exist. |
| | 392 | */ |
| | 393 | export function streamTraceFileLines( |
| 399 | fileSystem: FileSystem.FileSystem, | 394 | fileSystem: FileSystem.FileSystem, |
| 400 | path: string, | 395 | path: string, |
| 401 | ): Effect.Effect<TraceFileReadResult, TraceFileReadError> { | 396 | onLine: (line: string) => void, |
| 402 | return fileSystem.readFileString(path).pipe( | 397 | ): Effect.Effect<boolean, TraceFileReadError> { |
| 403 | Effect.map((text): TraceFileReadResult => ({ _tag: "Loaded", path, text })), | 398 | return fileSystem.stream(path).pipe( |
| | 399 | Stream.decodeText, |
| | 400 | Stream.splitLines, |
| | 401 | Stream.runForEachArray((lines) => Effect.sync(() => lines.forEach(onLine))), |
| | 402 | Effect.as(true), |
| 404 | Effect.catchTags({ | 403 | Effect.catchTags({ |
| 405 | PlatformError: (cause) => | 404 | PlatformError: (cause) => |
| 406 | isNotFoundError(cause) | 405 | isNotFoundError(cause) |
| 407 | ? Effect.succeed<TraceFileReadResult>({ _tag: "Missing", path }) | 406 | ? Effect.succeed(false) |
| 408 | : Effect.fail( | 407 | : Effect.fail( |
| 409 | new TraceFileReadError({ | 408 | new TraceFileReadError({ |
| 410 | traceFilePath: path, | 409 | traceFilePath: path, |
| ... | @@ -425,10 +424,11 @@ export const make = Effect.gen(function* () { | ... | @@ -425,10 +424,11 @@ export const make = Effect.gen(function* () { |
| 425 | const readAt = options.readAt ?? (yield* DateTime.now); | 424 | const readAt = options.readAt ?? (yield* DateTime.now); |
| 426 | const slowSpanThresholdMs = options.slowSpanThresholdMs ?? DEFAULT_SLOW_SPAN_THRESHOLD_MS; | 425 | const slowSpanThresholdMs = options.slowSpanThresholdMs ?? DEFAULT_SLOW_SPAN_THRESHOLD_MS; |
| 427 | const paths = toRotatedTracePaths(options.traceFilePath, options.maxFiles); | 426 | const paths = toRotatedTracePaths(options.traceFilePath, options.maxFiles); |
| | 427 | const aggregator = makeTraceDiagnosticsAggregator(slowSpanThresholdMs); |
| 428 | const results = yield* Effect.forEach( | 428 | const results = yield* Effect.forEach( |
| 429 | paths, | 429 | paths, |
| 430 | (path) => | 430 | (path) => |
| 431 | readTraceFile(fileSystem, path).pipe( | 431 | streamTraceFileLines(fileSystem, path, aggregator.addLine).pipe( |
| 432 | Effect.tapError((cause) => | 432 | Effect.tapError((cause) => |
| 433 | Effect.logWarning("Failed to read local trace file.").pipe( | 433 | Effect.logWarning("Failed to read local trace file.").pipe( |
| 434 | Effect.annotateLogs({ | 434 | Effect.annotateLogs({ |
| ... | @@ -440,15 +440,10 @@ export const make = Effect.gen(function* () { | ... | @@ -440,15 +440,10 @@ export const make = Effect.gen(function* () { |
| 440 | ), | 440 | ), |
| 441 | Effect.result, | 441 | Effect.result, |
| 442 | ), | 442 | ), |
| 443 | { | 443 | // Every file feeds one aggregator, so read them one at a time, oldest first. |
| 444 | concurrency: 1, | 444 | { concurrency: 1 }, |
| 445 | }, | | |
| 446 | ); | | |
| 447 | const files = results.flatMap((result) => | | |
| 448 | Result.isSuccess(result) && result.success._tag === "Loaded" | | |
| 449 | ? [{ path: result.success.path, text: result.success.text }] | | |
| 450 | : [], | | |
| 451 | ); | 445 | ); |
| | 446 | const foundFile = results.some((result) => Result.isSuccess(result) && result.success); |
| 452 | const readFailure = results.find(Result.isFailure); | 447 | const readFailure = results.find(Result.isFailure); |
| 453 | const readFailureError = readFailure | 448 | const readFailureError = readFailure |
| 454 | ? ({ | 449 | ? ({ |
| ... | @@ -457,7 +452,7 @@ export const make = Effect.gen(function* () { | ... | @@ -457,7 +452,7 @@ export const make = Effect.gen(function* () { |
| 457 | } satisfies TraceDiagnosticsErrorSummary) | 452 | } satisfies TraceDiagnosticsErrorSummary) |
| 458 | : undefined; | 453 | : undefined; |
| 459 | | 454 | |
| 460 | if (files.length === 0) { | 455 | if (!foundFile) { |
| 461 | return makeEmptyDiagnostics({ | 456 | return makeEmptyDiagnostics({ |
| 462 | traceFilePath: options.traceFilePath, | 457 | traceFilePath: options.traceFilePath, |
| 463 | scannedFilePaths: paths, | 458 | scannedFilePaths: paths, |
| ... | @@ -472,12 +467,10 @@ export const make = Effect.gen(function* () { | ... | @@ -472,12 +467,10 @@ export const make = Effect.gen(function* () { |
| 472 | }); | 467 | }); |
| 473 | } | 468 | } |
| 474 | | 469 | |
| 475 | return aggregateTraceDiagnostics({ | 470 | return aggregator.finish({ |
| 476 | traceFilePath: options.traceFilePath, | 471 | traceFilePath: options.traceFilePath, |
| 477 | files, | | |
| 478 | scannedFilePaths: paths, | 472 | scannedFilePaths: paths, |
| 479 | readAt, | 473 | readAt, |
| 480 | slowSpanThresholdMs, | | |
| 481 | ...(readFailureError ? { partialFailure: true, error: readFailureError } : {}), | 474 | ...(readFailureError ? { partialFailure: true, error: readFailureError } : {}), |
| 482 | }); | 475 | }); |
| 483 | }, | 476 | }, |