File: attributes_hashmap_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 (224 lines) | stat: -rw-r--r-- 7,874 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <stddef.h>
#include <stdint.h>
#include <functional>
#include <memory>
#include <string>
#include <utility>

#include "opentelemetry/nostd/function_ref.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/common/attributemap_hash.h"
#include "opentelemetry/sdk/metrics/aggregation/aggregation.h"
#include "opentelemetry/sdk/metrics/aggregation/drop_aggregation.h"
#include "opentelemetry/sdk/metrics/state/attributes_hashmap.h"
#include "opentelemetry/sdk/metrics/view/attributes_processor.h"

using namespace opentelemetry::sdk::metrics;
namespace nostd = opentelemetry::nostd;

TEST(AttributesHashMap, BasicTests)
{
  // Empty map
  AttributesHashMap hash_map;
  EXPECT_EQ(hash_map.Size(), 0);
  MetricAttributes m1 = {{"k1", "v1"}};

  EXPECT_EQ(hash_map.Get(m1), nullptr);
  EXPECT_EQ(hash_map.Has(m1), false);

  // Set
  std::unique_ptr<Aggregation> aggregation1(
      new DropAggregation());  //  = std::unique_ptr<Aggregation>(new DropAggregation);
  hash_map.Set(m1, std::move(aggregation1));
  hash_map.Get(m1)->Aggregate(static_cast<int64_t>(1));
  EXPECT_EQ(hash_map.Size(), 1);
  EXPECT_EQ(hash_map.Has(m1), true);

  // Set same key again
  auto aggregation2 = std::unique_ptr<Aggregation>(new DropAggregation());
  hash_map.Set(m1, std::move(aggregation2));
  hash_map.Get(m1)->Aggregate(static_cast<int64_t>(1));
  EXPECT_EQ(hash_map.Size(), 1);
  EXPECT_EQ(hash_map.Has(m1), true);

  // Set more enteria
  auto aggregation3   = std::unique_ptr<Aggregation>(new DropAggregation());
  MetricAttributes m3 = {{"k1", "v1"}, {"k2", "v2"}};
  hash_map.Set(m3, std::move(aggregation3));
  EXPECT_EQ(hash_map.Has(m1), true);
  EXPECT_EQ(hash_map.Has(m3), true);
  hash_map.Get(m3)->Aggregate(static_cast<int64_t>(1));
  EXPECT_EQ(hash_map.Size(), 2);

  // GetOrSetDefault
  std::function<std::unique_ptr<Aggregation>()> create_default_aggregation =
      []() -> std::unique_ptr<Aggregation> {
    return std::unique_ptr<Aggregation>(new DropAggregation);
  };
  MetricAttributes m4 = {{"k1", "v1"}, {"k2", "v2"}, {"k3", "v3"}};
  hash_map.GetOrSetDefault(m4, create_default_aggregation)->Aggregate(static_cast<int64_t>(1));
  EXPECT_EQ(hash_map.Size(), 3);

  // Set attributes with different order - shouldn't create a new entry.
  MetricAttributes m5 = {{"k2", "v2"}, {"k1", "v1"}};
  EXPECT_EQ(hash_map.Has(m5), true);

  // Set attributes with different order - shouldn't create a new entry.
  MetricAttributes m6 = {{"k1", "v2"}, {"k2", "v1"}};
  EXPECT_EQ(hash_map.Has(m6), false);

  // GetAllEnteries
  size_t count = 0;
  hash_map.GetAllEnteries(
      [&count](const MetricAttributes & /* attributes */, Aggregation & /* aggregation */) {
        count++;
        return true;
      });
  EXPECT_EQ(count, hash_map.Size());
}

class MetricAttributeMapHashForCollision
{
public:
  size_t operator()(const MetricAttributes & /*attributes*/) const { return 42; }
};

TEST(AttributesHashMap, CollisionTest)
{
  // The hash on MetricsAttributes will be ignored by MetricAttributeMapHashForCollision
  MetricAttributes m1 = {{"k1", "v1"}};
  MetricAttributes m2 = {{"k2", "v2"}};
  MetricAttributes m3 = {{"k1", "v1"}, {"k2", "v2"}};
  MetricAttributes m4 = {};

  AttributesHashMapWithCustomHash<MetricAttributeMapHashForCollision> hash_map;

  hash_map.Set(m1, std::unique_ptr<Aggregation>(new DropAggregation()));
  hash_map.Set(m2, std::unique_ptr<Aggregation>(new DropAggregation()));
  hash_map.Set(m3, std::unique_ptr<Aggregation>(new DropAggregation()));
  hash_map.Set(m4, std::unique_ptr<Aggregation>(new DropAggregation()));

  EXPECT_EQ(hash_map.Size(), 4);
  EXPECT_EQ(hash_map.Has(m1), true);
  EXPECT_EQ(hash_map.Has(m2), true);
  EXPECT_EQ(hash_map.Has(m3), true);
  EXPECT_EQ(hash_map.Has(m4), true);

  MetricAttributes m5 = {{"k2", "v1"}};
  EXPECT_EQ(hash_map.Has(m5), false);

  //
  // Verify only one bucket used based on the custom hash
  //
  size_t total_active_buckets = 0;
  size_t total_elements       = 0;
  for (size_t i = 0; i < hash_map.BucketCount(); i++)
  {
    size_t bucket_size = hash_map.BucketSize(i);
    if (bucket_size > 0)
    {
      total_active_buckets++;
      total_elements += bucket_size;
    }
  }
  EXPECT_EQ(total_active_buckets, 1);
  EXPECT_EQ(total_elements, 4);
}

TEST(AttributesHashMap, HashConsistencyAcrossStringTypes)
{
  const char *c_str                 = "teststring";
  std::string std_str               = "teststring";
  nostd::string_view nostd_str_view = "teststring";
#if __cplusplus >= 201703L
  std::string_view std_str_view = "teststring";
#endif

  size_t hash_c_str          = 0;
  size_t hash_std_str        = 0;
  size_t hash_nostd_str_view = 0;
#if __cplusplus >= 201703L
  size_t hash_std_str_view = 0;
#endif

  opentelemetry::sdk::common::GetHash(hash_c_str, c_str);
  opentelemetry::sdk::common::GetHash(hash_std_str, std_str);
  opentelemetry::sdk::common::GetHash(hash_nostd_str_view, nostd_str_view);
#if __cplusplus >= 201703L
  opentelemetry::sdk::common::GetHash(hash_std_str_view, std_str_view);
#endif

  EXPECT_EQ(hash_c_str, hash_std_str);
  EXPECT_EQ(hash_c_str, hash_nostd_str_view);
#if __cplusplus >= 201703L
  EXPECT_EQ(hash_c_str, hash_std_str_view);
#endif
}

TEST(AttributesHashMap, OverflowCardinalityLimitBehavior)
{
  // Configure a very small limit to exercise overflow logic easily.
  const size_t limit = 4;  // real attributes limit
  AttributesHashMapWithCustomHash<> map(limit);

  // We expect to be able to insert exactly 'limit' distinct real attribute sets.
  // After that, further distinct attributes should route to the overflow bucket,
  // which should appear only once regardless of how many additional unique sets arrive.

  // Insert distinct attributes up to the limit.
  for (size_t i = 0; i < limit; ++i)
  {
    MetricAttributes attr = {{"k", std::to_string(i)}};
    map.GetOrSetDefault(attr, []() { return std::unique_ptr<Aggregation>(new DropAggregation()); })
        ->Aggregate(static_cast<int64_t>(1));
  }

  // Size should be exactly 'limit' (no overflow yet)
  EXPECT_EQ(map.Size(), limit);

  // Insert one more distinct attribute; this should not increase the real attributes count
  MetricAttributes overflow_trigger = {{"k", "overflow"}};
  map.GetOrSetDefault(overflow_trigger,
                      []() { return std::unique_ptr<Aggregation>(new DropAggregation()); })
      ->Aggregate(static_cast<int64_t>(1));

  EXPECT_EQ(map.Size(), limit);

  // Insert several more unique attributes - size must remain constant (limit)
  for (size_t i = 0; i < limit - 1; ++i)
  {
    MetricAttributes extra_attr = {{"k", std::string("extra") + std::to_string(i)}};
    map.GetOrSetDefault(extra_attr,
                        []() { return std::unique_ptr<Aggregation>(new DropAggregation()); })
        ->Aggregate(static_cast<int64_t>(1));
  }
  EXPECT_EQ(map.Size(), limit);

  // Ensure overflow key was actually created and accessible via Get
  EXPECT_NE(map.Get(kOverflowAttributes), nullptr);

  // Ensure original real attributes still present
  for (size_t i = 0; i < limit - 1; ++i)
  {
    MetricAttributes attr = {{"k", std::to_string(i)}};
    EXPECT_NE(map.Get(attr), nullptr);
  }

  // Copy the hash map to a new map in non-determistic order and verify all entries are present
  AttributesHashMapWithCustomHash<> map_copy(limit);
  map.GetAllEnteries([&map_copy](const MetricAttributes &attributes, Aggregation &) {
    map_copy.Set(attributes, std::unique_ptr<Aggregation>(new DropAggregation()));
    return true;
  });
  EXPECT_EQ(map_copy.Size(), map.Size());
  EXPECT_NE(map_copy.Get(kOverflowAttributes), nullptr);
  for (size_t i = 0; i < limit - 1; ++i)
  {
    MetricAttributes attr = {{"k", std::to_string(i)}};
    EXPECT_NE(map_copy.Get(attr), nullptr);
  }
}