autometrics-rs icon indicating copy to clipboard operation
autometrics-rs copied to clipboard

Document conditional compilation use-cases

Open gagbo opened this issue 2 years ago • 0 comments

I think some of the feedback we got was to know whether it is possible to conditionally compile autometrics, and maybe we could add examples of this in the doc; at least what I think would solve the issue: the cfg_attr attribute macro.

Instrumenting only debug builds

#[cfg_attr(debug_assertions, autometrics::autometrics)]
fn foo() -> Result<(), String> {
}

Optionally instrumenting on a feature flag

# In Cargo.toml

[features]
metrics = [ "autometrics" ]

[dependencies] 
autometrics = { version = "0.6", optional = true }
#[cfg_attr(feature = "metrics", autometrics::autometrics)]
fn foo() -> Result<(), String> {
}

Mixing and matching

# In Cargo.toml

[features]
metrics = [ "autometrics" ]

[dependencies] 
autometrics = { version = "0.6", optional = true }
#[cfg_attr(all(debug_assertions, feature = "metrics"), autometrics::autometrics)]
fn foo_instrumented_on_debug_only() -> Result<(), String> {
}

#[cfg_attr(all(not(debug_assertions), feature = "metrics"), autometrics::autometrics)]
fn foo_instrumented_on_prod_only() -> Result<(), String> {
}

#[cfg_attr(feature = "metrics", autometrics::autometrics)]
fn foo_all_the_time() -> Result<(), String> {
}

gagbo avatar Sep 29 '23 15:09 gagbo