网页 综合 更新时间 2026-02-23 15:00:00

Build a dinosaur runner game with Deno, pt. 6 | Deno

自动蜘蛛 deno.com 正文约 6000 字
Build a dinosaur runner game with Deno, pt. 6 | Deno
This series of blog posts will guide you through building a simple browser-based dinosaur runner game using Deno.
In Stage 5, we gave players an identity: a name, a dino colour, a background theme, and a difficulty level, all persisted in PostgreSQL and loaded back on every visit. The game is now feature-complete. But shipping features is only half the story. The other half is understanding what’s happening once real players arrive.
Are the API routes fast enough? Is the leaderboard staying healthy? Is there a spike in errors after a deployment? You can’t answer those questions with console.log("Server is running!"). In this final stage, we’ll wire up an observability layer that makes every question answerable, leaning on Deno Deploy’s built-in logs, traces, and metrics dashboards so you don’t need to reach for a separate monitoring product to get started.
Keep reading and build along or view the entire source here.
By the end of this post you will have:
Both are visible in Deno Deploy’s built-in dashboards alongside the platform’s automatic metrics, no extra infrastructure required.
Observability is the ability to understand what your system is doing from the outside, by examining the data it produces. That data typically comes in three forms:
Deno Deploy provides a dashboard for all three. Logs and traces support custom instrumentation, you can add your own structured log events and custom spans. The Metrics dashboard shows platform-level data that Deno Deploy captures automatically, which we’ll look at in its own section.
We need one package: the OpenTelemetry API. On Deno Deploy, the runtime wires up the SDK and exporter for you, no other configuration required. The API package gives us the interfaces we call in our code; the platform handles where the data goes.
Add it to deno.json:
Create src/telemetry.ts to hold the shared tracer instance:
Keeping this in one place means every file uses the same tracer name and version, which groups all your custom spans together in the dashboard.
Every console.log call you make is captured by Deno Deploy and shown in the Logs tab of your app’s dashboard. But plain-text logs are hard to filter. The upgrade is to emit structured JSON, a single parseable object per event that you can search by any field.
Create a logging middleware in src/middleware/logging.ts:
Register it at the top of your middleware stack in src/main.ts, so it wraps every request:
After deploying, your Logs dashboard will show a clean stream of structured entries like this:
You can filter by any field, for example, typing status:500 to find all errors, or path:/api/scores to see only score submissions.
Beyond request logging, you can emit structured events from inside your route handlers for application-level insight. When a score is saved, we log everything useful about that game:
This creates a searchable audit trail of every game that was played. Open the Logs dashboard, filter by event:score_submitted, and you have a live feed of player activity without any dedicated analytics infrastructure. We do the same for customization saves:
Errors get the same treatment, using console.error means they’re easy to distinguish in the dashboard:
Tip: Logs emitted inside a custom span (see the next section) are automatically correlated with that trace in the dashboard. You can click a log line and jump straight to the trace it belongs to.
A trace is a record of a single operation as it flows through your system. It’s made up of spans. These are named, timed units of work that nest inside each other to form a waterfall diagram.
Deno Deploy automatically creates a root span for every incoming HTTP request and for every outbound fetch call your code makes. What we’re adding here are child spans around the business logic inside each route handler, so you can see exactly where time goes: is a slow /api/leaderboard response caused by the database query, or something in the response serialisation?
The pattern is the same everywhere: call tracer.startActiveSpan() with a name, do your work inside the callback, and call span.end() in a finally block so the span is always closed, even if an error is thrown.
Span attributes are key/value pairs attached to a span. They appear in the trace detail view and can be used to filter traces. For example, filtering for spans where leaderboard.rows_returned is 0 would highlight times the database returned an empty result.
span.recordException() captures the full error object, including the stack trace, and attaches it as a span event. This makes it much easier to debug production errors than reading plain error logs.
The score submission route does the most work: it validates input, inserts the score, and checks the resulting global rank. All of that lives in a single span, with the most useful facts attached as attributes:
span.addEvent() adds a timestamped marker inside the span, this is useful for significant moments that aren’t worth a whole new span. The new_global_record event will appear as a point on the timeline in the trace viewer every time someone sets a new record.
We add the same pattern to the customization routes at customization.load and customization.save, each carrying attributes for player name, theme, and the source of the settings (database, anonymous, or defaults):
Once deployed, open the Traces dashboard in Deno Deploy and click on any POST /api/scores request. You’ll see a waterfall: the outer HTTP span wrapping your score.submit span. Hover over your span to see the attributes and events you attached, and click through to any correlated log lines.
The Metrics dashboard in Deno Deploy shows platform-level data that the runtime collects automatically, no code changes required. For this game, these are the most useful panels and what they tell you:
HTTP req/min by status code is the clearest signal of overall health. A jump in 5xx responses after a deployment means something broke; a steady stream of 4xx responses on /api/scores might mean
访问原文 返回搜索 https://deno.com/blog/build-a-game-with-deno-6
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。