Skip to content

Commit

Permalink
Reparent tracing spans in sqlite
Browse files Browse the repository at this point in the history
Signed-off-by: Caleb Schoepp <[email protected]>
  • Loading branch information
calebschoepp committed Sep 16, 2024
1 parent 251b795 commit f1487d5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions crates/factor-sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rust-version.workspace = true
[dependencies]
async-trait = "0.1"
serde = { version = "1.0", features = ["rc"] }
spin-factor-observe = { path = "../factor-observe" }
spin-factors = { path = "../factors" }
spin-locked-app = { path = "../locked-app" }
spin-world = { path = "../world" }
Expand All @@ -26,5 +27,3 @@ tokio = { version = "1", features = ["macros", "rt"] }

[lints]
workspace = true

# TODO(Caleb): Setup observe reparenting
8 changes: 8 additions & 0 deletions crates/factor-sqlite/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;

use async_trait::async_trait;

use spin_factor_observe::ObserveContext;
use spin_factors::wasmtime::component::Resource;
use spin_factors::{anyhow, SelfInstanceBuilder};
use spin_world::v1::sqlite as v1;
Expand All @@ -16,6 +17,7 @@ pub struct InstanceState {
allowed_databases: Arc<HashSet<String>>,
connections: table::Table<Box<dyn Connection>>,
get_connection_creator: ConnectionCreatorGetter,
observe_context: ObserveContext,
}

impl InstanceState {
Expand All @@ -35,11 +37,13 @@ impl InstanceState {
pub fn new(
allowed_databases: Arc<HashSet<String>>,
get_connection_creator: ConnectionCreatorGetter,
observe_context: ObserveContext,
) -> Self {
Self {
allowed_databases,
connections: table::Table::new(256),
get_connection_creator,
observe_context,
}
}

Expand All @@ -66,6 +70,8 @@ impl v2::Host for InstanceState {
impl v2::HostConnection for InstanceState {
#[instrument(name = "spin_sqlite.open", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "sqlite", sqlite.backend = Empty))]
async fn open(&mut self, database: String) -> Result<Resource<v2::Connection>, v2::Error> {
self.observe_context.reparent_tracing_span();

if !self.allowed_databases.contains(&database) {
return Err(v2::Error::AccessDenied);
}
Expand All @@ -90,6 +96,8 @@ impl v2::HostConnection for InstanceState {
query: String,
parameters: Vec<v2::Value>,
) -> Result<v2::QueryResult, v2::Error> {
self.observe_context.reparent_tracing_span();

let conn = match self.get_connection(connection) {
Ok(c) => c,
Err(err) => return Err(err),
Expand Down
5 changes: 4 additions & 1 deletion crates/factor-sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::Arc;
use host::InstanceState;

use async_trait::async_trait;
use spin_factor_observe::ObserveContext;
use spin_factors::{anyhow, Factor};
use spin_locked_app::MetadataKey;
use spin_world::v1::sqlite as v1;
Expand Down Expand Up @@ -86,7 +87,7 @@ impl Factor for SqliteFactor {

fn prepare<T: spin_factors::RuntimeFactors>(
&self,
ctx: spin_factors::PrepareContext<T, Self>,
mut ctx: spin_factors::PrepareContext<T, Self>,
) -> spin_factors::anyhow::Result<Self::InstanceBuilder> {
let allowed_databases = ctx
.app_state()
Expand All @@ -95,9 +96,11 @@ impl Factor for SqliteFactor {
.cloned()
.unwrap_or_default();
let get_connection_creator = ctx.app_state().get_connection_creator.clone();
let observe_context = ObserveContext::from_prepare_context(&mut ctx)?;
Ok(InstanceState::new(
allowed_databases,
get_connection_creator,
observe_context,
))
}
}
Expand Down

0 comments on commit f1487d5

Please sign in to comment.