summaryrefslogtreecommitdiffhomepage
path: root/cloud/function
diff options
context:
space:
mode:
Diffstat (limited to 'cloud/function')
-rw-r--r--cloud/function/src/log-processor.ts49
-rw-r--r--cloud/function/sst-env.d.ts5
2 files changed, 54 insertions, 0 deletions
diff --git a/cloud/function/src/log-processor.ts b/cloud/function/src/log-processor.ts
new file mode 100644
index 000000000..70fcf5a44
--- /dev/null
+++ b/cloud/function/src/log-processor.ts
@@ -0,0 +1,49 @@
+import { Resource } from "@opencode/cloud-resource"
+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)
+ if (url.pathname !== "/zen/v1/chat/completions") return
+
+ let metrics = {
+ event_type: "completions",
+ "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"],
+ }
+ for (const log of event.logs) {
+ for (const message of log.message) {
+ if (!message.startsWith("_metric:")) continue
+ metrics = { ...metrics, ...JSON.parse(message.slice(8)) }
+ }
+ }
+ console.log(JSON.stringify(metrics, null, 2))
+
+ const ret = await fetch("https://api.honeycomb.io/1/events/zen", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ "X-Honeycomb-Event-Time": (event.eventTimestamp ?? Date.now()).toString(),
+ "X-Honeycomb-Team": Resource.HONEYCOMB_API_KEY.value,
+ },
+ body: JSON.stringify(metrics),
+ })
+ console.log(ret.status)
+ console.log(await ret.text())
+ }
+ },
+}
diff --git a/cloud/function/sst-env.d.ts b/cloud/function/sst-env.d.ts
index 5478b4334..efb047ff5 100644
--- a/cloud/function/sst-env.d.ts
+++ b/cloud/function/sst-env.d.ts
@@ -50,6 +50,10 @@ declare module "sst" {
"type": "sst.sst.Secret"
"value": string
}
+ "HONEYCOMB_API_KEY": {
+ "type": "sst.sst.Secret"
+ "value": string
+ }
"STRIPE_SECRET_KEY": {
"type": "sst.sst.Secret"
"value": string
@@ -76,6 +80,7 @@ declare module "sst" {
"AuthApi": cloudflare.Service
"AuthStorage": cloudflare.KVNamespace
"Bucket": cloudflare.R2Bucket
+ "LogProcessor": cloudflare.Service
}
}