Skip to content

Commit 03f9f00

Browse files
committed
feat(metrics): add cache metrics to dns-server
1 parent 79bb0e4 commit 03f9f00

2 files changed

Lines changed: 40 additions & 24 deletions

File tree

iroh-dns-server/src/metrics.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Metrics support for the server
22
3-
use iroh_metrics::{Counter, MetricsGroup};
3+
use iroh_metrics::{Counter, Gauge, MetricsGroup};
44

55
/// Metrics for iroh-dns-server
66
#[derive(Debug, Default, MetricsGroup)]
@@ -38,4 +38,8 @@ pub struct Metrics {
3838
pub store_packets_updated: Counter,
3939
/// Number of expired packets
4040
pub store_packets_expired: Counter,
41+
/// Current number of zones in the main cache
42+
pub cache_zones: Gauge,
43+
/// Current number of zones in the DHT cache
44+
pub cache_zones_dht: Gauge,
4145
}

iroh-dns-server/src/store.rs

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl ZoneStore {
8585

8686
/// Create a new zone store.
8787
pub fn new(store: SignedPacketStore, metrics: Arc<Metrics>) -> Self {
88-
let zone_cache = ZoneCache::new(DEFAULT_CACHE_CAPACITY);
88+
let zone_cache = ZoneCache::new(DEFAULT_CACHE_CAPACITY, metrics.clone());
8989
Self {
9090
store: Arc::new(store),
9191
cache: Arc::new(Mutex::new(zone_cache)),
@@ -103,22 +103,25 @@ impl ZoneStore {
103103
record_type: RecordType,
104104
) -> Result<Option<Arc<RecordSet>>> {
105105
trace!("store resolve");
106-
if let Some(rset) = self.cache.lock().await.resolve(pubkey, name, record_type) {
107-
debug!(
108-
len = rset.records_without_rrsigs().count(),
109-
"resolved from cache"
110-
);
111-
return Ok(Some(rset));
106+
107+
// Check cache first (short lock scope)
108+
{
109+
let mut cache = self.cache.lock().await;
110+
if let Some(rset) = cache.resolve(pubkey, name, record_type) {
111+
debug!(
112+
len = rset.records_without_rrsigs().count(),
113+
"resolved from cache"
114+
);
115+
return Ok(Some(rset));
116+
}
112117
}
113118

119+
// Check persistent store
114120
if let Some(packet) = self.store.get(pubkey).await? {
115121
trace!(packet_timestamp = ?packet.timestamp(), "store hit");
116-
return match self
117-
.cache
118-
.lock()
119-
.await
120-
.insert_and_resolve(&packet, name, record_type)
121-
{
122+
let mut cache = self.cache.lock().await;
123+
let result = cache.insert_and_resolve(&packet, name, record_type);
124+
return match result {
122125
Ok(Some(rset)) => {
123126
debug!(
124127
len = rset.records_without_rrsigs().count(),
@@ -137,20 +140,15 @@ impl ZoneStore {
137140
};
138141
};
139142

143+
// DHT fallback (lock not held during network call)
140144
if let Some(pkarr) = self.pkarr.as_ref() {
141145
let key = pkarr::PublicKey::try_from(pubkey.as_bytes()).expect("valid public key");
142-
// use the more expensive `resolve_most_recent` here.
143-
//
144-
// it will be cached for some time.
145146
debug!("DHT resolve {}", key.to_z32());
146147
let packet_opt = pkarr.resolve(&key).await;
147148
if let Some(packet) = packet_opt {
148149
debug!("DHT resolve successful {:?}", packet);
149-
return self
150-
.cache
151-
.lock()
152-
.await
153-
.insert_and_resolve_dht(&packet, name, record_type);
150+
let mut cache = self.cache.lock().await;
151+
return cache.insert_and_resolve_dht(&packet, name, record_type);
154152
} else {
155153
debug!("DHT resolve failed");
156154
}
@@ -192,13 +190,19 @@ struct ZoneCache {
192190
/// so we don't cache stale entries indefinitely.
193191
#[debug("dht_cache")]
194192
dht_cache: TtlCache<PublicKeyBytes, CachedZone>,
193+
#[debug("metrics")]
194+
metrics: Arc<Metrics>,
195195
}
196196

197197
impl ZoneCache {
198-
fn new(cap: usize) -> Self {
198+
fn new(cap: usize, metrics: Arc<Metrics>) -> Self {
199199
let cache = LruCache::new(NonZeroUsize::new(cap).expect("capacity must be larger than 0"));
200200
let dht_cache = TtlCache::new(cap);
201-
Self { cache, dht_cache }
201+
Self {
202+
cache,
203+
dht_cache,
204+
metrics,
205+
}
202206
}
203207

204208
fn resolve(
@@ -240,6 +244,9 @@ impl ZoneCache {
240244
let zone = CachedZone::from_signed_packet(signed_packet).anyerr()?;
241245
let res = zone.resolve(name, record_type);
242246
self.dht_cache.insert(pubkey, zone, DHT_CACHE_TTL);
247+
self.metrics
248+
.cache_zones_dht
249+
.set(self.dht_cache.iter().count() as i64);
243250
Ok(res)
244251
}
245252

@@ -258,6 +265,7 @@ impl ZoneCache {
258265
pubkey,
259266
CachedZone::from_signed_packet(signed_packet).anyerr()?,
260267
);
268+
self.metrics.cache_zones.set(self.cache.len() as i64);
261269
trace!("inserted into cache");
262270
Ok(())
263271
}
@@ -266,6 +274,10 @@ impl ZoneCache {
266274
fn remove(&mut self, pubkey: &PublicKeyBytes) {
267275
self.cache.pop(pubkey);
268276
self.dht_cache.remove(pubkey);
277+
self.metrics.cache_zones.set(self.cache.len() as i64);
278+
self.metrics
279+
.cache_zones_dht
280+
.set(self.dht_cache.iter().count() as i64);
269281
}
270282
}
271283

0 commit comments

Comments
 (0)