Skip to content

feat(sketchdb): surface accuracy envelope on PromQL response (§6.4, A+C) - #56

Merged
zzylol merged 1 commit into
mainfrom
feat/sketchdb-accuracy-in-promql-response
Apr 21, 2026
Merged

zzylol merged 1 commit into
mainfrom
feat/sketchdb-accuracy-in-promql-response

Conversation

@zzylol

@zzylol zzylol commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Addresses TODO.md blocker #2 sub-item #3. Queries answered by the sketch DB now carry a theoretical accuracy bound back to the client as two fields on the Prometheus HTTP response:

A — structured `accuracy` (top-level)

```json
"accuracy": {
"epsilon": 0.008125,
"delta": 0.0,
"kind": "relative_cardinality",
"per_segment": [ /* populated on schema-timeline crossings */ ]
}
```

C — human-readable `infos` (Prometheus 3.0 / Grafana 11+)

```json
"infos": ["accuracy: ε=0.008125, δ=0, kind=relative_cardinality"]
```

Compatibility

  • Standard Prometheus clients ignore unknown top-level fields — a regression test locks this down with a stripped-down decoder.
  • Grafana 10 users: `warnings` stays reserved for partial-result advisories (see PR feat(http): surface §7 Partial results on Prometheus warnings #49); accuracy doesn't hijack it.
  • Grafana 11+: renders `infos` inline automatically.

Cross-boundary semantics

When a query crosses a schema-timeline reconfigure, `per_segment` lists each piece's `(agg_id, range_ms, profile)`; the top-level envelope is (max ε, max δ) — a conservative upper bound across segments.

Tests

777 → 784 (+5), clippy + fmt clean.

🤖 Generated with Claude Code

Addresses TODO.md blocker #2 subitem #3. Queries answered by the
sketch DB now carry a theoretical accuracy bound back to the
client as two fields on the Prometheus HTTP response:

* **A — structured `accuracy` (top-level)**:
  ```json
  "accuracy": {
    "epsilon": 0.008125,
    "delta":   0.0,
    "kind":    "relative_cardinality",
    "per_segment": [...]   // populated on schema-timeline crossings
  }
  ```
* **C — human-readable `infos` (Prometheus 3.0 / Grafana 11+)**:
  ```json
  "infos": ["accuracy: ε=0.008125, δ=0, kind=relative_cardinality"]
  ```

Both are standard Prometheus-tolerant extensions — unknown
top-level fields are ignored by the upstream client/Grafana 10.
A regression test confirms a stripped-down "standard Prometheus"
decoder round-trips through our extended response.

## Wire shape

`warnings` stays reserved for partial-result / fallback
advisories (PR #49's schema-timeline dispatcher still uses it);
accuracy gets its own dedicated field so the semantics don't
mix.

When a query crosses a schema-timeline boundary, `per_segment`
lists each segment's `(agg_id, range_ms, profile)`. The
top-level `profile` is the max-ε, max-δ envelope across
segments — a conservative upper bound.

## Changes

* `stores/sketch_db/accuracy.rs`:
  * `AccuracyEnvelope { profile, per_segment }` + builders
    (`single`, `from_segments`)
  * `PerSegmentAccuracy { agg_id, range_ms, profile }`
  * `AccuracyProfile::summary()` / `AccuracyEnvelope::summary()`
    emit the `infos` one-liner.
* `engines/query_result.rs`: `InstantVector` / `RangeVector`
  carry `accuracy: Option<AccuracyEnvelope>`; new
  `QueryResult::{accuracy(), with_accuracy()}`.
* `drivers/query/adapters/prometheus_http.rs`:
  `PrometheusResponse::{infos, accuracy}` fields +
  `with_accuracy()` builder.
  `format_success_response` / `format_range_success_response`
  thread `result.accuracy()` onto the response.
* `engines/simple_engine.rs`:
  * `SimpleEngine::accuracy_envelope_for(agg_id)` — single-
    aggregation helper.
  * `execute_context` attaches single-agg accuracy.
  * Timeline dispatch builds per-segment accuracy list and
    attaches the multi-segment envelope.

## Tests

777 → 784 (+5 green):
* `prometheus_response_carries_accuracy_top_level_and_infos_mirror`
* `prometheus_response_without_accuracy_skips_both_fields`
* `prometheus_response_per_segment_contains_all_segments_with_worst_case_top`
* `accuracy_coexists_with_warnings_without_interference`
* `promql_standard_client_can_decode_response_ignoring_extensions`

clippy + fmt clean.
@zzylol
zzylol merged commit 1321832 into main Apr 21, 2026
@zzylol
zzylol deleted the feat/sketchdb-accuracy-in-promql-response branch May 9, 2026 18:00
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.

1 participant