Skip to content

feat: capture BigQuery query stats for mktplace metering#65

Merged
gabrielbressan-tfy merged 4 commits into
mainfrom
feat/bigquery-usage-metering
Jun 7, 2026
Merged

feat: capture BigQuery query stats for mktplace metering#65
gabrielbressan-tfy merged 4 commits into
mainfrom
feat/bigquery-usage-metering

Conversation

@gabrielbressan-tfy

Copy link
Copy Markdown
Collaborator

Summary

  • Adds query_stats_registry.py: thread-safe in-process registry accumulating QueryJob stats (total_bytes_billed, cache_hit, project_id) keyed by a mktplace_query_id correlation id passed via ctx.params
  • Modifies BigQueryHandler.native_query() to capture stats into the registry after each query execution (via new _record_query_stats helper)
  • Adds GET /api/sql/query_stats/<query_id> endpoint that pops and returns the stats JSON; returns {} for unknown ids

Why

mktplace needs to meter actual BigQuery bytes scanned for public datasources (Talentify pays GCP on-demand). The QueryJob.total_bytes_billed was previously discarded immediately. This PR propagates it back to the mktplace caller without touching the result DataFrame pipeline or DuckDB path.

Depends on

Requires the companion PR in Talentify/mktplace (feat/bigquery-usage-metering) which generates the mktplace_query_id, calls query_with_id, and records metering. Deploy this PR first (mindsdb hot-reloads on file change).

Test plan

  • Registry unit: accumulate / pop / TTL eviction
  • Handler: mock QueryJob with total_bytes_billed=1234567, cache_hit=False; verify registry entry; mock cache_hit=True; verify total_bytes_billed=0
  • Live (mindsdb SDK): POST /api/sql/query with params={"mktplace_query_id":"<uuid>"}; GET /api/sql/query_stats/<uuid> → non-zero bytes; repeat (cache hit) → bytes=0
  • Non-BQ query: GET /api/sql/query_stats/<unused-uuid>{}

🤖 Generated with Claude Code

gabrielbressan-tfy and others added 4 commits June 7, 2026 17:46
Adds a thread-safe in-process registry that accumulates QueryJob stats
(total_bytes_billed, cache_hit, project_id) keyed by a caller-supplied
mktplace_query_id passed via ctx.params. The BigQuery handler populates
the registry after each native_query execution; a new GET endpoint
/api/sql/query_stats/<query_id> pops and returns the stats so mktplace
can record usage metering.

Registry auto-evicts TTL-expired entries (5 min) and caps at 10k to
prevent memory leaks from abandoned query ids.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- _evict now runs at most once per 60s instead of scanning on every
  accumulate() call
- Read generic correlation_id from ctx.params instead of vendor-specific
  mktplace_query_id

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@gabrielbressan-tfy gabrielbressan-tfy merged commit 2914f32 into main Jun 7, 2026
2 of 3 checks passed
@gabrielbressan-tfy gabrielbressan-tfy deleted the feat/bigquery-usage-metering branch June 7, 2026 23:07
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