-
Notifications
You must be signed in to change notification settings - Fork 635
[SDK] Add Entity support to Resource #4490
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
Open
shashankxrm
wants to merge
19
commits into
open-telemetry:main
Choose a base branch
from
shashankxrm:fix/resource-entity-3652
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
91a6ce1
[RESOURCE] Add Entity value type
shashankxrm a36b00b
[RESOURCE] Add Entity storage to Resource
shashankxrm 375455e
[RESOURCE] Add Entity support to Resource
shashankxrm 7f1cb36
[RESOURCE] Add entity-aware Merge and Create support
shashankxrm e9c0cc6
Add Entity support entry to CHANGELOG
shashankxrm 77d9966
Update CMakeLists.txt for linespace adjustment
shashankxrm fd6f0d5
Update resource.cc to make insertion function more efficient
shashankxrm df5e721
Update resource.cc emplace function
shashankxrm 6f308c6
[RESOURCE] Fix CI formatting and static analysis issues
shashankxrm e324e71
Update resource_test.cc for placement change of include statement
shashankxrm a892a95
[RESOURCE] Fix clang-tidy copy warnings
shashankxrm fdf3436
Fix Entity constructor and special members
shashankxrm 5a9b022
Document Resource merge algorithms
shashankxrm ca82b27
Fix Resource merge ordering for entity schema
shashankxrm 314b38d
Optimize attribute-only Resource storage
shashankxrm f65b142
Normalize integer Entity identities
shashankxrm 71d3012
Include what you use fix
shashankxrm 7ee11fd
ci: retrigger MSVC job
shashankxrm c08b825
Guard process executable service name fallback
shashankxrm 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "opentelemetry/sdk/common/attribute_utils.h" | ||
| #include "opentelemetry/version.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace resource | ||
| { | ||
|
|
||
| using ResourceAttributes = opentelemetry::sdk::common::AttributeMap; | ||
|
|
||
| class Entity | ||
| { | ||
| public: | ||
| /** | ||
| * Constructs an Entity. Integer identity values (and integer arrays) are | ||
| * canonicalized to int64_t when they fit, so C++ integer widths do not | ||
| * affect identity equality or merge. Description values are not rewritten. | ||
| */ | ||
| Entity(const std::string &type, | ||
| const ResourceAttributes &identity, | ||
| const ResourceAttributes &description = ResourceAttributes{}, | ||
| const std::string &schema_url = std::string{}); | ||
|
|
||
| const std::string &GetType() const noexcept; | ||
| const ResourceAttributes &GetIdentity() const noexcept; | ||
| const ResourceAttributes &GetDescription() const noexcept; | ||
| const std::string &GetSchemaURL() const noexcept; | ||
|
|
||
| bool IsValid() const noexcept; | ||
|
|
||
| bool operator==(const Entity &other) const noexcept; | ||
|
|
||
| private: | ||
| std::string type_; | ||
| ResourceAttributes identity_; | ||
| ResourceAttributes description_; | ||
| std::string schema_url_; | ||
| }; | ||
|
|
||
| } // namespace resource | ||
| } // namespace sdk | ||
| OPENTELEMETRY_END_NAMESPACE |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #include "opentelemetry/sdk/resource/entity.h" | ||
|
|
||
| #include <cstdint> | ||
| #include <limits> | ||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "opentelemetry/nostd/variant.h" | ||
| #include "opentelemetry/sdk/common/attribute_utils.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace sdk | ||
| { | ||
| namespace resource | ||
| { | ||
| namespace | ||
| { | ||
|
|
||
| namespace nostd = opentelemetry::nostd; | ||
| using OwnedAttributeValue = opentelemetry::sdk::common::OwnedAttributeValue; | ||
|
|
||
| bool Uint64FitsInt64(std::uint64_t value) noexcept | ||
| { | ||
| return value <= static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()); | ||
| } | ||
|
|
||
| template <typename T> | ||
| OwnedAttributeValue WidenIntegerArray(const std::vector<T> &src) | ||
| { | ||
| std::vector<std::int64_t> dst; | ||
| dst.reserve(src.size()); | ||
| for (T value : src) | ||
| { | ||
| dst.push_back(static_cast<std::int64_t>(value)); | ||
| } | ||
| return OwnedAttributeValue{std::move(dst)}; | ||
| } | ||
|
|
||
| OwnedAttributeValue NormalizeIntegerIdentityValue(const OwnedAttributeValue &value) | ||
| { | ||
| if (nostd::holds_alternative<std::int32_t>(value)) | ||
| { | ||
| return OwnedAttributeValue{static_cast<std::int64_t>(nostd::get<std::int32_t>(value))}; | ||
| } | ||
| if (nostd::holds_alternative<std::uint32_t>(value)) | ||
| { | ||
| return OwnedAttributeValue{static_cast<std::int64_t>(nostd::get<std::uint32_t>(value))}; | ||
| } | ||
| if (nostd::holds_alternative<std::uint64_t>(value)) | ||
| { | ||
| const std::uint64_t raw = nostd::get<std::uint64_t>(value); | ||
| if (Uint64FitsInt64(raw)) | ||
| { | ||
| return OwnedAttributeValue{static_cast<std::int64_t>(raw)}; | ||
| } | ||
| return value; | ||
| } | ||
| if (nostd::holds_alternative<std::vector<std::int32_t>>(value)) | ||
| { | ||
| return WidenIntegerArray(nostd::get<std::vector<std::int32_t>>(value)); | ||
| } | ||
| if (nostd::holds_alternative<std::vector<std::uint32_t>>(value)) | ||
| { | ||
| return WidenIntegerArray(nostd::get<std::vector<std::uint32_t>>(value)); | ||
| } | ||
| if (nostd::holds_alternative<std::vector<std::uint64_t>>(value)) | ||
| { | ||
| const auto &src = nostd::get<std::vector<std::uint64_t>>(value); | ||
| for (std::uint64_t element : src) | ||
| { | ||
| if (!Uint64FitsInt64(element)) | ||
| { | ||
| return value; | ||
| } | ||
| } | ||
| return WidenIntegerArray(src); | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| void NormalizeIdentityIntegers(ResourceAttributes &identity) | ||
| { | ||
| for (auto &kv : identity) | ||
| { | ||
| kv.second = NormalizeIntegerIdentityValue(kv.second); | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| Entity::Entity(const std::string &type, | ||
| const ResourceAttributes &identity, | ||
| const ResourceAttributes &description, | ||
| const std::string &schema_url) | ||
| : type_(type), identity_(identity), description_(description), schema_url_(schema_url) | ||
| { | ||
| NormalizeIdentityIntegers(identity_); | ||
| for (const auto &kv : identity_) | ||
| { | ||
| description_.erase(kv.first); | ||
| } | ||
| } | ||
|
|
||
| const std::string &Entity::GetType() const noexcept | ||
| { | ||
| return type_; | ||
| } | ||
|
|
||
| const ResourceAttributes &Entity::GetIdentity() const noexcept | ||
| { | ||
| return identity_; | ||
| } | ||
|
|
||
| const ResourceAttributes &Entity::GetDescription() const noexcept | ||
| { | ||
| return description_; | ||
| } | ||
|
|
||
| const std::string &Entity::GetSchemaURL() const noexcept | ||
| { | ||
| return schema_url_; | ||
| } | ||
|
|
||
| bool Entity::IsValid() const noexcept | ||
| { | ||
| return !type_.empty() && !identity_.empty(); | ||
| } | ||
|
|
||
| bool Entity::operator==(const Entity &other) const noexcept | ||
| { | ||
| return type_ == other.type_ && identity_ == other.identity_ && | ||
| description_ == other.description_ && schema_url_ == other.schema_url_; | ||
| } | ||
|
|
||
| } // namespace resource | ||
| } // namespace sdk | ||
| OPENTELEMETRY_END_NAMESPACE |
Oops, something went wrong.
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.