instrument_descriptor_test.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include <gtest/gtest.h>
  4. #include <string>
  5. #include "opentelemetry/sdk/metrics/instruments.h"
  6. using namespace opentelemetry::sdk::metrics;
  7. InstrumentDescriptor CreateInstrumentDescriptor(
  8. const std::string &name = "counter",
  9. const std::string &description = "description",
  10. const std::string &unit = "unit",
  11. InstrumentType type = InstrumentType::kCounter,
  12. InstrumentValueType value_type = InstrumentValueType::kLong)
  13. {
  14. return {name, description, unit, type, value_type};
  15. }
  16. TEST(InstrumentDescriptorUtilTest, CaseInsensitiveAsciiEquals)
  17. {
  18. // same name
  19. EXPECT_TRUE(InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals("counter", "counter"));
  20. // same case-insensitive name
  21. EXPECT_TRUE(InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals("counter", "COUNTer"));
  22. EXPECT_TRUE(InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals("CountER", "counter"));
  23. // different case-insensitive name same string length
  24. EXPECT_FALSE(InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals("Counter_1", "counter_2"));
  25. EXPECT_FALSE(InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals("counter_1", "counter_2"));
  26. // different case-sensitive name different string length
  27. EXPECT_FALSE(InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals("counter", "Counter1"));
  28. EXPECT_FALSE(InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals("Counter1", "counter"));
  29. }
  30. // The following tests cover the spec requirements on detecting identical and duplicate instruments
  31. // https://github.com/open-telemetry/opentelemetry-specification/blob/9c8c30631b0e288de93df7452f91ed47f6fba330/specification/metrics/sdk.md?plain=1#L869
  32. TEST(InstrumentDescriptorUtilTest, IsDuplicate)
  33. {
  34. auto instrument_existing = CreateInstrumentDescriptor("counter");
  35. // not a duplicate - different name
  36. auto instrument_different_name = instrument_existing;
  37. instrument_different_name.name_ = "another_name";
  38. EXPECT_FALSE(
  39. InstrumentDescriptorUtil::IsDuplicate(instrument_existing, instrument_different_name));
  40. // not a duplicate - identical instrument
  41. auto instrument_identical = instrument_existing;
  42. EXPECT_FALSE(InstrumentDescriptorUtil::IsDuplicate(instrument_existing, instrument_identical));
  43. // not a duplicate - instrument with same case-insensitive name
  44. auto instrument_same_name_case_insensitive = instrument_existing;
  45. instrument_same_name_case_insensitive.name_ = "COUNTER";
  46. EXPECT_FALSE(InstrumentDescriptorUtil::IsDuplicate(instrument_existing,
  47. instrument_same_name_case_insensitive));
  48. // is duplicate by description
  49. auto instrument_different_desc = instrument_existing;
  50. instrument_different_desc.description_ = "another_description";
  51. EXPECT_TRUE(
  52. InstrumentDescriptorUtil::IsDuplicate(instrument_existing, instrument_different_desc));
  53. // is duplicate by unit
  54. auto instrument_different_unit = instrument_existing;
  55. instrument_different_unit.unit_ = "another_unit";
  56. EXPECT_TRUE(
  57. InstrumentDescriptorUtil::IsDuplicate(instrument_existing, instrument_different_unit));
  58. // is duplicate by kind - instrument type
  59. auto instrument_different_type = instrument_existing;
  60. instrument_different_type.type_ = InstrumentType::kHistogram;
  61. EXPECT_TRUE(
  62. InstrumentDescriptorUtil::IsDuplicate(instrument_existing, instrument_different_type));
  63. // is duplicate by kind - instrument value_type
  64. auto instrument_different_valuetype = instrument_existing;
  65. instrument_different_valuetype.value_type_ = InstrumentValueType::kDouble;
  66. EXPECT_TRUE(
  67. InstrumentDescriptorUtil::IsDuplicate(instrument_existing, instrument_different_valuetype));
  68. }
  69. TEST(InstrumentDescriptorTest, EqualNameCaseInsensitiveOperator)
  70. {
  71. // equal by name, description, unit, type and value type
  72. InstrumentEqualNameCaseInsensitive equal_operator{};
  73. auto instrument_existing = CreateInstrumentDescriptor("counter");
  74. auto instrument_identical = instrument_existing;
  75. EXPECT_TRUE(equal_operator(instrument_existing, instrument_identical));
  76. // equal by name with different case
  77. auto instrument_name_case_conflict = instrument_existing;
  78. instrument_name_case_conflict.name_ = "COUNTER";
  79. EXPECT_TRUE(equal_operator(instrument_existing, instrument_name_case_conflict));
  80. // not equal by name
  81. auto instrument_different_name = instrument_existing;
  82. instrument_different_name.name_ = "another_counter";
  83. EXPECT_FALSE(equal_operator(instrument_existing, instrument_different_name));
  84. // not equal by instrument value type
  85. auto instrument_different_valuetype = instrument_existing;
  86. instrument_different_valuetype.value_type_ = InstrumentValueType::kDouble;
  87. EXPECT_FALSE(equal_operator(instrument_existing, instrument_different_valuetype));
  88. // not equal by instrument type
  89. auto instrument_different_type = instrument_existing;
  90. instrument_different_type.type_ = InstrumentType::kObservableCounter;
  91. EXPECT_FALSE(equal_operator(instrument_existing, instrument_different_type));
  92. // not equal by description
  93. auto instrument_different_desc = instrument_existing;
  94. instrument_different_desc.description_ = "another description";
  95. EXPECT_FALSE(equal_operator(instrument_existing, instrument_different_desc));
  96. // not equal by unit
  97. auto instrument_different_unit = instrument_existing;
  98. instrument_different_unit.unit_ = "another unit";
  99. EXPECT_FALSE(equal_operator(instrument_existing, instrument_different_unit));
  100. }
  101. TEST(InstrumentDescriptorTest, HashOperator)
  102. {
  103. InstrumentDescriptorHash hash_operator{};
  104. // identical instrument - hash must match
  105. auto instrument_existing = CreateInstrumentDescriptor("counter");
  106. auto instrument_identical = instrument_existing;
  107. EXPECT_EQ(hash_operator(instrument_existing), hash_operator(instrument_identical));
  108. // name case conflict - hash must match
  109. auto instrument_name_case_conflict = instrument_existing;
  110. instrument_name_case_conflict.name_ = "COUNTER";
  111. EXPECT_EQ(hash_operator(instrument_existing), hash_operator(instrument_name_case_conflict));
  112. // different name
  113. auto instrument_different_name = instrument_existing;
  114. instrument_different_name.name_ = "another_counter";
  115. EXPECT_NE(hash_operator(instrument_existing), hash_operator(instrument_different_name));
  116. // different kind - instrument value type
  117. auto instrument_different_valuetype = instrument_existing;
  118. instrument_different_valuetype.value_type_ = InstrumentValueType::kFloat;
  119. EXPECT_NE(hash_operator(instrument_existing), hash_operator(instrument_different_valuetype));
  120. // different kind - instrument type
  121. auto instrument_different_type = instrument_existing;
  122. instrument_different_type.type_ = InstrumentType::kObservableUpDownCounter;
  123. EXPECT_NE(hash_operator(instrument_existing), hash_operator(instrument_different_type));
  124. // different description
  125. auto instrument_different_desc = instrument_existing;
  126. instrument_different_desc.description_ = "another description";
  127. EXPECT_NE(hash_operator(instrument_existing), hash_operator(instrument_different_desc));
  128. // different unit
  129. auto instrument_different_unit = instrument_existing;
  130. instrument_different_unit.unit_ = "another unit";
  131. EXPECT_NE(hash_operator(instrument_existing), hash_operator(instrument_different_unit));
  132. }