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

Commit 8dc1ac5

Browse files
authored
Tracing: Add Double/Float64 Attribute. (#518)
* Tracing: Add Double/Float64 Attribute. * Fix test coverage. * Revert commented changes.
1 parent f814904 commit 8dc1ac5

File tree

8 files changed

+43
-10
lines changed

8 files changed

+43
-10
lines changed

opencensus/trace/attributes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def _format_attribute_value(value):
2424
elif isinstance(value, six.string_types):
2525
value_type = 'string_value'
2626
value = utils._get_truncatable_str(value)
27+
elif isinstance(value, float):
28+
value_type = 'double_value'
2729
else:
2830
return None
2931

@@ -36,7 +38,8 @@ class Attributes(object):
3638
:type attributes: dict
3739
:param attributes: The set of attributes. Each attribute's key can be up
3840
to 128 bytes long. The value can be a string up to 256
39-
bytes, an integer, or the Boolean values true and false.
41+
bytes, an integer, a floating-point number, or the
42+
Boolean values true and false.
4043
"""
4144
def __init__(self, attributes=None):
4245
self.attributes = attributes or {}

opencensus/trace/exporters/jaeger_exporter.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@ def _convert_attribute_to_tag(key, attr):
312312
key=key,
313313
vLong=attr,
314314
vType=jaeger.TagType.LONG)
315+
if isinstance(attr, float):
316+
return jaeger.Tag(
317+
key=key,
318+
vDouble=attr,
319+
vType=jaeger.TagType.DOUBLE)
315320
logging.warn('Could not serialize attribute \
316321
{}:{} to tag'.format(key, attr))
317322
return None

opencensus/trace/exporters/ocagent/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def add_proto_attribute_value(
201201
pb_attributes,
202202
attribute_key,
203203
attribute_value):
204-
"""Sets string, int or boolean value on protobuf
204+
"""Sets string, int, boolean or float value on protobuf
205205
span, link or annotation attributes.
206206
207207
:type pb_attributes:

opencensus/trace/exporters/zipkin_exporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def _extract_tags_from_span(attr):
187187
return {}
188188
tags = {}
189189
for attribute_key, attribute_value in attr.items():
190-
if isinstance(attribute_value, (int, bool)):
190+
if isinstance(attribute_value, (int, bool, float)):
191191
value = str(attribute_value)
192192
elif isinstance(attribute_value, str):
193193
res, _ = check_str_length(str_to_check=attribute_value)

tests/unit/trace/exporters/test_jaeger_exporter.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,16 @@ def test_translate_to_jaeger(self):
180180
span_attributes = {
181181
'key_bool': False,
182182
'key_string': 'hello_world',
183-
'key_int': 3
183+
'key_int': 3,
184+
'key_double': 111.22,
185+
'key_unsupported_type': ()
184186
}
185187

186188
annotation_attributes = {
187189
'annotation_bool': True,
188190
'annotation_string': 'annotation_test',
189-
'key_float': .3
191+
'key_float': .3,
192+
'key_unsupported_type': {}
190193
}
191194

192195
link_attributes = {'key_bool': True}
@@ -319,6 +322,10 @@ def test_translate_to_jaeger(self):
319322
vStr='hello_world'),
320323
jaeger.Tag(
321324
key='key_int', vType=jaeger.TagType.LONG, vLong=3),
325+
jaeger.Tag(
326+
key='key_double',
327+
vType=jaeger.TagType.DOUBLE,
328+
vDouble=111.22),
322329
jaeger.Tag(
323330
key='status.code',
324331
vType=jaeger.TagType.LONG,
@@ -357,6 +364,10 @@ def test_translate_to_jaeger(self):
357364
key='annotation_string',
358365
vType=jaeger.TagType.STRING,
359366
vStr='annotation_test'),
367+
jaeger.Tag(
368+
key='key_float',
369+
vType=jaeger.TagType.DOUBLE,
370+
vDouble=0.3),
360371
jaeger.Tag(
361372
key='message',
362373
vType=jaeger.TagType.STRING,

tests/unit/trace/exporters/test_stackdriver_exporter.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ def test_translate_to_stackdriver(self, mr_mock):
159159
'value': 'value'
160160
}
161161
},
162+
'key_double': {
163+
'double_value': {
164+
'value': 123.45
165+
}
166+
},
162167
'http.host': {
163168
'string_value': {
164169
'truncated_byte_count': 0,
@@ -224,6 +229,11 @@ def test_translate_to_stackdriver(self, mr_mock):
224229
'value': 'value'
225230
}
226231
},
232+
'key_double': {
233+
'double_value': {
234+
'value': 123.45
235+
}
236+
},
227237
'/http/host': {
228238
'string_value': {
229239
'truncated_byte_count': 0,

tests/unit/trace/exporters/test_zipkin_exporter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ def test_translate_to_zipkin_span_kind_none(self):
138138
attributes={
139139
'test_key': False,
140140
'test_key2': 'raw_value',
141-
# these tags are malformed and should be omitted
142141
'test_key3': 0.1,
143142
},
144143
start_time='2017-08-15T18:02:26.071158Z',
@@ -207,7 +206,8 @@ def test_translate_to_zipkin_span_kind_none(self):
207206
'localEndpoint': local_endpoint_ipv6,
208207
'tags': {
209208
'test_key': 'False',
210-
'test_key2': 'raw_value'
209+
'test_key2': 'raw_value',
210+
'test_key3': '0.1'
211211
},
212212
'kind': 'SERVER',
213213
'annotations': [],
@@ -299,7 +299,6 @@ def test_translate_to_zipkin_with_annotations(self):
299299
attributes={
300300
'test_key': False,
301301
'test_key2': 'raw_value',
302-
# these tags are malformed and should be omitted
303302
'test_key3': 0.1,
304303
},
305304
start_time='2017-08-15T18:02:26.071158Z',
@@ -394,7 +393,8 @@ def test_translate_to_zipkin_with_annotations(self):
394393
local_endpoint_ipv6,
395394
'tags': {
396395
'test_key': 'False',
397-
'test_key2': 'raw_value'
396+
'test_key2': 'raw_value',
397+
'test_key3': '0.1'
398398
},
399399
'kind':
400400
'SERVER',
@@ -422,7 +422,7 @@ def test_translate_to_zipkin_with_annotations(self):
422422
self.assertEqual(zipkin_spans_ipv6, expected_zipkin_spans_ipv6)
423423

424424
def test_ignore_incorrect_spans(self):
425-
attributes = {'float_value': 0.1}
425+
attributes = {'unknown_value': {}}
426426
self.assertEqual(
427427
zipkin_exporter._extract_tags_from_span(attributes), {})
428428

tests/unit/trace/test_attributes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def test_format_attributes_json(self):
5959
'key1': 'test string',
6060
'key2': True,
6161
'key3': 100,
62+
'key4': 123.456,
6263
}
6364

6465
attributes = attributes_module.Attributes(attrs)
@@ -77,6 +78,9 @@ def test_format_attributes_json(self):
7778
},
7879
'key3': {
7980
'int_value': 100
81+
},
82+
'key4': {
83+
'double_value': 123.456
8084
}
8185
}
8286
}

0 commit comments

Comments
 (0)