MCPcopy
hub / github.com/anomalyco/opencode / tail

Function tail

packages/console/function/src/log-processor.ts:5–86  ·  view source on GitHub ↗
(events: TraceItem[])

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 7

ipPrefixFunction · 0.85
getLakeIngestFunction · 0.85
toLakeEventFunction · 0.85
fetchFunction · 0.70
textMethod · 0.65
logMethod · 0.45
allMethod · 0.45

Tested by

no test coverage detected