attribute_utils_test.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include <gtest/gtest.h>
  4. #include <stdint.h>
  5. #include <array>
  6. #include <initializer_list>
  7. #include <map>
  8. #include <string>
  9. #include <unordered_map>
  10. #include <utility>
  11. #include <vector>
  12. #include "opentelemetry/common/attribute_value.h"
  13. #include "opentelemetry/common/key_value_iterable_view.h"
  14. #include "opentelemetry/nostd/string_view.h"
  15. #include "opentelemetry/nostd/variant.h"
  16. #include "opentelemetry/sdk/common/attribute_utils.h"
  17. TEST(AttributeMapTest, DefaultConstruction)
  18. {
  19. opentelemetry::sdk::common::AttributeMap attribute_map;
  20. EXPECT_EQ(attribute_map.GetAttributes().size(), 0);
  21. }
  22. TEST(OrderedAttributeMapTest, DefaultConstruction)
  23. {
  24. opentelemetry::sdk::common::OrderedAttributeMap attribute_map;
  25. EXPECT_EQ(attribute_map.GetAttributes().size(), 0);
  26. }
  27. TEST(AttributeMapTest, AttributesConstruction)
  28. {
  29. const int kNumAttributes = 3;
  30. std::string keys[kNumAttributes] = {"attr1", "attr2", "attr3"};
  31. int values[kNumAttributes] = {15, 24, 37};
  32. std::map<std::string, int> attributes = {
  33. {keys[0], values[0]}, {keys[1], values[1]}, {keys[2], values[2]}};
  34. opentelemetry::common::KeyValueIterableView<std::map<std::string, int>> iterable(attributes);
  35. opentelemetry::sdk::common::AttributeMap attribute_map(iterable);
  36. for (int i = 0; i < kNumAttributes; i++)
  37. {
  38. EXPECT_EQ(opentelemetry::nostd::get<int>(attribute_map.GetAttributes().at(keys[i])), values[i]);
  39. }
  40. }
  41. TEST(OrderedAttributeMapTest, AttributesConstruction)
  42. {
  43. const int kNumAttributes = 3;
  44. std::string keys[kNumAttributes] = {"attr1", "attr2", "attr3"};
  45. int values[kNumAttributes] = {15, 24, 37};
  46. std::map<std::string, int> attributes = {
  47. {keys[0], values[0]}, {keys[1], values[1]}, {keys[2], values[2]}};
  48. opentelemetry::common::KeyValueIterableView<std::map<std::string, int>> iterable(attributes);
  49. opentelemetry::sdk::common::OrderedAttributeMap attribute_map(iterable);
  50. for (int i = 0; i < kNumAttributes; i++)
  51. {
  52. EXPECT_EQ(opentelemetry::nostd::get<int>(attribute_map.GetAttributes().at(keys[i])), values[i]);
  53. }
  54. }
  55. TEST(AttributeEqualToVisitorTest, AttributeValueEqualTo)
  56. {
  57. namespace sdk = opentelemetry::sdk::common;
  58. namespace api = opentelemetry::common;
  59. namespace nostd = opentelemetry::nostd;
  60. using AV = api::AttributeValue;
  61. using OV = sdk::OwnedAttributeValue;
  62. sdk::AttributeEqualToVisitor equal_to_visitor;
  63. // arithmetic types
  64. EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{bool(true)}, AV{bool(true)}));
  65. EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{int32_t(22)}, AV{int32_t(22)}));
  66. EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{int64_t(22)}, AV{int64_t(22)}));
  67. EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{uint32_t(22)}, AV{uint32_t(22)}));
  68. EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{uint64_t(22)}, AV{uint64_t(22)}));
  69. EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{double(22.0)}, AV{double(22.0)}));
  70. // string types
  71. EXPECT_TRUE(opentelemetry::nostd::visit(
  72. equal_to_visitor, OV{std::string("string to const char*")}, AV{"string to const char*"}));
  73. EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor,
  74. OV{std::string("string to string_view")},
  75. AV{nostd::string_view("string to string_view")}));
  76. // container types
  77. EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<bool>{true, false}},
  78. AV{std::array<const bool, 2>{true, false}}));
  79. EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<uint8_t>{33, 44}},
  80. AV{std::array<const uint8_t, 2>{33, 44}}));
  81. EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<int32_t>{33, 44}},
  82. AV{std::array<const int32_t, 2>{33, 44}}));
  83. EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<int64_t>{33, 44}},
  84. AV{std::array<const int64_t, 2>{33, 44}}));
  85. EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<uint32_t>{33, 44}},
  86. AV{std::array<const uint32_t, 2>{33, 44}}));
  87. EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<uint64_t>{33, 44}},
  88. AV{std::array<const uint64_t, 2>{33, 44}}));
  89. EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<double>{33.0, 44.0}},
  90. AV{std::array<const double, 2>{33.0, 44.0}}));
  91. EXPECT_TRUE(opentelemetry::nostd::visit(
  92. equal_to_visitor, OV{std::vector<std::string>{"a string", "another string"}},
  93. AV{std::array<const nostd::string_view, 2>{"a string", "another string"}}));
  94. }
  95. TEST(AttributeEqualToVisitorTest, AttributeValueNotEqualTo)
  96. {
  97. namespace sdk = opentelemetry::sdk::common;
  98. namespace api = opentelemetry::common;
  99. namespace nostd = opentelemetry::nostd;
  100. using AV = api::AttributeValue;
  101. using OV = sdk::OwnedAttributeValue;
  102. sdk::AttributeEqualToVisitor equal_to_visitor;
  103. // check different values of the same type
  104. EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{bool(true)}, AV{bool(false)}));
  105. EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{int32_t(22)}, AV{int32_t(33)}));
  106. EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{int64_t(22)}, AV{int64_t(33)}));
  107. EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{uint32_t(22)}, AV{uint32_t(33)}));
  108. EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{double(22.2)}, AV{double(33.3)}));
  109. EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::string("string one")},
  110. AV{"another string"}));
  111. EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::string("string one")},
  112. AV{nostd::string_view("another string")}));
  113. // check different value types
  114. EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{bool(true)}, AV{uint32_t(1)}));
  115. EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{int32_t(22)}, AV{uint32_t(22)}));
  116. // check containers of different element values
  117. EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<bool>{true, false}},
  118. AV{std::array<const bool, 2>{false, true}}));
  119. EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<int32_t>{22, 33}},
  120. AV{std::array<const int32_t, 2>{33, 44}}));
  121. EXPECT_FALSE(opentelemetry::nostd::visit(
  122. equal_to_visitor, OV{std::vector<std::string>{"a string", "another string"}},
  123. AV{std::array<const nostd::string_view, 2>{"a string", "a really different string"}}));
  124. // check containers of different element types
  125. EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<int32_t>{22, 33}},
  126. AV{std::array<const uint32_t, 2>{22, 33}}));
  127. }
  128. TEST(AttributeMapTest, EqualTo)
  129. {
  130. using Attributes = std::initializer_list<
  131. std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue>>;
  132. // check for case where both are empty
  133. Attributes attributes_empty = {};
  134. auto kv_iterable_empty =
  135. opentelemetry::common::MakeKeyValueIterableView<Attributes>(attributes_empty);
  136. opentelemetry::sdk::common::AttributeMap attribute_map_empty(kv_iterable_empty);
  137. EXPECT_TRUE(attribute_map_empty.EqualTo(kv_iterable_empty));
  138. // check for equality with a range of attributes and types
  139. Attributes attributes = {{"key0", "some value"}, {"key1", 1}, {"key2", 2.0}, {"key3", true}};
  140. auto kv_iterable_match = opentelemetry::common::MakeKeyValueIterableView<Attributes>(attributes);
  141. opentelemetry::sdk::common::AttributeMap attribute_map(attributes);
  142. EXPECT_TRUE(attribute_map.EqualTo(kv_iterable_match));
  143. // check for several cases where the attributes are different
  144. Attributes attributes_different_value = {
  145. {"key0", "some value"}, {"key1", 1}, {"key2", 2.0}, {"key3", false}};
  146. Attributes attributes_different_type = {
  147. {"key0", "some value"}, {"key1", 1.0}, {"key2", 2.0}, {"key3", true}};
  148. Attributes attributes_different_size = {{"key0", "some value"}};
  149. // check for the case where the number of attributes is the same but all keys are different
  150. Attributes attributes_different_all = {{"a", "b"}, {"c", "d"}, {"e", 4.0}, {"f", uint8_t(5)}};
  151. auto kv_iterable_different_value =
  152. opentelemetry::common::MakeKeyValueIterableView<Attributes>(attributes_different_value);
  153. auto kv_iterable_different_type =
  154. opentelemetry::common::MakeKeyValueIterableView<Attributes>(attributes_different_type);
  155. auto kv_iterable_different_size =
  156. opentelemetry::common::MakeKeyValueIterableView<Attributes>(attributes_different_size);
  157. auto kv_iterable_different_all =
  158. opentelemetry::common::MakeKeyValueIterableView<Attributes>(attributes_different_all);
  159. EXPECT_FALSE(attribute_map.EqualTo(kv_iterable_different_value));
  160. EXPECT_FALSE(attribute_map.EqualTo(kv_iterable_different_type));
  161. EXPECT_FALSE(attribute_map.EqualTo(kv_iterable_different_size));
  162. EXPECT_FALSE(attribute_map.EqualTo(kv_iterable_different_all));
  163. }