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

Introduce an observability crate using opentelemetry-rust #2535

Merged
merged 25 commits into from
Aug 8, 2024

Conversation

hlts2
Copy link
Collaborator

@hlts2 hlts2 commented Jul 24, 2024

Description

I Introduced an observability crate using opentelemetry-rust.

Important aspects of design

Since opentelemetry-rust is still under development, interface changes may occur in the future.
Therefore, this crate is designed to make the user as unaware of opentelemetry-rust as possible.
If the interface is changed in the future, only the macro needs to be modified.

  • The following example is the generation of the trace.
fn some_function(ctx: &opentelemetry::Context) {
    let ctx = ctx_span!(ctx, "some_function");
    defer!(ctx.span().end());

    trace_child(&ctx, i);
}

fn child_function(ctx: &opentelemetry::Context, i: i32) {
    let ctx = ctx_span!(ctx, "child_function");
    defer!(ctx.span().end());

    ctx.span()
        .set_attribute(KeyValue::new("id", "test")));
}
  • The following example is the generation of the metrics.
# sync
let conter = instrument!(
    InstrumentKind::Counter,
    i64,
    "agent_core_ngt_insert_vqueue_count",
    "Agent NGT insert vqueue count",
    Unit::new("1"),
);
counter.add(11.0, &[]); 


# async
instrument!(
    InstrumentKind::Counter,
    i64,
    "agent_core_ngt_insert_vqueue_count",
    "Agent NGT insert vqueue count",
    Unit::new("1"),
    agent.index_count,
    KeyValue::new("key-1", "value-1"),
);
  • The following example is the initialization of the observability object.
fn test() {
    let cfg = Config::new()
        .enabled(true)
        .attribute(SERVICE_NAME, "vald-lb-gateway")
        .attribute("target_pod", "target_pod")
        .attribute("target_node", "target_node")
        .attribute("exported_kubernetes_namespace", "default")
        .attribute("kubernetes_name", "vald-lb-gateway")
        .endpoint("http://localhost:2222/")
        .meter(
             Meter::new()
                 .enabled(true)
                 .export_timeout_duration(Duration::from_secs(10)),
         )
        .tracer(Tracer::new().enabled(true));
    let observe = ObservabilityImpl::new(cfg).unwrap();
    // observe.shutdown().unwrap();
}

Related Issue

Versions

  • Go Version: 1.22.5
  • Rust Version: 1.77.2
  • Docker Version: 20.10.8
  • Kubernetes Version: v1.30.3
  • NGT Version: 2.2.3

Checklist

Special notes for your reviewer

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced a new observability package for enhanced monitoring and tracing in Rust applications.
    • Added configuration capabilities for observability features, enabling easy setup for metrics and tracing.
    • Implemented macros for streamlined telemetry span and metrics management.
    • Established a structured approach for managing observability components, including initialization and graceful shutdown processes.
  • Documentation

    • Enhanced overall project organization with a new module structure for observability functionalities.

Copy link
Contributor

coderabbitai bot commented Jul 24, 2024

Important

Review skipped

Review was skipped due to path filters

Files ignored due to path filters (1)
  • rust/Cargo.lock is excluded by !**/*.lock

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

Walkthrough

This update introduces a new observability library to a Rust workspace, enhancing monitoring and tracing capabilities. Key additions include a Cargo.toml for the observability package, configuration structures for observability settings, macros for telemetry management, and core implementations for handling OpenTelemetry. The workspace members have been reorganized to prioritize observability features, facilitating seamless integration and improved performance tracking in applications.

Changes

Files Change Summary
rust/Cargo.toml Updated workspace members to include libs/observability, reordering existing members.
rust/libs/observability/Cargo.toml Introduced Cargo.toml defining metadata and dependencies for the new observability package.
rust/libs/observability/src/config.rs Created configuration structs (Config, Tracer, Meter) with methods for setting observability options.
rust/libs/observability/src/lib.rs Established module structure with public modules: config, macros, observability.
rust/libs/observability/src/macros.rs Added macros for telemetry management, such as tracer!, ctx_span!, and meter!.
rust/libs/observability/src/observability.rs Defined the Observability trait and its implementation for managing OpenTelemetry components.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application
    participant Config as Config
    participant Tracer as Tracer Provider
    participant Meter as Meter Provider
    participant Observability as Observability Manager

    App->>Config: Create configuration
    Config->>Observability: Build observability
    Observability->>Tracer: Initialize tracer if enabled
    Observability->>Meter: Initialize meter if enabled
    Observability->>App: Ready for telemetry
    App->>Observability: Shutdown observability
    Observability->>Tracer: Shutdown tracer
    Observability->>Meter: Shutdown meter
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@hlts2 hlts2 changed the title [WIP]: Introduce observability crate using opentelemetry-rust [WIP]: Introduce an observability crate using opentelemetry-rust Jul 24, 2024
@vdaas-ci
Copy link
Collaborator

[CHATOPS:HELP] ChatOps commands.

  • 🙆‍♀️ /approve - approve
  • 🍱 /format - format codes and add licenses
  • /gen-test - generate test codes
  • 🏷️ /label - add labels
  • 🔚 2️⃣ 🔚 /label actions/e2e-deploy - run E2E deploy & integration test

Signed-off-by: hlts2 <[email protected]>
Copy link

cloudflare-workers-and-pages bot commented Jul 24, 2024

Deploying vald with  Cloudflare Pages  Cloudflare Pages

Latest commit: fcde1cf
Status: ✅  Deploy successful!
Preview URL: https://13e87b74.vald.pages.dev
Branch Preview URL: https://feat-add-otel-rust.vald.pages.dev

View logs

@hlts2 hlts2 changed the title [WIP]: Introduce an observability crate using opentelemetry-rust Introduce an observability crate using opentelemetry-rust Jul 25, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between caa8f03 and b7983bc.

Files ignored due to path filters (1)
  • rust/Cargo.lock is excluded by !**/*.lock
Files selected for processing (6)
  • rust/Cargo.toml (1 hunks)
  • rust/libs/observability/Cargo.toml (1 hunks)
  • rust/libs/observability/src/config.rs (1 hunks)
  • rust/libs/observability/src/lib.rs (1 hunks)
  • rust/libs/observability/src/macros.rs (1 hunks)
  • rust/libs/observability/src/observability.rs (1 hunks)
Files skipped from review due to trivial changes (3)
  • rust/Cargo.toml
  • rust/libs/observability/Cargo.toml
  • rust/libs/observability/src/lib.rs
Additional comments not posted (18)
rust/libs/observability/src/config.rs (9)

22-27: LGTM!

The Config struct is well-defined with fields for enabling observability, attributes, tracer, and meter.


43-77: LGTM!

The Config struct methods use the builder pattern effectively for configuration.


79-93: LGTM!

The Default implementation for Config is straightforward, and the From<&Config> for Resource implementation correctly converts config attributes to OpenTelemetry resources.


30-34: LGTM!

The Tracer struct is well-defined with fields for enabling tracing and specifying the endpoint.


96-107: LGTM!

The Tracer struct methods use the builder pattern effectively for configuration.


110-114: LGTM!

The Default implementation for Tracer is straightforward.


36-41: LGTM!

The Meter struct is well-defined with fields for enabling metrics, specifying the endpoint, and export duration.


116-133: LGTM!

The Meter struct methods use the builder pattern effectively for configuration.


136-140: LGTM!

The Default implementation for Meter is straightforward.

rust/libs/observability/src/observability.rs (5)

28-31: LGTM!

The Observability trait is well-defined with methods for building and shutting down observability.


33-37: LGTM!

The ObservabilityImpl struct is well-defined with fields for config, meter provider, and tracer provider.


39-50: LGTM!

The ObservabilityImpl struct methods for accessing config are well-structured.


53-92: LGTM!

The build method initializes the meter and tracer providers based on the configuration.


95-116: LGTM!

The shutdown method ensures proper shutdown of the meter and tracer providers.

rust/libs/observability/src/macros.rs (4)

17-24: LGTM!

The tracer macro is well-defined and provides a convenient way to obtain a tracer instance.


28-41: LGTM!

The ctx_span macro is well-defined and provides a convenient way to create a span within a given context.


28-41: LGTM!

The meter macro is well-defined and provides a convenient way to obtain a meter instance.


63-149: LGTM!

The instrument macro is well-defined and provides a convenient way to create various types of instruments.

coderabbitai[bot]
coderabbitai bot previously approved these changes Jul 26, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b7983bc and 4d0a0ce.

Files selected for processing (1)
  • rust/libs/observability/src/observability.rs (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • rust/libs/observability/src/observability.rs

coderabbitai[bot]
coderabbitai bot previously approved these changes Aug 1, 2024
Signed-off-by: hlts2 <[email protected]>
coderabbitai[bot]
coderabbitai bot previously approved these changes Aug 1, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e32bf43 and 373cafc.

Files ignored due to path filters (1)
  • rust/Cargo.lock is excluded by !**/*.lock
Files selected for processing (1)
  • rust/libs/observability/src/config.rs (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • rust/libs/observability/src/config.rs

coderabbitai[bot]
coderabbitai bot previously approved these changes Aug 6, 2024
@hlts2 hlts2 self-assigned this Aug 7, 2024
@hlts2 hlts2 requested review from kmrmt, kpango and datelier August 7, 2024 09:05
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 373cafc and 9d79e74.

Files selected for processing (1)
  • rust/libs/observability/src/observability.rs (1 hunks)
Additional comments not posted (7)
rust/libs/observability/src/observability.rs (7)

1-15: LGTM! License header is correct.

The license header is consistent with the Apache License, Version 2.0.


16-24: LGTM! Import statements are appropriate.

The import statements include necessary modules and seem appropriate for the functionality provided.


27-27: LGTM! Constant declaration is appropriate.

The constant SERVICE_NAME is declared using opentelemetry_semantic_conventions and seems appropriate.


29-31: LGTM! Trait definition is appropriate.

The Observability trait includes a shutdown method, which is essential for graceful shutdown.


33-37: LGTM! Struct definition is appropriate.

The ObservabilityImpl struct includes fields for configuration, meter provider, and tracer provider, which are necessary for observability.


101-123: LGTM! Method implementation is appropriate.

The shutdown method correctly shuts down meter and tracer providers if enabled.


39-94: LGTM! Method implementation is appropriate.

The new method initializes the ObservabilityImpl struct and sets up meter and tracer providers if enabled.

However, ensure that the config method is correctly used in the codebase.

Verification successful

The config method is correctly defined and used.

The config method is implemented in the rust/libs/observability/src/observability.rs file and returns a reference to the Config struct. It is used within the same file to access configuration values.

  • rust/libs/observability/src/observability.rs: Definition and usage of the config method.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the `config` method in the codebase.

# Test: Search for the method usage. Expect: Correct usage of the method.
rg --type rust -A 5 $'fn config(&self) -> &Config'

Length of output: 51


Script:

#!/bin/bash
# Description: Verify the definition and usage of the `config` method in the codebase.

# Search for the definition of the `config` method.
rg --type rust 'fn config(&self) -> &Config'

# Search for the usage of the `config` method.
rg --type rust '.config()'

Length of output: 15831

@hlts2
Copy link
Collaborator Author

hlts2 commented Aug 8, 2024

@kmrmt @kpango
Thank you for your review!
I will merge this PR 🚀

@hlts2 hlts2 merged commit 0aca9d1 into main Aug 8, 2024
57 checks passed
@hlts2 hlts2 deleted the feat/add-otel-rust branch August 8, 2024 07:50
hlts2 added a commit that referenced this pull request Sep 11, 2024
* feat: add observability crate

Signed-off-by: hlts2 <[email protected]>

* fix: fix crate name

Signed-off-by: hlts2 <[email protected]>

* feat: add module files

Signed-off-by: hlts2 <[email protected]>

* feat: create config

Signed-off-by: hlts2 <[email protected]>

* fix: implement from trait

Signed-off-by: hlts2 <[email protected]>

* feat: add tracer and meter macro

Signed-off-by: hlts2 <[email protected]>

* feat: add license

Signed-off-by: hlts2 <[email protected]>

* fix: use full module path

Signed-off-by: hlts2 <[email protected]>

* fix: add shutdown method

Signed-off-by: hlts2 <[email protected]>

* feat: add build method to crate tracer provider and meter provider

Signed-off-by: hlts2 <[email protected]>

* fix: module path

Signed-off-by: hlts2 <[email protected]>

* fix: add export timeout duration and comment

Signed-off-by: hlts2 <[email protected]>

* feat: update interface

Signed-off-by: hlts2 <[email protected]>

* feat: add endpint configuration to root configuration

Signed-off-by: hlts2 <[email protected]>

* fix: update deps

Signed-off-by: hlts2 <[email protected]>

* fix: small refactor

Signed-off-by: hlts2 <[email protected]>

* fix: deleted build method to create trace and metrics provider

Signed-off-by: hlts2 <[email protected]>

---------

Signed-off-by: hlts2 <[email protected]>
hlts2 added a commit that referenced this pull request Sep 11, 2024
* feat: add observability crate

Signed-off-by: hlts2 <[email protected]>

* fix: fix crate name

Signed-off-by: hlts2 <[email protected]>

* feat: add module files

Signed-off-by: hlts2 <[email protected]>

* feat: create config

Signed-off-by: hlts2 <[email protected]>

* fix: implement from trait

Signed-off-by: hlts2 <[email protected]>

* feat: add tracer and meter macro

Signed-off-by: hlts2 <[email protected]>

* feat: add license

Signed-off-by: hlts2 <[email protected]>

* fix: use full module path

Signed-off-by: hlts2 <[email protected]>

* fix: add shutdown method

Signed-off-by: hlts2 <[email protected]>

* feat: add build method to crate tracer provider and meter provider

Signed-off-by: hlts2 <[email protected]>

* fix: module path

Signed-off-by: hlts2 <[email protected]>

* fix: add export timeout duration and comment

Signed-off-by: hlts2 <[email protected]>

* feat: update interface

Signed-off-by: hlts2 <[email protected]>

* feat: add endpint configuration to root configuration

Signed-off-by: hlts2 <[email protected]>

* fix: update deps

Signed-off-by: hlts2 <[email protected]>

* fix: small refactor

Signed-off-by: hlts2 <[email protected]>

* fix: deleted build method to create trace and metrics provider

Signed-off-by: hlts2 <[email protected]>

---------

Signed-off-by: hlts2 <[email protected]>
vankichi pushed a commit that referenced this pull request Sep 11, 2024
)

* feat: add observability crate



* fix: fix crate name



* feat: add module files



* feat: create config



* fix: implement from trait



* feat: add tracer and meter macro



* feat: add license



* fix: use full module path



* fix: add shutdown method



* feat: add build method to crate tracer provider and meter provider



* fix: module path



* fix: add export timeout duration and comment



* feat: update interface



* feat: add endpint configuration to root configuration



* fix: update deps



* fix: small refactor



* fix: deleted build method to create trace and metrics provider



---------

Signed-off-by: hlts2 <[email protected]>
@kpango kpango mentioned this pull request Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants