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

Commit 110c42b

Browse files
authored
Add a variant of Record() that uses the currently active TagMap. (#225)
1 parent 7eedfe0 commit 110c42b

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

opencensus/stats/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ cc_library(
118118
deps = [
119119
":core",
120120
"//opencensus/tags",
121+
"//opencensus/tags:context_util",
121122
"@com_google_absl//absl/strings",
122123
"@com_google_absl//absl/time",
123124
],
@@ -204,6 +205,7 @@ cc_test(
204205
":recording",
205206
":test_utils",
206207
"//opencensus/tags",
208+
"//opencensus/tags:with_tag_map",
207209
"@com_google_googletest//:gtest_main",
208210
],
209211
)

opencensus/stats/internal/recording.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,21 @@
1414

1515
#include "opencensus/stats/recording.h"
1616

17-
#include "absl/time/clock.h"
17+
#include <initializer_list>
18+
1819
#include "opencensus/stats/internal/delta_producer.h"
1920
#include "opencensus/stats/measure.h"
21+
#include "opencensus/tags/context_util.h"
2022
#include "opencensus/tags/tag_map.h"
2123

2224
namespace opencensus {
2325
namespace stats {
2426

27+
void Record(std::initializer_list<Measurement> measurements) {
28+
DeltaProducer::Get()->Record(measurements,
29+
opencensus::tags::GetCurrentTagMap());
30+
}
31+
2532
void Record(std::initializer_list<Measurement> measurements,
2633
opencensus::tags::TagMap tags) {
2734
DeltaProducer::Get()->Record(measurements, std::move(tags));

opencensus/stats/internal/stats_manager_test.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "opencensus/stats/testing/test_utils.h"
2121
#include "opencensus/stats/view.h"
2222
#include "opencensus/tags/tag_key.h"
23+
#include "opencensus/tags/tag_map.h"
24+
#include "opencensus/tags/with_tag_map.h"
2325

2426
namespace opencensus {
2527
namespace stats {
@@ -87,6 +89,33 @@ TEST_F(StatsManagerTest, Count) {
8789
::testing::Pair(::testing::ElementsAre("value1", "value2"), 1.0)));
8890
}
8991

92+
TEST_F(StatsManagerTest, CountTagsFromContext) {
93+
ViewDescriptor view_descriptor = ViewDescriptor()
94+
.set_measure(kFirstMeasureId)
95+
.set_name("count")
96+
.set_aggregation(Aggregation::Count())
97+
.add_column(key1_)
98+
.add_column(key2_);
99+
View view(view_descriptor);
100+
ASSERT_EQ(ViewData::Type::kInt64, view.GetData().type());
101+
EXPECT_TRUE(view.GetData().int_data().empty());
102+
103+
opencensus::tags::TagMap tags(
104+
{{key1_, "value1"}, {key2_, "value2"}, {key3_, "value3"}});
105+
Record({{FirstMeasure(), 2.0}, {FirstMeasure(), 3.0}});
106+
{
107+
opencensus::tags::WithTagMap wt(tags);
108+
Record({{FirstMeasure(), 4.0}});
109+
}
110+
testing::TestUtils::Flush();
111+
const opencensus::stats::ViewData data = view.GetData();
112+
EXPECT_THAT(
113+
data.int_data(),
114+
::testing::UnorderedElementsAre(
115+
::testing::Pair(::testing::ElementsAre("", ""), 2.0),
116+
::testing::Pair(::testing::ElementsAre("value1", "value2"), 1.0)));
117+
}
118+
90119
TEST_F(StatsManagerTest, SumDouble) {
91120
ViewDescriptor view_descriptor = ViewDescriptor()
92121
.set_measure(kFirstMeasureId)

opencensus/stats/recording.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,23 @@
2323
namespace opencensus {
2424
namespace stats {
2525

26-
// Records a list of Measurements under 'tags'. The recommended style is to
27-
// create Measurements as an initializer list, e.g.
26+
// Records a list of Measurements under the current Context's tags. The
27+
// recommended style is to create Measurements as an initializer list, e.g.:
2828
//
2929
// Record({{measure_double, 2.5}, {measure_int, 1ll}});
3030
//
3131
// Only floating point values may be recorded against MeasureDoubles and only
3232
// integral values against MeasureInt64s, to prevent silent loss of precision.
3333
// If a record call fails to compile, ensure that all types match (using
3434
// static_cast to double or int64_t if necessary).
35+
void Record(std::initializer_list<Measurement> measurements);
36+
37+
// Records a list of Measurements under the specified 'tags'. The current
38+
// Context's tags are ignored. e.g:
39+
//
40+
// Record({{measure_double, 2.5}}, {{key, "value"}});
3541
void Record(std::initializer_list<Measurement> measurements,
36-
opencensus::tags::TagMap tags = {});
42+
opencensus::tags::TagMap tags);
3743

3844
} // namespace stats
3945
} // namespace opencensus

0 commit comments

Comments
 (0)