Monitor continuous queries
You can monitor BigQuery continuous queries by using the following BigQuery tools:
Due to the long running nature of a BigQuery continuous query, metrics that are usually generated upon the completion of a SQL query might be absent or inaccurate.
Use INFORMATION_SCHEMA
views
You can use a number of the INFORMATION_SCHEMA
views to monitor continuous
queries and continuous query reservations.
View job details
You can use the
ASSIGNMENTS
and
JOBS
views to get
continuous query job metadata.
Return the metadata for all running continuous queries:
In the Google Cloud console, go to the BigQuery page.
In the query editor, run the following query:
SELECT jobs.job_id, jobs.user_email, jobs.query, jobs.state, assignment.reservation_name FROM `ADMIN_PROJECT_ID.region-LOCATION.INFORMATION_SCHEMA.ASSIGNMENTS` AS assignment INNER JOIN `PROJECT_ID.region-LOCATION.INFORMATION_SCHEMA.JOBS` AS jobs ON (UPPER(CONCAT('ADMIN_PROJECT_ID:LOCATION.', assignment.reservation_name)) = UPPER(jobs.reservation_id)) WHERE assignment.assignee_id = 'PROJECT_ID' AND assignment.job_type = 'CONTINUOUS' AND jobs.state = 'RUNNING' GROUP BY 1, 2, 3, 4, 5;
Replace the following:
ADMIN_PROJECT_ID
: the ID of the administration project that owns the reservation.LOCATION
: the location of the reservation.PROJECT_ID
: the ID of the project that is assigned to the reservation. Only information about continuous queries running in this project is returned.
View reservation assignment details
You can use the
ASSIGNMENTS
and
RESERVATIONS
views to get
continuous query reservation assignment details.
Return reservation assignment details for continuous queries:
In the Google Cloud console, go to the BigQuery page.
In the query editor, run the following query:
SELECT reservation.reservation_name, reservation.slot_capacity FROM `ADMIN_PROJECT_ID.region-LOCATION.INFORMATION_SCHEMA.ASSIGNMENTS` AS assignment INNER JOIN `ADMIN_PROJECT_ID.region-LOCATION.INFORMATION_SCHEMA.RESERVATIONS` AS reservation ON (assignment.reservation_name = reservation.reservation_name) WHERE assignment.assignee_id = 'PROJECT_ID' AND job_type = 'CONTINUOUS';
Replace the following:
ADMIN_PROJECT_ID
: the ID of the administration project that owns the reservation.LOCATION
: the location of the reservation.PROJECT_ID
: the ID of the project that is assigned to the reservation. Only information about continuous queries running in this project is returned.
View slot consumption information
You can use the
ASSIGNMENTS
,
RESERVATIONS
, and
JOBS_TIMELINE
views to get
continuous query slot consumption information.
Return slot consumption information for continuous queries:
In the Google Cloud console, go to the BigQuery page.
In the query editor, run the following query:
SELECT jobs.period_start, reservation.reservation_name, reservation.slot_capacity, SUM(jobs.period_slot_ms) / 1000 AS consumed_total_slots FROM `ADMIN_PROJECT_ID.region-LOCATION.INFORMATION_SCHEMA.ASSIGNMENTS` AS assignment INNER JOIN `ADMIN_PROJECT_ID.region-LOCATION.INFORMATION_SCHEMA.RESERVATIONS` AS reservation ON (assignment.reservation_name = reservation.reservation_name) INNER JOIN `PROJECT_ID.region-LOCATION.INFORMATION_SCHEMA.JOBS_TIMELINE` AS jobs ON ( UPPER(CONCAT('ADMIN_PROJECT_ID:LOCATION.', assignment.reservation_name)) = UPPER(jobs.reservation_id)) WHERE assignment.assignee_id = 'PROJECT_ID' AND assignment.job_type = 'CONTINUOUS' AND jobs.period_start BETWEEN TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 DAY) AND CURRENT_TIMESTAMP() GROUP BY 1, 2, 3 ORDER BY jobs.period_start DESC;
Replace the following:
ADMIN_PROJECT_ID
: the ID of the administration project that owns the reservation.LOCATION
: the location of the reservation.PROJECT_ID
: the ID of the project that is assigned to the reservation. Only information about continuous queries running in this project is returned.
You can also monitor continuous query reservations using other tools such as Metrics Explorer and administrative resource charts. For more information, see Monitor BigQuery reservations.
Use the query execution graph
You can use the query execution graph to get performance insights and general statistics for a continuous query. For more information, see View query performance insights.
View job history
You can view continuous query job details in your personal job history or the project's job history. For more information, see View job details.
Be aware that the historical list of jobs is sorted by the job start time, so continuous queries that have been running for a while might not be close to the start of the list.
Use the administrative jobs explorer
In the administrative jobs explorer, filter your jobs to show continuous queries by setting the Job category filter to Continuous query.
Use Cloud Monitoring
You can view metrics specific to BigQuery continuous queries by using Cloud Monitoring. For more information, see Create dashboards, charts, and alerts and read about the metrics available for visualization.
Alert on failed queries
Instead of routinely checking whether your continuous queries have failed, it can be helpful to create an alert to notify you of failure. One way to do this is to create a custom Cloud Logging log-based metric with a filter for your jobs, and a Cloud Monitoring alerting policy based on that metric:
- When you create a continuous query, use a
custom job ID prefix.
Multiple continuous queries can share the same prefix.
For example, you might use the prefix
prod-
to indicate a production query. In the Google Cloud console, go to the Log-based Metrics page.
Click Create metric. The Create logs metric panel appears.
For Metric type, select Counter.
In the Details section, give your metric a name. For example,
CUSTOM_JOB_ID_PREFIX-metric
.In the Filter selection section, enter the following into the Build filter editor:
resource.type = "bigquery_project" protoPayload.resourceName : "projects/PROJECT_ID/jobs/CUSTOM_JOB_ID_PREFIX" severity = ERROR
Replace the following:
PROJECT_ID
: the name of your project.CUSTOM_JOB_ID_PREFIX
: the name of the custom job ID prefix that you set for your continuous query.
Click Create metric.
In the navigation menu, click Log-based metrics. The metric you just created appears in the list of user-defined metrics.
In your metric's row, click
More actions, and then click Create alert from metric.Click Next. You don't need to change the default settings on the Policy configuration mode page.
Click Next. You don't need to change the default settings on the Configure alert trigger page.
Select your notification channels and enter a name for the alert policy.
Click Create policy.
You can test your alert by running a continuous query with the custom job ID prefix that you selected and then cancelling it. It might take a few minutes for the alert to reach your notification channel.
Retry failed queries
Retrying a failed continuous query might help avoid situations where a continuous pipeline is down for an extended period of time or requires human intervention to restart. Important things to consider when you retry a failed continuous query include the following:
- Whether reprocessing some amount of data processed by the previous query before it failed is tolerable.
- How to handle limiting retries or using exponential backoff.
One possible approach to automating query retry is the following:
Create a Cloud Logging sink based on an inclusion filter matching the following criteria to route logs to a Pub/Sub topic:
resource.type = "bigquery_project" protoPayload.resourceName : "projects/PROJECT_ID/jobs/CUSTOM_JOB_ID_PREFIX" severity = ERROR
Replace the following:
PROJECT_ID
: the name of your project.CUSTOM_JOB_ID_PREFIX
: the name of the custom job ID prefix that you set for your continuous query.
Create a Cloud Run function that is triggered in response to the Pub/Sub receiving logs matching your filter.
The Cloud Run function could accept the data payload from the Pub/Sub message and attempt to start a new continuous query using the same SQL syntax as the failed query, but at beginning just after the previous job stopped.
For example, you can use a function similar to the following:
Python
Before trying this sample, follow the Python setup instructions in the
BigQuery quickstart using
client libraries.
For more information, see the
BigQuery Python API
reference documentation.
To authenticate to BigQuery, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
What's next
- Learn how to create and run
continuous queries.