Schedule icon
Query icon
If icon
SlackIncomingWebhook icon
Log icon
Return icon

BigQuery High-Cost Query and Slot Contention Sentinel

Automated BigQuery FinOps workflow to query JOBS_BY_PROJECT, detect runaway multi-terabyte query scans, and alert Slack.

Categories
BusinessData

In Google BigQuery on-demand analysis models, query costs scale directly with the volume of bytes scanned ($6.25 per terabyte). An unpartitioned analytical query, unintended cross-join, or unconstrained exploratory query in a multi-user organization can trigger full-table scans across multi-terabyte datasets, producing hundreds or thousands of dollars in surprise cloud compute charges within seconds.

Furthermore, in capacity-based reservations, rogue queries with excessive slot consumption exhaust assigned slots, causing high-priority production pipelines and executive dashboards to stall in queuing states.

This blueprint establishes an automated FinOps guardrail for BigQuery. Running hourly or on-demand, it inspects INFORMATION_SCHEMA.JOBS_BY_PROJECT within your target GCP region to detect completed queries that scanned more than your configured terabyte threshold or consumed excessive slot computation time. When detected, it dispatches an actionable Slack alert with user attribution, estimated dollar impact, and query snippets.

How it works

  1. Scheduled FinOps Audit: The hourly_finops_audit trigger (io.kestra.plugin.core.trigger.Schedule) executes at minute 15 of every hour, or runs on-demand via the Kestra UI.
  2. Job History Analysis: The audit_expensive_jobs task (io.kestra.plugin.gcp.bigquery.Query) queries regional INFORMATION_SCHEMA.JOBS_BY_PROJECT, filtering completed queries in the lookback window that breached terabyte or slot thresholds and calculating estimated dollar costs.
  3. Evaluation Gate: The evaluate_job_anomalies flowable task (io.kestra.plugin.core.flow.If) branches based on whether any runaway queries were returned.
  4. Slack Alert Dispatch: If violations exist, notify_finops_slack (io.kestra.plugin.slack.notifications.SlackIncomingWebhook) delivers an alert card detailing the job ID, author email, billed terabytes, dollar cost, and query snippet.
  5. Nominal Logging: When all jobs run within normal bounds, log_nominal_capacity records nominal status in execution logs.
  6. Audit Manifest: The export_job_manifest task records telemetry outputs for cloud cost accounting records.

What you get

  • Early warning on runaway BigQuery queries before monthly invoice surprises.
  • Visibility into user-attributed cloud data warehouse spending.
  • Automated dollar cost estimation directly in Slack notifications.
  • Zero human overhead for continuous BigQuery compute and scan governance.

Who it is for

  • FinOps Practitioners and Cloud Economists governing GCP multi-tenant data warehouse costs.
  • BigQuery Administrators managing slot reservations and on-demand project budgets.
  • Data Engineering Leads establishing best-practice query design guardrails.

Why orchestrate this with Kestra

Google Cloud native billing exports operate on multi-hour delays, making real-time spend intervention difficult. Kestra provides declarative, fast orchestration: it queries BigQuery's near-real-time INFORMATION_SCHEMA, evaluates threshold breaches hourly, notifies the team directly in Slack, and securely handles GCP service account credentials.

Inputs

Name Type Default Description
region STRING us Google Cloud region where BigQuery query execution history resides.
lookback_hours INT 1 Historical time window in hours to inspect completed BigQuery query jobs.
max_billed_tb_threshold FLOAT 2.0 Flag any single query that scanned and billed more than this volume in terabytes.
max_slot_minutes_threshold INT 30 Flag queries consuming more than this cumulative slot computation time in minutes.
cost_per_tb_usd FLOAT 6.25 On-demand query pricing per terabyte.
slack_channel STRING #finops-alerts Slack channel destination for BigQuery FinOps cost alerts.

Expected outputs

  • {{ outputs.audit_expensive_jobs.rows }}: Array of job records containing job_id, user_email, billed_terabytes, estimated_cost_usd, slot_minutes_consumed, and query_snippet.
  • {{ outputs.audit_expensive_jobs.size }}: Total number of runaway queries detected during the lookback window.
  • {{ outputs.evaluate_job_anomalies }}: Result of conditional branch evaluation.
  • {{ outputs.export_job_manifest.value }}: Structured JSON telemetry manifest recording execution timestamp and status.

Prerequisites

  • A Google Cloud Platform (GCP) project with BigQuery enabled.
  • A GCP Service Account JSON with roles/bigquery.resourceViewer or roles/bigquery.admin to query project-level INFORMATION_SCHEMA.JOBS_BY_PROJECT.
  • A Slack Incoming Webhook URL targeting your FinOps notification channel.

Secrets

  • GCP_PROJECT_ID: Your Google Cloud Project ID.
  • GCP_SERVICE_ACCOUNT_JSON: Service account key JSON authorized to view project BigQuery jobs.
  • SLACK_WEBHOOK_URL: Slack Incoming Webhook endpoint URL used for sending alert notifications.

Quick start

  1. Configure GCP_PROJECT_ID, GCP_SERVICE_ACCOUNT_JSON, and SLACK_WEBHOOK_URL in your Kestra namespace secrets.
  2. Import this flow YAML into your Kestra instance.
  3. Click Execute in the UI to run an initial audit of recent BigQuery job executions.
  4. Review execution outputs to inspect query scan volumes and slot consumption metrics.

Common pitfalls and troubleshooting

  • Regional Dataset Addressing: In BigQuery, INFORMATION_SCHEMA.JOBS_BY_PROJECT must be qualified with the region where query jobs executed (e.g., `my-project`.`region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT). Ensure inputs.region matches your query location.
  • Resource Viewer IAM Requirement: Querying project-wide jobs requires bigquery.jobs.listAll permission, typically provided by the roles/bigquery.resourceViewer IAM role.
  • Script Child Jobs: Multi-statement SQL scripts produce child jobs; the query filters statement_type != 'SCRIPT' to focus on individual root queries.

How to extend

  • Add automated user notification directly by routing Slack direct messages to the email address in user_email.
  • Integrate with Jira to create an automatic query optimization ticket with the full query text.
  • Enforce project-level maximum bytes billed quotas using ALTER PROJECT ... SET OPTIONS (maximum_bytes_billed = ...).

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.