-
Notifications
You must be signed in to change notification settings - Fork 500
[SDK] Fix include instrumentation scope attributes in equal method #3214
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
Changes from 5 commits
64719c7
0f5a720
53ec99c
0379249
d0d6bba
69f4355
ae33393
ac5c33f
890b58b
6a78679
5ae1b09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,60 @@ struct AttributeConverter | |
} | ||
}; | ||
|
||
/** | ||
* Evaluates if an owned value (from an OwnedAttributeValue) is equal to another value (from a | ||
* non-owning AttributeValue). This only supports the checking equality with | ||
* nostd::visit(AttributeEqualToVisitor, OwnedAttributeValue, AttributeValue). | ||
*/ | ||
struct AttributeEqualToVisitor | ||
{ | ||
// Different types are not equal including containers of different element types | ||
template <typename T, typename U> | ||
bool operator()(const T &, const U &) const noexcept | ||
{ | ||
return false; | ||
} | ||
|
||
// Compare the same arithmatic types | ||
template <typename T> | ||
bool operator()(const T &owned_value, const T &value) const noexcept | ||
{ | ||
return owned_value == value; | ||
} | ||
|
||
// Compare std::string and const char* | ||
bool operator()(const std::string &owned_value, const char *value) const noexcept | ||
{ | ||
return owned_value == value; | ||
} | ||
|
||
// Compare std::string and nostd::string_view | ||
bool operator()(const std::string &owned_value, nostd::string_view value) const noexcept | ||
{ | ||
return owned_value == value; | ||
} | ||
|
||
// Compare std::vector<std::string> and nostd::span<const nostd::string_view> | ||
bool operator()(const std::vector<std::string> &owned_value, | ||
const nostd::span<const nostd::string_view> &value) const noexcept | ||
{ | ||
return owned_value.size() == value.size() && | ||
std::equal(owned_value.begin(), owned_value.end(), value.begin(), | ||
[](const std::string &owned_element, nostd::string_view element) { | ||
return owned_element == element; | ||
}); | ||
} | ||
|
||
// Compare nostd::span<const T> and std::vector<T> for arithmatic types | ||
dbarker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
template <typename T> | ||
bool operator()(const std::vector<T> &owned_value, | ||
const nostd::span<const T> &value) const noexcept | ||
{ | ||
return owned_value.size() == value.size() && | ||
std::equal(owned_value.begin(), owned_value.end(), value.begin()); | ||
} | ||
}; | ||
|
||
/** | ||
* Class for storing attributes. | ||
*/ | ||
|
@@ -162,8 +216,36 @@ class AttributeMap : public std::unordered_map<std::string, OwnedAttributeValue> | |
(*this)[std::string(key)] = nostd::visit(converter_, value); | ||
} | ||
|
||
bool EqualTo(const opentelemetry::common::KeyValueIterable &attributes) const noexcept | ||
{ | ||
if (attributes.size() != this->size()) | ||
{ | ||
return false; | ||
} | ||
|
||
const bool is_equal = attributes.ForEachKeyValue( | ||
[this](nostd::string_view key, | ||
const opentelemetry::common::AttributeValue &value) noexcept { | ||
// Perform a linear search to find the key assuming the map is small | ||
// This avoids temporary string creation from this->find(std::string(key)) | ||
for (const auto &kv : *this) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think we can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems we have to start the search from the C++20 gives the "Heterogeneous comparison lookup" version of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the details. Looks ok to me as is. |
||
{ | ||
if (kv.first == key) | ||
{ | ||
// Order of arguments is important here. OwnedAttributeValue is first then | ||
// AttributeValue AttributeEqualToVisitor does not support the reverse order | ||
return nostd::visit(equal_to_visitor_, kv.second, value); | ||
} | ||
} | ||
return false; | ||
}); | ||
|
||
return is_equal; | ||
} | ||
|
||
private: | ||
AttributeConverter converter_; | ||
AttributeEqualToVisitor equal_to_visitor_; | ||
marcalff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
/** | ||
|
Uh oh!
There was an error while loading. Please reload this page.