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

Commit 8e16ed0

Browse files
authored
Introduce the opencensus::tags namespace. (#192)
- Move stats::TagKey and stats::TagSet (renamed to TagMap) to tags:: - Add compatibility shims with deprecation warning. This commit does not change the API.
1 parent c064c22 commit 8e16ed0

10 files changed

Lines changed: 252 additions & 146 deletions

File tree

opencensus/stats/BUILD

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ cc_library(
6767
"internal/set_aggregation_window.cc",
6868
"internal/stats_exporter.cc",
6969
"internal/stats_manager.cc",
70-
"internal/tag_key.cc",
71-
"internal/tag_set.cc",
7270
"internal/view.cc",
7371
"internal/view_data.cc",
7472
"internal/view_data_impl.cc",
@@ -98,9 +96,9 @@ cc_library(
9896
],
9997
copts = DEFAULT_COPTS,
10098
deps = [
101-
"//opencensus/common/internal:hash_mix",
10299
"//opencensus/common/internal:stats_object",
103100
"//opencensus/common/internal:string_vector_hash",
101+
"//opencensus/tags",
104102
"@com_google_absl//absl/base:core_headers",
105103
"@com_google_absl//absl/memory",
106104
"@com_google_absl//absl/strings",
@@ -208,28 +206,6 @@ cc_test(
208206
],
209207
)
210208

211-
cc_test(
212-
name = "tag_key_test",
213-
size = "small",
214-
srcs = ["internal/tag_key_test.cc"],
215-
copts = TEST_COPTS,
216-
deps = [
217-
":core",
218-
"@com_google_googletest//:gtest_main",
219-
],
220-
)
221-
222-
cc_test(
223-
name = "tag_set_test",
224-
size = "small",
225-
srcs = ["internal/tag_set_test.cc"],
226-
copts = TEST_COPTS,
227-
deps = [
228-
":core",
229-
"@com_google_googletest//:gtest_main",
230-
],
231-
)
232-
233209
cc_test(
234210
name = "view_data_impl_test",
235211
srcs = ["internal/view_data_impl_test.cc"],

opencensus/stats/tag_key.h

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,16 @@
1515
#ifndef OPENCENSUS_STATS_TAG_KEY_H_
1616
#define OPENCENSUS_STATS_TAG_KEY_H_
1717

18-
#include <cstddef>
19-
#include <cstdint>
20-
#include <string>
21-
22-
#include "absl/strings/string_view.h"
18+
#include "absl/base/macros.h"
19+
#include "opencensus/tags/tag_key.h"
2320

2421
namespace opencensus {
2522
namespace stats {
2623

27-
// TagKey is a lightweight, immutable representation of a tag key. It has a
28-
// trivial destructor and can be safely used as a local static variable.
29-
class TagKey final {
30-
public:
31-
// Registers a tag key with 'name'. Registering the same name twice produces
32-
// equal TagKeys.
33-
static TagKey Register(absl::string_view name);
34-
35-
const std::string& name() const;
36-
37-
bool operator==(TagKey other) const { return id_ == other.id_; }
38-
bool operator!=(TagKey other) const { return id_ != other.id_; }
39-
bool operator<(TagKey other) const { return id_ < other.id_; }
40-
41-
// Returns a suitable hash of the TagKey. The implementation may change.
42-
std::size_t hash() const { return id_; }
43-
44-
private:
45-
friend class TagKeyRegistry;
46-
explicit TagKey(uint64_t id) : id_(id) {}
47-
48-
uint64_t id_;
49-
};
24+
ABSL_DEPRECATED(
25+
"TagKey has moved to opencensus::tags. This is a compatibility "
26+
"shim and will be removed on or after 2019-03-20")
27+
typedef opencensus::tags::TagKey TagKey;
5028

5129
} // namespace stats
5230
} // namespace opencensus

opencensus/stats/tag_set.h

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,16 @@
1515
#ifndef OPENCENSUS_STATS_TAG_SET_H_
1616
#define OPENCENSUS_STATS_TAG_SET_H_
1717

18-
#include <initializer_list>
19-
#include <string>
20-
#include <utility>
21-
#include <vector>
22-
23-
#include "absl/strings/string_view.h"
24-
#include "opencensus/stats/tag_key.h"
18+
#include "absl/base/macros.h"
19+
#include "opencensus/tags/tag_map.h"
2520

2621
namespace opencensus {
2722
namespace stats {
2823

29-
// TagSet represents a set of key-value tags, and provides efficient equality
30-
// and hash operations. A TagSet is expensive to construct, and should be shared
31-
// between uses where possible.
32-
// TagSet is immutable.
33-
class TagSet final {
34-
public:
35-
// Both constructors are not explicit so that Record({}, {{"k", "v"}}) works.
36-
// This constructor is needed because even though we copy to a vector
37-
// internally because c++ cannot deduce the conversion needed.
38-
TagSet(std::initializer_list<std::pair<TagKey, absl::string_view>> tags);
39-
// This constructor is needed so that callers can dynamically construct
40-
// tagsets. It takes the argument by value to allow it to be moved.
41-
TagSet(std::vector<std::pair<TagKey, std::string>> tags);
42-
43-
// Accesses the tags sorted by key (in an implementation-defined, not
44-
// lexicographic, order).
45-
const std::vector<std::pair<TagKey, std::string>>& tags() const {
46-
return tags_;
47-
}
48-
49-
struct Hash {
50-
std::size_t operator()(const TagSet& tag_set) const;
51-
};
52-
53-
bool operator==(const TagSet& other) const;
54-
bool operator!=(const TagSet& other) const { return !(*this == other); }
55-
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-
60-
private:
61-
void Initialize();
62-
63-
std::size_t hash_;
64-
// TODO: add an option to store string_views to avoid copies.
65-
std::vector<std::pair<TagKey, std::string>> tags_;
66-
};
24+
ABSL_DEPRECATED(
25+
"TagSet has moved to opencensus::tags::TagMap. This is a "
26+
"compatibility shim and will be removed on or after 2019-03-20")
27+
typedef opencensus::tags::TagMap TagSet;
6728

6829
} // namespace stats
6930
} // namespace opencensus

opencensus/tags/BUILD

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# OpenCensus C++ Tags library.
2+
#
3+
# Copyright 2018, OpenCensus Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
load("//opencensus:copts.bzl", "DEFAULT_COPTS", "TEST_COPTS")
18+
19+
licenses(["notice"]) # Apache License 2.0
20+
21+
package(default_visibility = ["//visibility:private"])
22+
23+
# The public tags API.
24+
cc_library(
25+
name = "tags",
26+
hdrs = [
27+
"tag_map.h",
28+
"tag_key.h",
29+
],
30+
srcs = [
31+
"internal/tag_key.cc",
32+
"internal/tag_map.cc",
33+
],
34+
copts = DEFAULT_COPTS,
35+
visibility = ["//visibility:public"],
36+
deps = [
37+
"//opencensus/common/internal:hash_mix",
38+
"@com_google_absl//absl/base:core_headers",
39+
"@com_google_absl//absl/strings",
40+
"@com_google_absl//absl/synchronization",
41+
],
42+
)
43+
44+
# Tests
45+
# ========================================================================= #
46+
47+
cc_test(
48+
name = "tag_key_test",
49+
srcs = ["internal/tag_key_test.cc"],
50+
copts = TEST_COPTS,
51+
deps = [
52+
":tags",
53+
"@com_google_googletest//:gtest_main",
54+
],
55+
)
56+
57+
cc_test(
58+
name = "tag_map_test",
59+
srcs = ["internal/tag_map_test.cc"],
60+
copts = TEST_COPTS,
61+
deps = [
62+
":tags",
63+
"@com_google_googletest//:gtest_main",
64+
],
65+
)
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "opencensus/stats/tag_key.h"
15+
#include "opencensus/tags/tag_key.h"
1616

1717
#include <cstdint>
1818
#include <string>
@@ -23,18 +23,18 @@
2323
#include "absl/synchronization/mutex.h"
2424

2525
namespace opencensus {
26-
namespace stats {
26+
namespace tags {
2727

28-
class TagKeyRegistry final {
28+
class TagKeyRegistry {
2929
public:
3030
static TagKeyRegistry* Get() {
31-
static TagKeyRegistry* global_tag_key_registry = new TagKeyRegistry();
31+
static TagKeyRegistry* global_tag_key_registry = new TagKeyRegistry;
3232
return global_tag_key_registry;
3333
}
3434

35-
TagKey Register(absl::string_view name);
35+
TagKey Register(absl::string_view name) LOCKS_EXCLUDED(mu_);
3636

37-
const std::string& TagKeyName(TagKey key) const {
37+
const std::string& TagKeyName(TagKey key) const LOCKS_EXCLUDED(mu_) {
3838
absl::ReaderMutexLock l(&mu_);
3939
return registered_tag_keys_[key.id_];
4040
}
@@ -69,5 +69,5 @@ const std::string& TagKey::name() const {
6969
return TagKeyRegistry::Get()->TagKeyName(*this);
7070
}
7171

72-
} // namespace stats
72+
} // namespace tags
7373
} // namespace opencensus
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "opencensus/stats/tag_key.h"
15+
#include "opencensus/tags/tag_key.h"
1616

1717
#include "gtest/gtest.h"
1818

1919
namespace opencensus {
20-
namespace stats {
20+
namespace tags {
2121
namespace {
2222

2323
TEST(TagKeyTest, Name) {
@@ -40,5 +40,5 @@ TEST(TagKeyTest, Inequality) {
4040
}
4141

4242
} // namespace
43-
} // namespace stats
43+
} // namespace tags
4444
} // namespace opencensus
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "opencensus/stats/tag_set.h"
15+
#include "opencensus/tags/tag_map.h"
1616

1717
#include <functional>
1818
#include <initializer_list>
@@ -24,12 +24,12 @@
2424
#include "absl/strings/str_join.h"
2525
#include "absl/strings/string_view.h"
2626
#include "opencensus/common/internal/hash_mix.h"
27-
#include "opencensus/stats/tag_key.h"
27+
#include "opencensus/tags/tag_key.h"
2828

2929
namespace opencensus {
30-
namespace stats {
30+
namespace tags {
3131

32-
TagSet::TagSet(
32+
TagMap::TagMap(
3333
std::initializer_list<std::pair<TagKey, absl::string_view>> tags) {
3434
tags_.reserve(tags.size());
3535
for (const auto& tag : tags) {
@@ -38,12 +38,12 @@ TagSet::TagSet(
3838
Initialize();
3939
}
4040

41-
TagSet::TagSet(std::vector<std::pair<TagKey, std::string>> tags)
41+
TagMap::TagMap(std::vector<std::pair<TagKey, std::string>> tags)
4242
: tags_(std::move(tags)) {
4343
Initialize();
4444
}
4545

46-
void TagSet::Initialize() {
46+
void TagMap::Initialize() {
4747
std::sort(tags_.begin(), tags_.end());
4848

4949
std::hash<std::string> hasher;
@@ -55,15 +55,15 @@ void TagSet::Initialize() {
5555
hash_ = mixer.get();
5656
}
5757

58-
std::size_t TagSet::Hash::operator()(const TagSet& tag_set) const {
59-
return tag_set.hash_;
58+
std::size_t TagMap::Hash::operator()(const TagMap& tags) const {
59+
return tags.hash_;
6060
}
6161

62-
bool TagSet::operator==(const TagSet& other) const {
62+
bool TagMap::operator==(const TagMap& other) const {
6363
return tags_ == other.tags_;
6464
}
6565

66-
std::string TagSet::DebugString() const {
66+
std::string TagMap::DebugString() const {
6767
return absl::StrCat(
6868
"{",
6969
absl::StrJoin(
@@ -75,5 +75,5 @@ std::string TagSet::DebugString() const {
7575
"}");
7676
}
7777

78-
} // namespace stats
78+
} // namespace tags
7979
} // namespace opencensus

0 commit comments

Comments
 (0)