Skip to content

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

martintmk
Copy link
Contributor

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

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

struct UserTypeMeasurementProcessor;

impl MeasurementProcessor for UserTypeMeasurementProcessor {
fn process<'a>(&self, attributes: Cow<'a, [KeyValue]>) -> Cow<'a, [KeyValue]> {
Copy link
Member

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

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 have updated the trait with this suggestion. Looks nicer indeed.

Copy link
Contributor Author

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.

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