Skip to content

Commit

Permalink
Merge pull request #17 from hasura:stream-response
Browse files Browse the repository at this point in the history
add trace spans, don't parse db response before forwarding it
  • Loading branch information
BenoitRanque authored Apr 23, 2024
2 parents c89d941 + 81509cf commit 7bafb9f
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 120 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.2.6]

- Add additional trace spans for SQL generation and query execution
- Do not parse db response as JSON, instead send it back directly

## [0.2.5]

- Implement validate cli command
Expand Down
111 changes: 95 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ members = [
]
resolver = "2"

package.version = "0.2.5"
package.version = "0.2.6"
package.edition = "2021"
6 changes: 4 additions & 2 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ version.workspace = true
edition.workspace = true

[dependencies]
bytes = "1.6.0"
peg = "0.8.2"
reqwest = { version = "0.12.3", features = [
"json",
"rustls-tls",
], default-features = false }
schemars = "0.8.16"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
peg = "0.8.2"
schemars = "0.8.16"
tracing = "0.1.40"
38 changes: 36 additions & 2 deletions crates/common/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::error::Error;

use bytes::Bytes;
use serde::{de::DeserializeOwned, Deserialize};
use tracing::Instrument;

use crate::config::ConnectionConfig;

Expand All @@ -12,7 +14,35 @@ pub fn get_http_client(
Ok(client)
}

pub async fn execute_query<T: DeserializeOwned>(
pub async fn execute_query(
client: &reqwest::Client,
connection_config: &ConnectionConfig,
statement: &str,
parameters: &Vec<(String, String)>,
) -> Result<Bytes, Box<dyn Error>> {
let response = client
.post(&connection_config.url)
.header("X-ClickHouse-User", &connection_config.username)
.header("X-ClickHouse-Key", &connection_config.password)
.query(parameters)
.body(statement.to_owned())
.send()
.instrument(tracing::info_span!("Execute HTTP request"))
.await?;

if response.error_for_status_ref().is_err() {
return Err(response.text().await?.into());
}

let response = response
.bytes()
.instrument(tracing::info_span!("Read HTTP response"))
.await?;

Ok(response)
}

pub async fn execute_json_query<T: DeserializeOwned>(
client: &reqwest::Client,
connection_config: &ConnectionConfig,
statement: &str,
Expand All @@ -25,13 +55,17 @@ pub async fn execute_query<T: DeserializeOwned>(
.query(parameters)
.body(statement.to_owned())
.send()
.instrument(tracing::info_span!("Execute HTTP request"))
.await?;

if response.error_for_status_ref().is_err() {
return Err(response.text().await?.into());
}

let payload: ClickHouseResponse<T> = response.json().await?;
let payload: ClickHouseResponse<T> = response
.json()
.instrument(tracing::info_span!("Parse HTTP response"))
.await?;

Ok(payload.data)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/ndc-clickhouse-cli/src/database_introspection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::error::Error;
use serde::Deserialize;

use common::{
client::{execute_query, get_http_client},
client::{execute_json_query, get_http_client},
config::ConnectionConfig,
};

Expand Down Expand Up @@ -44,5 +44,5 @@ pub async fn introspect_database(
) -> Result<Vec<TableInfo>, Box<dyn Error>> {
let introspection_sql = include_str!("./database_introspection.sql");
let client = get_http_client(connection_config)?;
execute_query::<TableInfo>(&client, connection_config, introspection_sql, &vec![]).await
execute_json_query::<TableInfo>(&client, connection_config, introspection_sql, &vec![]).await
}
4 changes: 3 additions & 1 deletion crates/ndc-clickhouse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ edition.workspace = true

[dependencies]
async-trait = "0.1.78"
bytes = "1.6.0"
common = { path = "../common" }
indexmap = "2.1.0"
ndc-sdk = { git = "https://github.com/hasura/ndc-sdk-rs", rev = "7b56fac", package = "ndc-sdk" }
ndc-sdk = { git = "https://github.com/hasura/ndc-sdk-rs", rev = "972dba6", package = "ndc-sdk" }
prometheus = "0.13.3"
reqwest = { version = "0.12.3", features = [
"json",
Expand All @@ -18,3 +19,4 @@ serde_json = "1.0.114"
sqlformat = "0.2.3"
strum = { version = "0.26.2", features = ["derive"] }
tokio = "1.36.0"
tracing = "0.1.40"
Loading

0 comments on commit 7bafb9f

Please sign in to comment.