Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 562efc5

Browse files
author
Ian Sturdy
authored
Change data type for Sum/int64 to int64. (#141)
1 parent 48e3f6d commit 562efc5

File tree

3 files changed

+65
-17
lines changed

3 files changed

+65
-17
lines changed

opencensus/exporters/stats/prometheus/internal/prometheus_utils.cc

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,20 @@ io::prometheus::client::MetricType MetricType(
5353
}
5454
}
5555

56-
void SetValue(double value, io::prometheus::client::Metric* metric) {
56+
void SetValue(double value, io::prometheus::client::MetricType type,
57+
io::prometheus::client::Metric* metric) {
5758
metric->mutable_untyped()->set_value(value);
5859
}
59-
void SetValue(int64_t value, io::prometheus::client::Metric* metric) {
60-
metric->mutable_counter()->set_value(value);
60+
void SetValue(int64_t value, io::prometheus::client::MetricType type,
61+
io::prometheus::client::Metric* metric) {
62+
if (type == io::prometheus::client::MetricType::COUNTER) {
63+
metric->mutable_counter()->set_value(value);
64+
} else {
65+
metric->mutable_untyped()->set_value(value);
66+
}
6167
}
6268
void SetValue(const opencensus::stats::Distribution& value,
69+
io::prometheus::client::MetricType type,
6370
io::prometheus::client::Metric* metric) {
6471
auto* histogram = metric->mutable_histogram();
6572
histogram->set_sample_count(value.count());
@@ -82,6 +89,7 @@ void SetValue(const opencensus::stats::Distribution& value,
8289
template <typename T>
8390
void SetData(const opencensus::stats::ViewDescriptor& descriptor,
8491
const opencensus::stats::ViewData::DataMap<T>& data, int64_t time,
92+
io::prometheus::client::MetricType type,
8593
io::prometheus::client::MetricFamily* metric_family) {
8694
for (const auto& row : data) {
8795
auto* metric = metric_family->add_metric();
@@ -91,7 +99,7 @@ void SetData(const opencensus::stats::ViewDescriptor& descriptor,
9199
label->set_name(SanitizeName(descriptor.columns()[i].name()));
92100
label->set_value(row.first[i]);
93101
}
94-
SetValue(row.second, metric);
102+
SetValue(row.second, type, metric);
95103
}
96104
}
97105

@@ -100,24 +108,26 @@ void SetData(const opencensus::stats::ViewDescriptor& descriptor,
100108
void SetMetricFamily(const opencensus::stats::ViewDescriptor& descriptor,
101109
const opencensus::stats::ViewData& data,
102110
io::prometheus::client::MetricFamily* metric_family) {
111+
const io::prometheus::client::MetricType type =
112+
MetricType(descriptor.aggregation().type());
103113
// TODO(sturdy): convert common units into base units (e.g. ms->s).
104114
metric_family->set_name(SanitizeName(absl::StrCat(
105115
descriptor.name(), "_", descriptor.measure_descriptor().units())));
106116
metric_family->set_help(descriptor.description());
107-
metric_family->set_type(MetricType(descriptor.aggregation().type()));
117+
metric_family->set_type(type);
108118

109119
const int64_t time = absl::ToUnixMillis(data.end_time());
110120
switch (data.type()) {
111121
case opencensus::stats::ViewData::Type::kDouble: {
112-
SetData(descriptor, data.double_data(), time, metric_family);
122+
SetData(descriptor, data.double_data(), time, type, metric_family);
113123
break;
114124
}
115125
case opencensus::stats::ViewData::Type::kInt64: {
116-
SetData(descriptor, data.int_data(), time, metric_family);
126+
SetData(descriptor, data.int_data(), time, type, metric_family);
117127
break;
118128
}
119129
case opencensus::stats::ViewData::Type::kDistribution: {
120-
SetData(descriptor, data.distribution_data(), time, metric_family);
130+
SetData(descriptor, data.distribution_data(), time, type, metric_family);
121131
break;
122132
}
123133
}

opencensus/stats/internal/stats_manager_test.cc

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ TEST_F(StatsManagerTest, Count) {
8484
::testing::Pair(::testing::ElementsAre("value1", "value2"), 1.0)));
8585
}
8686

87-
TEST_F(StatsManagerTest, Sum) {
87+
TEST_F(StatsManagerTest, SumDouble) {
8888
ViewDescriptor view_descriptor = ViewDescriptor()
89-
.set_measure(kSecondMeasureId)
90-
.set_name("sum")
89+
.set_measure(kFirstMeasureId)
90+
.set_name("sum_double")
9191
.set_aggregation(Aggregation::Sum())
9292
.add_column(key1_)
9393
.add_column(key2_);
@@ -96,12 +96,12 @@ TEST_F(StatsManagerTest, Sum) {
9696
EXPECT_TRUE(view.GetData().double_data().empty());
9797

9898
// Stats under a different measure should be ignored.
99-
Record({{FirstMeasure(), 1.0}});
99+
Record({{SecondMeasure(), 1}});
100100
testing::TestUtils::Flush();
101101
EXPECT_TRUE(view.GetData().double_data().empty());
102102

103-
Record({{SecondMeasure(), 2}, {SecondMeasure(), 3}});
104-
Record({{SecondMeasure(), 4}},
103+
Record({{FirstMeasure(), 2.0}, {FirstMeasure(), 3.0}});
104+
Record({{FirstMeasure(), 4.0}},
105105
{{key1_, "value1"}, {key2_, "value2"}, {key3_, "value3"}});
106106
testing::TestUtils::Flush();
107107
const opencensus::stats::ViewData data = view.GetData();
@@ -112,6 +112,34 @@ TEST_F(StatsManagerTest, Sum) {
112112
::testing::Pair(::testing::ElementsAre("value1", "value2"), 4.0)));
113113
}
114114

115+
TEST_F(StatsManagerTest, SumInt) {
116+
ViewDescriptor view_descriptor = ViewDescriptor()
117+
.set_measure(kSecondMeasureId)
118+
.set_name("sum_int")
119+
.set_aggregation(Aggregation::Sum())
120+
.add_column(key1_)
121+
.add_column(key2_);
122+
View view(view_descriptor);
123+
ASSERT_EQ(ViewData::Type::kInt64, view.GetData().type());
124+
EXPECT_TRUE(view.GetData().int_data().empty());
125+
126+
// Stats under a different measure should be ignored.
127+
Record({{FirstMeasure(), 1.0}});
128+
testing::TestUtils::Flush();
129+
EXPECT_TRUE(view.GetData().int_data().empty());
130+
131+
Record({{SecondMeasure(), 2}, {SecondMeasure(), 3}});
132+
Record({{SecondMeasure(), 4}},
133+
{{key1_, "value1"}, {key2_, "value2"}, {key3_, "value3"}});
134+
testing::TestUtils::Flush();
135+
const opencensus::stats::ViewData data = view.GetData();
136+
EXPECT_THAT(
137+
data.int_data(),
138+
::testing::UnorderedElementsAre(
139+
::testing::Pair(::testing::ElementsAre("", ""), 5),
140+
::testing::Pair(::testing::ElementsAre("value1", "value2"), 4)));
141+
}
142+
115143
TEST_F(StatsManagerTest, Distribution) {
116144
ViewDescriptor view_descriptor =
117145
ViewDescriptor()

opencensus/stats/internal/view_data_impl.cc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include "absl/memory/memory.h"
2222
#include "opencensus/stats/distribution.h"
23+
#include "opencensus/stats/measure_descriptor.h"
24+
#include "opencensus/stats/view_descriptor.h"
2325

2426
namespace opencensus {
2527
namespace stats {
@@ -31,7 +33,12 @@ ViewDataImpl::Type ViewDataImpl::TypeForDescriptor(
3133
case AggregationWindow::Type::kDelta:
3234
switch (descriptor.aggregation().type()) {
3335
case Aggregation::Type::kSum:
34-
return ViewDataImpl::Type::kDouble;
36+
switch (descriptor.measure_descriptor().type()) {
37+
case MeasureDescriptor::Type::kDouble:
38+
return ViewDataImpl::Type::kDouble;
39+
case MeasureDescriptor::Type::kInt64:
40+
return ViewDataImpl::Type::kInt64;
41+
}
3542
case Aggregation::Type::kCount:
3643
return ViewDataImpl::Type::kInt64;
3744
case Aggregation::Type::kDistribution:
@@ -169,7 +176,8 @@ void ViewDataImpl::Add(double value, const std::vector<std::string>& tag_values,
169176
break;
170177
}
171178
case Type::kInt64: {
172-
++int_data_[tag_values];
179+
int_data_[tag_values] +=
180+
(aggregation_.type() == Aggregation::Type::kSum ? value : 1);
173181
break;
174182
}
175183
case Type::kDistribution: {
@@ -218,7 +226,9 @@ void ViewDataImpl::Merge(const std::vector<std::string>& tag_values,
218226
break;
219227
}
220228
case Type::kInt64: {
221-
int_data_[tag_values] += data.count();
229+
int_data_[tag_values] +=
230+
(aggregation_.type() == Aggregation::Type::kSum ? data.sum()
231+
: data.count());
222232
break;
223233
}
224234
case Type::kDistribution: {

0 commit comments

Comments
 (0)