Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database: add tracing #125

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/database"
keywords = ["cuprate", "database"]

[features]
# default = ["heed", "redb", "service", "tracing"]
default = ["heed", "redb", "service"]
# default = ["redb", "service"]
# default = ["redb-memory", "service"]
Expand Down Expand Up @@ -44,9 +45,10 @@ thread_local = { workspace = true }
rayon = { workspace = true, optional = true }

# Optional features.
heed = { version = "0.20.0", features = ["read-txn-no-tls"], optional = true }
redb = { version = "2.1.0", optional = true }
serde = { workspace = true, optional = true }
heed = { version = "0.20.0", features = ["read-txn-no-tls"], optional = true }
redb = { version = "2.1.0", optional = true }
serde = { workspace = true, optional = true }
tracing = { workspace = true, optional = true }

[dev-dependencies]
bytemuck = { version = "1.14.3", features = ["must_cast", "derive", "min_const_generics", "extern_crate_alloc"] }
Expand Down
1 change: 1 addition & 0 deletions database/src/backend/heed/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const PANIC_MSG_MISSING_TABLE: &str =

//---------------------------------------------------------------------------------------------------- ConcreteEnv
/// A strongly typed, concrete database environment, backed by `heed`.
#[derive(Debug)]
pub struct ConcreteEnv {
/// The actual database environment.
///
Expand Down
1 change: 1 addition & 0 deletions database/src/backend/redb/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{

//---------------------------------------------------------------------------------------------------- ConcreteEnv
/// A strongly typed, concrete database environment, backed by `redb`.
#[derive(Debug)]
pub struct ConcreteEnv {
/// The actual database environment.
env: redb::Database,
Expand Down
8 changes: 6 additions & 2 deletions database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
//!
//! `ConcreteEnv` invariants you can rely on:
//! - It implements [`Env`]
//! - It implements [`Debug`]
//! - Upon [`Drop::drop`], all database data will sync to disk
//!
//! Note that `ConcreteEnv` itself is not a clonable type,
Expand All @@ -78,8 +79,9 @@
//!
//! The default is `heed`.
//!
//! `tracing` is always enabled and cannot be disabled via feature-flag.
//! <!-- FIXME: tracing should be behind a feature flag -->
//! Logging with [`tracing`](https://docs.rs/tracing)
//! can be enabled with the `tracing` feature flag,
//! this is disabled by default.
//!
//! # Invariants when not using `service`
//! `cuprate_database` can be used without the `service` feature enabled but
Expand Down Expand Up @@ -274,6 +276,8 @@ pub mod resize;
mod key;
pub use key::Key;

mod macros;

mod storable;
pub use storable::{Storable, StorableBytes, StorableVec};

Expand Down
155 changes: 155 additions & 0 deletions database/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
//! General macros.

//---------------------------------------------------------------------------------------------------- Logging
// The below macros/types/functions allow for conditional
// logging with `tracing` depending on if the feature flag
// is enabled or not.
//
// These macros (e.g. [`info2`]) are used instead of the tracing macro directly.
// #[tracing::instrument] will still need to be handled by us by using `#[cfg_attr]`.

/// Dummy structure with methods that copy the ones on [`tracing::Span`].
///
/// This is used when "tracing" is disabled such that span functions:
/// ```rust,ignore
/// let span: DummySpan = info_span2!("span");
/// let _guard = span.enter();
/// ```
/// will still work as this struct will just be returned, then
/// [`DummySpan::enter`] will be called instead, which is just a dummy method.
pub(crate) struct DummySpan;

impl DummySpan {
/// Emulates [`tracing::Span::enter`].
#[inline]
pub(crate) const fn enter(&self) -> &Self {
self
}
}

/// Drop impl is needed to satisfy clippy.
impl Drop for DummySpan {
#[inline]
fn drop(&mut self) {}
}

/// [`tracing::warn_span`].
macro_rules! warn_span2 {
($($token:tt)*) => {{
cfg_if::cfg_if! {
if #[cfg(feature = "tracing")] {
tracing::warn_span!($($token)*)
} else {
$crate::macros::DummySpan
}
}
}};
}
pub(crate) use warn_span2;

/// [`tracing::warn`].
macro_rules! warn2 {
($($token:tt)*) => {
#[cfg(feature = "tracing")]
tracing::warn!($($token)*)
};
}
pub(crate) use warn2;

/// [`tracing::trace_span`].
macro_rules! trace_span2 {
($($token:tt)*) => {{
cfg_if::cfg_if! {
if #[cfg(feature = "tracing")] {
tracing::trace_span!($($token)*)
} else {
$crate::macros::DummySpan
}
}
}};
}
pub(crate) use trace_span2;

/// [`tracing::trace`].
macro_rules! trace2 {
($($token:tt)*) => {
#[cfg(feature = "tracing")]
tracing::trace!($($token)*)
};
}
pub(crate) use trace2;

/// [`tracing::info_span`].
macro_rules! info_span2 {
($($token:tt)*) => {{
cfg_if::cfg_if! {
if #[cfg(feature = "tracing")] {
tracing::info_span!($($token)*)
} else {
$crate::macros::DummySpan
}
}
}};
}
pub(crate) use info_span2;

/// [`tracing::info`].
macro_rules! info2 {
($($token:tt)*) => {
#[cfg(feature = "tracing")]
tracing::info!($($token)*)
};
}
pub(crate) use info2;

/// [`tracing::error_span`].
macro_rules! error_span2 {
($($token:tt)*) => {{
cfg_if::cfg_if! {
if #[cfg(feature = "tracing")] {
tracing::error_span!($($token)*)
} else {
$crate::macros::DummySpan
}
}
}};
}
pub(crate) use error_span2;

/// [`tracing::error`].
macro_rules! error2 {
($($token:tt)*) => {
#[cfg(feature = "tracing")]
tracing::error!($($token)*)
};
}
pub(crate) use error2;

/// [`tracing::debug_span`].
macro_rules! debug_span2 {
($($token:tt)*) => {{
cfg_if::cfg_if! {
if #[cfg(feature = "tracing")] {
tracing::debug_span!($($token)*)
} else {
$crate::macros::DummySpan
}
}
}};
}
pub(crate) use debug_span2;

/// [`tracing::debug`].
macro_rules! debug2 {
($($token:tt)*) => {
#[cfg(feature = "tracing")]
tracing::debug!($($token)*)
};
}
pub(crate) use debug2;

//---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)]
mod test {
// use super::*;
}
Loading