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

Update metrics dependency from 0.21.0 to 0.22.0 #34

Merged
merged 2 commits into from
Dec 27, 2023
Merged

Update metrics dependency from 0.21.0 to 0.22.0 #34

merged 2 commits into from
Dec 27, 2023

Conversation

zohnannor
Copy link
Contributor

@zohnannor zohnannor commented Dec 26, 2023

Updated:

  • metrics to 0.22
  • metrics-exporter-prometheus to 0.13
  • Reduced allocations in describe & collect. Collector now takes a prefix upon it's instantiation, builds the strings for metrics names and reuses them onward. No need to allocate new string each time.

This is a breaking change due to removal of Collector::prefix method. Though it can be brought back, but I don't think there is much use cases for that.

Additionally:

  • I've replaced the doc string in lib.rs with include_str!("../README.md") so that it stays updated. But I've notices the README.tpl file, it seems you are generating the README from the lib.rs docs? If you'll accept my proposal (with include_str), the tpl file could be deleted.
  • I've removed redundant [] in the markdown links, not sure why they were there.
  • I've removed Diff check step from the fmt job, there is a --check flag in cargo-fmt that does that?

Supersedes #32, #33.

Summary by CodeRabbit

  • Refactor

    • Updated the testing workflow for improved code format verification.
    • Streamlined the Collector struct for enhanced metrics handling.
  • Documentation

    • Improved the formatting of URLs and code examples in the main README.
    • Refined the presentation of crate references in example READMEs.
  • Chores

    • Removed unnecessary brackets in documentation links for clarity.

Copy link

coderabbitai bot commented Dec 26, 2023

Walkthrough

The project has undergone a cleanup and refactoring phase. A step in the CI pipeline for diff checking has been removed, and the formatting step has been made more stringent. Documentation has been polished across several markdown files, with improved readability and style. The core library code has seen a refactor, with a structural change in the Collector struct, enhancing the way metrics are handled and represented.

Changes

File Path Change Summary
.github/workflows/test.yml Removed "Diff check" step; updated "Format" step to use cargo fmt --check.
README.md Updated crate's documentation, including formatting of URLs and code examples.
examples/basic/README.md Minor modification in the text for improved clarity.
examples/with_axum/README.md Removed square brackets around crate names, refined crate references.
examples/with_axum/src/main.rs Replaced Server usage with TcpListener and axum::serve; simplified address binding.
src/lib.rs Significant refactoring of the Collector struct, including removal of prefix and addition of metric fields.

Poem

🐰✨
In the code where we hop,
We've trimmed and we've cleaned,
Now the metrics are sleek,
Like the fields, they're pristine.
🌟📊

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 with CodeRabbit Bot (@coderabbitai)

  • You can directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • You can tag CodeRabbit on specific lines of code or entire files in the PR by tagging @coderabbitai in a comment. Examples:
    • @coderabbitai generate unit tests for this file
    • @coderabbitai modularize this function
  • You can tag @coderabbitai in a PR comment and ask questions about the PR and the codebase. Examples:
    • @coderabbitai gather interesting statistics about this repository and render them in a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai generate unit tests for the src/utils.ts file.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

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

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4882190 and 00976c9.
Files ignored due to filter (3)
  • Cargo.toml
  • examples/basic/Cargo.toml
  • examples/with_axum/Cargo.toml
Files selected for processing (5)
  • .github/workflows/test.yml (1 hunks)
  • README.md (4 hunks)
  • examples/basic/README.md (1 hunks)
  • examples/with_axum/README.md (1 hunks)
  • src/lib.rs (5 hunks)
Files skipped from review due to trivial changes (4)
  • .github/workflows/test.yml
  • README.md
  • examples/basic/README.md
  • examples/with_axum/README.md
Additional comments: 6
src/lib.rs (6)
  • 1-1: Including the README.md content directly in the library documentation using include_str!.

  • 10-17: Added individual fields for metrics to the Collector struct.

  • 30-39: The default method initializes the new fields with static metric names.

  • 51-62: The new method uses format! and leak() to create 'static strings for metric names with a prefix. This approach permanently allocates memory for these strings, which may not be reclaimed. Ensure this is the intended behavior and acceptable in the context of this library's usage patterns.

  • 83-125: The describe method now uses the new static string fields to describe gauges.

  • 147-175: The collect method now uses the new static string fields to collect metrics. This change should improve performance by reducing memory allocations.

@zohnannor

This comment was marked as off-topic.

Copy link
Owner

@lambdalisue lambdalisue left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution. I appreciate it. I have a few requests for adjustments:

  1. Each PR should address a single topic. Please split the PR into multiple ones, each focusing on a specific topic.

  2. Generally, we discourage introducing breaking changes. If there's a significant benefit to the new style, please provide a clear explanation. Otherwise, consider maintaining the current API.

  3. Regarding the README.md file, I prefer not to include it in the lib.rs file since the content serves a slightly different purpose.

Additionally, please craft a more informative commit message.

.github/workflows/test.yml Show resolved Hide resolved
src/lib.rs Outdated
pub fn new(prefix: impl AsRef<str>) -> Self {
let prefix = prefix.as_ref();
Self {
cpu_seconds_total: format!("{prefix}process_cpu_seconds_total").leak(),
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think it's good idea to use leak() here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This way we are allocating the String only once, and then reuse it in gauge!() macros for metrics names. The &'static str also helps -- it unties the lifetime from the &self reference.

The only downside of leaking is that the data of Strings allocated won't be freed until the program exits. In a contrived scenario, where one creates a ton of Collectors with prefix provided and then drops them this would cause problems. But from my understanding, the Collector is meant to be created once at the start and used for the entire runtime of the program.

Still, we can counter this by Box::leaking the strings allocated and then giving the Collector the Drop impl that will unsafely Box::from_raw them to drop properly. If you're fine with that, I'll implement this in differend PR.

Copy link
Owner

Choose a reason for hiding this comment

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

How about utilizing String to store each label instead of &'static str in Collectors? I'm asking because the advantages of using &'static str in Collectors are not clear to me. Is there a significant difference in memory usage or any other factors to consider?

src/lib.rs Outdated
pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
self.prefix = prefix.into();
self
pub fn new(prefix: impl AsRef<str>) -> Self {
Copy link
Owner

@lambdalisue lambdalisue Dec 26, 2023

Choose a reason for hiding this comment

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

Breaking changes without significant benefit is not acceptable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll bring back prefix so that it will work as before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sad to use impl Into<String> in it though, as it is not required to build a String.

Copy link
Owner

Choose a reason for hiding this comment

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

Changing from impl Into<String> to impl AsRef<str> is OK because it's easy to notice and fix.

Copy link

@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.

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 00976c9 and 8cfa713.
Files ignored due to filter (3)
  • Cargo.toml
  • examples/basic/Cargo.toml
  • examples/with_axum/Cargo.toml
Files selected for processing (3)
  • README.md (4 hunks)
  • examples/with_axum/src/main.rs (3 hunks)
  • src/lib.rs (6 hunks)
Files skipped from review due to trivial changes (1)
  • README.md
Files skipped from review as they are similar to previous changes (1)
  • src/lib.rs
Additional comments: 1
examples/with_axum/src/main.rs (1)
  • 1-4: The introduction of axum and tokio imports aligns with the changes to the server setup. Ensure that these new dependencies are reflected in Cargo.toml and that their versions are compatible with the rest of the project.

examples/with_axum/src/main.rs Show resolved Hide resolved
examples/with_axum/src/main.rs Show resolved Hide resolved
@zohnannor zohnannor changed the title Bump metrics dependencies versions Update metrics dependency from 0.21.0 to 0.22.0 Dec 26, 2023
@zohnannor
Copy link
Contributor Author

zohnannor commented Dec 26, 2023

I've removed the changes regarding unnecessary allocations elimination and will provide a new PR after this one is merged. I decided to leave the documentation, markdown links, examples changes along with the version bump in this PR. Should I move them also?

Copy link
Owner

@lambdalisue lambdalisue left a comment

Choose a reason for hiding this comment

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

LGTM

@lambdalisue lambdalisue merged commit 0a12cef into lambdalisue:main Dec 27, 2023
@zohnannor zohnannor deleted the bump-metrics branch December 27, 2023 07:38
lambdalisue added a commit that referenced this pull request Jan 6, 2024
Originally, I expressed a preference not to include `README.md`
in the crate documentation. However, I have since reconsidered
this decision as I couldn't recall the process of generating
`README.md` from `README.tpl`. (I do remember using the
`cargo-readme` crate for this purpose, though).

#34 (review)
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.

2 participants