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

[REVIEW TO PROCESS] Add Counted Macro #99

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

manuelgdlvh
Copy link

@manuelgdlvh manuelgdlvh commented Aug 2, 2024

Moved and refined from pull request: open-telemetry/opentelemetry-rust#1972

Changes

I am developing a macros crate designed to streamline the generation of observability-related code across various types of metrics and logging functionalities. The initial focus of this crate is on implementing a basic counter, but our goal is to support a wide range of observability features

The crate leverages Rust's powerful attribute macros to simplify the configuration and integration process. By using these attribute macros, developers can avoid repetitive boilerplate code and configure observability tools with maximum flexibility. This approach ensures that the observability code remains concise, readable, and maintainable, allowing developers to focus more on their core application logic.

Key benefits of this macros crate include:

Reduction of Boilerplate Code: By abstracting common patterns and setups into macros, we significantly reduce the amount of boilerplate code developers need to write and maintain.

Consistency: The macros enforce consistent implementation of observability features across different parts of the codebase, reducing the likelihood of errors and inconsistencies.

Flexibility: The attribute macros are designed to be highly configurable, allowing developers to customize the generated code to fit their specific needs without sacrificing simplicity.

Current output result of macro:

    #[counted(
        name = "redis_hits",
        meter_provider = "meter_provider",
        description = "redis cache hits",
        labels = "key, value"
    )]
    pub fn test() {
    }
static LABELS: std::sync::OnceLock<[opentelemetry_contrib::opentelemetry::KeyValue; 1], > = std::sync::OnceLock::new();
        let labels_value = LABELS
            .get_or_init(|| {
                [opentelemetry_contrib::opentelemetry::KeyValue::new("key", "value")]
            });

static COUNTER: std::sync::OnceLock<opentelemetry_contrib::opentelemetry::metrics::Counter<u64>,> = std::sync::OnceLock::new();
         let counter_value = COUNTER
            .get_or_init(|| {
                let meter = opentelemetry_contrib::opentelemetry::global::meter(
                    "meter_provider",
                );
                meter
                    .u64_counter("redis_hits")
                    .with_description("redis cache hits")
                    .init()
            });
counter_value.add(1, labels_value);

Design:
counted_macro_design

Hi @cijothomas ,
Regarding the performance issue in the generation of KeyValue that you mentioned, the continuous creation is currently mitigated by using static variables. Another way to mitigate it could be an enabled attribute that prevents the generation of this code, but it would not be at runtime.

Any improvements you see that could be introduced or features to inaugurate this crate, which would contain the other possible macros, let me know and as soon as everything is ready, I will start with the documentation, testing, and other phases.

UPDATE:
Using enabled attribute to false:

    #[counted(
        name = "redis_hits",
        meter_provider = "meter_provider",
        description = "redis cache hits",
        labels = "key, value",
        enabled = false
    )]
    pub fn test() {

    }

Output:

 pub fn test() {}

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

@manuelgdlvh manuelgdlvh requested a review from a team August 2, 2024 17:38
@manuelgdlvh manuelgdlvh changed the title [WIP] Add Counted Macro [REVIEW TO PROCESS] Add Counted Macro Aug 15, 2024
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

Successfully merging this pull request may close these issues.

1 participant