Skip to content

Commit f5c911b

Browse files
committed
Move instruments up a level of abstraction in sqlite
Signed-off-by: Caleb Schoepp <[email protected]>
1 parent 0f14f0c commit f5c911b

File tree

7 files changed

+15
-12
lines changed

7 files changed

+15
-12
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/factor-sqlite/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ spin-world = { path = "../world" }
1717
table = { path = "../table" }
1818
tokio = "1"
1919
toml = "0.8"
20+
tracing = { workspace = true }
2021

2122
[dev-dependencies]
2223
spin-factors-test = { path = "../factors-test" }

crates/factor-sqlite/src/host.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use spin_factors::wasmtime::component::Resource;
77
use spin_factors::{anyhow, SelfInstanceBuilder};
88
use spin_world::v1::sqlite as v1;
99
use spin_world::v2::sqlite as v2;
10+
use tracing::field::Empty;
11+
use tracing::{instrument, Level};
1012

1113
use crate::{Connection, ConnectionCreator};
1214

@@ -62,6 +64,7 @@ impl v2::Host for InstanceState {
6264

6365
#[async_trait]
6466
impl v2::HostConnection for InstanceState {
67+
#[instrument(name = "spin_sqlite.open", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "sqlite", sqlite.backend = Empty))]
6568
async fn open(&mut self, database: String) -> Result<Resource<v2::Connection>, v2::Error> {
6669
if !self.allowed_databases.contains(&database) {
6770
return Err(v2::Error::AccessDenied);
@@ -70,12 +73,17 @@ impl v2::HostConnection for InstanceState {
7073
.ok_or(v2::Error::NoSuchDatabase)?
7174
.create_connection(&database)
7275
.await?;
76+
tracing::Span::current().record(
77+
"sqlite.backend",
78+
conn.summary().as_deref().unwrap_or("unknown"),
79+
);
7380
self.connections
7481
.push(conn)
7582
.map_err(|()| v2::Error::Io("too many connections opened".to_string()))
7683
.map(Resource::new_own)
7784
}
7885

86+
#[instrument(name = "spin_sqlite.execute", skip(self, connection), err(level = Level::INFO), fields(otel.kind = "client", db.system = "sqlite", otel.name = query, sqlite.backend = Empty))]
7987
async fn execute(
8088
&mut self,
8189
connection: Resource<v2::Connection>,
@@ -86,6 +94,10 @@ impl v2::HostConnection for InstanceState {
8694
Ok(c) => c,
8795
Err(err) => return Err(err),
8896
};
97+
tracing::Span::current().record(
98+
"sqlite.backend",
99+
conn.summary().as_deref().unwrap_or("unknown"),
100+
);
89101
conn.query(&query, parameters).await
90102
}
91103

crates/sqlite-inproc/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ rusqlite = { version = "0.29.0", features = ["bundled"] }
1313
spin-factor-sqlite = { path = "../factor-sqlite" }
1414
spin-world = { path = "../world" }
1515
tokio = "1"
16-
tracing = { workspace = true }
1716

1817
[lints]
1918
workspace = true

crates/sqlite-inproc/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ use async_trait::async_trait;
88
use once_cell::sync::OnceCell;
99
use spin_factor_sqlite::Connection;
1010
use spin_world::v2::sqlite;
11-
use tracing::{instrument, Level};
1211

1312
/// The location of an in-process sqlite database.
1413
#[derive(Debug, Clone)]
1514
pub enum InProcDatabaseLocation {
1615
/// An in-memory sqlite database.
1716
InMemory,
18-
/// The path to the directory containing the sqlite database.
17+
/// The path to the sqlite database.
1918
Path(PathBuf),
2019
}
2120

@@ -74,7 +73,6 @@ impl InProcConnection {
7473

7574
#[async_trait]
7675
impl Connection for InProcConnection {
77-
#[instrument(name = "spin_sqlite_inproc.query", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "sqlite", otel.name = query))]
7876
async fn query(
7977
&self,
8078
query: &str,
@@ -89,7 +87,6 @@ impl Connection for InProcConnection {
8987
.map_err(|e| sqlite::Error::Io(e.to_string()))?
9088
}
9189

92-
#[instrument(name = "spin_sqlite_inproc.execute_batch", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "sqlite", db.statements = statements))]
9390
async fn execute_batch(&self, statements: &str) -> anyhow::Result<()> {
9491
let connection = self.db_connection()?;
9592
let statements = statements.to_owned();

crates/sqlite-libsql/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ spin-factor-sqlite = { path = "../factor-sqlite" }
1515
spin-world = { path = "../world" }
1616
sqlparser = "0.34"
1717
tokio = { version = "1", features = ["full"] }
18-
tracing = { workspace = true }
1918

2019
[lints]
2120
workspace = true

crates/sqlite-libsql/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use spin_factor_sqlite::Connection;
44
use spin_world::v2::sqlite as v2;
55
use spin_world::v2::sqlite::{self, RowResult};
66
use tokio::sync::OnceCell;
7-
use tracing::{instrument, Level};
87

98
/// A lazy wrapper around a [`LibSqlConnection`] that implements the [`Connection`] trait.
109
pub struct LazyLibSqlConnection {
@@ -65,7 +64,6 @@ pub struct LibSqlConnection {
6564
}
6665

6766
impl LibSqlConnection {
68-
#[instrument(name = "spin_sqlite_libsql.create_connection", skip(token), err(level = Level::INFO), fields(otel.kind = "client", db.system = "sqlite"))]
6967
pub async fn create(url: String, token: String) -> anyhow::Result<Self> {
7068
let db = libsql::Builder::new_remote(url, token).build().await?;
7169
let inner = db.connect()?;
@@ -74,7 +72,6 @@ impl LibSqlConnection {
7472
}
7573

7674
impl LibSqlConnection {
77-
#[instrument(name = "spin_sqlite_libsql.query", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "sqlite", otel.name = query))]
7875
pub async fn query(
7976
&self,
8077
query: &str,
@@ -94,7 +91,6 @@ impl LibSqlConnection {
9491
})
9592
}
9693

97-
#[instrument(name = "spin_sqlite_libsql.execute_batch", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "sqlite", db.statements = statements))]
9894
pub async fn execute_batch(&self, statements: &str) -> anyhow::Result<()> {
9995
self.inner.execute_batch(statements).await?;
10096

0 commit comments

Comments
 (0)