refactor(core): replace legacy logger with Effect logging (#31310)
This commit is contained in:
parent
0a7cb20e66
commit
c06ad7c881
152 changed files with 697 additions and 2242 deletions
|
|
@ -1,5 +1,11 @@
|
|||
import { afterEach, describe, expect, test } from "bun:test"
|
||||
import { resource } from "@opencode-ai/core/effect/observability"
|
||||
import { NodeFileSystem } from "@effect/platform-node"
|
||||
import { Effect, Layer, Logger } from "effect"
|
||||
import fs from "fs/promises"
|
||||
import os from "os"
|
||||
import path from "path"
|
||||
import { fileLogger } from "../../src/observability/logging"
|
||||
import { resource } from "../../src/observability/otlp"
|
||||
|
||||
const otelResourceAttributes = process.env.OTEL_RESOURCE_ATTRIBUTES
|
||||
const opencodeClient = process.env.OPENCODE_CLIENT
|
||||
|
|
@ -42,5 +48,62 @@ describe("resource", () => {
|
|||
"service.namespace": "anomalyco",
|
||||
})
|
||||
expect(resource().attributes["service.instance.id"]).not.toBe("override")
|
||||
expect(resource().attributes["opencode.run"]).toMatch(/^[0-9a-f]{8}$/)
|
||||
})
|
||||
})
|
||||
|
||||
test("file logger appends concurrent runs with a run on every line", async () => {
|
||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "opencode-log-test-"))
|
||||
await using _ = {
|
||||
async [Symbol.asyncDispose]() {
|
||||
await fs.rm(dir, { recursive: true, force: true })
|
||||
},
|
||||
}
|
||||
const file = path.join(dir, "opencode.log")
|
||||
const write = (runID: string) =>
|
||||
Effect.forEach(
|
||||
Array.from({ length: 50 }, (_, index) => index),
|
||||
(index) => Effect.logInfo(`entry-${index}`),
|
||||
).pipe(
|
||||
Effect.provide(Logger.layer([fileLogger(file, runID)]).pipe(Layer.provide(NodeFileSystem.layer), Layer.orDie)),
|
||||
Effect.scoped,
|
||||
)
|
||||
|
||||
await Effect.runPromise(Effect.all([write("run-a"), write("run-b")], { concurrency: "unbounded" }))
|
||||
|
||||
const lines = (await Bun.file(file).text()).trim().split("\n")
|
||||
expect(lines).toHaveLength(100)
|
||||
expect(lines.filter((line) => line.includes("run=run-a"))).toHaveLength(50)
|
||||
expect(lines.filter((line) => line.includes("run=run-b"))).toHaveLength(50)
|
||||
expect(lines.every((line) => line.startsWith("timestamp=") && line.includes(" level=INFO "))).toBe(true)
|
||||
expect(lines.every((line) => !line.includes(" fiber="))).toBe(true)
|
||||
expect(lines.every((line) => !line.startsWith("{"))).toBe(true)
|
||||
})
|
||||
|
||||
test("file logger flattens nested objects", async () => {
|
||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "opencode-log-test-"))
|
||||
await using _ = {
|
||||
async [Symbol.asyncDispose]() {
|
||||
await fs.rm(dir, { recursive: true, force: true })
|
||||
},
|
||||
}
|
||||
const file = path.join(dir, "opencode.log")
|
||||
|
||||
await Effect.logInfo("request complete", {
|
||||
request: { method: "GET", timing: { duration: 42 } },
|
||||
tags: ["api", "test"],
|
||||
}).pipe(
|
||||
Effect.annotateLogs({ session: { id: "session-1" } }),
|
||||
Effect.provide(Logger.layer([fileLogger(file, "run-a")]).pipe(Layer.provide(NodeFileSystem.layer), Layer.orDie)),
|
||||
Effect.scoped,
|
||||
Effect.runPromise,
|
||||
)
|
||||
|
||||
const line = (await Bun.file(file).text()).trim()
|
||||
expect(line).toContain('message="request complete"')
|
||||
expect(line).toContain("request.method=GET")
|
||||
expect(line).toContain("request.timing.duration=42")
|
||||
expect(line).toContain('tags="[\\\"api\\\",\\\"test\\\"]"')
|
||||
expect(line).toContain("session.id=session-1")
|
||||
expect(line).not.toContain("request={")
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue