Conventions
Code style
OpenPerf rules for code and tests, on top of the shared rules in AGENTS.md.
Code
- Write code for cases that occur. No fallbacks, duplicate checks or values without a consumer.
- Make each decision in one place: the layer that owns it. Others rely on it.
- An arrow function whose body doesn't fit on one line gets a block with
return. Components returning JSX are the exception. - Document what a published package exports from its entry points, such as
@openperf/sdk/node, only where the name and type don't tell the whole story: defaults, limits, value formats, errors and when it does nothing. Skip a JSDoc that restates the name. Write one or two plain sentences, state each fact once where it applies, put defaults in@defaultand errors in@throws. Types, interfaces and classes get no JSDoc; document their properties instead. Internal code stays without comments.
Tests
- Test logic that can break on its own: branching, parsing, edge cases, security checks. Don't test code that only restates itself, such as templates, lookup tables or config.
- A test that needs live stores is
*.integration.test.ts. Everything else is*.test.tswith mocks. - A test that runs the whole stack in Docker is
*.e2e.test.tsintools/e2e. See End-to-end tests. - Test a behavior once, at the layer that owns it. A helper gets its own test only for edge cases its callers' tests don't reach.
- Don't test what a library guarantees:
Intlformatting, zod parsing,new URL. - Assert results and bound parameters, not SQL text.
- Permission checks live in
session.guard.integration.test.ts. Feature tests cover their own rules, such as cross-organization access. - Fold cases that differ only by input into
it.each. Setup shared by several files goes to the module'stesting/directory. bun run test:coveragewrites LCOV coverage for unit and integration tests.
SDK tests
Every fix starts with a test that fails on the old code. Run SDK tests with bun run --cwd packages/sdk test.
| Area | How | Helpers |
|---|---|---|
core | Plain unit tests | |
server | initServer with dsn: receiver.dsn, then assert receiver.readExport() | startOtlpReceiver |
bun | Real Bun.serve({ port: 0 }) and fetch in the test process | getBun, listenLocally |
| Bun process | runProcess with bun --preload instrument.ts app | runProcess, bun/libs/testing/fixtures |
| Node process | Bundle the fixture with bun build, then run it with node --import | runProcess, findNode |
browser | registerDom() in files that need a DOM: happy-dom, fake timers, fake fetch | createTestClient and its readExport |
cli | runCli against a local node:http server and a temp directory | listenLocally |
- One
initServerper file, because the OpenTelemetry provider is global. A different config gets its own file, likeinit-server.unsampled.test.ts. - The receiver keeps everything a file exported. Find spans and errors by a unique name or path, not by index or kind alone.
- Test exits, signals and unhandled rejections in a child process.
readExportflushes only the test process, so the child must send its data itself. Child process tests in a file run withdescribe.concurrent. - Don't race a timer against the SDK's exit. To prove the SDK ends a process, keep the child
alive longer than
EXIT_FLUSH_TIMEOUT_MS; to prove it keeps running, let the child print and end on its own. bun run testputs anodethat runs Bun first onPATH. Start real Node withfindNode().- Bundle Node fixtures into
node_modules/.cacheinside this package, withdependenciesexternal, so Node resolvesimport-in-the-middleand OpenTelemetry. - Don't assert durations. Assert the cause of a slowdown, or attach a before and after measurement to the PR when nothing observable changes.
ClickHouse migrations
- One statement per file, and every statement can run twice (
IF NOT EXISTS, no unguarded backfills): a migration file may run again after a partial failure. - Retention is a
TTLin the table DDL; change it withALTER TABLE … MODIFY TTLin a new migration.
ClickHouse reads
- Lists, summaries and charts read hourly rollups through
buildRollupSource: full hours come from the rollup, the edges of the window from the raw table. Raw tables answer drill-downs by a selective key: a trace, a fingerprint, a log page, the hours a rollup picked for samples. - Each rollup in
clickhouse/libs/rollupsmirrors its materialized view.rollups.integration.test.tsfails when the two drift. - Keep aggregate states bounded:
quantilesTDigestfor durations,uniqCombined(12)for unique counts.