diff --git a/Cargo.lock b/Cargo.lock index 550e2e78..3ab74856 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -887,6 +887,7 @@ dependencies = [ "serde_json", "serde_yaml", "sha-1", + "strum", "thiserror", "thread_local", "tikv-jemallocator", @@ -2790,6 +2791,28 @@ dependencies = [ "der", ] +[[package]] +name = "strum" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d8cec3501a5194c432b2b7976db6b7d10ec95c253208b45f83f7136aa985e29" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6cf59daf282c0a494ba14fd21610a0325f9f90ec9d1231dea26bcb1d696c946" +dependencies = [ + "heck 0.4.1", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + [[package]] name = "subslice" version = "0.2.3" diff --git a/Cargo.toml b/Cargo.toml index 3ab71e30..9fb3466a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -129,6 +129,7 @@ serde_html_form = "0.2.6" serde_json = { version = "1.0.117", features = ["raw_value"] } serde_yaml = "0.9.34" sha-1 = "0.10.1" +strum = { version = "0.26.2", features = ["derive"] } thiserror = "1.0.61" thread_local = "1.1.8" tikv-jemallocator = { version = "0.5.4", features = ["unprefixed_malloc_on_supported_platforms"], optional = true } diff --git a/src/observability.rs b/src/observability.rs index 4e4cec0a..820487d5 100644 --- a/src/observability.rs +++ b/src/observability.rs @@ -18,6 +18,7 @@ use opentelemetry_sdk::{ metrics::{new_view, Aggregation, Instrument, SdkMeterProvider, Stream}, Resource, }; +use strum::AsRefStr; use tokio::time::Instant; use tracing_flame::{FlameLayer, FlushGuard}; use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Layer, Registry}; @@ -41,7 +42,7 @@ impl Drop for Guard { } /// Type to record cache performance in a tracing span field. -#[derive(Clone, Copy)] +#[derive(Clone, Copy, AsRefStr)] pub(crate) enum FoundIn { /// Found in cache Cache, @@ -55,20 +56,10 @@ pub(crate) enum FoundIn { } impl FoundIn { - /// Returns a stringified representation of the current value - fn as_str(self) -> &'static str { - match self { - FoundIn::Cache => "Cache", - FoundIn::Database => "Database", - FoundIn::Remote => "Remote", - FoundIn::Nothing => "Nothing", - } - } - /// Record the current value to the current [`tracing::Span`] // TODO: use tracing::Value instead if it ever becomes accessible pub(crate) fn record(self, field: &str) { - tracing::Span::current().record(field, self.as_str()); + tracing::Span::current().record(field, self.as_ref()); } }