Skip to content

Add Meter SDK Class#212

Merged
reyang merged 35 commits into
open-telemetry:masterfrom
Brandon-Kimberly:meter-sdk
Aug 6, 2020
Merged

Add Meter SDK Class#212
reyang merged 35 commits into
open-telemetry:masterfrom
Brandon-Kimberly:meter-sdk

Conversation

@Brandon-Kimberly
Copy link
Copy Markdown
Contributor

@Brandon-Kimberly Brandon-Kimberly commented Jul 27, 2020

This PR adds the header and implementation files for the SDK Meter class as well as a suite of unit tests. This class functions as the accumulator in the SDK spec. This class provides the user a way to create new metric instruments, and collect those instruments’ updates for exporting as well as a facility for recording to a batch of instruments in a single API call.

IMPORTANT NOTES:

  1. The code in this PR depends on the SDK metric instruments code in PR Add Synchronous Metric Instruments SDK #179 and PR Add Asynchronous Metric Instruments SDK #191, and the Meter API code in PR Add OpenTelemetry Meter API #162.
  2. THIS CODE DOES NOT CURRENTLY COMPILE. (<-- This issue has been resolved. See bottom of description for details)
    1. I will point out the specific code where I am seeing this problem in inline comments. I have also posted a description of this issue directly below and in issue #216.

Issue

The problem I am running into currently is that, I need to cast from an API component to an SDK component. This is because all functions and variables related to the collection of metrics are, according to the spec, only defined in the SDK. This means that we cannot have virtual functions in the API to access the information we need to collect and send for export such as the instruments’ aggregators.

The issue with that is, nostd::shared_ptr does not work with std::dynamic_pointer_cast. We cannot simply cast to a raw pointer as this could cause memory leaks or segfaults should we try to access memory that was freed by the user’s shared_ptr to the instrument. We need to use nostd::shared_ptr because the pointers to the created instruments that are returned to the user, must also be placed into a map so that their measurements can be collected later. The pointer in the map must be deleted when the user’s pointer goes out of scope or it will segfault on the next call to Collect(). I am unsure about how to implement this with raw pointers.

I believe the cleanest solution to this problem could be to implement dynamic_pointer_cast for nostd::shared_ptr. I have tested my class with std::shared_ptr and std::dynamic_pointer_cast and all functionality works as intended. In order to provide this functionality to nostd::shared_ptr I believe we would need to add an aliasing constructor to the class. I have looked into this a bit but I am out of my depth with STL code.

Any help or advice on how to fix this problem would be greatly appreciated. I am currently blocked on this problem and it is preventing me from being able to complete contributing a fully functional metrics API and SDK.

UPDATE
Thanks to help from @pyohannes I was able to resolve the issue involving dynamic_pointer_cast. This PR is still dependent on code in PR #191 and PR #162 and should not be merged before them but the code is complete and awaiting more reviews.

-Create Synchronous and Asynchronous Instruments
-Collect Synchronous and Asynchronous Insturments
-Collect Synchronous and Asynchronous Instruments after their returned shared_ptr is deleted
-RecordBatch for all 4 primitive types
-String utility functions, IsValid() and NameAlreadyExists()
Metric instruments are collected from in the reverse order that they were created. The test assumed that they were collected in-order. This has been rectified by making all ASSERTs in the RecordBatch test examine the first aggregator in the vector of records returned from m->collect()
@Brandon-Kimberly Brandon-Kimberly requested a review from a team July 27, 2020 01:59
@Brandon-Kimberly Brandon-Kimberly changed the title Add Meter SDK Class Add Meter SDK Class [WIP] Jul 27, 2020
Comment thread sdk/src/metrics/meter.cc Outdated
{
continue;
}
auto cast_ptr = nostd::dynamic_pointer_cast<SynchronousInstrument<short>>(pair.second);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This specifically is where the problem lays. I need nostd::dynamic_pointer_cast to be implemented for nostd::shared_ptr. I cannot simply cast to a raw pointer as we are editing memory that the user has a shared_ptr to and this could easily lead to segfaults or undefined behavior.

I have tested my code with std::shared_ptr replacing all nostd::shared_ptrs in this class and all tests pass.

If someone could possibly look into implementing this for nostd::shared_ptr or point me in the right direction to add this functionality myself I would greatly appreciate it. Thanks!

@Brandon-Kimberly Brandon-Kimberly changed the title Add Meter SDK Class [WIP] Add Meter SDK Class Jul 30, 2020
@Brandon-Kimberly
Copy link
Copy Markdown
Contributor Author

This PR is ready to be reviewed again now that the issue with nostd::shared_ptr has been resolved.

@reyang
Copy link
Copy Markdown
Member

reyang commented Jul 30, 2020

@Brandon-Kimberly would you rebase and resolve the conflicts?

These tests are no longer necessary as additional functionality that covers these cases has been added to the Synchronous and Asynchronous SDK instruments merged in [PR #191](#191).
@codecov
Copy link
Copy Markdown

codecov Bot commented Aug 5, 2020

Codecov Report

Merging #212 into master will decrease coverage by 1.31%.
The diff coverage is 90.39%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #212      +/-   ##
==========================================
- Coverage   93.70%   92.39%   -1.32%     
==========================================
  Files         135      140       +5     
  Lines        5388     6088     +700     
==========================================
+ Hits         5049     5625     +576     
- Misses        339      463     +124     
Impacted Files Coverage Δ
sdk/src/metrics/meter.cc 88.21% <88.21%> (ø)
sdk/test/metrics/meter_test.cc 94.51% <94.51%> (ø)
sdk/include/opentelemetry/sdk/metrics/meter.h 100.00% <100.00%> (+93.33%) ⬆️
...pi/include/opentelemetry/metrics/observer_result.h 0.00% <0.00%> (ø)
...clude/opentelemetry/sdk/metrics/sync_instruments.h 35.11% <0.00%> (ø)
...lude/opentelemetry/sdk/metrics/async_instruments.h 48.10% <0.00%> (ø)
sdk/include/opentelemetry/sdk/metrics/instrument.h 89.23% <0.00%> (ø)
api/include/opentelemetry/nostd/string_view.h 97.36% <0.00%> (+0.07%) ⬆️
... and 1 more

@Brandon-Kimberly
Copy link
Copy Markdown
Contributor Author

@reyang This PR has been rebased and all CI checks pass. I believe it is ready to merge pending review. :)

Comment thread sdk/include/opentelemetry/sdk/metrics/meter.h Outdated
Comment thread sdk/include/opentelemetry/sdk/metrics/meter.h Outdated
Comment thread sdk/src/metrics/meter.cc
Copy link
Copy Markdown
Member

@reyang reyang left a comment

Choose a reason for hiding this comment

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

LGTM.

@reyang reyang added the pr:please-merge This PR is ready to be merged by a Maintainer (rebased, CI passed, has enough valid approvals, etc.) label Aug 6, 2020
@reyang reyang merged commit 6f6978d into open-telemetry:master Aug 6, 2020
GerHobbelt pushed a commit to GerHobbelt/opentelemetry-cpp that referenced this pull request Jun 17, 2025
…-20250127.x

Update dependency abseil-cpp to v20250127.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr:please-merge This PR is ready to be merged by a Maintainer (rebased, CI passed, has enough valid approvals, etc.)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants