Skip to content

Commit d377a61

Browse files
authored
Merge pull request #2398 from calebschoepp/trace-all-the-things
feat(*): Add tracing to some more host components
2 parents 65b2e45 + 8af29e4 commit d377a61

File tree

24 files changed

+110
-11
lines changed

24 files changed

+110
-11
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ wasmtime-wasi-http = "18.0.1"
134134

135135
spin-componentize = { path = "crates/componentize" }
136136

137+
[workspace.lints.clippy]
138+
# TODO: Remove this once https://github.com/rust-lang/rust-clippy/issues/12281 is resolved
139+
blocks_in_conditions = "allow"
140+
137141
[[bin]]
138142
name = "spin"
139143
path = "src/bin/spin.rs"

crates/key-value-azure/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ spin-key-value = { path = "../key-value" }
1313
spin-core = { path = "../core" }
1414
tokio = "1"
1515
url = "2"
16+
tracing = { workspace = true }
17+
18+
[lints]
19+
workspace = true

crates/key-value-azure/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use futures::StreamExt;
99
use serde::{Deserialize, Serialize};
1010
use spin_core::async_trait;
1111
use spin_key_value::{log_error, Error, Store, StoreManager};
12+
use tracing::{instrument, Level};
1213

1314
pub struct KeyValueAzureCosmos {
1415
client: CollectionClient,
@@ -46,11 +47,13 @@ struct AzureCosmosStore {
4647

4748
#[async_trait]
4849
impl Store for AzureCosmosStore {
50+
#[instrument(name = "spin_key_value_azure.get", skip(self), err(level = Level::INFO), fields(otel.kind = "client"))]
4951
async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, Error> {
5052
let pair = self.get_pair(key).await?;
5153
Ok(pair.map(|p| p.value))
5254
}
5355

56+
#[instrument(name = "spin_key_value_azure.set", skip(self, value), err(level = Level::INFO), fields(otel.kind = "client"))]
5457
async fn set(&self, key: &str, value: &[u8]) -> Result<(), Error> {
5558
let pair = Pair {
5659
id: key.to_string(),
@@ -64,6 +67,7 @@ impl Store for AzureCosmosStore {
6467
Ok(())
6568
}
6669

70+
#[instrument(name = "spin_key_value_azure.delete", skip(self), err(level = Level::INFO), fields(otel.kind = "client"))]
6771
async fn delete(&self, key: &str) -> Result<(), Error> {
6872
if self.exists(key).await? {
6973
let document_client = self.client.document_client(key, &key).map_err(log_error)?;
@@ -72,10 +76,12 @@ impl Store for AzureCosmosStore {
7276
Ok(())
7377
}
7478

79+
#[instrument(name = "spin_key_value_azure.exists", skip(self), err(level = Level::INFO), fields(otel.kind = "client"))]
7580
async fn exists(&self, key: &str) -> Result<bool, Error> {
7681
Ok(self.get_pair(key).await?.is_some())
7782
}
7883

84+
#[instrument(name = "spin_key_value_azure.get_keys", skip(self), err(level = Level::INFO), fields(otel.kind = "client"))]
7985
async fn get_keys(&self) -> Result<Vec<String>, Error> {
8086
self.get_keys().await
8187
}

crates/key-value-redis/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ spin-key-value = { path = "../key-value" }
1111
spin-core = { path = "../core" }
1212
spin-world = { path = "../world" }
1313
tokio = "1"
14-
url = "2"
14+
url = "2"
15+
tracing = { workspace = true }
16+
17+
[lints]
18+
workspace = true

crates/key-value-redis/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use spin_core::async_trait;
44
use spin_key_value::{log_error, Error, Store, StoreManager};
55
use std::sync::Arc;
66
use tokio::sync::{Mutex, OnceCell};
7+
use tracing::{instrument, Level};
78
use url::Url;
89

910
pub struct KeyValueRedis {
@@ -24,6 +25,7 @@ impl KeyValueRedis {
2425

2526
#[async_trait]
2627
impl StoreManager for KeyValueRedis {
28+
#[instrument(name = "spin_key_value_redis.get_store", skip(self), err(level = Level::INFO), fields(otel.kind = "client"))]
2729
async fn get(&self, _name: &str) -> Result<Arc<dyn Store>, Error> {
2830
let connection = self
2931
.connection
@@ -53,11 +55,13 @@ struct RedisStore {
5355

5456
#[async_trait]
5557
impl Store for RedisStore {
58+
#[instrument(name = "spin_key_value_redis.get", skip(self), err(level = Level::INFO), fields(otel.kind = "client"))]
5659
async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, Error> {
5760
let mut conn = self.connection.lock().await;
5861
conn.get(key).await.map_err(log_error)
5962
}
6063

64+
#[instrument(name = "spin_key_value_redis.set", skip(self, value), err(level = Level::INFO), fields(otel.kind = "client"))]
6165
async fn set(&self, key: &str, value: &[u8]) -> Result<(), Error> {
6266
self.connection
6367
.lock()
@@ -67,6 +71,7 @@ impl Store for RedisStore {
6771
.map_err(log_error)
6872
}
6973

74+
#[instrument(name = "spin_key_value_redis.delete", skip(self), err(level = Level::INFO), fields(otel.kind = "client"))]
7075
async fn delete(&self, key: &str) -> Result<(), Error> {
7176
self.connection
7277
.lock()
@@ -76,6 +81,7 @@ impl Store for RedisStore {
7681
.map_err(log_error)
7782
}
7883

84+
#[instrument(name = "spin_key_value_redis.exists", skip(self), err(level = Level::INFO), fields(otel.kind = "client"))]
7985
async fn exists(&self, key: &str) -> Result<bool, Error> {
8086
self.connection
8187
.lock()
@@ -85,6 +91,7 @@ impl Store for RedisStore {
8591
.map_err(log_error)
8692
}
8793

94+
#[instrument(name = "spin_key_value_redis.get_keys", skip(self), err(level = Level::INFO), fields(otel.kind = "client"))]
8895
async fn get_keys(&self) -> Result<Vec<String>, Error> {
8996
self.connection
9097
.lock()

crates/key-value-sqlite/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ tokio = "1"
1212
spin-key-value = { path = "../key-value" }
1313
spin-core = { path = "../core" }
1414
spin-world = { path = "../world" }
15+
tracing = { workspace = true }
16+
17+
[lints]
18+
workspace = true

crates/key-value-sqlite/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{
88
sync::{Arc, Mutex},
99
};
1010
use tokio::task;
11+
use tracing::{instrument, Level};
1112

1213
pub enum DatabaseLocation {
1314
InMemory,
@@ -30,6 +31,7 @@ impl KeyValueSqlite {
3031

3132
#[async_trait]
3233
impl StoreManager for KeyValueSqlite {
34+
#[instrument(name = "spin_key_value_sqlite.get_store", skip(self), err(level = Level::INFO), fields(otel.kind = "client"))]
3335
async fn get(&self, name: &str) -> Result<Arc<dyn Store>, Error> {
3436
let connection = task::block_in_place(|| {
3537
self.connection.get_or_try_init(|| {
@@ -74,6 +76,7 @@ struct SqliteStore {
7476

7577
#[async_trait]
7678
impl Store for SqliteStore {
79+
#[instrument(name = "spin_key_value_sqlite.get", skip(self), err(level = Level::INFO), fields(otel.kind = "client"))]
7780
async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, Error> {
7881
task::block_in_place(|| {
7982
self.connection
@@ -89,6 +92,7 @@ impl Store for SqliteStore {
8992
})
9093
}
9194

95+
#[instrument(name = "spin_key_value_sqlite.set", skip(self, value), err(level = Level::INFO), fields(otel.kind = "client"))]
9296
async fn set(&self, key: &str, value: &[u8]) -> Result<(), Error> {
9397
task::block_in_place(|| {
9498
self.connection
@@ -105,6 +109,7 @@ impl Store for SqliteStore {
105109
})
106110
}
107111

112+
#[instrument(name = "spin_key_value_sqlite.delete", skip(self), err(level = Level::INFO), fields(otel.kind = "client"))]
108113
async fn delete(&self, key: &str) -> Result<(), Error> {
109114
task::block_in_place(|| {
110115
self.connection
@@ -118,10 +123,12 @@ impl Store for SqliteStore {
118123
})
119124
}
120125

126+
#[instrument(name = "spin_key_value_sqlite.exists", skip(self), err(level = Level::INFO), fields(otel.kind = "client"))]
121127
async fn exists(&self, key: &str) -> Result<bool, Error> {
122128
Ok(self.get(key).await?.is_some())
123129
}
124130

131+
#[instrument(name = "spin_key_value_sqlite.get_keys", skip(self), err(level = Level::INFO), fields(otel.kind = "client"))]
125132
async fn get_keys(&self) -> Result<Vec<String>, Error> {
126133
task::block_in_place(|| {
127134
self.connection

crates/key-value/src/util.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use tokio::{
1111
sync::Mutex as AsyncMutex,
1212
task::{self, JoinHandle},
1313
};
14+
use tracing::Instrument;
1415

1516
const DEFAULT_CACHE_SIZE: usize = 256;
1617

@@ -74,7 +75,7 @@ impl StoreManager for DelegatingStoreManager {
7475
/// instances.
7576
///
7677
/// Note that, because writes are asynchronous and return immediately, durability is _not_ guaranteed. I/O errors
77-
/// may occur asyncronously after the write operation has returned control to the guest, which may result in the
78+
/// may occur asynchronously after the write operation has returned control to the guest, which may result in the
7879
/// write being lost without the guest knowing. In the future, a separate `write-durable` function could be added
7980
/// to key-value.wit to provide either synchronous or asynchronous feedback on durability for guests which need it.
8081
pub struct CachingStoreManager<T> {
@@ -119,15 +120,16 @@ impl CachingStoreState {
119120
/// the result. This ensures that write order is preserved.
120121
fn spawn(&mut self, task: impl Future<Output = Result<(), Error>> + Send + 'static) {
121122
let previous_task = self.previous_task.take();
122-
self.previous_task = Some(task::spawn(async move {
123+
let task = async move {
123124
if let Some(previous_task) = previous_task {
124125
previous_task
125126
.await
126127
.map_err(|e| Error::Other(format!("{e:?}")))??
127128
}
128129

129130
task.await
130-
}))
131+
};
132+
self.previous_task = Some(task::spawn(task.in_current_span()))
131133
}
132134

133135
async fn flush(&mut self) -> Result<(), Error> {

crates/llm-local/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ tracing = { workspace = true }
3131
default = []
3232
metal = ["llm/metal"]
3333
cublas = ["llm/cublas"]
34+
35+
[lints]
36+
workspace = true

0 commit comments

Comments
 (0)