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

feat(common): add tag! macro #453

Merged
merged 3 commits into from
May 30, 2024
Merged

feat(common): add tag! macro #453

merged 3 commits into from
May 30, 2024

Conversation

morrisonlevi
Copy link
Contributor

@morrisonlevi morrisonlevi commented May 24, 2024

What does this PR do?

This does a few things:

  1. Creates a tag! macro which does compile-time checking for some invariants.
  2. Changes a Tag to use Cow<'static, str> instead of a String.
  3. Makes Tag::from_value private. Use tag! if the key and value are known at compile-time. If not, use Tag::new.
  4. Tag::new returns an anyhow::Result<Tag> now instead of a Result<Tag, Cow<_>>.

Motivation

As libdatadog usage has grown, we have more Rust users of the lib and therefore more static Rust strs we can trust at compile-time and avoid runtime allocations.

Additional Notes

This was previously stacked on another PR, but not anymore.

How to test the change?

Everything should run as normal. It's recommended to use tag! over Tag::new if the key and value are both static strs.

@github-actions github-actions bot added profiling Relates to the profiling* modules. sidecar common labels May 24, 2024
@morrisonlevi morrisonlevi mentioned this pull request May 24, 2024
@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 59.57447% with 19 lines in your changes are missing coverage. Please review.

Project coverage is 68.05%. Comparing base (512cdc5) to head (f829d07).

Additional details and impacted files
@@                       Coverage Diff                        @@
##           julio/APMSP-1004-add-metrics     #453      +/-   ##
================================================================
- Coverage                         68.23%   68.05%   -0.18%     
================================================================
  Files                               193      192       -1     
  Lines                             24899    24746     -153     
================================================================
- Hits                              16989    16841     -148     
+ Misses                             7910     7905       -5     
Components Coverage Δ
crashtracker 19.34% <66.66%> (ø)
datadog-alloc 98.76% <ø> (ø)
data-pipeline 51.45% <ø> (ø)
data-pipeline-ffi 0.00% <ø> (ø)
ddcommon 85.36% <100.00%> (+0.11%) ⬆️
ddcommon-ffi 74.93% <ø> (ø)
ddtelemetry 53.72% <ø> (-2.37%) ⬇️
ipc 81.27% <ø> (ø)
profiling 78.09% <100.00%> (ø)
profiling-ffi 60.05% <100.00%> (ø)
serverless 0.00% <ø> (ø)
sidecar 36.02% <7.14%> (-0.02%) ⬇️
sidecar-ffi 0.00% <ø> (ø)
spawn-worker 54.98% <ø> (ø)
trace-mini-agent 69.12% <ø> (ø)
trace-normalization 97.79% <ø> (ø)
trace-obfuscation 95.74% <ø> (ø)
trace-protobuf 33.33% <ø> (ø)
trace-utils 89.37% <ø> (ø)

@hoolioh hoolioh force-pushed the julio/APMSP-1004-add-metrics branch 2 times, most recently from dbb87cd to 711e6e2 Compare May 27, 2024 16:15
Copy link
Contributor

@danielsn danielsn left a comment

Choose a reason for hiding this comment

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

Nice

profiling-ffi/src/exporter.rs Outdated Show resolved Hide resolved
sidecar/src/self_telemetry.rs Outdated Show resolved Hide resolved
This does a few things:

 1. Creates a `tag!` macro which does compile-time checking for some
    invariants.
 2. Changes a `Tag` to use `Cow<'static, str>` instead of a `String`.
    As libdatadog usage has grown, we have more Rust users of the lib
    and therefore more static Rust strs we can trust at compile-time
    and avoid runtime allocations.
 3. Makes `Tag::from_value` private. Use `tag!` or `Tag::new`.
@morrisonlevi morrisonlevi changed the base branch from julio/APMSP-1004-add-metrics to main May 28, 2024 23:50
@ekump
Copy link
Contributor

ekump commented May 29, 2024

This is cool. While reviewing #450 I was thinking to myself "man, it sucks we can't know these tags are valid at compile time".

/// the <KEY>:<VALUE> format are preferred.
pub fn from_value<'a, IntoCow>(chunk: IntoCow) -> Result<Self, Cow<'static, str>>
/// Validates a tag.
fn from_value<'a, IntoCow>(chunk: IntoCow) -> Result<Self, Cow<'static, str>>
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be try_from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would make it a public since it's a trait impl. It's an implementation detail used by parse_tags and new.

Copy link
Contributor

@bantonsson bantonsson left a comment

Choose a reason for hiding this comment

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

Really nice!

I wasn't completely sure what this would end up as in the generated binary, so I dug into it with readelf and objdump and it's indeed reusing constant tag data defined in multiple places.

@morrisonlevi morrisonlevi marked this pull request as ready for review May 30, 2024 16:08
@morrisonlevi morrisonlevi requested review from a team as code owners May 30, 2024 16:08
@morrisonlevi morrisonlevi requested a review from omerli May 30, 2024 16:08
@morrisonlevi morrisonlevi merged commit ebe9a6a into main May 30, 2024
27 checks passed
@morrisonlevi morrisonlevi deleted the levi/tag-macro branch May 30, 2024 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
common profiling Relates to the profiling* modules. sidecar telemetry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants