A runnable OpenTelemetry example is documentation that starts
Fastify OTEL #214 added a getting-started example with --import, registerOnInitialization and a real OTLP exporter. Why runnable examples are product work.
The package documented the API but did not give a complete, runnable starting point. The related issue pointed at an expanded example and an undefined provider. A reader could copy a snippet and still not know where the SDK starts, where the exporter is configured or why register is not called in the app.
This is one of the pull requests behind Six merged PRs in 72 hours. The series looks at the failure, the fix and the review evidence, one merge at a time.
The change: +58 across 5 files. PR 214.
The bug
That gap is expensive. OpenTelemetry setup has a lifecycle: the SDK must start before the instrumented library is loaded, the exporter needs an endpoint, and the app should not initialize the same SDK twice. A short snippet can hide all three.
The PR adds examples/getting-started with otel.js, app.js, a package.json start script and a README. The example starts the Node SDK with --import, uses registerOnInitialization: true and does not call register in the app.
Why it mattered
A documentation example is a contract with a new user. If it cannot start, the reader cannot tell whether the library is broken, their environment is wrong or the docs are stale. The fastest way to lose a contributor is a quickstart that needs three unstated prerequisites.
The fix
The example uses the Node SDK’s --import flag to start instrumentation before the app, sets registerOnInitialization: true, and links the example from the main README. It starts the Fastify app on port 3000 and the SDK starts the OTLP HTTP exporter.
The evidence
The PR includes the exact commands: cd examples/getting-started, npm install, npm start. That is the level of specificity a getting-started path needs. The example is small, but it covers the lifecycle that a snippet usually omits.
What I would repeat
Treat a runnable example as a test of the onboarding flow. If you cannot copy it into a clean directory and run one command, the documentation is incomplete. Examples also prevent drift: when the API changes, the example breaks at the same time as the user would.
Sources
If this was useful, connect with me on LinkedIn; I write about the bugs behind merged pull requests and the evidence that gets them accepted.
FAQ
- What does the Fastify OTEL example include?
- otel.js, app.js, a package.json start script and a README, under examples/getting-started.
- Why does it use --import?
- The Node SDK needs to start before the instrumented app loads. --import makes that ordering explicit.
- Does the app call register?
- No. The example sets registerOnInitialization: true and starts the SDK from the preload file.
Join the Newsletter
Subscribe to get my latest content by email.
We won't send you spam. Unsubscribe at any time.