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

metrics: implement batch observer #429

Merged
merged 3 commits into from
Jan 18, 2021
Merged

metrics: implement batch observer #429

merged 3 commits into from
Jan 18, 2021

Conversation

jtescher
Copy link
Member

This patch adds an implementation of the metrics batch observer. The API is not identical to the example in the spec as it is inconvenient to register metrics after they are moved into the observer callback in rust. Instead the Meter::batch_observer method accepts a closure in which instruments can be registered before being moved into the callback.

meter.batch_observer(|batch| {
    let inst = batch.u64_sum_observer("example").init();

    move |result| {
        result.observe(&[KeyValue::new("a", "1")], inst.observation(42)]);
    }
});

This patch adds an implementation of the metrics batch observer. The API
is not identical to the example in [the
spec](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/metrics/api.md#batch-observer)
as it is inconvenient to register metrics after they are moved into the
observer callback in rust. Instead the `Meter::batch_observer` method
accepts a closure in which instruments can be registered before being
moved into the callback.

```rust
meter.batch_observer(|batch| {
    let inst = batch.u64_sum_observer("example").init();

    move |result| {
        result.observe(&[KeyValue::new("a", "1")], inst.observation(42)]);
    }
});
```
@jtescher jtescher requested a review from a team January 17, 2021 03:18
@codecov
Copy link

codecov bot commented Jan 17, 2021

Codecov Report

Merging #429 (e2c8c87) into master (6728799) will increase coverage by 0.08%.
The diff coverage is 19.35%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #429      +/-   ##
==========================================
+ Coverage   48.76%   48.85%   +0.08%     
==========================================
  Files          66       66              
  Lines        5438     5453      +15     
==========================================
+ Hits         2652     2664      +12     
- Misses       2786     2789       +3     
Impacted Files Coverage Δ
opentelemetry/src/metrics/async_instrument.rs 0.00% <0.00%> (ø)
opentelemetry/src/metrics/meter.rs 71.42% <ø> (+21.42%) ⬆️
opentelemetry/src/metrics/mod.rs 0.00% <ø> (ø)
opentelemetry/src/metrics/registry.rs 0.00% <0.00%> (ø)
opentelemetry/src/sdk/metrics/mod.rs 0.00% <0.00%> (ø)
opentelemetry/src/metrics/noop.rs 29.31% <100.00%> (+9.31%) ⬆️
opentelemetry/src/metrics/observer.rs 60.00% <100.00%> (+60.00%) ⬆️
...ntelemetry/src/sdk/metrics/aggregators/ddsketch.rs 76.95% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6728799...e2c8c87. Read the comment docs.

Copy link
Contributor

@djc djc left a comment

Choose a reason for hiding this comment

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

I don't really know the semantics of this code, so just some style remarks.

The nested closure patterns seems a little ugly, but I can't think of a good way around it right now.

{
let observer = builder(BatchObserver::new(self))?;
self.core
.new_batch_observer(AsyncRunner::Batch(Box::new(observer)))
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd probably just inline the call to builder() here, so you don't have to name the intermediate.

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you suggesting this specifically?

self.core
    .new_batch_observer(AsyncRunner::Batch(Box::new(builder(BatchObserver::new(
        self,
    ))?)))

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah. Maybe the way rustfmt treats it's not actually an improvement in this case...

{
let observer = builder(BatchObserver::new(self));
self.core
.new_batch_observer(AsyncRunner::Batch(Box::new(observer)))
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

opentelemetry/src/sdk/metrics/mod.rs Outdated Show resolved Hide resolved
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

@jtescher
Copy link
Member Author

The nested closure patterns seems a little ugly, but I can't think of a good way around it right now.

@djc yeah it's not ideal, not sure that there is better syntax for this type of setup currently though. Can iterate on this API if someone comes up with a cleaner way of expressing this as metrics still are not stable.

@jtescher jtescher merged commit c3bdce4 into master Jan 18, 2021
@jtescher jtescher deleted the metrics-batch-observer branch January 18, 2021 18:36
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