feat: capture BigQuery query stats for mktplace metering#65
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
query_stats_registry.py: thread-safe in-process registry accumulatingQueryJobstats (total_bytes_billed,cache_hit,project_id) keyed by amktplace_query_idcorrelation id passed viactx.paramsBigQueryHandler.native_query()to capture stats into the registry after each query execution (via new_record_query_statshelper)GET /api/sql/query_stats/<query_id>endpoint that pops and returns the stats JSON; returns{}for unknown idsWhy
mktplace needs to meter actual BigQuery bytes scanned for public datasources (Talentify pays GCP on-demand). The
QueryJob.total_bytes_billedwas 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 themktplace_query_id, callsquery_with_id, and records metering. Deploy this PR first (mindsdb hot-reloads on file change).Test plan
accumulate/pop/ TTL evictionQueryJobwithtotal_bytes_billed=1234567,cache_hit=False; verify registry entry; mockcache_hit=True; verifytotal_bytes_billed=0/api/sql/querywithparams={"mktplace_query_id":"<uuid>"}; GET/api/sql/query_stats/<uuid>→ non-zero bytes; repeat (cache hit) →bytes=0/api/sql/query_stats/<unused-uuid>→{}🤖 Generated with Claude Code