|
1 | 1 | use super::execution_unit::QueryHash; |
| 2 | +use super::metrics::QueryMetrics; |
2 | 3 | use super::module_subscription_manager::{ |
3 | 4 | from_tx_offset, spawn_send_worker, BroadcastError, BroadcastQueue, Plan, SubscriptionGaugeStats, |
4 | 5 | SubscriptionManager, TransactionOffset, |
@@ -132,6 +133,24 @@ impl SubscriptionMetrics { |
132 | 133 | } |
133 | 134 | } |
134 | 135 |
|
| 136 | +/// Records subscription query metrics |
| 137 | +fn record_query_metrics(database_identity: &Identity, query_metrics: Vec<QueryMetrics>) { |
| 138 | + for qm in query_metrics { |
| 139 | + WORKER_METRICS |
| 140 | + .subscription_rows_examined |
| 141 | + .with_label_values(database_identity, &qm.scan_type, &qm.table_name, &qm.unindexed_columns) |
| 142 | + .observe(qm.rows_scanned as f64); |
| 143 | + WORKER_METRICS |
| 144 | + .subscription_query_execution_time_micros |
| 145 | + .with_label_values(database_identity, &qm.scan_type, &qm.table_name, &qm.unindexed_columns) |
| 146 | + .observe(qm.execution_time_micros as f64); |
| 147 | + WORKER_METRICS |
| 148 | + .subscription_queries_total |
| 149 | + .with_label_values(database_identity, &qm.scan_type, &qm.table_name, &qm.unindexed_columns) |
| 150 | + .inc(); |
| 151 | + } |
| 152 | +} |
| 153 | + |
135 | 154 | /// Inner result type of [`ModuleSubscriptions::commit_and_broadcast_event`]. |
136 | 155 | pub type CommitAndBroadcastEventResult = Result<CommitAndBroadcastEventSuccess, WriteConflict>; |
137 | 156 |
|
@@ -345,17 +364,22 @@ impl ModuleSubscriptions { |
345 | 364 | auth, |
346 | 365 | )?; |
347 | 366 |
|
| 367 | + let database_identity = self.relational_db.database_identity(); |
348 | 368 | let tx = DeltaTx::from(tx); |
349 | | - match sender.config.protocol { |
| 369 | + let (update, metrics, query_metrics) = match sender.config.protocol { |
350 | 370 | Protocol::Binary => { |
351 | | - let (update, metrics) = execute_plans(auth, queries, &tx, update_type)?; |
352 | | - Ok((FormatSwitch::Bsatn(update), metrics)) |
| 371 | + let (update, metrics, query_metrics) = execute_plans(auth, queries, &tx, update_type)?; |
| 372 | + (FormatSwitch::Bsatn(update), metrics, query_metrics) |
353 | 373 | } |
354 | 374 | Protocol::Text => { |
355 | | - let (update, metrics) = execute_plans(auth, queries, &tx, update_type)?; |
356 | | - Ok((FormatSwitch::Json(update), metrics)) |
| 375 | + let (update, metrics, query_metrics) = execute_plans(auth, queries, &tx, update_type)?; |
| 376 | + (FormatSwitch::Json(update), metrics, query_metrics) |
357 | 377 | } |
358 | | - } |
| 378 | + }; |
| 379 | + |
| 380 | + record_query_metrics(&database_identity, query_metrics); |
| 381 | + |
| 382 | + Ok((update, metrics)) |
359 | 383 | } |
360 | 384 |
|
361 | 385 | /// Add a subscription to a single query. |
@@ -896,13 +920,17 @@ impl ModuleSubscriptions { |
896 | 920 | drop(compile_timer); |
897 | 921 |
|
898 | 922 | let tx = DeltaTx::from(&*tx); |
899 | | - let (database_update, metrics) = match sender.config.protocol { |
900 | | - Protocol::Binary => execute_plans(&auth, &queries, &tx, TableUpdateType::Subscribe) |
901 | | - .map(|(table_update, metrics)| (FormatSwitch::Bsatn(table_update), metrics))?, |
902 | | - Protocol::Text => execute_plans(&auth, &queries, &tx, TableUpdateType::Subscribe) |
903 | | - .map(|(table_update, metrics)| (FormatSwitch::Json(table_update), metrics))?, |
| 923 | + let (database_update, metrics, query_metrics) = match sender.config.protocol { |
| 924 | + Protocol::Binary => execute_plans(&auth, &queries, &tx, TableUpdateType::Subscribe).map( |
| 925 | + |(table_update, metrics, query_metrics)| (FormatSwitch::Bsatn(table_update), metrics, query_metrics), |
| 926 | + )?, |
| 927 | + Protocol::Text => execute_plans(&auth, &queries, &tx, TableUpdateType::Subscribe).map( |
| 928 | + |(table_update, metrics, query_metrics)| (FormatSwitch::Json(table_update), metrics, query_metrics), |
| 929 | + )?, |
904 | 930 | }; |
905 | 931 |
|
| 932 | + record_query_metrics(&database_identity, query_metrics); |
| 933 | + |
906 | 934 | // It acquires the subscription lock after `eval`, allowing `add_subscription` to run concurrently. |
907 | 935 | // This also makes it possible for `broadcast_event` to get scheduled before the subsequent part here |
908 | 936 | // but that should not pose an issue. |
|
0 commit comments