(
client: &mut AuthedClient,
query: &PrometheusSqlQuery<'_>,
metrics_registry: &MetricsRegistry,
metrics_by_name: &mut BTreeMap<String, GenericGaugeVec<AtomicF64>>,
cluster: Option
| 114 | static PER_REPLICA_LABELS: &[&str] = &["replica_full_name", "instance_id", "replica_id"]; |
| 115 | |
| 116 | async fn execute_promsql_query( |
| 117 | client: &mut AuthedClient, |
| 118 | query: &PrometheusSqlQuery<'_>, |
| 119 | metrics_registry: &MetricsRegistry, |
| 120 | metrics_by_name: &mut BTreeMap<String, GenericGaugeVec<AtomicF64>>, |
| 121 | cluster: Option<(&Cluster, &ClusterReplica)>, |
| 122 | ) { |
| 123 | assert_eq!(query.per_replica, cluster.is_some()); |
| 124 | |
| 125 | let mut res = SqlResponse { |
| 126 | results: Vec::new(), |
| 127 | }; |
| 128 | |
| 129 | execute_request(client, query.to_sql_request(cluster), &mut res) |
| 130 | .await |
| 131 | .expect("valid SQL query"); |
| 132 | |
| 133 | let result = match res.results.as_slice() { |
| 134 | // Each query issued is preceded by several SET commands |
| 135 | // to make sure it is routed to the right cluster replica. |
| 136 | [ |
| 137 | SqlResult::Ok { .. }, |
| 138 | SqlResult::Ok { .. }, |
| 139 | SqlResult::Ok { .. }, |
| 140 | result, |
| 141 | ] => result, |
| 142 | // Transient errors are fine, like if the cluster or replica |
| 143 | // was dropped before the promsql query was executed. We |
| 144 | // should not see errors in the steady state. |
| 145 | _ => { |
| 146 | info!( |
| 147 | "error executing prometheus query {}: {:?}", |
| 148 | query.metric_name, res |
| 149 | ); |
| 150 | return; |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | let SqlResult::Rows { desc, rows, .. } = result else { |
| 155 | info!( |
| 156 | "did not receive rows for SQL query for prometheus metric {}: {:?}, {:?}", |
| 157 | query.metric_name, result, cluster |
| 158 | ); |
| 159 | return; |
| 160 | }; |
| 161 | |
| 162 | let gauge_vec = metrics_by_name |
| 163 | .entry(query.metric_name.to_string()) |
| 164 | .or_insert_with(|| { |
| 165 | let mut label_names: Vec<String> = desc |
| 166 | .columns |
| 167 | .iter() |
| 168 | .filter(|col| col.name != query.value_column_name) |
| 169 | .map(|col| col.name.clone()) |
| 170 | .collect(); |
| 171 | |
| 172 | if query.per_replica { |
| 173 | label_names.extend(PER_REPLICA_LABELS.iter().map(|label| label.to_string())); |
no test coverage detected