Skip to content

Commit f1487d5

Browse files
committed
Reparent tracing spans in sqlite
Signed-off-by: Caleb Schoepp <caleb.schoepp@fermyon.com>
1 parent 251b795 commit f1487d5

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ rust-version.workspace = true
1111
[dependencies]
1212
async-trait = "0.1"
1313
serde = { version = "1.0", features = ["rc"] }
14+
spin-factor-observe = { path = "../factor-observe" }
1415
spin-factors = { path = "../factors" }
1516
spin-locked-app = { path = "../locked-app" }
1617
spin-world = { path = "../world" }
@@ -26,5 +27,3 @@ tokio = { version = "1", features = ["macros", "rt"] }
2627

2728
[lints]
2829
workspace = true
29-
30-
# TODO(Caleb): Setup observe reparenting

crates/factor-sqlite/src/host.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::sync::Arc;
33

44
use async_trait::async_trait;
55

6+
use spin_factor_observe::ObserveContext;
67
use spin_factors::wasmtime::component::Resource;
78
use spin_factors::{anyhow, SelfInstanceBuilder};
89
use spin_world::v1::sqlite as v1;
@@ -16,6 +17,7 @@ pub struct InstanceState {
1617
allowed_databases: Arc<HashSet<String>>,
1718
connections: table::Table<Box<dyn Connection>>,
1819
get_connection_creator: ConnectionCreatorGetter,
20+
observe_context: ObserveContext,
1921
}
2022

2123
impl InstanceState {
@@ -35,11 +37,13 @@ impl InstanceState {
3537
pub fn new(
3638
allowed_databases: Arc<HashSet<String>>,
3739
get_connection_creator: ConnectionCreatorGetter,
40+
observe_context: ObserveContext,
3841
) -> Self {
3942
Self {
4043
allowed_databases,
4144
connections: table::Table::new(256),
4245
get_connection_creator,
46+
observe_context,
4347
}
4448
}
4549

@@ -66,6 +70,8 @@ impl v2::Host for InstanceState {
6670
impl v2::HostConnection for InstanceState {
6771
#[instrument(name = "spin_sqlite.open", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "sqlite", sqlite.backend = Empty))]
6872
async fn open(&mut self, database: String) -> Result<Resource<v2::Connection>, v2::Error> {
73+
self.observe_context.reparent_tracing_span();
74+
6975
if !self.allowed_databases.contains(&database) {
7076
return Err(v2::Error::AccessDenied);
7177
}
@@ -90,6 +96,8 @@ impl v2::HostConnection for InstanceState {
9096
query: String,
9197
parameters: Vec<v2::Value>,
9298
) -> Result<v2::QueryResult, v2::Error> {
99+
self.observe_context.reparent_tracing_span();
100+
93101
let conn = match self.get_connection(connection) {
94102
Ok(c) => c,
95103
Err(err) => return Err(err),

crates/factor-sqlite/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::sync::Arc;
77
use host::InstanceState;
88

99
use async_trait::async_trait;
10+
use spin_factor_observe::ObserveContext;
1011
use spin_factors::{anyhow, Factor};
1112
use spin_locked_app::MetadataKey;
1213
use spin_world::v1::sqlite as v1;
@@ -86,7 +87,7 @@ impl Factor for SqliteFactor {
8687

8788
fn prepare<T: spin_factors::RuntimeFactors>(
8889
&self,
89-
ctx: spin_factors::PrepareContext<T, Self>,
90+
mut ctx: spin_factors::PrepareContext<T, Self>,
9091
) -> spin_factors::anyhow::Result<Self::InstanceBuilder> {
9192
let allowed_databases = ctx
9293
.app_state()
@@ -95,9 +96,11 @@ impl Factor for SqliteFactor {
9596
.cloned()
9697
.unwrap_or_default();
9798
let get_connection_creator = ctx.app_state().get_connection_creator.clone();
99+
let observe_context = ObserveContext::from_prepare_context(&mut ctx)?;
98100
Ok(InstanceState::new(
99101
allowed_databases,
100102
get_connection_creator,
103+
observe_context,
101104
))
102105
}
103106
}

0 commit comments

Comments
 (0)