Skip to Content
OpenPerf

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 @default and 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.ts with mocks.
  • A test that runs the whole stack in Docker is *.e2e.test.ts in tools/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: Intl formatting, 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's testing/ directory.
  • bun run test:coverage writes 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.

AreaHowHelpers
corePlain unit tests
serverinitServer with dsn: receiver.dsn, then assert receiver.readExport()startOtlpReceiver
bunReal Bun.serve({ port: 0 }) and fetch in the test processgetBun, listenLocally
Bun processrunProcess with bun --preload instrument.ts apprunProcess, bun/libs/testing/fixtures
Node processBundle the fixture with bun build, then run it with node --importrunProcess, findNode
browserregisterDom() in files that need a DOM: happy-dom, fake timers, fake fetchcreateTestClient and its readExport
clirunCli against a local node:http server and a temp directorylistenLocally
  • One initServer per file, because the OpenTelemetry provider is global. A different config gets its own file, like init-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. readExport flushes only the test process, so the child must send its data itself. Child process tests in a file run with describe.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 test puts a node that runs Bun first on PATH. Start real Node with findNode().
  • Bundle Node fixtures into node_modules/.cache inside this package, with dependencies external, so Node resolves import-in-the-middle and 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 TTL in the table DDL; change it with ALTER TABLE … MODIFY TTL in 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/rollups mirrors its materialized view. rollups.integration.test.ts fails when the two drift.
  • Keep aggregate states bounded: quantilesTDigest for durations, uniqCombined(12) for unique counts.

On this page