Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit beac0c2

Browse files
songy23liyanhui1228
authored andcommitted
Tags: Set and get current tag_map in thread local. (#257)
1 parent de0a401 commit beac0c2

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
import threading
16+
17+
_thread_local = threading.local()
18+
19+
20+
def get_current_tag_map():
21+
return getattr(_thread_local, 'current_tag_map', None)
22+
23+
24+
def set_current_tag_map(current_tag_map):
25+
setattr(_thread_local, 'current_tag_map', current_tag_map)
26+
27+
28+
def clear():
29+
"""Clear the thread local, used in test."""
30+
_thread_local.__dict__.clear()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
import unittest
16+
17+
from opencensus.tags import execution_context
18+
from opencensus.tags import tag_key as tag_key_module
19+
from opencensus.tags import tag_map as tag_map_module
20+
from opencensus.tags import tag_value as tag_value_module
21+
22+
23+
class TestTagExecutionContext(unittest.TestCase):
24+
25+
def tearDown(self):
26+
execution_context.clear()
27+
28+
def test_unset_tag_map(self):
29+
result = execution_context.get_current_tag_map()
30+
31+
self.assertIsNone(result)
32+
33+
def test_set_and_get_tag_map(self):
34+
key = tag_key_module.TagKey('key')
35+
value = tag_value_module.TagValue('value')
36+
tag_map = tag_map_module.TagMap()
37+
tag_map.insert(key, value)
38+
39+
execution_context.set_current_tag_map(tag_map)
40+
41+
result = execution_context.get_current_tag_map()
42+
43+
self.assertEqual(result, tag_map)

0 commit comments

Comments
 (0)