Skip to content

Commit 5bb5706

Browse files
MasterPtatoNathanFlurry
authored andcommitted
fix(cache): add traces
1 parent 9cc03f0 commit 5bb5706

File tree

9 files changed

+75
-25
lines changed

9 files changed

+75
-25
lines changed

packages/common/api-helper/build/src/macro_util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ pub fn __deserialize_query<T: DeserializeOwned + Send>(route: &Url) -> GlobalRes
237237
}
238238

239239
#[doc(hidden)]
240+
#[tracing::instrument(skip_all, name="setup_ctx")]
240241
pub async fn __with_ctx<
241242
A: auth::ApiAuth + Send,
242243
DB: chirp_workflow::db::Database + Sync + 'static,

packages/common/api-helper/build/src/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ pub fn as_auth_expired<T>(res: GlobalResult<T>) -> GlobalResult<T> {
284284
}
285285
}
286286

287+
#[tracing::instrument(skip_all)]
287288
pub async fn basic_rate_limit(
288289
config: &rivet_config::Config,
289290
rate_limit_ctx: crate::auth::AuthRateLimitCtx<'_>,

packages/common/cache/build/src/driver.rs

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66
use moka::future::{Cache, CacheBuilder};
77
use redis::AsyncCommands;
88
use rivet_pools::prelude::*;
9+
use tracing::Instrument;
910

1011
use crate::{error::Error, metrics};
1112

@@ -21,6 +22,7 @@ pub enum Driver {
2122

2223
impl Driver {
2324
/// Fetch multiple values from cache at once
25+
#[tracing::instrument(skip_all, fields(driver=%self))]
2426
pub async fn fetch_values<'a>(
2527
&'a self,
2628
base_key: &'a str,
@@ -33,6 +35,7 @@ impl Driver {
3335
}
3436

3537
/// Set multiple values in cache at once
38+
#[tracing::instrument(skip_all, fields(driver=%self))]
3639
pub async fn set_values<'a>(
3740
&'a self,
3841
base_key: &'a str,
@@ -45,6 +48,7 @@ impl Driver {
4548
}
4649

4750
/// Delete multiple keys from cache
51+
#[tracing::instrument(skip_all, fields(driver=%self))]
4852
pub async fn delete_keys<'a>(
4953
&'a self,
5054
base_key: &'a str,
@@ -95,6 +99,7 @@ impl Driver {
9599
}
96100

97101
/// Increment a rate limit counter and return the new count
102+
#[tracing::instrument(skip_all, fields(driver=%self))]
98103
pub async fn rate_limit_increment<'a>(
99104
&'a self,
100105
key: &'a str,
@@ -123,6 +128,15 @@ impl Driver {
123128
}
124129
}
125130

131+
impl std::fmt::Display for Driver {
132+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
133+
match self {
134+
Driver::Redis(_) => write!(f, "redis"),
135+
Driver::InMemory(_) => write!(f, "in_memory"),
136+
}
137+
}
138+
}
139+
126140
/// Redis cache driver implementation
127141
#[derive(Clone)]
128142
pub struct RedisDriver {
@@ -176,6 +190,7 @@ impl RedisDriver {
176190

177191
match mget_cmd
178192
.query_async::<_, Vec<Option<CacheValue>>>(&mut redis_conn)
193+
.instrument(tracing::info_span!("redis_query"))
179194
.await
180195
{
181196
Ok(values) => {
@@ -215,7 +230,11 @@ impl RedisDriver {
215230
.ignore();
216231
}
217232

218-
match pipe.query_async(&mut redis_conn).await {
233+
match pipe
234+
.query_async(&mut redis_conn)
235+
.instrument(tracing::info_span!("redis_query"))
236+
.await
237+
{
219238
Ok(()) => {
220239
tracing::trace!("successfully wrote to cache");
221240
Ok(())
@@ -242,7 +261,11 @@ impl RedisDriver {
242261
.with_label_values(&[&base_key])
243262
.inc_by(redis_keys.len() as u64);
244263

245-
match redis_conn.del::<_, ()>(redis_keys).await {
264+
match redis_conn
265+
.del::<_, ()>(redis_keys)
266+
.instrument(tracing::info_span!("redis_query"))
267+
.await
268+
{
246269
Ok(_) => {
247270
tracing::trace!("successfully deleted keys");
248271
Ok(())
@@ -300,7 +323,11 @@ impl RedisDriver {
300323
pipe.incr(key, 1);
301324
pipe.pexpire(key, ttl_ms as usize).ignore();
302325

303-
match pipe.query_async::<_, (i64,)>(&mut redis_conn).await {
326+
match pipe
327+
.query_async::<_, (i64,)>(&mut redis_conn)
328+
.instrument(tracing::info_span!("redis_query"))
329+
.await
330+
{
304331
Ok((incr,)) => Ok(incr),
305332
Err(err) => {
306333
tracing::error!(?err, ?key, "failed to increment rate limit key");
@@ -415,9 +442,14 @@ impl InMemoryDriver {
415442

416443
let mut result = Vec::with_capacity(keys.len());
417444

418-
for key in keys {
419-
result.push(cache.get(&key).await.map(|x| x.value.clone()));
445+
// Async block for metrics
446+
async {
447+
for key in keys {
448+
result.push(cache.get(&key).await.map(|x| x.value.clone()));
449+
}
420450
}
451+
.instrument(tracing::info_span!("get"))
452+
.await;
421453

422454
tracing::debug!(
423455
cached_len = result.iter().filter(|x| x.is_some()).count(),
@@ -435,16 +467,21 @@ impl InMemoryDriver {
435467
) -> Result<(), Error> {
436468
let cache = self.cache.clone();
437469

438-
for (key, value, expire_at) in keys_values {
439-
// Create an entry with the value and expiration time
440-
let entry = ExpiringValue {
441-
value,
442-
expiry_time: expire_at,
443-
};
444-
445-
// Store in cache - expiry will be handled by ValueExpiry
446-
cache.insert(key, entry).await;
470+
// Async block for metrics
471+
async {
472+
for (key, value, expire_at) in keys_values {
473+
// Create an entry with the value and expiration time
474+
let entry = ExpiringValue {
475+
value,
476+
expiry_time: expire_at,
477+
};
478+
479+
// Store in cache - expiry will be handled by ValueExpiry
480+
cache.insert(key, entry).await;
481+
}
447482
}
483+
.instrument(tracing::info_span!("set"))
484+
.await;
448485

449486
tracing::trace!("successfully wrote to in-memory cache with per-key expiry");
450487
Ok(())
@@ -465,10 +502,15 @@ impl InMemoryDriver {
465502
.with_label_values(&[&base_key])
466503
.inc_by(keys.len() as u64);
467504

468-
for key in keys {
469-
// Use remove instead of invalidate to ensure it's actually removed
470-
cache.remove(&key).await;
505+
// Async block for metrics
506+
async {
507+
for key in keys {
508+
// Use remove instead of invalidate to ensure it's actually removed
509+
cache.remove(&key).await;
510+
}
471511
}
512+
.instrument(tracing::info_span!("remove"))
513+
.await;
472514

473515
tracing::trace!("successfully deleted keys from in-memory cache");
474516
Ok(())

packages/common/cache/build/src/rate_limit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ impl CacheInner {
1818
///
1919
/// This is infallible in order to make sure that anything that depends on
2020
/// this never fails.
21+
#[tracing::instrument(skip_all)]
2122
pub async fn rate_limit(
2223
&self,
2324
key: &impl CacheKey,

packages/core/api/actor/src/auth.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct CheckOutput {
2626

2727
#[async_trait]
2828
impl ApiAuth for Auth {
29+
#[tracing::instrument(skip_all)]
2930
async fn new(
3031
config: rivet_config::Config,
3132
api_token: Option<String>,
@@ -46,6 +47,7 @@ impl ApiAuth for Auth {
4647
})
4748
}
4849

50+
#[tracing::instrument(skip_all)]
4951
async fn rate_limit(
5052
config: &rivet_config::Config,
5153
rate_limit_ctx: AuthRateLimitCtx<'_>,

packages/core/api/actor/src/route/actors.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ pub async fn create(
232232
query.global.environment.as_deref(),
233233
query.endpoint_type,
234234
)
235-
.instrument(tracing::info_span!("proxy request"))
235+
.instrument(tracing::info_span!("proxy_request", base_path=%config.base_path))
236236
.await
237237
{
238238
Ok(res) => Ok(res),
@@ -427,7 +427,7 @@ pub async fn destroy(
427427
query.global.environment.as_deref(),
428428
query.override_kill_timeout,
429429
)
430-
.instrument(tracing::info_span!("proxy request"))
430+
.instrument(tracing::info_span!("proxy_request", base_path=%config.base_path))
431431
.await
432432
{
433433
Ok(res) => Ok(res),
@@ -561,7 +561,7 @@ pub async fn upgrade(
561561
query.project.as_deref(),
562562
query.environment.as_deref(),
563563
)
564-
.instrument(tracing::info_span!("proxy request"))
564+
.instrument(tracing::info_span!("proxy_request", base_path=%config.base_path))
565565
.await
566566
{
567567
Ok(res) => Ok(res),
@@ -705,7 +705,7 @@ pub async fn upgrade_all(
705705
query.project.as_deref(),
706706
query.environment.as_deref(),
707707
)
708-
.instrument(tracing::info_span!("proxy request"))
708+
.instrument(tracing::info_span!("proxy_request", base_path=%config.base_path))
709709
.await
710710
{
711711
Ok(res) => Ok(res),
@@ -873,7 +873,7 @@ async fn list_actors_inner(
873873
query.include_destroyed,
874874
query.cursor.as_deref(),
875875
)
876-
.instrument(tracing::info_span!("proxy request")),
876+
.instrument(tracing::info_span!("proxy_request", base_path=%config.base_path)),
877877
)
878878
.await;
879879

packages/core/api/status/src/auth.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub struct Auth {
88

99
#[async_trait]
1010
impl ApiAuth for Auth {
11+
#[tracing::instrument(skip_all)]
1112
async fn new(
1213
config: rivet_config::Config,
1314
api_token: Option<String>,

packages/core/api/status/src/route/actor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub async fn status(
208208
Some(&system_test_env),
209209
None,
210210
)
211-
.instrument(tracing::info_span!("actor create request"))
211+
.instrument(tracing::info_span!("actor_create_request"))
212212
.await?;
213213
let actor_id = res.actor.id;
214214

@@ -240,7 +240,7 @@ pub async fn status(
240240
Some(&system_test_env),
241241
None,
242242
)
243-
.instrument(tracing::info_span!("actor destroy request"))
243+
.instrument(tracing::info_span!("actor_destroy_request"))
244244
.await?;
245245

246246
// Unwrap res
@@ -350,7 +350,7 @@ async fn test_http(
350350
client
351351
.get(format!("{actor_origin}/health"))
352352
.send()
353-
.instrument(tracing::info_span!("health request"))
353+
.instrument(tracing::info_span!("health_request"))
354354
.await?
355355
.error_for_status()?;
356356

packages/edge/api/actor/src/auth.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct CheckOutput {
2525

2626
#[async_trait]
2727
impl ApiAuth for Auth {
28+
#[tracing::instrument(skip_all)]
2829
async fn new(
2930
config: rivet_config::Config,
3031
api_token: Option<String>,
@@ -44,6 +45,7 @@ impl ApiAuth for Auth {
4445
})
4546
}
4647

48+
#[tracing::instrument(skip_all)]
4749
async fn rate_limit(
4850
config: &rivet_config::Config,
4951
rate_limit_ctx: AuthRateLimitCtx<'_>,

0 commit comments

Comments
 (0)