File: instrument_descriptor_test.cc

package info (click to toggle)
opentelemetry-cpp 1.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,372 kB
  • sloc: cpp: 96,239; sh: 1,766; makefile: 36; python: 31
file content (163 lines) | stat: -rw-r--r-- 7,449 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <string>

#include "opentelemetry/sdk/metrics/instruments.h"

using namespace opentelemetry::sdk::metrics;

InstrumentDescriptor CreateInstrumentDescriptor(
    const std::string &name        = "counter",
    const std::string &description = "description",
    const std::string &unit        = "unit",
    InstrumentType type            = InstrumentType::kCounter,
    InstrumentValueType value_type = InstrumentValueType::kLong)
{
  return {name, description, unit, type, value_type};
}

TEST(InstrumentDescriptorUtilTest, CaseInsensitiveAsciiEquals)
{
  // same name
  EXPECT_TRUE(InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals("counter", "counter"));

  // same case-insensitive name
  EXPECT_TRUE(InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals("counter", "COUNTer"));
  EXPECT_TRUE(InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals("CountER", "counter"));

  // different case-insensitive name same string length
  EXPECT_FALSE(InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals("Counter_1", "counter_2"));
  EXPECT_FALSE(InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals("counter_1", "counter_2"));

  // different case-sensitive name different string length
  EXPECT_FALSE(InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals("counter", "Counter1"));
  EXPECT_FALSE(InstrumentDescriptorUtil::CaseInsensitiveAsciiEquals("Counter1", "counter"));
}

// The following tests cover the spec requirements on detecting identical and duplicate instruments
// https://github.com/open-telemetry/opentelemetry-specification/blob/9c8c30631b0e288de93df7452f91ed47f6fba330/specification/metrics/sdk.md?plain=1#L869
TEST(InstrumentDescriptorUtilTest, IsDuplicate)
{
  auto instrument_existing = CreateInstrumentDescriptor("counter");

  // not a duplicate - different name
  auto instrument_different_name  = instrument_existing;
  instrument_different_name.name_ = "another_name";
  EXPECT_FALSE(
      InstrumentDescriptorUtil::IsDuplicate(instrument_existing, instrument_different_name));

  // not a duplicate - identical instrument
  const auto &instrument_identical = instrument_existing;
  EXPECT_FALSE(InstrumentDescriptorUtil::IsDuplicate(instrument_existing, instrument_identical));

  // not a duplicate - instrument with same case-insensitive name
  auto instrument_same_name_case_insensitive  = instrument_existing;
  instrument_same_name_case_insensitive.name_ = "COUNTER";
  EXPECT_FALSE(InstrumentDescriptorUtil::IsDuplicate(instrument_existing,
                                                     instrument_same_name_case_insensitive));

  // is duplicate by description
  auto instrument_different_desc         = instrument_existing;
  instrument_different_desc.description_ = "another_description";
  EXPECT_TRUE(
      InstrumentDescriptorUtil::IsDuplicate(instrument_existing, instrument_different_desc));

  // is duplicate by unit
  auto instrument_different_unit  = instrument_existing;
  instrument_different_unit.unit_ = "another_unit";
  EXPECT_TRUE(
      InstrumentDescriptorUtil::IsDuplicate(instrument_existing, instrument_different_unit));

  // is duplicate by kind - instrument type
  auto instrument_different_type  = instrument_existing;
  instrument_different_type.type_ = InstrumentType::kHistogram;
  EXPECT_TRUE(
      InstrumentDescriptorUtil::IsDuplicate(instrument_existing, instrument_different_type));

  // is duplicate by kind - instrument value_type
  auto instrument_different_valuetype        = instrument_existing;
  instrument_different_valuetype.value_type_ = InstrumentValueType::kDouble;
  EXPECT_TRUE(
      InstrumentDescriptorUtil::IsDuplicate(instrument_existing, instrument_different_valuetype));
}

TEST(InstrumentDescriptorTest, EqualNameCaseInsensitiveOperator)
{
  // equal by name, description, unit, type and value type
  InstrumentEqualNameCaseInsensitive equal_operator{};
  auto instrument_existing         = CreateInstrumentDescriptor("counter");
  const auto &instrument_identical = instrument_existing;
  EXPECT_TRUE(equal_operator(instrument_existing, instrument_identical));

  // equal by name with different case
  auto instrument_name_case_conflict  = instrument_existing;
  instrument_name_case_conflict.name_ = "COUNTER";
  EXPECT_TRUE(equal_operator(instrument_existing, instrument_name_case_conflict));

  // not equal by name
  auto instrument_different_name  = instrument_existing;
  instrument_different_name.name_ = "another_counter";
  EXPECT_FALSE(equal_operator(instrument_existing, instrument_different_name));

  // not equal by instrument value type
  auto instrument_different_valuetype        = instrument_existing;
  instrument_different_valuetype.value_type_ = InstrumentValueType::kDouble;
  EXPECT_FALSE(equal_operator(instrument_existing, instrument_different_valuetype));

  // not equal by instrument type
  auto instrument_different_type  = instrument_existing;
  instrument_different_type.type_ = InstrumentType::kObservableCounter;
  EXPECT_FALSE(equal_operator(instrument_existing, instrument_different_type));

  // not equal by description
  auto instrument_different_desc         = instrument_existing;
  instrument_different_desc.description_ = "another description";
  EXPECT_FALSE(equal_operator(instrument_existing, instrument_different_desc));

  // not equal by unit
  auto instrument_different_unit  = instrument_existing;
  instrument_different_unit.unit_ = "another unit";
  EXPECT_FALSE(equal_operator(instrument_existing, instrument_different_unit));
}

TEST(InstrumentDescriptorTest, HashOperator)
{
  InstrumentDescriptorHash hash_operator{};

  // identical instrument - hash must match
  auto instrument_existing         = CreateInstrumentDescriptor("counter");
  const auto &instrument_identical = instrument_existing;
  EXPECT_EQ(hash_operator(instrument_existing), hash_operator(instrument_identical));

  // name case conflict - hash must match
  auto instrument_name_case_conflict  = instrument_existing;
  instrument_name_case_conflict.name_ = "COUNTER";
  EXPECT_EQ(hash_operator(instrument_existing), hash_operator(instrument_name_case_conflict));

  // different name
  auto instrument_different_name  = instrument_existing;
  instrument_different_name.name_ = "another_counter";
  EXPECT_NE(hash_operator(instrument_existing), hash_operator(instrument_different_name));

  // different kind - instrument value type
  auto instrument_different_valuetype        = instrument_existing;
  instrument_different_valuetype.value_type_ = InstrumentValueType::kFloat;
  EXPECT_NE(hash_operator(instrument_existing), hash_operator(instrument_different_valuetype));

  // different kind - instrument type
  auto instrument_different_type  = instrument_existing;
  instrument_different_type.type_ = InstrumentType::kObservableUpDownCounter;
  EXPECT_NE(hash_operator(instrument_existing), hash_operator(instrument_different_type));

  // different description
  auto instrument_different_desc         = instrument_existing;
  instrument_different_desc.description_ = "another description";
  EXPECT_NE(hash_operator(instrument_existing), hash_operator(instrument_different_desc));

  // different unit
  auto instrument_different_unit  = instrument_existing;
  instrument_different_unit.unit_ = "another unit";
  EXPECT_NE(hash_operator(instrument_existing), hash_operator(instrument_different_unit));
}