Skip to content

Commit

Permalink
Merge branch 'main' into fix/enum-crdb-error
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Fernández authored Nov 15, 2023
2 parents 75b855f + 4d2e008 commit f4d396b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
5 changes: 3 additions & 2 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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CONFIG_PATH = ./query-engine/connector-test-kit-rs/test-configs
CONFIG_FILE = .test_config
SCHEMA_EXAMPLES_PATH = ./query-engine/example_schemas
DEV_SCHEMA_FILE = dev_datamodel.prisma
DRIVER_ADAPTERS_BRANCH ?= main
DRIVER_ADAPTERS_BRANCH ?= revert-esm

LIBRARY_EXT := $(shell \
case "$$(uname -s)" in \
Expand Down
1 change: 1 addition & 0 deletions query-engine/black-box-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ user-facing-errors.workspace = true
insta = "1.7.1"
enumflags2 = "0.7"
query-engine-metrics = {path = "../metrics"}
regex = "1.9.3"
25 changes: 24 additions & 1 deletion query-engine/black-box-tests/tests/metrics/smoke_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use query_engine_tests::*;
/// Asserts common basics for composite type writes.
#[test_suite(schema(schema))]
mod smoke_tests {
use regex::Regex;
fn schema() -> String {
let schema = indoc! {
r#"model Person {
Expand All @@ -14,6 +15,24 @@ mod smoke_tests {
schema.to_owned()
}

fn assert_value_in_range(metrics: &str, metric: &str, low: f64, high: f64) {
let regex = Regex::new(format!(r"{metric}\s+([+-]?\d+(\.\d+)?)").as_str()).unwrap();
match regex.captures(&metrics) {
Some(capture) => {
let value = capture.get(1).unwrap().as_str().parse::<f64>().unwrap();
assert!(
value >= low && value <= high,
"expected {} value of {} to be between {} and {}",
metric,
value,
low,
high
);
}
None => panic!("Metric {} not found in metrics text", metric),
}
}

#[connector_test]
#[rustfmt::skip]
async fn expected_metrics_rendered(r: Runner) -> TestResult<()> {
Expand Down Expand Up @@ -62,6 +81,8 @@ mod smoke_tests {
// counters
assert_eq!(metrics.matches("# HELP prisma_client_queries_total The total number of Prisma Client queries executed").count(), 1);
assert_eq!(metrics.matches("# TYPE prisma_client_queries_total counter").count(), 1);
assert_eq!(metrics.matches("prisma_client_queries_total 1").count(), 1);


assert_eq!(metrics.matches("# HELP prisma_datasource_queries_total The total number of datasource queries executed").count(), 1);
assert_eq!(metrics.matches("# TYPE prisma_datasource_queries_total counter").count(), 1);
Expand All @@ -81,13 +102,15 @@ mod smoke_tests {

assert_eq!(metrics.matches("# HELP prisma_pool_connections_busy The number of pool connections currently executing datasource queries").count(), 1);
assert_eq!(metrics.matches("# TYPE prisma_pool_connections_busy gauge").count(), 1);
assert_value_in_range(&metrics, "prisma_pool_connections_busy", 0f64, 1f64);

assert_eq!(metrics.matches("# HELP prisma_pool_connections_idle The number of pool connections that are not busy running a query").count(), 1);
assert_eq!(metrics.matches("# TYPE prisma_pool_connections_idle gauge").count(), 1);

assert_eq!(metrics.matches("# HELP prisma_pool_connections_open The number of pool connections currently open").count(), 1);
assert_eq!(metrics.matches("# TYPE prisma_pool_connections_open gauge").count(), 1);

assert_value_in_range(&metrics, "prisma_pool_connections_open", 0f64, 1f64);

// histograms
assert_eq!(metrics.matches("# HELP prisma_client_queries_duration_histogram_ms The distribution of the time Prisma Client queries took to run end to end").count(), 1);
assert_eq!(metrics.matches("# TYPE prisma_client_queries_duration_histogram_ms histogram").count(), 1);
Expand Down
13 changes: 8 additions & 5 deletions query-engine/driver-adapters/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,20 @@ fn js_value_to_quaint(
column_type: ColumnType,
column_name: &str,
) -> quaint::Result<QuaintValue<'static>> {
let parse_number_as_i64 = |n: &serde_json::Number| {
n.as_i64().ok_or(conversion_error!(
"number must be an integer in column '{column_name}', got '{n}'"
))
};

// Note for the future: it may be worth revisiting how much bloat so many panics with different static
// strings add to the compiled artefact, and in case we should come up with a restricted set of panic
// messages, or even find a way of removing them altogether.
match column_type {
ColumnType::Int32 => match json_value {
serde_json::Value::Number(n) => {
// n.as_i32() is not implemented, so we need to downcast from i64 instead
n.as_i64()
.ok_or(conversion_error!("number must be an integer in column '{column_name}'"))
parse_number_as_i64(&n)
.and_then(|n| -> quaint::Result<i32> {
n.try_into()
.map_err(|e| conversion_error!("cannot convert {n} to i32 in column '{column_name}': {e}"))
Expand All @@ -273,9 +278,7 @@ fn js_value_to_quaint(
)),
},
ColumnType::Int64 => match json_value {
serde_json::Value::Number(n) => n.as_i64().map(QuaintValue::int64).ok_or(conversion_error!(
"number must be an i64 in column '{column_name}', got {n}"
)),
serde_json::Value::Number(n) => parse_number_as_i64(&n).map(QuaintValue::int64),
serde_json::Value::String(s) => s.parse::<i64>().map(QuaintValue::int64).map_err(|e| {
conversion_error!("string-encoded number must be an i64 in column '{column_name}', got {s}: {e}")
}),
Expand Down

0 comments on commit f4d396b

Please sign in to comment.