-
Notifications
You must be signed in to change notification settings - Fork 534
experiment: prototype measurement processor #2797
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
base: main
Are you sure you want to change the base?
Conversation
struct UserTypeMeasurementProcessor; | ||
|
||
impl MeasurementProcessor for UserTypeMeasurementProcessor { | ||
fn process<'a>(&self, attributes: Cow<'a, [KeyValue]>) -> Cow<'a, [KeyValue]> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could the trait be modified to accept slice of attributes, and return Option ? If processor has no interest in updating, it can return None, at which point the caller uses the original slice itself.
If processor wants to change, it'll create a new Vec! and return it.
Cost is paid only when one wants to enrich..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated the trait with this suggestion. Looks nicer indeed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: One big advantage of using Cow
is the re-use of owned Vec
in case multiple processors are modifying the attributes:
impl MeasurementProcessor for UserTypeMeasurementProcessor {
fn process<'a>(&self, attributes: Cow<'a, [KeyValue]>) -> Cow<'a, [KeyValue]> {
match Context::current().get::<UserType>() {
Some(user_type) => {
let mut attrs = attributes.into_owned();
attrs.push(KeyValue::new("user_type", user_type.as_str()));
Cow::Owned(attrs)
}
// No changes to the attributes
None => attributes,
}
}
}
The into_owned
will either:
- Return already allocated vector:
Cow::Owned
- Create a new vector in case the
Cow
contained the borrowed data.
Hey @martintmk, I love this experiment! We have a formal proposal for the MeasurementProcessor concept, which you can find here: open-telemetry/opentelemetry-specification#4318 It would be wonderful if you could slightly refactor this PoC to follow the proposal. This would help with getting the proposal merged. From a functional standpoint, it would require redefining the MeasurementProcessor trait from: pub trait MeasurementProcessor: Send + Sync + 'static {
fn process<'a>(&self, attributes: &[KeyValue]) -> Option<Vec<KeyValue>>;
} to something like: pub trait MeasurementProcessor: Send + Sync + 'static {
fn process<F>(&self, measurement: Measurement, next_processor: F)
where
F: FnOnce(Measurement);
}
|
In this PR I am trying to experiment with dynamic metric enrichment. This API tries to do the enrichment in a naive way and should not affect he performance if this feature is not used.
I have experimented with using
Cow<'a, [KeyValue]>
as an argument to the processor. The processor might decide to not modify the attributes and, in that case, the original slice is returned without any additional allocations.When measurement processors are not used, I have not detected any changes in throughput.
Fixes #
Design discussion issue (if applicable) #
Changes
Please provide a brief description of the changes here.
Merge requirement checklist
CHANGELOG.md
files updated for non-trivial, user-facing changes