Skip to content

fix(blocks): declare second-scale bucket boundaries for duration histograms - #427

Merged
hugo-ccabral merged 2 commits into
mainfrom
fix/otel-histogram-bucket-boundaries
Aug 13, 2026
Merged

hugo-ccabral merged 2 commits into
mainfrom
fix/otel-histogram-bucket-boundaries

Conversation

@nicacioliveira

@nicacioliveira nicacioliveira commented Aug 4, 2026 •

Copy link
Copy Markdown

Problem

Every duration metric here is recorded in seconds — the record helpers divide by 1000, and METRIC_METADATA already declares unit: "s". That part is correct per SemConv.

The buckets, though, are configured somewhere else entirely. Adapters wire up histogramRecord themselves, and the reference implementation in this file's own docblock called createHistogram(name) with no advice. That inherits the OTel SDK defaults — [0, 5, 10, 25, 50, 75, 100, 250, 500, 1000] — which are milliseconds.

Unit and buckets had drifted apart, and nothing in the type system connected them.

Measured impact

Production ClickHouse, before this change:

metric avg (s) share in first bucket
http.server.request.duration 0.251 99.92%
deco.cms.resolve.duration 0.485 99.82%
http.client.request.duration 0.458 99.93%
deco.loader.duration 0.142 99.95%

78.7M observations/hour on http.server.request.duration alone, essentially all of them in bucket 1. Every quantile reads back as ~0.

The consequence is not theoretical: the latency dashboards fall back to otel_traces, which is tail-sampled at ~1.7%. A p95 over a short window is then often just the second-slowest sampled request. That produced a live misdiagnosis yesterday — a p95 of 66s computed from 124 sampled spans looked like a regression and wasn't.

For contrast, the deno scope already gets this right for seconds (0.005, 0.01, 0.025, 0.05, …), and the legacy deco-cx/deco runtime is fine because it records milliseconds into millisecond buckets (6% and 54% in the first bucket, spread properly).

Changes

  • Export DURATION_BUCKET_BOUNDARIES_SECONDS — the SemConv-recommended defaults for HTTP duration histograms.
  • Add an optional boundaries field to METRIC_METADATA, set on all four second-valued metrics. The map is already exported and already consulted for unit/description, so adapters pick this up at histogram creation — including deco.cms.resolve.duration, which has no record call site in this file. That is why the fix lives in the metadata map rather than in an extra histogramRecord argument.
  • Update the docblock's reference adapter to forward boundaries into advice, and state that it is required rather than optional.
  • Three regression tests: every unit: "s" metric must declare boundaries, they must be second-scaled (first boundary < 1 — a millisecond set starts at 5), sorted and deduped, and must cover both the observed average range and the 10s+ tail.

MeterAdapter is unchanged, so no adapter breaks.

Scope

Only new data is affected. Existing histogram rows stay unusable for quantiles — dashboards reading history will need the time range moved past the deploy.

Adapters that never consult METRIC_METADATA will keep inheriting SDK defaults. The docblock now shows the correct wiring, but if there is an in-repo adapter that should be updated in the same change, point me at it and I will add it here.

Verification

  • tsc --noEmit clean
  • vitest run packages/blocks/ → 629 passed / 46 files

🤖 Generated with Claude Code


Summary by cubic

Fixes seconds-valued duration histograms by declaring second-scale bucket boundaries and wiring them through METRIC_METADATA, restoring accurate quantiles; previously, millisecond defaults put ~100% of traffic in the first bucket.

  • Exported DURATION_BUCKET_BOUNDARIES_SECONDS (SemConv HTTP duration defaults).

  • Added optional boundaries to METRIC_METADATA and set it for http.server.request.duration, http.client.request.duration, deco.cms.resolve.duration, and deco.loader.duration.

  • Updated the docblock’s reference adapter to pass boundaries to histogram advice (required to avoid millisecond defaults).

  • Added tests ensuring all unit: "s" metrics declare second-scale boundaries that are sorted, deduped, and span sub-50ms to 10s+. Merged main without changing this guard.

  • Migration

    • Only new data uses the fixed buckets; adjust dashboards to post-deploy windows.
    • Adapters must forward METRIC_METADATA.boundaries to histogram advice to avoid millisecond default buckets.

Written for commit 310ffdf. Summary will update on new commits.

Review in cubic

…ograms

The record helpers divide by 1000, so every duration metric is emitted in
seconds — correct per SemConv, and METRIC_METADATA already declares unit: "s".
But the buckets are configured somewhere else entirely: adapters wire up
`histogramRecord` themselves, and the reference implementation in this file's
own docblock called `createHistogram(name)` with no advice. That inherits the
OTel SDK defaults, `[0, 5, 10, 25, 50, 75, 100, 250, 500, 1000]`, which are
milliseconds.

Unit and buckets had drifted apart, and nothing in the type system connected
them.

Measured on the production ClickHouse before this change:

  metric                          avg (s)   first bucket
  http.server.request.duration      0.251        99.92%
  deco.cms.resolve.duration         0.485        99.82%
  http.client.request.duration      0.458        99.93%
  deco.loader.duration              0.142        99.95%

78.7M observations/hour on http.server.request.duration alone, essentially all
of them in bucket 1. Every quantile read back as ~0, so the latency dashboards
fell back to `otel_traces` — which is tail-sampled at ~1.7%, meaning a p95 over
a short window was often just the second-slowest sampled request. That cost us
a live misdiagnosis: a p95 of 66s computed from 124 sampled spans looked like a
regression and wasn't.

For contrast, the `deno` scope gets this right for seconds today
(`0.005, 0.01, 0.025, 0.05, ...`), and the legacy deco-cx/deco runtime is fine
because it records milliseconds into millisecond buckets.

Changes:

- Export DURATION_BUCKET_BOUNDARIES_SECONDS, the SemConv-recommended defaults
  for HTTP duration histograms.
- Add an optional `boundaries` field to METRIC_METADATA and set it on all four
  second-valued metrics. The map is already exported and already consulted for
  unit/description, so adapters pick this up at histogram creation — including
  deco.cms.resolve.duration, which has no record call site in this file.
- Update the docblock's reference adapter to forward `boundaries` to `advice`,
  and say plainly that it is required rather than optional.
- Add three regression tests asserting every unit: "s" metric declares
  boundaries and that they are second-scaled, so the next duration metric that
  ships without them fails here instead of in a dashboard months later.

Only new data is affected; existing histogram rows stay unusable for quantiles.

Verified: tsc --noEmit clean, 629/629 tests pass across 46 files.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
hugo-ccabral
hugo-ccabral previously approved these changes Aug 6, 2026
Conflict was in observability.test.ts only: both sides appended a new
describe block at the end of the file (#447's normalizePath cardinality
cases and this PR's METRIC_METADATA bucket guards), so git overlapped the
two regions. Kept both, and merged the import list.

observability.ts auto-merged cleanly. Verified semantically, not just
textually: main added SWR cache instrumentation, so the guard in this PR
("every metric with unit 's' must declare boundaries") could have started
failing. All 4 second-scale metrics in the merged METRIC_METADATA carry
DURATION_BUCKET_BOUNDARIES_SECONDS.

Gates: 673 tests pass (51 files), blocks typecheck clean. biome lint
diagnostics on these two files are identical to main's baseline; the
resolution also fixes the import-sort error main currently carries.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@hugo-ccabral
hugo-ccabral merged commit e9785af into main Aug 13, 2026
1 check passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 7.37.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants