Skip to content

Commit

Permalink
feat(outbound-redis): Trace outbound-redis host component
Browse files Browse the repository at this point in the history
Signed-off-by: Caleb Schoepp <caleb.schoepp@fermyon.com>
  • Loading branch information
calebschoepp committed Mar 26, 2024
1 parent 64dd99b commit ef1d276
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions crates/outbound-redis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ spin-outbound-networking = { path = "../outbound-networking" }
table = { path = "../table" }
tokio = { version = "1", features = ["sync"] }
tracing = { workspace = true }

# TODO: Remove this once https://github.com/rust-lang/rust-clippy/issues/12281 is resolved
[lints.clippy]
blocks_in_conditions = "allow"
24 changes: 21 additions & 3 deletions crates/outbound-redis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use spin_world::v2::redis::{
};

pub use host_component::OutboundRedisComponent;
use tracing::{instrument, Level};

struct RedisResults(Vec<RedisResult>);

Expand Down Expand Up @@ -72,14 +73,18 @@ impl v2::Host for OutboundRedis {}

#[async_trait]
impl v2::HostConnection for OutboundRedis {
#[instrument(name = "open_redis_connection", skip(self), err(level = Level::INFO))]
async fn open(&mut self, address: String) -> Result<Result<Resource<RedisConnection>, Error>> {
if !self.is_address_allowed(&address) {
return Ok(Err(Error::InvalidAddress));
let e = Error::InvalidAddress;
tracing::event!(target:module_path!(), Level::INFO, error = %e);
return Ok(Err(e));
}

self.establish_connection(address).await
}

#[instrument(name = "publish_msg_redis", skip(self, connection, payload), err(level = Level::INFO), fields(otel.kind = "producer"))]
async fn publish(
&mut self,
connection: Resource<RedisConnection>,
Expand All @@ -96,6 +101,7 @@ impl v2::HostConnection for OutboundRedis {
.await)
}

#[instrument(name = "get_value_redis", skip(self, connection), err(level = Level::INFO), fields(otel.kind = "client"))]
async fn get(
&mut self,
connection: Resource<RedisConnection>,
Expand All @@ -109,6 +115,7 @@ impl v2::HostConnection for OutboundRedis {
.await)
}

#[instrument(name = "set_value_redis", skip(self, connection, value), err(level = Level::INFO), fields(otel.kind = "client"))]
async fn set(
&mut self,
connection: Resource<RedisConnection>,
Expand All @@ -123,6 +130,7 @@ impl v2::HostConnection for OutboundRedis {
.await)
}

#[instrument(name = "incr_value_redis", skip(self, connection), err(level = Level::INFO), fields(otel.kind = "client"))]
async fn incr(
&mut self,
connection: Resource<RedisConnection>,
Expand All @@ -136,6 +144,7 @@ impl v2::HostConnection for OutboundRedis {
.await)
}

#[instrument(name = "del_keys_redis", skip(self, connection), err(level = Level::INFO), fields(otel.kind = "client"))]
async fn del(
&mut self,
connection: Resource<RedisConnection>,
Expand All @@ -149,6 +158,7 @@ impl v2::HostConnection for OutboundRedis {
.await)
}

#[instrument(name = "set_add_values_redis", skip(self, connection, values), err(level = Level::INFO), fields(otel.kind = "client"))]
async fn sadd(
&mut self,
connection: Resource<RedisConnection>,
Expand All @@ -169,6 +179,7 @@ impl v2::HostConnection for OutboundRedis {
.await)
}

#[instrument(name = "set_retrieve_values_redis", skip(self, connection), err(level = Level::INFO), fields(otel.kind = "client"))]
async fn smembers(
&mut self,
connection: Resource<RedisConnection>,
Expand All @@ -182,6 +193,7 @@ impl v2::HostConnection for OutboundRedis {
.await)
}

#[instrument(name = "set_remove_values_redis", skip(self, connection, values), err(level = Level::INFO), fields(otel.kind = "client"))]
async fn srem(
&mut self,
connection: Resource<RedisConnection>,
Expand All @@ -196,6 +208,7 @@ impl v2::HostConnection for OutboundRedis {
.await)
}

#[instrument(name = "exec_command_redis", skip(self, connection), err(level = Level::INFO), fields(otel.kind = "client"))]
async fn execute(
&mut self,
connection: Resource<RedisConnection>,
Expand All @@ -214,10 +227,15 @@ impl v2::HostConnection for OutboundRedis {
}
});

cmd.query_async::<_, RedisResults>(conn)
let inner = cmd
.query_async::<_, RedisResults>(conn)
.await
.map(|values| values.0)
.map_err(other_error)
.map_err(other_error);
if let Err(e) = &inner {
tracing::event!(target:module_path!(), Level::INFO, error = %e);
}
inner
}
.await)
}
Expand Down

0 comments on commit ef1d276

Please sign in to comment.