summaryrefslogtreecommitdiffstats
path: root/src/jaegertracing/opentelemetry-cpp/sdk/test/_metrics/gauge_aggregator_test.cc
blob: 9565e06296175aa248eaba00434c82bc648f3eac (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
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#ifdef ENABLE_METRICS_PREVIEW
#  include <gtest/gtest.h>
#  include <thread>

#  include "opentelemetry/sdk/_metrics/aggregator/gauge_aggregator.h"

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

TEST(GaugeAggregator, Update)
{
  // This tests that the aggregator updates the maintained value correctly
  // after a call to the update() function.
  auto agg = new GaugeAggregator<int>(metrics_api::InstrumentKind::Counter);

  // Verify default value
  ASSERT_EQ(agg->get_values()[0], 0);

  // Verify that the value updates correctly
  agg->update(1);
  ASSERT_EQ(agg->get_values()[0], 1);

  // Verify that the value continually updates correctly
  for (int i = 0; i < 10; ++i)
  {
    agg->update(i);
  }
  ASSERT_EQ(agg->get_values()[0], 9);
  delete agg;
}

TEST(GaugeAggregator, Checkpoint)
{
  // This tests that the aggregator correctly updates the
  // checkpoint_ value after a call to update() followed
  // by a call to checkpoint().
  GaugeAggregator<int> agg(metrics_api::InstrumentKind::Counter);

  // Verify default checkpoint, before updates
  ASSERT_EQ(agg.get_checkpoint()[0], 0);

  agg.update(10);
  agg.checkpoint();

  // Verify that the new checkpoint contains the update value
  ASSERT_EQ(agg.get_checkpoint()[0], 10);
}

TEST(GaugeAggregator, Merge)
{
  // This tests that the values_ vector is updated correctly after
  // two aggregators are merged together.
  GaugeAggregator<int> agg1(metrics_api::InstrumentKind::Counter);
  GaugeAggregator<int> agg2(metrics_api::InstrumentKind::Counter);

  agg1.update(1);
  agg2.update(2);

  agg1.merge(agg2);

  // Verify that the aggregators merged and the value was updated correctly
  ASSERT_EQ(agg1.get_values()[0], 2);
}

TEST(GaugeAggregator, BadMerge)
{
  // This verifies that we encounter and error when we try to merge
  // two aggregators of different numeric types together.
  GaugeAggregator<int> agg1(metrics_api::InstrumentKind::Counter);
  GaugeAggregator<int> agg2(metrics_api::InstrumentKind::ValueRecorder);

  agg1.update(1);
  agg2.update(2);
  agg1.merge(agg2);

  // Verify that the aggregators did NOT merge
  std::vector<int> correct{1};
  ASSERT_EQ(agg1.get_values(), correct);
}

TEST(GaugeAggregator, Types)
{
  // This test verifies that we do not encounter any errors when
  // using various numeric types.
  GaugeAggregator<int> agg_int(metrics_api::InstrumentKind::Counter);
  GaugeAggregator<long> agg_long(metrics_api::InstrumentKind::Counter);
  GaugeAggregator<float> agg_float(metrics_api::InstrumentKind::Counter);
  GaugeAggregator<double> agg_double(metrics_api::InstrumentKind::Counter);

  for (int i = 1; i <= 10; ++i)
  {
    agg_int.update(i);
    agg_long.update(i);
  }

  for (float i = 1.0; i <= 10.0; i += 1)
  {
    agg_float.update(i);
    agg_double.update(i);
  }

  ASSERT_EQ(agg_int.get_values()[0], 10);
  ASSERT_EQ(agg_long.get_values()[0], 10);
  ASSERT_EQ(agg_float.get_values()[0], 10.0);
  ASSERT_EQ(agg_double.get_values()[0], 10.0);
}

static void callback(GaugeAggregator<int> &agg)
{
  for (int i = 1; i <= 10000; ++i)
  {
    agg.update(i);
  }
}

TEST(GaugeAggregator, Concurrency)
{
  // This test checks that the aggregator updates appropriately
  // when called in a multi-threaded context.
  GaugeAggregator<int> agg(metrics_api::InstrumentKind::Counter);

  std::thread first(&callback, std::ref(agg));
  std::thread second(&callback, std::ref(agg));

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

  ASSERT_EQ(agg.get_values()[0], 10000);
}
#endif