Skip to content

Commit

Permalink
refactor: use Cow<'static, str> instead of &'static str
Browse files Browse the repository at this point in the history
Signed-off-by: MrCroxx <[email protected]>
  • Loading branch information
MrCroxx committed Dec 4, 2024
1 parent 54893fe commit 7f5909b
Show file tree
Hide file tree
Showing 11 changed files with 289 additions and 194 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ example:

full: check-all test-all example udeps

fast: check test example
fast: ffmt check test example

msrv:
shellcheck ./scripts/*
Expand Down
20 changes: 10 additions & 10 deletions foyer-common/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt::Debug;
use std::{borrow::Cow, fmt::Debug};

/// Counter metric operations.
pub trait CounterOps: Send + Sync + 'static + Debug {
Expand All @@ -39,44 +39,44 @@ pub trait HistogramOps: Send + Sync + 'static + Debug {
/// A vector of counters.
pub trait CounterVecOps: Send + Sync + 'static + Debug {
/// Get a counter within the vector of counters.
fn counter(&self, labels: &[&'static str]) -> impl CounterOps;
fn counter(&self, labels: &[Cow<'static, str>]) -> impl CounterOps;
}

/// A vector of gauges.
pub trait GaugeVecOps: Send + Sync + 'static + Debug {
/// Get a gauge within the vector of gauges.
fn gauge(&self, labels: &[&'static str]) -> impl GaugeOps;
fn gauge(&self, labels: &[Cow<'static, str>]) -> impl GaugeOps;
}

/// A vector of histograms.
pub trait HistogramVecOps: Send + Sync + 'static + Debug {
/// Get a histogram within the vector of histograms.
fn histogram(&self, labels: &[&'static str]) -> impl HistogramOps;
fn histogram(&self, labels: &[Cow<'static, str>]) -> impl HistogramOps;
}

/// Metrics registry.
pub trait RegistryOps: Send + Sync + 'static + Debug {
/// Register a vector of counters to the registry.
fn register_counter_vec(
&self,
name: &'static str,
desc: &'static str,
name: impl Into<Cow<'static, str>>,
desc: impl Into<Cow<'static, str>>,
label_names: &'static [&'static str],
) -> impl CounterVecOps;

/// Register a vector of gauges to the registry.
fn register_gauge_vec(
&self,
name: &'static str,
desc: &'static str,
name: impl Into<Cow<'static, str>>,
desc: impl Into<Cow<'static, str>>,
label_names: &'static [&'static str],
) -> impl GaugeVecOps;

/// Register a vector of histograms to the registry.
fn register_histogram_vec(
&self,
name: &'static str,
desc: &'static str,
name: impl Into<Cow<'static, str>>,
desc: impl Into<Cow<'static, str>>,
label_names: &'static [&'static str],
) -> impl HistogramVecOps;
}
Expand Down
138 changes: 92 additions & 46 deletions foyer-common/src/metrics/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::borrow::Cow;

use super::{BoxedCounter, BoxedGauge, BoxedHistogram, GaugeVecOps, HistogramVecOps, RegistryOps};
use crate::metrics::CounterVecOps;

Expand Down Expand Up @@ -92,10 +94,12 @@ pub struct Metrics {

impl Metrics {
/// Create a new metric with the given name.
pub fn new<R>(name: &'static str, registry: &R) -> Self
pub fn new<R>(name: impl Into<Cow<'static, str>>, registry: &R) -> Self
where
R: RegistryOps,
{
let name = name.into();

/* in-memory cache metrics */

let foyer_memory_op_total = registry.register_counter_vec(
Expand All @@ -106,18 +110,20 @@ impl Metrics {
let foyer_memory_usage =
registry.register_gauge_vec("foyer_memory_usage", "foyer in-memory cache usage", &["name"]);

let memory_insert = foyer_memory_op_total.counter(&[name, "insert"]).boxed();
let memory_replace = foyer_memory_op_total.counter(&[name, "replace"]).boxed();
let memory_hit = foyer_memory_op_total.counter(&[name, "hit"]).boxed();
let memory_miss = foyer_memory_op_total.counter(&[name, "miss"]).boxed();
let memory_remove = foyer_memory_op_total.counter(&[name, "remove"]).boxed();
let memory_evict = foyer_memory_op_total.counter(&[name, "evict"]).boxed();
let memory_reinsert = foyer_memory_op_total.counter(&[name, "reinsert"]).boxed();
let memory_release = foyer_memory_op_total.counter(&[name, "release"]).boxed();
let memory_queue = foyer_memory_op_total.counter(&[name, "queue"]).boxed();
let memory_fetch = foyer_memory_op_total.counter(&[name, "fetch"]).boxed();
let memory_insert = foyer_memory_op_total.counter(&[name.clone(), "insert".into()]).boxed();
let memory_replace = foyer_memory_op_total.counter(&[name.clone(), "replace".into()]).boxed();
let memory_hit = foyer_memory_op_total.counter(&[name.clone(), "hit".into()]).boxed();
let memory_miss = foyer_memory_op_total.counter(&[name.clone(), "miss".into()]).boxed();
let memory_remove = foyer_memory_op_total.counter(&[name.clone(), "remove".into()]).boxed();
let memory_evict = foyer_memory_op_total.counter(&[name.clone(), "evict".into()]).boxed();
let memory_reinsert = foyer_memory_op_total
.counter(&[name.clone(), "reinsert".into()])
.boxed();
let memory_release = foyer_memory_op_total.counter(&[name.clone(), "release".into()]).boxed();
let memory_queue = foyer_memory_op_total.counter(&[name.clone(), "queue".into()]).boxed();
let memory_fetch = foyer_memory_op_total.counter(&[name.clone(), "fetch".into()]).boxed();

let memory_usage = foyer_memory_usage.gauge(&[name]).boxed();
let memory_usage = foyer_memory_usage.gauge(&[name.clone()]).boxed();

/* disk cache metrics */

Expand Down Expand Up @@ -170,45 +176,75 @@ impl Metrics {
&["name", "op"],
);

let storage_enqueue = foyer_storage_op_total.counter(&[name, "enqueue"]).boxed();
let storage_hit = foyer_storage_op_total.counter(&[name, "hit"]).boxed();
let storage_miss = foyer_storage_op_total.counter(&[name, "miss"]).boxed();
let storage_delete = foyer_storage_op_total.counter(&[name, "delete"]).boxed();
let storage_enqueue = foyer_storage_op_total
.counter(&[name.clone(), "enqueue".into()])
.boxed();
let storage_hit = foyer_storage_op_total.counter(&[name.clone(), "hit".into()]).boxed();
let storage_miss = foyer_storage_op_total.counter(&[name.clone(), "miss".into()]).boxed();
let storage_delete = foyer_storage_op_total.counter(&[name.clone(), "delete".into()]).boxed();

let storage_enqueue_duration = foyer_storage_op_duration.histogram(&[name, "enqueue"]).boxed();
let storage_hit_duration = foyer_storage_op_duration.histogram(&[name, "hit"]).boxed();
let storage_miss_duration = foyer_storage_op_duration.histogram(&[name, "miss"]).boxed();
let storage_delete_duration = foyer_storage_op_duration.histogram(&[name, "delete"]).boxed();
let storage_enqueue_duration = foyer_storage_op_duration
.histogram(&[name.clone(), "enqueue".into()])
.boxed();
let storage_hit_duration = foyer_storage_op_duration
.histogram(&[name.clone(), "hit".into()])
.boxed();
let storage_miss_duration = foyer_storage_op_duration
.histogram(&[name.clone(), "miss".into()])
.boxed();
let storage_delete_duration = foyer_storage_op_duration
.histogram(&[name.clone(), "delete".into()])
.boxed();

let storage_queue_rotate = foyer_storage_inner_op_total.counter(&[name, "queue_rotate"]).boxed();
let storage_queue_drop = foyer_storage_inner_op_total.counter(&[name, "queue_drop"]).boxed();
let storage_queue_rotate = foyer_storage_inner_op_total
.counter(&[name.clone(), "queue_rotate".into()])
.boxed();
let storage_queue_drop = foyer_storage_inner_op_total
.counter(&[name.clone(), "queue_drop".into()])
.boxed();

let storage_queue_rotate_duration = foyer_storage_inner_op_duration
.histogram(&[name, "queue_rotate"])
.histogram(&[name.clone(), "queue_rotate".into()])
.boxed();

let storage_disk_write = foyer_storage_disk_io_total.counter(&[name, "write"]).boxed();
let storage_disk_read = foyer_storage_disk_io_total.counter(&[name, "read"]).boxed();
let storage_disk_flush = foyer_storage_disk_io_total.counter(&[name, "flush"]).boxed();
let storage_disk_write = foyer_storage_disk_io_total
.counter(&[name.clone(), "write".into()])
.boxed();
let storage_disk_read = foyer_storage_disk_io_total
.counter(&[name.clone(), "read".into()])
.boxed();
let storage_disk_flush = foyer_storage_disk_io_total
.counter(&[name.clone(), "flush".into()])
.boxed();

let storage_disk_write_bytes = foyer_storage_disk_io_bytes.counter(&[name, "write"]).boxed();
let storage_disk_read_bytes = foyer_storage_disk_io_bytes.counter(&[name, "read"]).boxed();
let storage_disk_write_bytes = foyer_storage_disk_io_bytes
.counter(&[name.clone(), "write".into()])
.boxed();
let storage_disk_read_bytes = foyer_storage_disk_io_bytes
.counter(&[name.clone(), "read".into()])
.boxed();

let storage_disk_write_duration = foyer_storage_disk_io_duration.histogram(&[name, "write"]).boxed();
let storage_disk_read_duration = foyer_storage_disk_io_duration.histogram(&[name, "read"]).boxed();
let storage_disk_flush_duration = foyer_storage_disk_io_duration.histogram(&[name, "flush"]).boxed();
let storage_disk_write_duration = foyer_storage_disk_io_duration
.histogram(&[name.clone(), "write".into()])
.boxed();
let storage_disk_read_duration = foyer_storage_disk_io_duration
.histogram(&[name.clone(), "read".into()])
.boxed();
let storage_disk_flush_duration = foyer_storage_disk_io_duration
.histogram(&[name.clone(), "flush".into()])
.boxed();

let storage_region_total = foyer_storage_region.gauge(&[name, "total"]).boxed();
let storage_region_clean = foyer_storage_region.gauge(&[name, "clean"]).boxed();
let storage_region_evictable = foyer_storage_region.gauge(&[name, "evictable"]).boxed();
let storage_region_total = foyer_storage_region.gauge(&[name.clone(), "total".into()]).boxed();
let storage_region_clean = foyer_storage_region.gauge(&[name.clone(), "clean".into()]).boxed();
let storage_region_evictable = foyer_storage_region.gauge(&[name.clone(), "evictable".into()]).boxed();

let storage_region_size_bytes = foyer_storage_region_size_bytes.gauge(&[name]).boxed();
let storage_region_size_bytes = foyer_storage_region_size_bytes.gauge(&[name.clone()]).boxed();

let storage_entry_serialize_duration = foyer_storage_entry_serde_duration
.histogram(&[name, "serialize"])
.histogram(&[name.clone(), "serialize".into()])
.boxed();
let storage_entry_deserialize_duration = foyer_storage_entry_serde_duration
.histogram(&[name, "deserialize"])
.histogram(&[name.clone(), "deserialize".into()])
.boxed();

/* hybrid cache metrics */
Expand All @@ -224,16 +260,26 @@ impl Metrics {
&["name", "op"],
);

let hybrid_insert = foyer_hybrid_op_total.counter(&[name, "insert"]).boxed();
let hybrid_hit = foyer_hybrid_op_total.counter(&[name, "hit"]).boxed();
let hybrid_miss = foyer_hybrid_op_total.counter(&[name, "miss"]).boxed();
let hybrid_remove = foyer_hybrid_op_total.counter(&[name, "remove"]).boxed();
let hybrid_insert = foyer_hybrid_op_total.counter(&[name.clone(), "insert".into()]).boxed();
let hybrid_hit = foyer_hybrid_op_total.counter(&[name.clone(), "hit".into()]).boxed();
let hybrid_miss = foyer_hybrid_op_total.counter(&[name.clone(), "miss".into()]).boxed();
let hybrid_remove = foyer_hybrid_op_total.counter(&[name.clone(), "remove".into()]).boxed();

let hybrid_insert_duration = foyer_hybrid_op_duration.histogram(&[name, "insert"]).boxed();
let hybrid_hit_duration = foyer_hybrid_op_duration.histogram(&[name, "hit"]).boxed();
let hybrid_miss_duration = foyer_hybrid_op_duration.histogram(&[name, "miss"]).boxed();
let hybrid_remove_duration = foyer_hybrid_op_duration.histogram(&[name, "remove"]).boxed();
let hybrid_fetch_duration = foyer_hybrid_op_duration.histogram(&[name, "fetch"]).boxed();
let hybrid_insert_duration = foyer_hybrid_op_duration
.histogram(&[name.clone(), "insert".into()])
.boxed();
let hybrid_hit_duration = foyer_hybrid_op_duration
.histogram(&[name.clone(), "hit".into()])
.boxed();
let hybrid_miss_duration = foyer_hybrid_op_duration
.histogram(&[name.clone(), "miss".into()])
.boxed();
let hybrid_remove_duration = foyer_hybrid_op_duration
.histogram(&[name.clone(), "remove".into()])
.boxed();
let hybrid_fetch_duration = foyer_hybrid_op_duration
.histogram(&[name.clone(), "fetch".into()])
.boxed();

Self {
memory_insert,
Expand Down
32 changes: 22 additions & 10 deletions foyer-common/src/metrics/registry/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::borrow::Cow;

use crate::metrics::{CounterOps, CounterVecOps, GaugeOps, GaugeVecOps, HistogramOps, HistogramVecOps, RegistryOps};

/// Noop metrics placeholder.
Expand All @@ -23,7 +25,7 @@ impl CounterOps for NoopMetricsRegistry {
}

impl CounterVecOps for NoopMetricsRegistry {
fn counter(&self, _: &[&'static str]) -> impl CounterOps {
fn counter(&self, _: &[Cow<'static, str>]) -> impl CounterOps {
NoopMetricsRegistry
}
}
Expand All @@ -37,7 +39,7 @@ impl GaugeOps for NoopMetricsRegistry {
}

impl GaugeVecOps for NoopMetricsRegistry {
fn gauge(&self, _: &[&'static str]) -> impl GaugeOps {
fn gauge(&self, _: &[Cow<'static, str>]) -> impl GaugeOps {
NoopMetricsRegistry
}
}
Expand All @@ -47,24 +49,34 @@ impl HistogramOps for NoopMetricsRegistry {
}

impl HistogramVecOps for NoopMetricsRegistry {
fn histogram(&self, _: &[&'static str]) -> impl HistogramOps {
fn histogram(&self, _: &[Cow<'static, str>]) -> impl HistogramOps {
NoopMetricsRegistry
}
}

impl RegistryOps for NoopMetricsRegistry {
fn register_counter_vec(&self, _: &'static str, _: &'static str, _: &'static [&'static str]) -> impl CounterVecOps {
fn register_counter_vec(
&self,
_: impl Into<Cow<'static, str>>,
_: impl Into<Cow<'static, str>>,
_: &'static [&'static str],
) -> impl CounterVecOps {
NoopMetricsRegistry
}

fn register_gauge_vec(&self, _: &'static str, _: &'static str, _: &'static [&'static str]) -> impl GaugeVecOps {
fn register_gauge_vec(
&self,
_: impl Into<Cow<'static, str>>,
_: impl Into<Cow<'static, str>>,
_: &'static [&'static str],
) -> impl GaugeVecOps {
NoopMetricsRegistry
}

fn register_histogram_vec(
&self,
_: &'static str,
_: &'static str,
_: impl Into<Cow<'static, str>>,
_: impl Into<Cow<'static, str>>,
_: &'static [&'static str],
) -> impl HistogramVecOps {
NoopMetricsRegistry
Expand All @@ -80,17 +92,17 @@ mod tests {
let noop = NoopMetricsRegistry;

let cv = noop.register_counter_vec("test_counter_1", "test counter 1", &["label1", "label2"]);
let c = cv.counter(&["l1", "l2"]);
let c = cv.counter(&["l1".into(), "l2".into()]);
c.increase(42);

let gv = noop.register_gauge_vec("test_gauge_1", "test gauge 1", &["label1", "label2"]);
let g = gv.gauge(&["l1", "l2"]);
let g = gv.gauge(&["l1".into(), "l2".into()]);
g.increase(514);
g.decrease(114);
g.absolute(114514);

let hv = noop.register_histogram_vec("test_histogram_1", "test histogram 1", &["label1", "label2"]);
let h = hv.histogram(&["l1", "l2"]);
let h = hv.histogram(&["l1".into(), "l2".into()]);
h.record(114.514);
}
}
Loading

0 comments on commit 7f5909b

Please sign in to comment.