cloudaxe-opencode/packages/console/function/src/log-processor.ts

65 lines
2.4 KiB
TypeScript
Raw Normal View History

2025-10-05 01:33:39 +00:00
import { Resource } from "@opencode-ai/console-resource"
2025-09-08 19:46:57 +00:00
import type { TraceItem } from "@cloudflare/workers-types"
export default {
async tail(events: TraceItem[]) {
for (const event of events) {
if (!event.event) continue
if (!("request" in event.event)) continue
if (event.event.request.method !== "POST") continue
const url = new URL(event.event.request.url)
2025-09-23 13:15:55 +00:00
if (
url.pathname !== "/zen/v1/chat/completions" &&
url.pathname !== "/zen/v1/messages" &&
2025-11-18 22:42:46 +00:00
url.pathname !== "/zen/v1/responses" &&
2026-02-26 16:17:15 +00:00
!url.pathname.startsWith("/zen/v1/models/") &&
url.pathname !== "/zen/go/v1/chat/completions" &&
url.pathname !== "/zen/go/v1/messages" &&
url.pathname !== "/zen/go/v1/responses" &&
!url.pathname.startsWith("/zen/go/v1/models/")
2025-09-23 13:15:55 +00:00
)
continue
2025-09-08 19:46:57 +00:00
2026-02-10 20:55:31 +00:00
let data = {
2025-09-08 19:46:57 +00:00
"cf.continent": event.event.request.cf?.continent,
"cf.country": event.event.request.cf?.country,
"cf.city": event.event.request.cf?.city,
"cf.region": event.event.request.cf?.region,
"cf.latitude": event.event.request.cf?.latitude,
"cf.longitude": event.event.request.cf?.longitude,
"cf.timezone": event.event.request.cf?.timezone,
duration: event.wallTime,
request_length: parseInt(event.event.request.headers["content-length"] ?? "0"),
status: event.event.response?.status ?? 0,
ip: event.event.request.headers["x-real-ip"],
}
2026-02-10 22:36:32 +00:00
const time = new Date(event.eventTimestamp ?? Date.now()).toISOString()
2026-02-10 20:55:31 +00:00
const events = []
2025-09-08 19:46:57 +00:00
for (const log of event.logs) {
for (const message of log.message) {
if (!message.startsWith("_metric:")) continue
2026-02-10 20:55:31 +00:00
const json = JSON.parse(message.slice(8))
data = { ...data, ...json }
if ("llm.error.code" in json) {
2026-02-10 22:56:10 +00:00
events.push({ time, data: { ...data, event_type: "llm.error" } })
2026-02-10 20:55:31 +00:00
}
2025-09-08 19:46:57 +00:00
}
}
2026-02-10 22:56:10 +00:00
events.push({ time, data: { ...data, event_type: "completions" } })
2026-02-10 20:55:31 +00:00
console.log(JSON.stringify(data, null, 2))
2025-09-08 19:46:57 +00:00
2026-02-10 20:55:31 +00:00
const ret = await fetch("https://api.honeycomb.io/1/batch/zen", {
2025-09-08 19:46:57 +00:00
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Honeycomb-Team": Resource.HONEYCOMB_API_KEY.value,
},
2026-02-10 20:55:31 +00:00
body: JSON.stringify(events),
2025-09-08 19:46:57 +00:00
})
console.log(ret.status)
console.log(await ret.text())
}
},
}