Skip to content

Document conditional compilation use-cases #143

Description

@gagbo

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> {
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions