11# Observability & Instrumentation
22
3- ClickStack is an observability product, so our own code should be a shining example
4- of well-instrumented software. When you add or change a feature, instrument it
5- as part of the change - not as an afterthought.
3+ ClickStack is an observability product, so our own code should be a shining
4+ example of well-instrumented software. When you add or change a feature,
5+ instrument it as part of the change - not as an afterthought.
66
77This guide covers the conventions and the shared helpers. The principles are
88aligned with the
@@ -11,9 +11,9 @@ aligned with the
1111## TL;DR (the non-negotiables)
1212
13131 . ** Every team-scoped operation carries team + user context.** Use
14- ` setBusinessContext(...) ` so ` hyperdx.team.id ` / ` user.id ` end up on
15- the trace. Auth middleware already does this for HTTP requests; background
16- jobs and other entry points must do it themselves.
14+ ` setBusinessContext(...) ` so ` hyperdx.team.id ` / ` user.id ` end up on the
15+ trace. Auth middleware already does this for HTTP requests; background jobs
16+ and other entry points must do it themselves.
17172 . ** If something is worth a log, put it on the span first.** The active span is
1818 the wide event for the current unit of work — attach the fact there as an
1919 attribute. If it also marks a countable event worth aggregating (an error, a
@@ -30,14 +30,15 @@ aligned with the
3030 already in flight, and put point-in-time / gauge-style values (sizes, counts,
3131 depths, current state) there rather than in a gauge. This is an internal
3232 engineering preference, not a rule against metrics — counters and histograms
33- remain first-class, feed alerts and SLOs, and many ClickStack deployments depend
34- heavily on them. See [ Wide events over gauges] ( #wide-events-over-gauges ) .
33+ remain first-class, feed alerts and SLOs, and many ClickStack deployments
34+ depend heavily on them. See
35+ [ Wide events over gauges] ( #wide-events-over-gauges ) .
35365 . ** Use the shared helpers** in
3637 [ ` packages/api/src/utils/instrumentation.ts ` ] ( ../packages/api/src/utils/instrumentation.ts ) .
3738 Don't hand-roll tracer/meter lifecycle.
38- 6 . ** A new surface that queries ClickHouse declares its query attribution.**
39- One provider or one ` withQueryAttribution ` scope at the entry point, not a
40- change at each query. See [ Query attribution] ( #query-attribution ) .
39+ 6 . ** A new surface that queries ClickHouse declares its query attribution.** One
40+ provider or one ` withQueryAttribution ` scope at the entry point, not a change
41+ at each query. See [ Query attribution] ( #query-attribution ) .
4142
4243## The shared helper library
4344
@@ -116,16 +117,16 @@ Standard attribute keys:
116117| -------------------------- | ---------------------------------------------------------- |
117118| ` hyperdx.team.id ` | Owning team (multi-tenancy boundary). Set this everywhere. |
118119| ` user.id ` | Acting user (OTel ` user.* ` semconv). |
119- | ` user.email ` | Acting user's email (OTel ` user.* ` semconv). |
120+ | ` user.email ` | Acting user's email (OTel ` user.* ` semconv). |
120121| ` hyperdx.<domain>.<field> ` | Domain IDs, e.g. ` hyperdx.alert.id ` , ` hyperdx.source.id ` . |
121122| ` feature_flag.<name> ` | Evaluated feature/config flag state. |
122123
123124### Wide events over gauges
124125
125126> ** Scope:** this is ClickStack's _ internal_ instrumentation philosophy for the
126127> code in this repo — not a recommendation that users pick wide events over
127- > metrics. Metrics are a first-class ClickStack signal that many operators rely on;
128- > nothing here discourages them. What follows is simply how _ we_ prefer to
128+ > metrics. Metrics are a first-class ClickStack signal that many operators rely
129+ > on; nothing here discourages them. What follows is simply how _ we_ prefer to
129130> instrument our own services.
130131
131132We instrument in the spirit of
@@ -214,10 +215,10 @@ To make a piece of functionality SLO-able, wrap it with `withOperationMetrics`
214215a streaming proxy). Both emit a standard pair of SLI signals tagged with a
215216stable, low-cardinality ` operation ` name and ` outcome ` (` success ` | ` error ` ):
216217
217- | Metric | Type | Use as |
218- | ------------------------------- | --------- | --------------- |
218+ | Metric | Type | Use as |
219+ | ------------------------------- | --------- | ---------------- |
219220| ` hyperdx.operation.requests ` | counter | availability SLI |
220- | ` hyperdx.operation.duration_ms ` | histogram | latency SLI |
221+ | ` hyperdx.operation.duration_ms ` | histogram | latency SLI |
221222
222223``` ts
223224import { withOperationMetrics } from ' @/utils/instrumentation' ;
@@ -230,27 +231,27 @@ const chartConfig = await withOperationMetrics(
230231```
231232
232233Because every operation reports through the same two metrics, an SLO is just a
233- filter on ` operation ` — availability = ` outcome:success ` / total, latency =
234- the duration histogram. Keep ` operation ` a constant (e.g. ` ai.assistant ` ,
234+ filter on ` operation ` — availability = ` outcome:success ` / total, latency = the
235+ duration histogram. Keep ` operation ` a constant (e.g. ` ai.assistant ` ,
235236` clickhouse_proxy.query ` ); never interpolate IDs or user input into it. Reach
236237for this on functionality with real failure modes worth a target (external
237238dependencies, query proxies, AI calls) — not on thin CRUD that the HTTP
238239auto-instrumentation and error middleware already cover.
239240
240241## Query attribution
241242
242- Every ClickHouse query ClickStack runs is tagged with what asked for it, so a row
243- in the customer's ` system.query_log ` points back to a dashboard tile, a saved
244- search, an alert, or an MCP tool. This is the one piece of our instrumentation
245- that lands in * their * data rather than ours, so it stays small, carries no
246- identity, and must never be able to fail a query.
243+ Every ClickHouse query ClickStack runs is tagged with what asked for it, so a
244+ row in the customer's ` system.query_log ` points back to a dashboard tile, a
245+ saved search, an alert, or an MCP tool. This is the one piece of our
246+ instrumentation that lands in _ their _ data rather than ours, so it stays small,
247+ carries no identity, and must never be able to fail a query.
247248
248249Two channels carry it, set by ` BaseClickhouseClient.query ` :
249250
250- | Where | Shape | What it answers |
251- | --- | --- | --- |
251+ | Where | Shape | What it answers |
252+ | ------------- | ----------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- --- |
252253| ` log_comment ` | JSON payload, e.g. ` {"v":1,"surface":"dashboard","dashboard":"…","tile":"…"} ` | After the fact: ` GROUP BY JSONExtractString(log_comment, 'tile') ` over ` system.query_log ` |
253- | ` query_id ` | ` hdx-<surface>-<uuid> ` | Right now: reading ` system.processes ` while a query is still running |
254+ | ` query_id ` | ` hdx-<surface>-<uuid> ` | Right now: reading ` system.processes ` while a query is still running |
254255
255256The vocabulary lives in
256257[ ` packages/common-utils/src/clickhouse/attribution.ts ` ] ( ../packages/common-utils/src/clickhouse/attribution.ts ) .
@@ -304,14 +305,13 @@ calls `/api/v1/prometheus/query{,_range}`, and `/labels` and
304305` buildLogComment(attribution) ` in the ` x-hyperdx-query-attribution ` header
305306(` QUERY_ATTRIBUTION_HEADER ` ), and the router parses it with ` parseLogComment `
306307into a ` withQueryAttribution ` scope layered over the auth middleware's ` api `
307- scope. The browser's surface and ids win; ` trace ` and ` label ` stay the
308- server's. Queries the route runs through a ClickHouse client (label lookups
309- before 26.9, and the ` prometheusQuery() ` /` prometheusQueryRange() ` fallback)
310- pick the scope up as usual. Requests proxied to ClickHouse's Prometheus HTTP
311- API (queries on 26.6+, label lookups on 26.9+) are tagged
312- by ` clickhousePrometheusRequest ` : ` log_comment ` as a URL setting, ` query_id ` as
313- the ` X-ClickHouse-Query-Id ` header, because the handler rejects ` query_id ` as an
314- unknown setting in the URL.
308+ scope. The browser's surface and ids win; ` trace ` and ` label ` stay the server's.
309+ Queries the route runs through a ClickHouse client (label lookups before 26.9,
310+ and the ` prometheusQuery() ` /` prometheusQueryRange() ` fallback) pick the scope up
311+ as usual. Requests proxied to ClickHouse's Prometheus HTTP API (queries on
312+ 26.6+, label lookups on 26.9+) are tagged by ` clickhousePrometheusRequest ` :
313+ ` log_comment ` as a URL setting, ` query_id ` as the ` X-ClickHouse-Query-Id `
314+ header, because the handler rejects ` query_id ` as an unknown setting in the URL.
315315
316316### Rules
317317
@@ -356,7 +356,7 @@ unknown setting in the URL.
356356- SLO operation metrics (` withOperationMetrics ` ) on an external dependency:
357357 ` packages/api/src/routers/api/ai.ts `
358358- SLO operation metrics (` recordOperationOutcome ` ) on a streaming proxy:
359- ` packages/api/src/routers/api/clickhouseProxy .ts `
359+ ` packages/api/src/utils/proxyOutcome .ts `
360360- End-to-end + sub-operation SLO metrics on a background job (alert evaluation
361361 and its data fetch): ` packages/api/src/tasks/checkAlerts/index.ts `
362362 (` alerts.evaluate ` , ` alerts.query ` )
0 commit comments