-
Notifications
You must be signed in to change notification settings - Fork 569
Add Aggregation Storage #1213
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
Merged
Merged
Add Aggregation Storage #1213
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a96a200
Aggregation storage
lalitb b3e5874
Merge branch 'main' into agg-store
lalitb f586b04
leftover changes
lalitb 988d34e
remove unexpected chars
lalitb 308b19c
another attempt to fix build
lalitb 20d5440
fix struct init
lalitb 96a1ef5
review comments
lalitb 8751cda
Merge branch 'main' into agg-store
lalitb e223cb7
benchmark fix
lalitb 7b9fb31
Merge branch 'agg-store' of github.com:lalitb/opentelemetry-cpp into …
lalitb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
sdk/include/opentelemetry/sdk/metrics/state/attributes_hashmap.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #pragma once | ||
| #ifndef ENABLE_METRICS_PREVIEW | ||
| # include "opentelemetry/common/spin_lock_mutex.h" | ||
| # include "opentelemetry/nostd/function_ref.h" | ||
| # include "opentelemetry/sdk/common/attribute_utils.h" | ||
| # include "opentelemetry/sdk/common/attributemap_hash.h" | ||
| # include "opentelemetry/sdk/metrics/aggregation/aggregation.h" | ||
| # include "opentelemetry/sdk/metrics/instruments.h" | ||
| # include "opentelemetry/version.h" | ||
|
|
||
| # include <functional> | ||
| # include <memory> | ||
| # include <mutex> | ||
| # include <unordered_map> | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace metrics | ||
| { | ||
| using opentelemetry::sdk::common::OrderedAttributeMap; | ||
|
|
||
| class AttributeHashGenerator | ||
| { | ||
| public: | ||
| size_t operator()(const MetricAttributes &attributes) const | ||
| { | ||
| return opentelemetry::sdk::common::GetHashForAttributeMap(attributes); | ||
| } | ||
| }; | ||
|
|
||
| class AttributesHashMap | ||
| { | ||
| public: | ||
| Aggregation *Get(const MetricAttributes &attributes) const | ||
| { | ||
| std::lock_guard<opentelemetry::common::SpinLockMutex> guard(GetLock()); | ||
| auto it = hash_map_.find(attributes); | ||
| if (it != hash_map_.end()) | ||
| { | ||
| return it->second.get(); | ||
| } | ||
| return nullptr; | ||
| } | ||
|
|
||
| /** | ||
| * @return check if key is present in hash | ||
| * | ||
| */ | ||
| bool Has(const MetricAttributes &attributes) const | ||
| { | ||
| std::lock_guard<opentelemetry::common::SpinLockMutex> guard(GetLock()); | ||
| return (hash_map_.find(attributes) == hash_map_.end()) ? false : true; | ||
| } | ||
|
|
||
| /** | ||
| * @return the pointer to value for given key if present. | ||
| * If not present, it uses the provided callback to generate | ||
| * value and store in the hash | ||
| */ | ||
| Aggregation *GetOrSetDefault(const MetricAttributes &attributes, | ||
| std::function<std::unique_ptr<Aggregation>()> aggregation_callback) | ||
| { | ||
| std::lock_guard<opentelemetry::common::SpinLockMutex> guard(GetLock()); | ||
| auto it = hash_map_.find(attributes); | ||
| if (it != hash_map_.end()) | ||
| { | ||
| return it->second.get(); | ||
| } | ||
|
|
||
| hash_map_[attributes] = std::move(aggregation_callback()); | ||
| return hash_map_[attributes].get(); | ||
| } | ||
|
|
||
| /** | ||
| * Set the value for given key, overwriting the value if already present | ||
| */ | ||
| void Set(const MetricAttributes &attributes, std::unique_ptr<Aggregation> value) | ||
| { | ||
| std::lock_guard<opentelemetry::common::SpinLockMutex> guard(GetLock()); | ||
| hash_map_[attributes] = std::move(value); | ||
| } | ||
|
|
||
| /** | ||
| * Iterate the hash to yield key and value stored in hash. | ||
| */ | ||
| bool GetAllEnteries( | ||
| nostd::function_ref<bool(const MetricAttributes &, Aggregation &)> callback) const | ||
| { | ||
| std::lock_guard<opentelemetry::common::SpinLockMutex> guard(GetLock()); | ||
| for (auto &kv : hash_map_) | ||
| { | ||
| if (!callback(kv.first, *(kv.second.get()))) | ||
| { | ||
| return false; // callback is not prepared to consume data | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Return the size of hash. | ||
| */ | ||
| size_t Size() | ||
| { | ||
| std::lock_guard<opentelemetry::common::SpinLockMutex> guard(GetLock()); | ||
| return hash_map_.size(); | ||
| } | ||
|
|
||
| private: | ||
| std::unordered_map<MetricAttributes, std::unique_ptr<Aggregation>, AttributeHashGenerator> | ||
| hash_map_; | ||
|
|
||
| static opentelemetry::common::SpinLockMutex &GetLock() noexcept | ||
| { | ||
| static opentelemetry::common::SpinLockMutex lock; | ||
| return lock; | ||
| } | ||
| }; | ||
| } // namespace metrics | ||
|
|
||
| } // namespace sdk | ||
| OPENTELEMETRY_END_NAMESPACE | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.