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: produce measurement with attributes #43

Merged
merged 9 commits into from
Aug 8, 2023

Conversation

ymgyt
Copy link
Contributor

@ymgyt ymgyt commented Aug 4, 2023

Motivation

Support #32
This change will allow user to associate Attributes with metrics from tracing.

Solution

  1. MetricsLayer is only interested in events that contain fields with metrics prefixes such as counter, so I have utilized the tracing_subscriber Filter trait.
  2. Since it's necessary to pass Attributes when outputting Measurements, it's required to complete the visit for all fields first before invoking the Metrics API(Counter:add()).
    SmallVec is used to hold Attribute KeyValues, Metrics names, and Instruments for invocation, aiming to avoid allocations in typical use cases.

@ymgyt ymgyt requested a review from jtescher as a code owner August 4, 2023 17:07
|| name.starts_with(METRIC_PREFIX_MONOTONIC_COUNTER)
|| name.starts_with(METRIC_PREFIX_HISTOGRAM)
})
}
}

impl<S> Layer<S> for MetricsLayer
where
S: Subscriber + for<'span> LookupSpan<'span>,
{
fn on_event(&self, event: &tracing::Event<'_>, _ctx: Context<'_, S>) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To avoid allocating each time on_event() is called, we determine if the event has a metrics field.
However, this caused RwLock::read() to be called for each on_event().

I am concerned that an implementation that gets read lock for every on_event may not be acceptable.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah we may want to see if there is another way, or have this be an opt-in for non-performance sensitive cases

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 will try per layer filtering

src/metrics.rs Outdated Show resolved Hide resolved
pub(crate) instruments: &'a Instruments,
pub(crate) meter: &'a Meter,
attributes: &'a mut SmallVec<[KeyValue; 8]>,
visited_metrics: &'a mut SmallVec<[(&'static str, InstrumentType); 2]>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

SmallVec size has no specific basis.

@ymgyt
Copy link
Contributor Author

ymgyt commented Aug 5, 2023

@jtescher
By tracing_subscriber Filter trait, I avoid using RwLock. Additionally, I have introduced SmallVec to minimize allocations.

@ymgyt ymgyt force-pushed the metrics-attributes branch from 125bc1f to c365058 Compare August 5, 2023 09:15
@ymgyt ymgyt marked this pull request as draft August 5, 2023 11:53
@ymgyt ymgyt force-pushed the metrics-attributes branch from 2992efc to 3cfc96f Compare August 5, 2023 12:29
@ymgyt ymgyt marked this pull request as ready for review August 5, 2023 13:07
@ymgyt ymgyt force-pushed the metrics-attributes branch from 262e15a to 2c0dc3a Compare August 5, 2023 13:51
@ymgyt
Copy link
Contributor Author

ymgyt commented Aug 6, 2023

Here are the benchmark results on my local machine

Screen Shot 2023-08-06 at 16 04 49

Copy link
Collaborator

@jtescher jtescher left a comment

Choose a reason for hiding this comment

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

Nice, looks good

Cargo.toml Outdated
@@ -35,6 +35,7 @@ once_cell = "1.13.0"
# Fix minimal-versions
async-trait = { version = "0.1.56", optional = true }
thiserror = { version = "1.0.31", optional = true }
smallvec = "1.0"
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: can this be option and enabled with the metrics feature?

@ymgyt
Copy link
Contributor Author

ymgyt commented Aug 8, 2023

@jtescher Thanks for the review, I made smallvec optional

I will update the document with follow up PR

@ymgyt ymgyt requested a review from jtescher August 8, 2023 06:37
@jtescher
Copy link
Collaborator

jtescher commented Aug 8, 2023

Looks great, thanks @ymgyt

@jnicholls
Copy link

Nice work!

@ymgyt ymgyt deleted the metrics-attributes branch August 9, 2023 03:18
jtescher pushed a commit that referenced this pull request Aug 12, 2023
## Motivation

* fix broken link in doc
* update histogram metrics field prefix (`value` -> `histogram`)
* add metrics layer attributes description
  #43
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.

3 participants