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

Document conditional compilation use-cases #143

Open
gagbo opened this issue Sep 29, 2023 · 0 comments
Open

Document conditional compilation use-cases #143

gagbo opened this issue Sep 29, 2023 · 0 comments

Comments

@gagbo
Copy link
Member

gagbo commented Sep 29, 2023

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> {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant