Skip to content

feat(entities): a span per SQL statement — all 245 of them, from one change (telemetry P10) - #411

Merged
athvin merged 1 commit into
mainfrom
bm/telemetry-p10-statement-spans
Nov 2, 2026
Merged

athvin merged 1 commit into
mainfrom
bm/telemetry-p10-statement-spans

Conversation

@athvin

@athvin athvin commented Nov 2, 2026

Copy link
Copy Markdown
Owner

"Attribute every single SQL statement" — the payload of the programme. 244 declared statements plus the bulk-append path, instrumented by a ~145-line change to the emitters rather than 245 edits.

Why the emitters are the right seam

Every persistent statement in this repo is a declared .sql + .yaml pair, and every one becomes Rust through five functions in er-sql-check/src/emit.rs. The statement's identity — the (entity, folder, stem) triple that names its file — is already in scope where each emitter writes its conn.prepare / client.query line.

So attribution is by construction: a statement added tomorrow is attributed without anyone remembering to, and er-entity verify fails if a generated file drifts.

const SQL: &str = "SELECT record_key\nFROM lake.main.int_std_records\n…";
let __span = ::er_span::sql_span(
    ::er_span::Store::Lake, "int_std_record", "read", "all_keys_live",
    "int_std_records", SQL,
);
let __entered = __span.enter();
let mut stmt = ::er_span::check(&__span, conn.prepare(SQL))?;
let batches = ::er_span::check(&__span, stmt.query_arrow(…))?.collect::<Vec<_>>();
::er_span::rows(&__span, batches.iter().map(|b| b.num_rows() as u64).sum());

The async path uses instrument, never enter

A span guard held across an .await attributes whatever else that thread runs to this span. The control-plane functions are async — so the wrong form would have been generated into 110 call sites simultaneously.

They use ::er_span::instrument(&__span, fut).await and only enter the span afterwards, for the synchronous row mapping. A test asserts the absence of the guard form, not merely the presence of the right one.

Three classes of SQL, three treatments

Class db.query.text Instead
Declared (244) exported, gated on ER_TRACE_SQL_TEXT —
append_rows never generator_span + compile-time summary
Engine-core generators never existing er-span treatment

Declared statements are provably literal-free: the analyser rewrites every $name to a bound $N, so the text carries placeholders and never values. The template is recorded even for a snapshot-pinned read — splicing the version in would make the attribute unique per call.

append_rows assembles SQL at runtime (500 placeholder tuples joined), so it gets a summary — INSERT INTO lake.main.raw_records VALUES (…) — and one span for the whole append, not one per chunk. Chunking is a bind limit, not a unit of work; a span per chunk would make a 10k-row append look like twenty statements.

Cost, and the PII boundary

This adds no log volume. A tracing span emits no record on its own — only events do. So 245 spans are pure context today, and become exported spans the day the OTLP exporter lands.

sql_span takes no parameter argument — there is nowhere to put a bind value. In a deduplication engine the bind values are the payload, so §10.4's rule is a property of the signature rather than a line in a document.

What to review

The emitter and its tests, not the 3,747-line generated diff. The tests now pin the statement identity, the store, the declared relation, and the absence of the across-await guard. Four existing assertions changed because they pinned the old emitted shape; entity is threaded into three emitters so the span can carry the declared relation.

make check green at exit 0 — including entities-check, which byte-compares all 51 regenerated files.

🤖 Generated with Claude Code

…change (telemetry P10)

"Attribute every single SQL statement" — the payload of the programme. 244
declared statements plus the bulk-append path, instrumented by a ~145-line
change to the emitters rather than 245 edits.

## Why the emitters are the right seam

Every persistent statement in this repo is a declared `.sql` + `.yaml` pair,
and every one of them becomes Rust through five functions in
`er-sql-check/src/emit.rs`. The statement's identity — the `(entity, folder,
stem)` triple that names its file — is **already in scope** at the moment each
emitter writes its `conn.prepare` / `client.query` line. So attribution is by
construction: a statement added tomorrow is attributed without anyone
remembering to, and `er-entity verify` fails if a generated file drifts.

For a lake read the emitted body is now:

    const SQL: &str = "SELECT record_key\nFROM lake.main.int_std_records\n…";
    let __span = ::er_span::sql_span(
        ::er_span::Store::Lake, "int_std_record", "read", "all_keys_live",
        "int_std_records", SQL,
    );
    let __entered = __span.enter();
    let mut stmt = ::er_span::check(&__span, conn.prepare(SQL))?;
    let batches = ::er_span::check(&__span, stmt.query_arrow(…))?.collect();
    ::er_span::rows(&__span, batches.iter().map(|b| b.num_rows() as u64).sum());

## The async path uses instrument, never enter

A `tracing` span guard held across an `.await` attributes whatever else that
thread runs to this span. The control-plane functions are async, so the wrong
form would have been GENERATED into 110 call sites simultaneously. They use
`::er_span::instrument(&__span, fut).await` and only enter the span afterwards,
for the synchronous row mapping. A test asserts the absence of the guard form,
not just the presence of the right one.

## Three classes of SQL, three treatments

- **Declared** statements export their template as `db.query.text`, gated on
  `ER_TRACE_SQL_TEXT`. Provably literal-free: the analyser rewrites every
  `$name` to a bound `$N`, so the text carries placeholders and never values.
  The TEMPLATE is recorded even for a snapshot-pinned read — splicing the
  version in would make the attribute unique per call.
- **`append_rows`** assembles its SQL at runtime (500 placeholder tuples
  joined), so it gets a `generator_span` with a compile-time summary —
  `INSERT INTO lake.main.raw_records VALUES (…)` — and never its text. One span
  for the whole append, not one per chunk: chunking is a bind limit, not a unit
  of work, and a span per chunk would make a 10k-row append look like twenty
  statements.
- Engine-core generator SQL keeps its own treatment in `er-span`.

## Cost, and the PII boundary

**This adds no log volume.** A `tracing` span emits no record on its own —
only events do — so 245 spans are pure context today and become exported spans
the day the OTLP exporter lands.

`sql_span` takes no parameter argument: there is nowhere to put a bind value.
In a deduplication engine the bind values are the payload, so §10.4's rule is a
property of the signature rather than a line in a document.

## What a reviewer should read

The emitter and its tests, not the 3,747-line generated diff. The tests now pin
the statement identity, the store, the declared relation, and the absence of
the across-await guard. Four existing assertions changed because they pinned
the old emitted shape; `entity` is threaded into three emitters so the span can
carry the declared relation.

`make check` green at exit 0 — including `entities-check`, which byte-compares
all 51 regenerated files.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@athvin
athvin merged commit be98067 into main Nov 2, 2026
34 checks passed
@athvin
athvin deleted the bm/telemetry-p10-statement-spans branch November 2, 2026 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants