diff --git a/src/index.js b/src/index.js index ce7643f..28a0be6 100644 --- a/src/index.js +++ b/src/index.js @@ -56,25 +56,46 @@ export default { * @param {any} _ctx */ async fetch(request, env, _ctx) { + const start = Date.now(); const { pathname } = new URL(request.url); + const method = request.method; - if (request.method === "GET" && pathname === "/") { - return new Response("miti99bot ok", { - status: 200, - headers: { "content-type": "text/plain" }, - }); - } + const response = await route(request, env, pathname); - if (request.method === "POST" && pathname === "/webhook") { - try { - const handler = await getWebhookHandler(env); - return await handler(request); - } catch (err) { - console.error("webhook handler failed", err); - return new Response("internal error", { status: 500 }); - } - } - - return new Response("not found", { status: 404 }); + // Structured request log for Workers Observability dashboard. + console.log(JSON.stringify({ + msg: "req", + method, + path: pathname, + status: response.status, + ms: Date.now() - start, + })); + return response; }, }; + +/** + * @param {Request} request + * @param {any} env + * @param {string} pathname + */ +async function route(request, env, pathname) { + if (request.method === "GET" && pathname === "/") { + return new Response("miti99bot ok", { + status: 200, + headers: { "content-type": "text/plain" }, + }); + } + + if (request.method === "POST" && pathname === "/webhook") { + try { + const handler = await getWebhookHandler(env); + return await handler(request); + } catch (err) { + console.error("webhook handler failed", err); + return new Response("internal error", { status: 500 }); + } + } + + return new Response("not found", { status: 404 }); +} diff --git a/wrangler.toml b/wrangler.toml index 8ae188c..8d5b92f 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -35,6 +35,22 @@ database_id = "261b54e7-0fdb-4fe7-8ed9-2e8a8bcf459c" [triggers] crons = ["0 17 * * *"] +# Workers Observability — captures console.* logs, request metadata, and +# invocation traces in the Cloudflare dashboard (Observability → Logs). +# 200k events/day included on free plan. `head_sampling_rate = 1` keeps +# all invocations; drop to 0.1 for 10% sampling if volume grows. +[observability] +enabled = true +head_sampling_rate = 1 + +[observability.logs] +enabled = true +invocation_logs = true + +[observability.traces] +enabled = true +head_sampling_rate = 1 + # Secrets (set via `wrangler secret put `, NOT in this file): # TELEGRAM_BOT_TOKEN — bot token from @BotFather # TELEGRAM_WEBHOOK_SECRET — arbitrary high-entropy string, also set in .env.deploy