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

Commit c595c24

Browse files
authored
Add GetCurrentTagMap() and GetTagMapFromContext(). (#218)
1 parent c916aea commit c595c24

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed

opencensus/context/context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace opencensus {
2525
namespace tags {
26+
class ContextPeer;
2627
class WithTagMap;
2728
} // namespace tags
2829
namespace trace {
@@ -66,6 +67,7 @@ class Context {
6667

6768
friend class ContextTestPeer;
6869
friend class WithContext;
70+
friend class ::opencensus::tags::ContextPeer;
6971
friend class ::opencensus::tags::WithTagMap;
7072
friend class ::opencensus::trace::ContextPeer;
7173
friend class ::opencensus::trace::WithSpan;

opencensus/tags/BUILD

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ cc_library(
4141
],
4242
)
4343

44+
cc_library(
45+
name = "context_util",
46+
srcs = ["internal/context_util.cc"],
47+
hdrs = ["context_util.h"],
48+
copts = DEFAULT_COPTS,
49+
visibility = ["//visibility:public"],
50+
deps = [
51+
":tags",
52+
"//opencensus/context",
53+
],
54+
)
55+
4456
cc_library(
4557
name = "with_tag_map",
4658
srcs = ["internal/with_tag_map.cc"],
@@ -56,6 +68,19 @@ cc_library(
5668
# Tests
5769
# ========================================================================= #
5870

71+
cc_test(
72+
name = "context_util_test",
73+
srcs = ["internal/context_util_test.cc"],
74+
copts = TEST_COPTS,
75+
deps = [
76+
":context_util",
77+
":tags",
78+
":with_tag_map",
79+
"//opencensus/context",
80+
"@com_google_googletest//:gtest_main",
81+
],
82+
)
83+
5984
cc_test(
6085
name = "tag_key_test",
6186
srcs = ["internal/tag_key_test.cc"],

opencensus/tags/context_util.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef OPENCENSUS_TAGS_CONTEXT_UTIL_H_
16+
#define OPENCENSUS_TAGS_CONTEXT_UTIL_H_
17+
18+
#include "opencensus/context/context.h"
19+
#include "opencensus/tags/tag_map.h"
20+
21+
namespace opencensus {
22+
namespace tags {
23+
24+
// Returns the TagMap from the current Context.
25+
const TagMap& GetCurrentTagMap();
26+
27+
// Returns the TagMap from the given Context.
28+
const TagMap& GetTagMapFromContext(const opencensus::context::Context& ctx);
29+
30+
} // namespace tags
31+
} // namespace opencensus
32+
33+
#endif // OPENCENSUS_TAGS_CONTEXT_UTIL_H_
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "opencensus/tags/context_util.h"
16+
17+
#include "opencensus/context/context.h"
18+
#include "opencensus/tags/tag_map.h"
19+
20+
using ::opencensus::context::Context;
21+
22+
namespace opencensus {
23+
namespace tags {
24+
25+
class ContextPeer {
26+
public:
27+
static const TagMap& GetTagMapFromContext(const Context& ctx) {
28+
return ctx.tags_;
29+
}
30+
};
31+
32+
const TagMap& GetCurrentTagMap() {
33+
return GetTagMapFromContext(Context::Current());
34+
}
35+
36+
const TagMap& GetTagMapFromContext(const Context& ctx) {
37+
return ContextPeer::GetTagMapFromContext(ctx);
38+
}
39+
40+
} // namespace tags
41+
} // namespace opencensus
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "opencensus/tags/context_util.h"
16+
17+
#include "gtest/gtest.h"
18+
#include "opencensus/context/context.h"
19+
#include "opencensus/tags/tag_key.h"
20+
#include "opencensus/tags/tag_map.h"
21+
#include "opencensus/tags/with_tag_map.h"
22+
23+
// Not in namespace ::opencensus::context in order to better reflect what user
24+
// code should look like.
25+
26+
namespace {
27+
28+
// Returns an example TagMap for testing.
29+
opencensus::tags::TagMap Tags1() {
30+
static const auto k1 = opencensus::tags::TagKey::Register("key1");
31+
static const auto k2 = opencensus::tags::TagKey::Register("key2");
32+
return opencensus::tags::TagMap({{k1, "val1"}, {k2, "val2"}});
33+
}
34+
35+
TEST(FromContextTest, GetCurrentTagMap) {
36+
EXPECT_TRUE(opencensus::tags::GetCurrentTagMap().tags().empty());
37+
{
38+
opencensus::tags::WithTagMap wt(Tags1());
39+
EXPECT_EQ(Tags1().tags(), opencensus::tags::GetCurrentTagMap().tags());
40+
}
41+
}
42+
43+
TEST(FromContextTest, GetTagMapFromContext) {
44+
opencensus::context::Context ctx = opencensus::context::Context::Current();
45+
EXPECT_TRUE(opencensus::tags::GetTagMapFromContext(ctx).tags().empty());
46+
{
47+
opencensus::tags::WithTagMap wt(Tags1());
48+
ctx = opencensus::context::Context::Current();
49+
}
50+
EXPECT_EQ(Tags1().tags(), opencensus::tags::GetTagMapFromContext(ctx).tags());
51+
}
52+
53+
} // namespace

0 commit comments

Comments
 (0)