summaryrefslogtreecommitdiffstats
path: root/src/jaegertracing/opentelemetry-cpp/sdk/test/_metrics/counter_aggregator_test.cc
blob: 7ef452e7707e6839de4aab0e462d28bfdf9514b2 (plain)
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#ifdef ENABLE_METRICS_PREVIEW
#  include "opentelemetry/sdk/_metrics/aggregator/counter_aggregator.h"

#  include <gtest/gtest.h>
#  include <numeric>
#  include <thread>

namespace metrics_api = opentelemetry::metrics;

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{

TEST(CounterAggregator, NoUpdates)
{
  CounterAggregator<int> alpha(metrics_api::InstrumentKind::Counter);

  EXPECT_EQ(alpha.get_checkpoint().size(), 1);
  EXPECT_EQ(alpha.get_checkpoint()[0], 0);

  alpha.checkpoint();
  EXPECT_EQ(alpha.get_checkpoint().size(), 1);
  EXPECT_EQ(alpha.get_checkpoint()[0], 0);
}

TEST(CounterAggregator, Update)
{
  CounterAggregator<int> alpha(metrics_api::InstrumentKind::Counter);
  CounterAggregator<int> beta(metrics_api::InstrumentKind::Counter);

  for (int i = 0; i < 123456; i++)
  {
    alpha.update(1);
  }

  int sum = 0;
  for (int i = 0; i < 100; i++)
  {
    int tmp = std::rand() % 128;
    beta.update(tmp);
    sum += tmp;
  }

  EXPECT_EQ(alpha.get_checkpoint()[0], 0);  // checkpoint shouldn't change even with updates
  EXPECT_EQ(beta.get_checkpoint()[0], 0);

  alpha.checkpoint();
  beta.checkpoint();

  EXPECT_EQ(alpha.get_checkpoint()[0], 123456);
  EXPECT_EQ(beta.get_checkpoint()[0], sum);

  alpha.update(15);
  alpha.checkpoint();
  EXPECT_EQ(alpha.get_checkpoint()[0], 15);  // reset to 0 after first checkpoint call
}

// callback update function used to test concurrent calls
void incrementingCallback(Aggregator<int> &agg)
{
  for (int i = 0; i < 2000000; i++)
  {
    agg.update(1);
  }
}

TEST(CounterAggregator, Concurrency)
{
  CounterAggregator<int> alpha(metrics_api::InstrumentKind::Counter);

  // spawn new threads that initiate the callback
  std::thread first(incrementingCallback, std::ref(alpha));
  std::thread second(incrementingCallback, std::ref(alpha));

  first.join();
  second.join();

  alpha.checkpoint();

  // race conditions result in values below 2*2000000
  EXPECT_EQ(alpha.get_checkpoint()[0], 2 * 2000000);
}

TEST(CounterAggregator, Merge)
{
  CounterAggregator<int> alpha(metrics_api::InstrumentKind::Counter);
  CounterAggregator<int> beta(metrics_api::InstrumentKind::Counter);

  alpha.merge(beta);

  alpha.checkpoint();
  EXPECT_EQ(alpha.get_checkpoint()[0], 0);  // merge with no updates

  for (int i = 0; i < 500; i++)
  {
    alpha.update(1);
  }

  for (int i = 0; i < 700; i++)
  {
    beta.update(1);
  }

  alpha.merge(beta);
  alpha.checkpoint();
  EXPECT_EQ(alpha.get_checkpoint()[0], 1200);

  // HistogramAggregator gamma(metrics_api::BoundInstrumentKind::BoundValueRecorder);
  // ASSERT_THROW(alpha.merge(gamma), AggregatorMismatch);
}

}  // namespace metrics
}  // namespace sdk
OPENTELEMETRY_END_NAMESPACE
#endif