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

Commit 922702b

Browse files
authored
Minor refactor Tags (#602)
1 parent 16e4767 commit 922702b

File tree

11 files changed

+51
-110
lines changed

11 files changed

+51
-110
lines changed

contrib/opencensus-ext-postgresql/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
long_description=open('README.rst').read(),
4141
install_requires=[
4242
'opencensus >= 0.4.dev0, < 1.0.0',
43-
'psycopg2 >= 2.7.3.1',
43+
'psycopg2-binary >= 2.7.3.1',
4444
],
4545
extras_require={},
4646
license='Apache-2.0',

opencensus/stats/measurement_map.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import logging
1616

1717
from opencensus.common import utils
18-
from opencensus.tags import execution_context
18+
from opencensus.tags import TagContext
1919

2020

2121
logger = logging.getLogger(__name__)
@@ -90,13 +90,13 @@ def measure_put_attachment(self, key, value):
9090

9191
self._attachments[key] = value
9292

93-
def record(self, tag_map_tags=None):
93+
def record(self, tags=None):
9494
"""records all the measures at the same time with a tag_map.
9595
tag_map could either be explicitly passed to the method, or implicitly
96-
read from current execution context.
96+
read from current runtime context.
9797
"""
98-
if tag_map_tags is None:
99-
tag_map_tags = execution_context.get_current_tag_map()
98+
if tags is None:
99+
tags = TagContext.get()
100100
if self._invalid:
101101
logger.warning("Measurement map has included negative value "
102102
"measurements, refusing to record")
@@ -112,7 +112,7 @@ def record(self, tag_map_tags=None):
112112
return
113113

114114
self.measure_to_view_map.record(
115-
tags=tag_map_tags,
115+
tags=tags,
116116
measurement_map=self.measurement_map,
117117
timestamp=utils.to_iso_str(),
118118
attachments=self.attachments

opencensus/tags/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from opencensus.common.runtime_context import RuntimeContext
1516
from opencensus.tags.tag import Tag
1617
from opencensus.tags.tag_key import TagKey
1718
from opencensus.tags.tag_value import TagValue
1819
from opencensus.tags.tag_map import TagMap
1920

20-
__all__ = ['Tag', 'TagKey', 'TagValue', 'TagMap']
21+
__all__ = ['Tag', 'TagContext', 'TagKey', 'TagValue', 'TagMap']
22+
23+
TagContext = RuntimeContext.register_slot('tag_context', None)

opencensus/tags/execution_context.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

opencensus/tags/tag.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,25 @@
1313
# limitations under the License.
1414

1515
from collections import namedtuple
16+
from opencensus.tags.tag_key import TagKey
17+
from opencensus.tags.tag_value import TagValue
1618

1719
Tag_ = namedtuple('Tag', ['key', 'value'])
1820

1921

2022
class Tag(Tag_):
2123
"""A tag, in the format [KEY]:[VALUE].
2224
23-
:type key: '~opencensus.tags.tag_key.TagKey'
24-
:param key: Key in the tag
25+
:type key: str
26+
:param key: The name of the tag
2527
26-
:type value: '~opencensus.tags.tag_key.TagValue'
27-
:param value: Value of the key in the tag.
28+
:type value: str
29+
:param value: The value of the tag
2830
2931
"""
30-
pass
32+
def __new__(cls, key, value):
33+
return super(Tag, cls).__new__(
34+
cls,
35+
key=TagKey(key),
36+
value=TagValue(value),
37+
)

opencensus/tags/tag_key.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def __new__(cls, name):
2929
:param name: The name of the key
3030
:return: TagKey
3131
"""
32-
if not is_valid_tag_name(name):
33-
raise ValueError(_TAG_NAME_ERROR)
32+
if not isinstance(name, cls):
33+
if not is_valid_tag_name(name):
34+
raise ValueError(_TAG_NAME_ERROR)
3435
return super(TagKey, cls).__new__(cls, name)

opencensus/tags/tag_value.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def __new__(cls, value):
2929
:param value: A string representing the value of a key in a tag
3030
:return: TagValue
3131
"""
32-
if not is_valid_tag_value(value):
33-
raise ValueError(_TAG_VALUE_ERROR)
32+
if not isinstance(value, cls):
33+
if not is_valid_tag_value(value):
34+
raise ValueError(_TAG_VALUE_ERROR)
3435
return super(TagValue, cls).__new__(cls, value)

tests/unit/stats/test_measurement_map.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import unittest
1717

1818
from opencensus.stats import measurement_map as measurement_map_module
19-
from opencensus.tags import execution_context
19+
from opencensus.tags import Tag
20+
from opencensus.tags import TagContext
21+
from opencensus.tags import TagMap
2022

2123
logger_patch = mock.patch('opencensus.stats.measurement_map.logger')
2224

@@ -130,16 +132,14 @@ def test_record_against_explicit_tag_map(self):
130132
measure_to_view_map=measure_to_view_map)
131133

132134
tags = {'testtag1': 'testtag1val'}
133-
measurement_map.record(tag_map_tags=tags)
135+
measurement_map.record(tags=tags)
134136
self.assertTrue(measure_to_view_map.record.called)
135137

136138
def test_record_against_implicit_tag_map(self):
137139
measure_to_view_map = mock.Mock()
138140
measurement_map = measurement_map_module.MeasurementMap(
139141
measure_to_view_map=measure_to_view_map)
140-
141-
tags = {'testtag1': 'testtag1val'}
142-
execution_context.set_current_tag_map(tags)
142+
TagContext.set(TagMap(tags=[Tag('testtag1', 'testtag1val')]))
143143
measurement_map.record()
144144
self.assertTrue(measure_to_view_map.record.called)
145145

tests/unit/tags/test_tag.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313
# limitations under the License.
1414

1515
import unittest
16-
from opencensus.tags import Tag, TagKey, TagValue
16+
from opencensus.tags import Tag
1717

1818

1919
class TestTag(unittest.TestCase):
2020

2121
def test_constructor(self):
2222
key = 'key1'
2323
value = 'value1'
24-
tag = Tag(key=TagKey(key), value=TagValue(value))
24+
tag = Tag(key=key, value=value)
2525

2626
self.assertEqual(tag.key, key)
2727
self.assertEqual(tag.value, value)
28+
self.assertRaises(ValueError, lambda: Tag(key='', value=value))
29+
self.assertRaises(ValueError, lambda: Tag(key=key, value='\0'))

tests/unit/tags/test_tag_execution_context.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)