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

Commit d0718f0

Browse files
authored
Add TagSet::DebugString(). (#179)
1 parent 7ff62f7 commit d0718f0

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

opencensus/stats/internal/tag_set.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <utility>
2121
#include <vector>
2222

23+
#include "absl/strings/str_cat.h"
24+
#include "absl/strings/str_join.h"
2325
#include "absl/strings/string_view.h"
2426
#include "opencensus/common/internal/hash_mix.h"
2527
#include "opencensus/stats/tag_key.h"
@@ -61,5 +63,17 @@ bool TagSet::operator==(const TagSet& other) const {
6163
return tags_ == other.tags_;
6264
}
6365

66+
std::string TagSet::DebugString() const {
67+
return absl::StrCat(
68+
"{",
69+
absl::StrJoin(
70+
tags_, ", ",
71+
[](std::string* o, std::pair<const TagKey&, const std::string&> kv) {
72+
absl::StrAppend(o, "\"", kv.first.name(), "\": \"", kv.second,
73+
"\"");
74+
}),
75+
"}");
76+
}
77+
6478
} // namespace stats
6579
} // namespace opencensus

opencensus/stats/internal/tag_set_test.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

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

17+
#include <iostream>
1718
#include <string>
1819
#include <unordered_map>
1920
#include <utility>
@@ -26,6 +27,8 @@ namespace opencensus {
2627
namespace stats {
2728
namespace {
2829

30+
using ::testing::HasSubstr;
31+
2932
TEST(TagSetTest, ConstructorsEquivalent) {
3033
TagKey key = TagKey::Register("k");
3134
const std::vector<std::pair<TagKey, std::string>> tags({{key, "v"}});
@@ -89,6 +92,18 @@ TEST(TagSetTest, UnorderedMap) {
8992
EXPECT_EQ(1, map.erase(ts));
9093
}
9194

95+
TEST(TagSetTest, DebugStringContainsTags) {
96+
TagKey k1 = TagKey::Register("key1");
97+
TagKey k2 = TagKey::Register("key2");
98+
TagSet ts({{k1, "value1"}, {k2, "value2"}});
99+
const std::string s = ts.DebugString();
100+
std::cout << s << "\n";
101+
EXPECT_THAT(s, HasSubstr("key1"));
102+
EXPECT_THAT(s, HasSubstr("value1"));
103+
EXPECT_THAT(s, HasSubstr("key2"));
104+
EXPECT_THAT(s, HasSubstr("value2"));
105+
}
106+
92107
} // namespace
93108
} // namespace stats
94109
} // namespace opencensus

opencensus/stats/tag_set.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class TagSet final {
5353
bool operator==(const TagSet& other) const;
5454
bool operator!=(const TagSet& other) const { return !(*this == other); }
5555

56+
// Returns a human-readable string for debugging. Do not rely on its format or
57+
// try to parse it. Do not use it to retrieve tags.
58+
std::string DebugString() const;
59+
5660
private:
5761
void Initialize();
5862

0 commit comments

Comments
 (0)