Skip to content

Commit

Permalink
Propagate errors from Clickhouse all the time
Browse files Browse the repository at this point in the history
  • Loading branch information
cpg314 committed Jun 14, 2024
1 parent 4a95cd0 commit 19f6454
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ch-grafana-cache"
version = "0.1.3"
version = "0.1.4"
edition = "2021"

[dependencies]
Expand Down
37 changes: 22 additions & 15 deletions src/clickhouse.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::sync::Arc;

use reqwest_middleware::RequestBuilder;
use tracing::*;

#[derive(clap::Parser)]
Expand Down Expand Up @@ -53,17 +54,28 @@ impl ChClient {
cache: Default::default(),
}
}
async fn send_query(&self, builder: RequestBuilder) -> anyhow::Result<reqwest::Response> {
debug!("Sending query");
let resp = builder.send().await?;
if !resp.status().is_success() {
anyhow::bail!(
"{}: {}",
resp.status(),
resp.text().await.unwrap_or_default()
);
}
Ok(resp)
}
#[instrument(skip(self))]
pub async fn query_native(&self, query: String) -> anyhow::Result<u64> {
debug!("Sending query");
Ok(self
.clone()
.builder
.query(&[("default_format", "Native")])
.body(query)
.send()
.send_query(
self.clone()
.builder
.query(&[("default_format", "Native")])
.body(query),
)
.await?
.error_for_status()?
.content_length()
.unwrap_or_default())
}
Expand All @@ -76,14 +88,9 @@ impl ChClient {
}
}
debug!("Sending query");
let resp = self.clone().builder.body(query.clone()).send().await?;
if !resp.status().is_success() {
anyhow::bail!(
"{}: {}",
resp.status(),
resp.text().await.unwrap_or_default()
);
}
let resp = self
.send_query(self.clone().builder.body(query.clone()))
.await?;
let hit = resp.headers().get("x-cache").map_or(false, |c| c == "HIT");
trace!(hit, "Received response");
let rows: Vec<ResultRow> = resp
Expand Down

0 comments on commit 19f6454

Please sign in to comment.