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

Adding a dynamic dispatch to Aggregator Selector #497

Conversation

dawid-nowak
Copy link
Contributor

@TommyCpp
Have a look and let me know if something like this could work?

Rationale:
As a user, I would like to be able to supply my own aggregator selector so I can better control how data is aggregated.
If no aggregator is set by a user, sensible defaults are provided.

@dawid-nowak dawid-nowak requested a review from a team as a code owner March 27, 2021 22:46
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Mar 27, 2021

CLA Signed

The committers are authorized under a signed CLA.

use std::sync::Arc;

#[derive(Debug)]
struct NOPAggregator();
Copy link
Contributor

Choose a reason for hiding this comment

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

Style nits: this should be named NopAggregator (maybe NoopAggregator is even easier to understand for most readers?) and you should just leave off the ().

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I think we most used Noop[Component Name] to name the no-op components.

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 think i will keep this example as is and add another example with a custom aggregator selector.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed altogether

Comment on lines 29 to 32
use opentelemetry::metrics::Descriptor;
use opentelemetry::sdk::export::metrics::Aggregator;
use opentelemetry::sdk::export::metrics::AggregatorSelector;
use std::sync::Arc;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe move this part to the top to keep the imports at the same place?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved to a different example and moved to the top

@dawid-nowak dawid-nowak force-pushed the adding_dynamic_dispatch_to_aggregator_selector branch from f23311c to 2eb9af1 Compare March 29, 2021 14:02
@dawid-nowak
Copy link
Contributor Author

  • Added a separate example for custom aggregator selector
  • By the way I had to change const LEMONS_KEY: Key = Key::from_static_str("ex.com/lemons"); to
    const LEMONS_KEY: Key = Key::from_static_str("lemons"); as otherwise otlp collector is complaining about it when converting from otlp to prometheus
2021-03-29T13:48:32.013Z	error	prometheusexporter/collector.go:259	failed to convert metric ex.com.one: "ex.com/lemons" is not a valid label name for metric "perf_ex_com_one"	{"component_kind": "exporter", "component_type": "prometheus", "component_name": "prometheus"}

Copy link
Contributor

@TommyCpp TommyCpp left a comment

Choose a reason for hiding this comment

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

LGTM:+1: Thanks

opentelemetry-otlp/src/metric.rs Outdated Show resolved Hide resolved
ES: ExportKindFor + Send + Sync + Clone + 'static,
SP: Fn(PushControllerWorker) -> SO,
I: Fn(time::Duration) -> IO,
{
aggregator_selector: AS,
aggregator_selector: Box<dyn AggregatorSelector + Send + Sync + 'static>,
Copy link
Member

Choose a reason for hiding this comment

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

Are all these other box changes still needed?

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 think so. I tried it before and was running into problems. If my understanding is correct if you remove Box::dyn then the compiler does static dispatch and turns T into a concrete type. So without dyns, it works in a situation where both opentelemetry_otlp::new_metrics_pipeline(tokio::spawn, delayed_interval) and .with_aggregator_selector() use the same type T.

Copy link
Member

Choose a reason for hiding this comment

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

If you replace the method body with the full:

pub fn with_aggregator_selector<T>(
    self,
    aggregator_selector: T,
) -> OtlpMetricPipelineBuilder<T, ES, SP, SO, I, IO>
where
    T: AggregatorSelector + Send + Sync + 'static,
{
    OtlpMetricPipelineBuilder {
        aggregator_selector,
        export_selector: self.export_selector,
        spawn: self.spawn,
        interval: self.interval,
        export_config: self.export_config,
        tonic_config: self.tonic_config,
        resource: self.resource,
        stateful: self.stateful,
        period: self.period,
        timeout: self.timeout,
    }
}

Then you don't need any other change.

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 a feeling I am missing something here.
If I use the snippet from above I am getting compilation errors on both the main and this branch.
If I remove Box::dyns I am pretty much back to the code that is currently on the main branch. In there the aggregator selector in new_metrics_pipeline must be of the same type as in with_aggreagtor_selector.

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 removed more dyns from new_metrics_pipeline_with_selector but i am not sure if i can remove the remaining ones

Copy link
Member

Choose a reason for hiding this comment

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

Something along the lines of 88b1904 should work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jtescher I have missed that. The code is much simpler indeed, thanks. I have merged your suggestions, so hopefully, we are good now.

Copy link
Member

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

Looks good, thanks @dawid-nowak

@jtescher jtescher merged commit b7bd0d9 into open-telemetry:main Apr 7, 2021
TommyCpp added a commit to TommyCpp/opentelemetry-rust that referenced this pull request Apr 20, 2021
Follow up on open-telemetry#497, which allows users to bring their own aggregator selector.

Also updated the name of examples.
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.

None yet

4 participants