log-processor.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { Resource } from "@opencode-ai/console-resource"
  2. import type { TraceItem } from "@cloudflare/workers-types"
  3. export default {
  4. async tail(events: TraceItem[]) {
  5. for (const event of events) {
  6. if (!event.event) continue
  7. if (!("request" in event.event)) continue
  8. if (event.event.request.method !== "POST") continue
  9. const url = new URL(event.event.request.url)
  10. if (
  11. url.pathname !== "/zen/v1/chat/completions" &&
  12. url.pathname !== "/zen/v1/messages" &&
  13. url.pathname !== "/zen/v1/responses" &&
  14. !url.pathname.startsWith("/zen/v1/models/") &&
  15. url.pathname !== "/zen/go/v1/chat/completions" &&
  16. url.pathname !== "/zen/go/v1/messages" &&
  17. url.pathname !== "/zen/go/v1/responses" &&
  18. !url.pathname.startsWith("/zen/go/v1/models/")
  19. )
  20. return
  21. let data = {
  22. "cf.continent": event.event.request.cf?.continent,
  23. "cf.country": event.event.request.cf?.country,
  24. "cf.city": event.event.request.cf?.city,
  25. "cf.region": event.event.request.cf?.region,
  26. "cf.latitude": event.event.request.cf?.latitude,
  27. "cf.longitude": event.event.request.cf?.longitude,
  28. "cf.timezone": event.event.request.cf?.timezone,
  29. duration: event.wallTime,
  30. request_length: parseInt(event.event.request.headers["content-length"] ?? "0"),
  31. status: event.event.response?.status ?? 0,
  32. ip: event.event.request.headers["x-real-ip"],
  33. }
  34. const time = new Date(event.eventTimestamp ?? Date.now()).toISOString()
  35. const events = []
  36. for (const log of event.logs) {
  37. for (const message of log.message) {
  38. if (!message.startsWith("_metric:")) continue
  39. const json = JSON.parse(message.slice(8))
  40. data = { ...data, ...json }
  41. if ("llm.error.code" in json) {
  42. events.push({ time, data: { ...data, event_type: "llm.error" } })
  43. }
  44. }
  45. }
  46. events.push({ time, data: { ...data, event_type: "completions" } })
  47. console.log(JSON.stringify(data, null, 2))
  48. const ret = await fetch("https://api.honeycomb.io/1/batch/zen", {
  49. method: "POST",
  50. headers: {
  51. "Content-Type": "application/json",
  52. "X-Honeycomb-Team": Resource.HONEYCOMB_API_KEY.value,
  53. },
  54. body: JSON.stringify(events),
  55. })
  56. console.log(ret.status)
  57. console.log(await ret.text())
  58. }
  59. },
  60. }