instrumentationscope_test.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include <gtest/gtest.h>
  4. #include <stdint.h>
  5. #include <initializer_list>
  6. #include <memory>
  7. #include <string>
  8. #include <unordered_map>
  9. #include <utility>
  10. #include <vector>
  11. #include "opentelemetry/common/attribute_value.h"
  12. #include "opentelemetry/common/key_value_iterable_view.h"
  13. #include "opentelemetry/nostd/span.h"
  14. #include "opentelemetry/nostd/string_view.h"
  15. #include "opentelemetry/nostd/variant.h"
  16. #include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
  17. #include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
  18. using namespace opentelemetry;
  19. using namespace opentelemetry::sdk::instrumentationscope;
  20. TEST(InstrumentationScope, CreateInstrumentationScope)
  21. {
  22. std::string library_name = "opentelemetry-cpp";
  23. std::string library_version = "0.1.0";
  24. std::string schema_url = "https://opentelemetry.io/schemas/1.2.0";
  25. uint32_t attrubite_value3[] = {7, 8, 9};
  26. auto instrumentation_scope = InstrumentationScope::Create(
  27. library_name, library_version, schema_url,
  28. {{"attribute-key1", "attribute-value"},
  29. {"attribute-key2", static_cast<int32_t>(123)},
  30. {"attribute-key3", opentelemetry::nostd::span<uint32_t>(attrubite_value3)}});
  31. EXPECT_EQ(instrumentation_scope->GetName(), library_name);
  32. EXPECT_EQ(instrumentation_scope->GetVersion(), library_version);
  33. EXPECT_EQ(instrumentation_scope->GetSchemaURL(), schema_url);
  34. auto attribute1 = instrumentation_scope->GetAttributes().find("attribute-key1");
  35. auto attribute2 = instrumentation_scope->GetAttributes().find("attribute-key2");
  36. auto attribute3 = instrumentation_scope->GetAttributes().find("attribute-key3");
  37. ASSERT_FALSE(attribute1 == instrumentation_scope->GetAttributes().end());
  38. ASSERT_TRUE(opentelemetry::nostd::holds_alternative<std::string>(attribute1->second));
  39. EXPECT_EQ(opentelemetry::nostd::get<std::string>(attribute1->second), "attribute-value");
  40. ASSERT_FALSE(attribute2 == instrumentation_scope->GetAttributes().end());
  41. ASSERT_TRUE(opentelemetry::nostd::holds_alternative<int32_t>(attribute2->second));
  42. EXPECT_EQ(opentelemetry::nostd::get<int32_t>(attribute2->second), 123);
  43. ASSERT_FALSE(attribute3 == instrumentation_scope->GetAttributes().end());
  44. ASSERT_TRUE(opentelemetry::nostd::holds_alternative<std::vector<uint32_t>>(attribute3->second));
  45. const std::vector<uint32_t> &attribute3_values =
  46. opentelemetry::nostd::get<std::vector<uint32_t>>(attribute3->second);
  47. EXPECT_EQ(attribute3_values.size(), 3);
  48. for (std::vector<uint32_t>::size_type i = 0; i < 3; ++i)
  49. {
  50. EXPECT_EQ(attribute3_values[i], attrubite_value3[i]);
  51. }
  52. }
  53. TEST(InstrumentationScope, CreateInstrumentationScopeWithLoopForAttributes)
  54. {
  55. std::string library_name = "opentelemetry-cpp";
  56. std::string library_version = "0.1.0";
  57. std::string schema_url = "https://opentelemetry.io/schemas/1.2.0";
  58. uint32_t attrubite_value3[] = {7, 8, 9};
  59. std::unordered_map<std::string, opentelemetry::common::AttributeValue> attributes = {
  60. {"attribute-key1", "attribute-value"},
  61. {"attribute-key2", static_cast<int32_t>(123)},
  62. {"attribute-key3", opentelemetry::nostd::span<uint32_t>(attrubite_value3)}};
  63. auto instrumentation_scope =
  64. InstrumentationScope::Create(library_name, library_version, schema_url, attributes);
  65. EXPECT_EQ(instrumentation_scope->GetName(), library_name);
  66. EXPECT_EQ(instrumentation_scope->GetVersion(), library_version);
  67. EXPECT_EQ(instrumentation_scope->GetSchemaURL(), schema_url);
  68. auto attribute1 = instrumentation_scope->GetAttributes().find("attribute-key1");
  69. auto attribute2 = instrumentation_scope->GetAttributes().find("attribute-key2");
  70. auto attribute3 = instrumentation_scope->GetAttributes().find("attribute-key3");
  71. ASSERT_FALSE(attribute1 == instrumentation_scope->GetAttributes().end());
  72. ASSERT_TRUE(opentelemetry::nostd::holds_alternative<std::string>(attribute1->second));
  73. EXPECT_EQ(opentelemetry::nostd::get<std::string>(attribute1->second), "attribute-value");
  74. ASSERT_FALSE(attribute2 == instrumentation_scope->GetAttributes().end());
  75. ASSERT_TRUE(opentelemetry::nostd::holds_alternative<int32_t>(attribute2->second));
  76. EXPECT_EQ(opentelemetry::nostd::get<int32_t>(attribute2->second), 123);
  77. ASSERT_FALSE(attribute3 == instrumentation_scope->GetAttributes().end());
  78. ASSERT_TRUE(opentelemetry::nostd::holds_alternative<std::vector<uint32_t>>(attribute3->second));
  79. const std::vector<uint32_t> &attribute3_values =
  80. opentelemetry::nostd::get<std::vector<uint32_t>>(attribute3->second);
  81. EXPECT_EQ(attribute3_values.size(), 3);
  82. for (std::vector<uint32_t>::size_type i = 0; i < 3; ++i)
  83. {
  84. EXPECT_EQ(attribute3_values[i], attrubite_value3[i]);
  85. }
  86. }
  87. TEST(InstrumentationScope, CreateInstrumentationScopeWithKeyValueIterableAttributes)
  88. {
  89. std::string library_name = "opentelemetry-cpp";
  90. std::string library_version = "0.1.0";
  91. std::string schema_url = "https://opentelemetry.io/schemas/1.2.0";
  92. uint32_t attrubite_value3[] = {7, 8, 9};
  93. std::unordered_map<std::string, opentelemetry::common::AttributeValue> attributes = {
  94. {"attribute-key1", "attribute-value"},
  95. {"attribute-key2", static_cast<int32_t>(123)},
  96. {"attribute-key3", opentelemetry::nostd::span<uint32_t>(attrubite_value3)}};
  97. opentelemetry::common::KeyValueIterableView<
  98. std::unordered_map<std::string, opentelemetry::common::AttributeValue>>
  99. attributes_view{attributes};
  100. auto instrumentation_scope =
  101. InstrumentationScope::Create(library_name, library_version, schema_url, attributes_view);
  102. EXPECT_EQ(instrumentation_scope->GetName(), library_name);
  103. EXPECT_EQ(instrumentation_scope->GetVersion(), library_version);
  104. EXPECT_EQ(instrumentation_scope->GetSchemaURL(), schema_url);
  105. auto attribute1 = instrumentation_scope->GetAttributes().find("attribute-key1");
  106. auto attribute2 = instrumentation_scope->GetAttributes().find("attribute-key2");
  107. auto attribute3 = instrumentation_scope->GetAttributes().find("attribute-key3");
  108. ASSERT_FALSE(attribute1 == instrumentation_scope->GetAttributes().end());
  109. ASSERT_TRUE(opentelemetry::nostd::holds_alternative<std::string>(attribute1->second));
  110. EXPECT_EQ(opentelemetry::nostd::get<std::string>(attribute1->second), "attribute-value");
  111. ASSERT_FALSE(attribute2 == instrumentation_scope->GetAttributes().end());
  112. ASSERT_TRUE(opentelemetry::nostd::holds_alternative<int32_t>(attribute2->second));
  113. EXPECT_EQ(opentelemetry::nostd::get<int32_t>(attribute2->second), 123);
  114. ASSERT_FALSE(attribute3 == instrumentation_scope->GetAttributes().end());
  115. ASSERT_TRUE(opentelemetry::nostd::holds_alternative<std::vector<uint32_t>>(attribute3->second));
  116. const std::vector<uint32_t> &attribute3_values =
  117. opentelemetry::nostd::get<std::vector<uint32_t>>(attribute3->second);
  118. EXPECT_EQ(attribute3_values.size(), 3);
  119. for (std::vector<uint32_t>::size_type i = 0; i < 3; ++i)
  120. {
  121. EXPECT_EQ(attribute3_values[i], attrubite_value3[i]);
  122. }
  123. }
  124. TEST(InstrumentationScope, SetAttribute)
  125. {
  126. std::string library_name = "opentelemetry-cpp";
  127. std::string library_version = "0.1.0";
  128. std::string schema_url = "https://opentelemetry.io/schemas/1.2.0";
  129. uint32_t attrubite_value3[] = {7, 8, 9};
  130. auto instrumentation_scope =
  131. InstrumentationScope::Create(library_name, library_version, schema_url);
  132. EXPECT_EQ(instrumentation_scope->GetName(), library_name);
  133. EXPECT_EQ(instrumentation_scope->GetVersion(), library_version);
  134. EXPECT_EQ(instrumentation_scope->GetSchemaURL(), schema_url);
  135. EXPECT_EQ(instrumentation_scope->GetAttributes().size(), 0);
  136. instrumentation_scope->SetAttribute("attribute-key1", "attribute-value");
  137. instrumentation_scope->SetAttribute("attribute-key2", static_cast<int32_t>(123));
  138. instrumentation_scope->SetAttribute("attribute-key3",
  139. opentelemetry::nostd::span<uint32_t>(attrubite_value3));
  140. EXPECT_EQ(instrumentation_scope->GetAttributes().size(), 3);
  141. auto attribute1 = instrumentation_scope->GetAttributes().find("attribute-key1");
  142. auto attribute2 = instrumentation_scope->GetAttributes().find("attribute-key2");
  143. auto attribute3 = instrumentation_scope->GetAttributes().find("attribute-key3");
  144. ASSERT_FALSE(attribute1 == instrumentation_scope->GetAttributes().end());
  145. ASSERT_TRUE(opentelemetry::nostd::holds_alternative<std::string>(attribute1->second));
  146. EXPECT_EQ(opentelemetry::nostd::get<std::string>(attribute1->second), "attribute-value");
  147. ASSERT_FALSE(attribute2 == instrumentation_scope->GetAttributes().end());
  148. ASSERT_TRUE(opentelemetry::nostd::holds_alternative<int32_t>(attribute2->second));
  149. EXPECT_EQ(opentelemetry::nostd::get<int32_t>(attribute2->second), 123);
  150. ASSERT_FALSE(attribute3 == instrumentation_scope->GetAttributes().end());
  151. ASSERT_TRUE(opentelemetry::nostd::holds_alternative<std::vector<uint32_t>>(attribute3->second));
  152. const std::vector<uint32_t> &attribute3_values =
  153. opentelemetry::nostd::get<std::vector<uint32_t>>(attribute3->second);
  154. EXPECT_EQ(attribute3_values.size(), 3);
  155. for (std::vector<uint32_t>::size_type i = 0; i < 3; ++i)
  156. {
  157. EXPECT_EQ(attribute3_values[i], attrubite_value3[i]);
  158. }
  159. }
  160. TEST(InstrumentationScope, LegacyInstrumentationLibrary)
  161. {
  162. std::string library_name = "opentelemetry-cpp";
  163. std::string library_version = "0.1.0";
  164. std::string schema_url = "https://opentelemetry.io/schemas/1.2.0";
  165. auto instrumentation_library =
  166. opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create(
  167. library_name, library_version, schema_url);
  168. EXPECT_EQ(instrumentation_library->GetName(), library_name);
  169. EXPECT_EQ(instrumentation_library->GetVersion(), library_version);
  170. EXPECT_EQ(instrumentation_library->GetSchemaURL(), schema_url);
  171. }
  172. TEST(InstrumentationScope, Equal)
  173. {
  174. using Attributes = std::initializer_list<
  175. std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue>>;
  176. Attributes attributes_empty = {};
  177. Attributes attributes_match = {
  178. {"key0", "some value"}, {"key1", 1}, {"key2", 2.0}, {"key3", true}};
  179. Attributes attributes_different = {{"key42", "some other"}};
  180. auto kv_iterable_empty =
  181. opentelemetry::common::MakeKeyValueIterableView<Attributes>(attributes_empty);
  182. auto kv_iterable_match =
  183. opentelemetry::common::MakeKeyValueIterableView<Attributes>(attributes_match);
  184. auto kv_iterable_different =
  185. opentelemetry::common::MakeKeyValueIterableView<Attributes>(attributes_different);
  186. // try with no attributes added to the instrumentation scope
  187. auto instrumentation_scope =
  188. opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
  189. "library_name", "library_version", "schema_url");
  190. // the instrumentation scope is equal if all parameters are equal (must handle nullptr attributes
  191. // or empty attributes)
  192. EXPECT_TRUE(instrumentation_scope->equal("library_name", "library_version", "schema_url"));
  193. EXPECT_TRUE(
  194. instrumentation_scope->equal("library_name", "library_version", "schema_url", nullptr));
  195. EXPECT_TRUE(instrumentation_scope->equal("library_name", "library_version", "schema_url",
  196. &kv_iterable_empty));
  197. // the instrumentation scope is not equal if any parameter is different
  198. EXPECT_FALSE(instrumentation_scope->equal("library_name", ""));
  199. EXPECT_FALSE(instrumentation_scope->equal("library_name", "library_version", ""));
  200. EXPECT_FALSE(instrumentation_scope->equal("library_name", "library_version", "schema_url",
  201. &kv_iterable_different));
  202. // try with attributes added to the instrumentation scope
  203. auto instrumentation_scope_w_attributes =
  204. opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
  205. "library_name", "library_version", "schema_url", attributes_match);
  206. // the instrumentation scope is equal if all parameters including all attribute keys, types, and
  207. // values are equal
  208. EXPECT_TRUE(instrumentation_scope_w_attributes->equal("library_name", "library_version",
  209. "schema_url", &kv_iterable_match));
  210. EXPECT_FALSE(instrumentation_scope_w_attributes->equal("library_name", "library_version",
  211. "schema_url", nullptr));
  212. EXPECT_FALSE(instrumentation_scope_w_attributes->equal("library_name", "library_version",
  213. "schema_url", &kv_iterable_empty));
  214. EXPECT_FALSE(instrumentation_scope_w_attributes->equal("library_name", "library_version",
  215. "schema_url", &kv_iterable_different));
  216. }
  217. TEST(InstrumentationScope, OperatorEqual)
  218. {
  219. using Attributes = std::initializer_list<
  220. std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue>>;
  221. Attributes attributes_empty = {};
  222. Attributes attributes_match = {
  223. {"key0", "some value"}, {"key1", 1}, {"key2", 2.0}, {"key3", true}};
  224. Attributes attributes_different = {{"key42", "some other"}};
  225. auto instrumentation_scope_1a =
  226. opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
  227. "library_name", "library_version", "schema_url");
  228. auto instrumentation_scope_1b =
  229. opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
  230. "library_name", "library_version", "schema_url");
  231. auto instrumentation_scope_2a =
  232. opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
  233. "library_name_2", "library_version", "schema_url");
  234. auto instrumentation_scope_2b =
  235. opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
  236. "library_name_2", "library_version", "schema_url", attributes_empty);
  237. auto instrumentation_scope_3a =
  238. opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
  239. "library_name_2", "library_version", "schema_url", attributes_match);
  240. auto instrumentation_scope_3b =
  241. opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
  242. "library_name_2", "library_version", "schema_url", attributes_match);
  243. auto instrumentation_scope_4 =
  244. opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
  245. "library_name_2", "library_version", "schema_url", attributes_different);
  246. EXPECT_EQ(*instrumentation_scope_1a, *instrumentation_scope_1b);
  247. EXPECT_FALSE(*instrumentation_scope_1a == *instrumentation_scope_2a);
  248. EXPECT_EQ(*instrumentation_scope_2a, *instrumentation_scope_2b);
  249. EXPECT_EQ(*instrumentation_scope_3a, *instrumentation_scope_3b);
  250. EXPECT_FALSE(*instrumentation_scope_3a == *instrumentation_scope_4);
  251. }