File: attribute_utils_test.cc

package info (click to toggle)
opentelemetry-cpp 1.19.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,744 kB
  • sloc: cpp: 79,029; sh: 1,640; makefile: 43; python: 31
file content (193 lines) | stat: -rw-r--r-- 9,550 bytes parent folder | download | duplicates (2)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <stdint.h>
#include <array>
#include <initializer_list>
#include <map>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/common/key_value_iterable_view.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/nostd/variant.h"
#include "opentelemetry/sdk/common/attribute_utils.h"

TEST(AttributeMapTest, DefaultConstruction)
{
  opentelemetry::sdk::common::AttributeMap attribute_map;
  EXPECT_EQ(attribute_map.GetAttributes().size(), 0);
}

TEST(OrderedAttributeMapTest, DefaultConstruction)
{
  opentelemetry::sdk::common::OrderedAttributeMap attribute_map;
  EXPECT_EQ(attribute_map.GetAttributes().size(), 0);
}

TEST(AttributeMapTest, AttributesConstruction)
{
  const int kNumAttributes              = 3;
  std::string keys[kNumAttributes]      = {"attr1", "attr2", "attr3"};
  int values[kNumAttributes]            = {15, 24, 37};
  std::map<std::string, int> attributes = {
      {keys[0], values[0]}, {keys[1], values[1]}, {keys[2], values[2]}};

  opentelemetry::common::KeyValueIterableView<std::map<std::string, int>> iterable(attributes);
  opentelemetry::sdk::common::AttributeMap attribute_map(iterable);

  for (int i = 0; i < kNumAttributes; i++)
  {
    EXPECT_EQ(opentelemetry::nostd::get<int>(attribute_map.GetAttributes().at(keys[i])), values[i]);
  }
}

TEST(OrderedAttributeMapTest, AttributesConstruction)
{
  const int kNumAttributes              = 3;
  std::string keys[kNumAttributes]      = {"attr1", "attr2", "attr3"};
  int values[kNumAttributes]            = {15, 24, 37};
  std::map<std::string, int> attributes = {
      {keys[0], values[0]}, {keys[1], values[1]}, {keys[2], values[2]}};

  opentelemetry::common::KeyValueIterableView<std::map<std::string, int>> iterable(attributes);
  opentelemetry::sdk::common::OrderedAttributeMap attribute_map(iterable);

  for (int i = 0; i < kNumAttributes; i++)
  {
    EXPECT_EQ(opentelemetry::nostd::get<int>(attribute_map.GetAttributes().at(keys[i])), values[i]);
  }
}

TEST(AttributeEqualToVisitorTest, AttributeValueEqualTo)
{
  namespace sdk   = opentelemetry::sdk::common;
  namespace api   = opentelemetry::common;
  namespace nostd = opentelemetry::nostd;

  using AV = api::AttributeValue;
  using OV = sdk::OwnedAttributeValue;

  sdk::AttributeEqualToVisitor equal_to_visitor;

  // arithmetic types
  EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{bool(true)}, AV{bool(true)}));
  EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{int32_t(22)}, AV{int32_t(22)}));
  EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{int64_t(22)}, AV{int64_t(22)}));
  EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{uint32_t(22)}, AV{uint32_t(22)}));
  EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{uint64_t(22)}, AV{uint64_t(22)}));
  EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{double(22.0)}, AV{double(22.0)}));

  // string types
  EXPECT_TRUE(opentelemetry::nostd::visit(
      equal_to_visitor, OV{std::string("string to const char*")}, AV{"string to const char*"}));
  EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor,
                                          OV{std::string("string to string_view")},
                                          AV{nostd::string_view("string to string_view")}));

  // container types
  EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<bool>{true, false}},
                                          AV{std::array<const bool, 2>{true, false}}));
  EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<uint8_t>{33, 44}},
                                          AV{std::array<const uint8_t, 2>{33, 44}}));
  EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<int32_t>{33, 44}},
                                          AV{std::array<const int32_t, 2>{33, 44}}));
  EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<int64_t>{33, 44}},
                                          AV{std::array<const int64_t, 2>{33, 44}}));
  EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<uint32_t>{33, 44}},
                                          AV{std::array<const uint32_t, 2>{33, 44}}));
  EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<uint64_t>{33, 44}},
                                          AV{std::array<const uint64_t, 2>{33, 44}}));
  EXPECT_TRUE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<double>{33.0, 44.0}},
                                          AV{std::array<const double, 2>{33.0, 44.0}}));
  EXPECT_TRUE(opentelemetry::nostd::visit(
      equal_to_visitor, OV{std::vector<std::string>{"a string", "another string"}},
      AV{std::array<const nostd::string_view, 2>{"a string", "another string"}}));
}

TEST(AttributeEqualToVisitorTest, AttributeValueNotEqualTo)
{
  namespace sdk   = opentelemetry::sdk::common;
  namespace api   = opentelemetry::common;
  namespace nostd = opentelemetry::nostd;

  using AV = api::AttributeValue;
  using OV = sdk::OwnedAttributeValue;

  sdk::AttributeEqualToVisitor equal_to_visitor;

  // check different values of the same type
  EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{bool(true)}, AV{bool(false)}));
  EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{int32_t(22)}, AV{int32_t(33)}));
  EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{int64_t(22)}, AV{int64_t(33)}));
  EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{uint32_t(22)}, AV{uint32_t(33)}));
  EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{double(22.2)}, AV{double(33.3)}));
  EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::string("string one")},
                                           AV{"another string"}));
  EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::string("string one")},
                                           AV{nostd::string_view("another string")}));

  // check different value types
  EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{bool(true)}, AV{uint32_t(1)}));
  EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{int32_t(22)}, AV{uint32_t(22)}));

  // check containers of different element values
  EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<bool>{true, false}},
                                           AV{std::array<const bool, 2>{false, true}}));
  EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<int32_t>{22, 33}},
                                           AV{std::array<const int32_t, 2>{33, 44}}));
  EXPECT_FALSE(opentelemetry::nostd::visit(
      equal_to_visitor, OV{std::vector<std::string>{"a string", "another string"}},
      AV{std::array<const nostd::string_view, 2>{"a string", "a really different string"}}));

  // check containers of different element types
  EXPECT_FALSE(opentelemetry::nostd::visit(equal_to_visitor, OV{std::vector<int32_t>{22, 33}},
                                           AV{std::array<const uint32_t, 2>{22, 33}}));
}

TEST(AttributeMapTest, EqualTo)
{
  using Attributes = std::initializer_list<
      std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue>>;

  // check for case where both are empty
  Attributes attributes_empty = {};
  auto kv_iterable_empty =
      opentelemetry::common::MakeKeyValueIterableView<Attributes>(attributes_empty);
  opentelemetry::sdk::common::AttributeMap attribute_map_empty(kv_iterable_empty);
  EXPECT_TRUE(attribute_map_empty.EqualTo(kv_iterable_empty));

  // check for equality with a range of attributes and types
  Attributes attributes  = {{"key0", "some value"}, {"key1", 1}, {"key2", 2.0}, {"key3", true}};
  auto kv_iterable_match = opentelemetry::common::MakeKeyValueIterableView<Attributes>(attributes);
  opentelemetry::sdk::common::AttributeMap attribute_map(attributes);
  EXPECT_TRUE(attribute_map.EqualTo(kv_iterable_match));

  // check for several cases where the attributes are different
  Attributes attributes_different_value = {
      {"key0", "some value"}, {"key1", 1}, {"key2", 2.0}, {"key3", false}};
  Attributes attributes_different_type = {
      {"key0", "some value"}, {"key1", 1.0}, {"key2", 2.0}, {"key3", true}};
  Attributes attributes_different_size = {{"key0", "some value"}};

  // check for the case where the number of attributes is the same but all keys are different
  Attributes attributes_different_all = {{"a", "b"}, {"c", "d"}, {"e", 4.0}, {"f", uint8_t(5)}};

  auto kv_iterable_different_value =
      opentelemetry::common::MakeKeyValueIterableView<Attributes>(attributes_different_value);
  auto kv_iterable_different_type =
      opentelemetry::common::MakeKeyValueIterableView<Attributes>(attributes_different_type);
  auto kv_iterable_different_size =
      opentelemetry::common::MakeKeyValueIterableView<Attributes>(attributes_different_size);
  auto kv_iterable_different_all =
      opentelemetry::common::MakeKeyValueIterableView<Attributes>(attributes_different_all);

  EXPECT_FALSE(attribute_map.EqualTo(kv_iterable_different_value));
  EXPECT_FALSE(attribute_map.EqualTo(kv_iterable_different_type));
  EXPECT_FALSE(attribute_map.EqualTo(kv_iterable_different_size));
  EXPECT_FALSE(attribute_map.EqualTo(kv_iterable_different_all));
}