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(metrics): multi-instrument async callback support #2966

Merged
merged 3 commits into from
May 25, 2022

Conversation

legendecas
Copy link
Member

@legendecas legendecas commented May 13, 2022

Which problem is this PR solving?

Added multi-instrument async callback support.

Fixes #2956

Short description of the changes

  • Added ValueType support in async instruments.
  • Changes on meter.createObservableCounter, meter.createObservableGauge, meter.createObservableUpDownCounter
    • removed the second parameter callback
    • returns an Observable object on which callbacks can be registered or unregistered.
  • Added meter.addBatchObservableCallback and meter.removeBatchObservableCallback.
  • One callback associated with a set of instruments are called once for each collection. (spec clarification: Clarify async instrument callback identity opentelemetry-specification#2538)

After the change, observable instruments can be used with like:

const meter = api.metrics.getMeter('test-meter');
// Creating an async instrument, similar to synchronous instruments
const observableCounter = meter.createObservableCounter('observable-counter', {}); 

// Register a single-instrument callback
observableCounter.addCallback(async (observableResult) => {  
  // ... do async stuff
  observableResult.observe(1, { attr: 'value' });
});

// Register a multi-instrument callback
meter.addBatchObservableCallback(async (batchObservableResult) => { 
   // ... do async stuff
  batchObservableResult.observe(observableCounter, 1, { attr: 'value' });

  // This is been dropped since it is not associated with the callback at registration.
  batchObservableResult.observe(otherObservable, 2); 
}, [ observableCounter ]);

Type of change

  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

How Has This Been Tested?

  • ObservableRegistry
  • Meter.createObservableXXX
  • MetricCollector.collect integrated tests for batch observable callbacks
  • ObservableResult trunc values for ValueType.INT

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated

@codecov
Copy link

codecov bot commented May 13, 2022

Codecov Report

Merging #2966 (fe1bf62) into main (610808d) will decrease coverage by 2.03%.
The diff coverage is 92.85%.

❗ Current head fe1bf62 differs from pull request most recent head 8e07cea. Consider uploading reports for the commit 8e07cea to get more accurate results

@@            Coverage Diff             @@
##             main    #2966      +/-   ##
==========================================
- Coverage   92.54%   90.51%   -2.04%     
==========================================
  Files         183       68     -115     
  Lines        6025     1876    -4149     
  Branches     1284      400     -884     
==========================================
- Hits         5576     1698    -3878     
+ Misses        449      178     -271     
Impacted Files Coverage Δ
...ages/opentelemetry-api-metrics/src/types/Metric.ts 100.00% <ø> (ø)
...ackages/opentelemetry-api-metrics/src/NoopMeter.ts 97.22% <92.85%> (+24.49%) ⬆️
...s/opentelemetry-core/src/platform/node/sdk-info.ts 0.00% <0.00%> (-100.00%) ⬇️
...opentelemetry-core/src/platform/node/globalThis.ts 0.00% <0.00%> (-100.00%) ⬇️
...pentelemetry-core/src/platform/node/performance.ts 0.00% <0.00%> (-100.00%) ⬇️
...emetry-api-metrics/src/platform/node/globalThis.ts 0.00% <0.00%> (-100.00%) ⬇️
...lemetry-resources/src/detectors/ProcessDetector.ts 31.81% <0.00%> (-68.19%) ⬇️
...pentelemetry-resources/src/detectors/OSDetector.ts 26.66% <0.00%> (-60.00%) ⬇️
...ntelemetry-resources/src/detectors/HostDetector.ts 23.52% <0.00%> (-52.95%) ⬇️
...perimental/packages/otlp-exporter-base/src/util.ts 79.41% <0.00%> (-17.65%) ⬇️
... and 118 more

@legendecas legendecas force-pushed the metrics-ff/batch branch 3 times, most recently from 22a5521 to 18760f7 Compare May 13, 2022 08:32
@legendecas legendecas marked this pull request as ready for review May 13, 2022 15:47
@legendecas legendecas requested a review from a team May 13, 2022 15:47
@legendecas
Copy link
Member Author

I'm marking this ready to be reviewed to let people drop a comment on the new observable APIs. But I'd still like the spec change open-telemetry/opentelemetry-specification#2538 to land first.

Copy link
Member

@vmarchaud vmarchaud left a comment

Choose a reason for hiding this comment

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

as always i'm not the most familiar with metrics (ajd this is a large PR) but code wise lgtm

Copy link
Member

@dyladan dyladan left a comment

Choose a reason for hiding this comment

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

Huge effort thanks so much. I'm going to approve but there are some small things that you can do if you want:

  • Add tests for the metrics API package for the changed lines. Looks like they're not included in the coverage
  • Documentation on how this feature is used
  • In the PR a short description of the design of this feature and if you copied another implementation for it might make it easier for others to review.

@legendecas
Copy link
Member Author

@dyladan thank you for the suggestion! I've added some new docs in the README of sdk-metrics-base and updated the short description in the OP.

Copy link
Member

@pichlermarc pichlermarc 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 as per my understanding of the spec. 🙂
Thanks for the great work! 🚀

@dyladan
Copy link
Member

dyladan commented May 24, 2022

All my questions are resolved. Up to you if you want to merge or wait for more reviews.

@legendecas
Copy link
Member Author

I'll try to land this by the end of tomorrow to get things rolling. Thank you all for reviewing!

@legendecas legendecas merged commit ba3e320 into open-telemetry:main May 25, 2022
@legendecas legendecas deleted the metrics-ff/batch branch May 25, 2022 03:13
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.

Multi-instrument observable callback
4 participants