Skip to content

Commit

Permalink
feat: promote tracing to GA (#5087)
Browse files Browse the repository at this point in the history
  • Loading branch information
aqrln authored Dec 16, 2024
1 parent a5ab6a9 commit 40232c9
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 27 deletions.
7 changes: 7 additions & 0 deletions libs/query-engine-common/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub struct EngineBuilderNative {
pub struct EngineBuilder {
pub schema: Arc<psl::ValidatedSchema>,
pub engine_protocol: EngineProtocol,
pub enable_tracing: bool,

#[cfg(not(target_arch = "wasm32"))]
pub native: EngineBuilderNative,
Expand All @@ -68,6 +69,7 @@ pub struct ConnectedEngine {
pub query_schema: Arc<QuerySchema>,
pub executor: crate::Executor,
pub engine_protocol: EngineProtocol,
pub tracing_enabled: bool,

#[cfg(not(target_arch = "wasm32"))]
pub native: ConnectedEngineNative,
Expand All @@ -87,6 +89,10 @@ impl ConnectedEngine {
pub fn engine_protocol(&self) -> EngineProtocol {
self.engine_protocol
}

pub fn tracing_enabled(&self) -> bool {
self.tracing_enabled
}
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -114,6 +120,7 @@ pub struct ConstructorOptions {
pub log_level: String,
#[serde(default)]
pub log_queries: bool,
pub enable_tracing: bool,

#[cfg(not(target_arch = "wasm32"))]
#[serde(flatten)]
Expand Down
2 changes: 1 addition & 1 deletion psl/psl-core/src/common/preview_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ impl<'a> FeatureMapWithProvider<'a> {
| PrismaSchemaFolder
| RelationJoins
| StrictUndefinedChecks
| Tracing
| Views
}),
native: HashMap::from([
Expand Down Expand Up @@ -218,6 +217,7 @@ impl<'a> FeatureMapWithProvider<'a> {
| ReferentialActions
| ReferentialIntegrity
| SelectRelationCount
| Tracing
| TransactionApi
| UncheckedScalarInputs
}),
Expand Down
2 changes: 1 addition & 1 deletion psl/psl/tests/config/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn nice_error_for_unknown_generator_preview_feature() {
.unwrap_err();

let expectation = expect![[r#"
[1;91merror[0m: [1mThe preview feature "foo" is not known. Expected one of: deno, driverAdapters, metrics, multiSchema, nativeDistinct, postgresqlExtensions, tracing, views, relationJoins, prismaSchemaFolder, omitApi, strictUndefinedChecks[0m
[1;91merror[0m: [1mThe preview feature "foo" is not known. Expected one of: deno, driverAdapters, metrics, multiSchema, nativeDistinct, postgresqlExtensions, views, relationJoins, prismaSchemaFolder, omitApi, strictUndefinedChecks[0m
--> schema.prisma:3
 | 
 2 |  provider = "prisma-client-js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ model Blog {
title String
@@fulltext([content, title])
}
// [1;91merror[0m: [1mThe preview feature "fullTextSearchPostgres" is not known. Expected one of: deno, driverAdapters, metrics, multiSchema, nativeDistinct, postgresqlExtensions, tracing, views, relationJoins, prismaSchemaFolder, omitApi, strictUndefinedChecks[0m
// [1;91merror[0m: [1mThe preview feature "fullTextSearchPostgres" is not known. Expected one of: deno, driverAdapters, metrics, multiSchema, nativeDistinct, postgresqlExtensions, views, relationJoins, prismaSchemaFolder, omitApi, strictUndefinedChecks[0m
// --> schema.prisma:3
//  | 
//  2 |  provider = "prisma-client-js"
Expand Down
6 changes: 5 additions & 1 deletion quaint/src/pooled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,16 @@ impl Quaint {
}
#[cfg(feature = "mysql")]
s if s.starts_with("mysql") => {
let url = crate::connector::MysqlUrl::new(url::Url::parse(s)?)?;
let mut url = crate::connector::MysqlUrl::new(url::Url::parse(s)?)?;
let connection_limit = url.connection_limit();
let pool_timeout = url.pool_timeout();
let max_connection_lifetime = url.max_connection_lifetime();
let max_idle_connection_lifetime = url.max_idle_connection_lifetime();

if is_tracing_enabled {
url.query_params.statement_cache_size = 0;
}

let manager = QuaintManager::Mysql { url };
let mut builder = Builder::new(s, manager)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ impl Runner {
datasource,
},
schema.configuration.preview_features(),
true,
)
.await?;
let connector = query_executor.primary_connector();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub trait FromSource {
source: &psl::Datasource,
url: &str,
features: psl::PreviewFeatures,
tracing_enabled: bool,
) -> connector_interface::Result<Self>
where
Self: Connector + Sized;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl FromSource for Mssql {
_source: &Datasource,
url: &str,
features: PreviewFeatures,
_tracing_enabled: bool,
) -> connector_interface::Result<Self> {
let database_str = url;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl FromSource for Mysql {
_: &psl::Datasource,
url: &str,
features: psl::PreviewFeatures,
_tracing_enabled: bool,
) -> connector_interface::Result<Mysql> {
let connection_info = get_connection_info(url)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use connector_interface::{
Connection, Connector,
};
use psl::builtin_connectors::COCKROACH;
use psl::PreviewFeature;
use quaint::{connector::PostgresFlavour, pooled::Quaint, prelude::ConnectionInfo};
use std::time::Duration;

Expand All @@ -30,6 +29,7 @@ impl FromSource for PostgreSql {
source: &psl::Datasource,
url: &str,
features: psl::PreviewFeatures,
tracing_enabled: bool,
) -> connector_interface::Result<Self> {
let database_str = url;

Expand All @@ -41,7 +41,7 @@ impl FromSource for PostgreSql {
})
})?;

let mut builder = Quaint::builder_with_tracing(url, features.contains(PreviewFeature::Tracing))
let mut builder = Quaint::builder_with_tracing(url, tracing_enabled)
.map_err(SqlError::from)
.map_err(|sql_error| sql_error.into_connector_error(&err_conn_info))?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl FromSource for Sqlite {
_source: &psl::Datasource,
url: &str,
features: psl::PreviewFeatures,
_tracing_enabled: bool,
) -> connector_interface::Result<Sqlite> {
let database_str = url;

Expand Down
1 change: 1 addition & 0 deletions query-engine/driver-adapters/executor/src/qe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function queryEngineOptions(datamodel: string) {
logQueries: true,
env: process.env,
ignoreEnvVarErrors: false,
enableTracing: true,
};
}

Expand Down
8 changes: 6 additions & 2 deletions query-engine/query-engine-c-abi/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub struct ConstructorOptions {
base_path: *const c_char,
log_level: *const c_char,
log_queries: bool,
enable_tracing: bool,
datasource_overrides: *const c_char,
env: *const c_char,
ignore_env_var_errors: bool,
Expand Down Expand Up @@ -175,6 +176,7 @@ impl QueryEngine {
let builder = EngineBuilder {
schema: Arc::new(schema),
engine_protocol,
enable_tracing: constructor_options.enable_tracing,
native: EngineBuilderNative { config_dir, env },
};

Expand All @@ -184,7 +186,7 @@ impl QueryEngine {
constructor_options.log_queries,
log_level,
Box::new(log_callback),
false,
constructor_options.enable_tracing,
);

Ok(Self {
Expand Down Expand Up @@ -238,7 +240,7 @@ impl QueryEngine {
datasource: data_source,
};

let executor = load_executor(connector_kind, preview_features).await?;
let executor = load_executor(connector_kind, preview_features, builder.enable_tracing).await?;
let connector = executor.primary_connector();

let conn_span = tracing::info_span!(
Expand Down Expand Up @@ -267,6 +269,7 @@ impl QueryEngine {
query_schema: Arc::new(query_schema.unwrap()),
executor: executor?,
engine_protocol: builder.engine_protocol,
tracing_enabled: builder.enable_tracing,
native: ConnectedEngineNative {
config_dir: builder.native.config_dir.clone(),
env: builder.native.env.clone(),
Expand Down Expand Up @@ -346,6 +349,7 @@ impl QueryEngine {
let builder = EngineBuilder {
schema: engine.schema.clone(),
engine_protocol: engine.engine_protocol(),
enable_tracing: engine.tracing_enabled(),
native: EngineBuilderNative {
config_dir: engine.native.config_dir.clone(),
env: engine.native.env.clone(),
Expand Down
7 changes: 5 additions & 2 deletions query-engine/query-engine-node-api/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl QueryEngine {
datamodel,
log_level,
log_queries,
enable_tracing,
native,
} = napi_env.from_js_value(options).expect(
r###"
Expand Down Expand Up @@ -140,12 +141,12 @@ impl QueryEngine {
.map_err(|errors| ApiError::conversion(errors, schema.db.source_assert_single()))?;

let enable_metrics = config.preview_features().contains(PreviewFeature::Metrics);
let enable_tracing = config.preview_features().contains(PreviewFeature::Tracing);
let engine_protocol = engine_protocol.unwrap_or(EngineProtocol::Json);

let builder = EngineBuilder {
schema: Arc::new(schema),
engine_protocol,
enable_tracing,
native: EngineBuilderNative { config_dir, env },
};

Expand Down Expand Up @@ -212,7 +213,7 @@ impl QueryEngine {
_phantom: PhantomData,
},
};
let executor = load_executor(connector_kind, preview_features).await?;
let executor = load_executor(connector_kind, preview_features, builder.enable_tracing).await?;
let connector = executor.primary_connector();

let conn_span = tracing::info_span!(
Expand Down Expand Up @@ -247,6 +248,7 @@ impl QueryEngine {
query_schema: Arc::new(query_schema),
executor,
engine_protocol: builder.engine_protocol,
tracing_enabled: builder.enable_tracing,
native: ConnectedEngineNative {
config_dir: builder.native.config_dir.clone(),
env: builder.native.env.clone(),
Expand Down Expand Up @@ -292,6 +294,7 @@ impl QueryEngine {
let builder = EngineBuilder {
schema: engine.schema.clone(),
engine_protocol: engine.engine_protocol(),
enable_tracing: engine.tracing_enabled(),
native: EngineBuilderNative {
config_dir: engine.native.config_dir.clone(),
env: engine.native.env.clone(),
Expand Down
12 changes: 6 additions & 6 deletions query-engine/query-engine-wasm/src/wasm/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};
use driver_adapters::JsObject;
use js_sys::Function as JsFunction;
use psl::{ConnectorRegistry, PreviewFeature};
use psl::ConnectorRegistry;
use quaint::connector::ExternalConnector;
use query_core::{
protocol::EngineProtocol,
Expand Down Expand Up @@ -60,23 +60,20 @@ impl QueryEngine {
datamodel,
log_level,
log_queries,
enable_tracing,
} = options;

// Note: if we used `psl::validate`, we'd add ~1MB to the Wasm artifact (before gzip).
let schema = psl::parse_without_validation(datamodel.into(), CONNECTOR_REGISTRY);

let js_queryable = Arc::new(driver_adapters::from_js(adapter));

let enable_tracing = schema
.configuration
.preview_features()
.contains(PreviewFeature::Tracing);

let engine_protocol = EngineProtocol::Json;

let builder = EngineBuilder {
schema: Arc::new(schema),
engine_protocol,
enable_tracing,
};

let log_level = log_level.parse::<LevelFilter>().unwrap_or(Level::INFO.into());
Expand Down Expand Up @@ -116,6 +113,7 @@ impl QueryEngine {
_phantom: PhantomData,
},
preview_features,
builder.enable_tracing,
)
.await?;
let connector = executor.primary_connector();
Expand All @@ -142,6 +140,7 @@ impl QueryEngine {
query_schema: Arc::new(query_schema),
executor,
engine_protocol: builder.engine_protocol,
tracing_enabled: builder.enable_tracing,
}) as crate::Result<ConnectedEngine>
}
.instrument(span)
Expand Down Expand Up @@ -176,6 +175,7 @@ impl QueryEngine {
let builder = EngineBuilder {
schema: engine.schema.clone(),
engine_protocol: engine.engine_protocol(),
enable_tracing: engine.tracing_enabled(),
};

*inner = Inner::Builder(builder);
Expand Down
11 changes: 10 additions & 1 deletion query-engine/query-engine/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ impl PrismaContext {
schema::build(arced_schema, enabled_features.contains(Feature::RawQueries))
});

let enable_tracing = logger.tracing_config().should_capture();

let executor_fut = async move {
let config = &arced_schema_2.configuration;
let preview_features = config.preview_features();
Expand All @@ -68,8 +70,15 @@ impl PrismaContext {
.ok_or_else(|| PrismaError::ConfigurationError("No valid data source found".into()))?;

let url = datasource.load_url(|key| env::var(key).ok())?;

// Load executor
let executor = load_executor(ConnectorKind::Rust { url, datasource }, preview_features).await?;
let executor = load_executor(
ConnectorKind::Rust { url, datasource },
preview_features,
enable_tracing,
)
.await?;

let connector = executor.primary_connector();

let conn_span = tracing::info_span!(
Expand Down
Loading

0 comments on commit 40232c9

Please sign in to comment.