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

Commit 2d6cb2e

Browse files
songy23c24t
authored andcommitted
Exporter/OcAgent: Support double attribute. (#526)
1 parent 74eb039 commit 2d6cb2e

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

opencensus/trace/exporters/ocagent/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def add_proto_attribute_value(
211211
:type attribute_key: str
212212
:param attribute_key: attribute key to set
213213
214-
:type attribute_value: str or int or bool
214+
:type attribute_value: str or int or bool or float
215215
:param attribute_value: attribute value
216216
"""
217217

@@ -224,6 +224,9 @@ def add_proto_attribute_value(
224224
elif isinstance(attribute_value, str):
225225
pb_attributes.attribute_map[attribute_key].\
226226
string_value.value = attribute_value
227+
elif isinstance(attribute_value, float):
228+
pb_attributes.attribute_map[attribute_key].\
229+
double_value = attribute_value
227230
else:
228231
pb_attributes.attribute_map[attribute_key].\
229232
string_value.value = str(attribute_value)

tests/unit/trace/exporters/ocagent/test_trace_exporter_utils.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def test_basic_span_translation(self):
4141
attributes={
4242
'test_str_key': 'test_str_value',
4343
'test_int_key': 1,
44-
'test_bool_key': False
44+
'test_bool_key': False,
45+
'test_double_key': 567.89
4546
},
4647
start_time='2017-08-15T18:02:26.071158Z',
4748
end_time='2017-08-15T18:02:36.071158Z',
@@ -64,7 +65,7 @@ def test_basic_span_translation(self):
6465
hex_encoder(pb_span.parent_span_id)[0], b'6e0c63257de34c93')
6566
self.assertEqual(pb_span.kind, 0)
6667

67-
self.assertEqual(len(pb_span.attributes.attribute_map), 3)
68+
self.assertEqual(len(pb_span.attributes.attribute_map), 4)
6869
self.assertEqual(
6970
pb_span.attributes.attribute_map['test_str_key'],
7071
trace_pb2.AttributeValue(
@@ -75,6 +76,8 @@ def test_basic_span_translation(self):
7576
trace_pb2.AttributeValue(int_value=1))
7677
self.assertEqual(pb_span.attributes.attribute_map['test_bool_key'],
7778
trace_pb2.AttributeValue(bool_value=False))
79+
self.assertEqual(pb_span.attributes.attribute_map['test_double_key'],
80+
trace_pb2.AttributeValue(double_value=567.89))
7881

7982
self.assertEqual(pb_span.start_time.ToJsonString(),
8083
'2017-08-15T18:02:26.071158Z')
@@ -182,7 +185,8 @@ def test_translate_links(self):
182185
attributes={
183186
'test_str_key': 'test_str_value',
184187
'test_int_key': 1,
185-
'test_bool_key': False
188+
'test_bool_key': False,
189+
'test_double_key': 567.89
186190
})),
187191
link_module.Link(
188192
trace_id='1e0c63257de34c92bf9efcd03927272e',
@@ -230,7 +234,7 @@ def test_translate_links(self):
230234
self.assertEqual(pb_span.links.link[2].type, 2)
231235

232236
self.assertEqual(
233-
len(pb_span.links.link[0].attributes.attribute_map), 3)
237+
len(pb_span.links.link[0].attributes.attribute_map), 4)
234238
self.assertEqual(
235239
len(pb_span.links.link[1].attributes.attribute_map), 0)
236240
self.assertEqual(
@@ -248,6 +252,9 @@ def test_translate_links(self):
248252
self.assertEqual(
249253
pb_span.links.link[0].attributes.attribute_map['test_bool_key'],
250254
trace_pb2.AttributeValue(bool_value=False))
255+
self.assertEqual(
256+
pb_span.links.link[0].attributes.attribute_map['test_double_key'],
257+
trace_pb2.AttributeValue(double_value=567.89))
251258

252259
def test_translate_time_events(self):
253260

@@ -270,7 +277,8 @@ def test_translate_time_events(self):
270277
attributes={
271278
'test_str_key': 'test_str_value',
272279
'test_int_key': 1,
273-
'test_bool_key': False
280+
'test_bool_key': False,
281+
'test_double_key': 567.89
274282
}))),
275283
time_event_module.TimeEvent(
276284
timestamp=annotation1_ts,
@@ -328,7 +336,7 @@ def test_translate_time_events(self):
328336
self.assertEqual(event0.annotation.description.value, "hi there0")
329337
self.assertEqual(event1.annotation.description.value, "hi there1")
330338

331-
self.assertEqual(len(event0.annotation.attributes.attribute_map), 3)
339+
self.assertEqual(len(event0.annotation.attributes.attribute_map), 4)
332340
self.assertEqual(len(event1.annotation.attributes.attribute_map), 0)
333341

334342
self.assertEqual(
@@ -343,6 +351,9 @@ def test_translate_time_events(self):
343351
self.assertEqual(
344352
event0.annotation.attributes.attribute_map['test_bool_key'],
345353
trace_pb2.AttributeValue(bool_value=False))
354+
self.assertEqual(
355+
event0.annotation.attributes.attribute_map['test_double_key'],
356+
trace_pb2.AttributeValue(double_value=567.89))
346357

347358
self.assertEqual(event2.message_event.id, 0)
348359
self.assertEqual(event3.message_event.id, 1)
@@ -474,13 +485,14 @@ def test_set_annotation_with_attributes(self):
474485
attributes={
475486
'test_str_key': 'test_str_value',
476487
'test_int_key': 1,
477-
'test_bool_key': False
488+
'test_bool_key': False,
489+
'test_double_key': 567.89
478490
}))
479491

480492
utils.set_proto_annotation(pb_event.annotation, annotation)
481493

482494
self.assertEqual(pb_event.annotation.description.value, "hi there")
483-
self.assertEqual(len(pb_event.annotation.attributes.attribute_map), 3)
495+
self.assertEqual(len(pb_event.annotation.attributes.attribute_map), 4)
484496
self.assertEqual(
485497
pb_event.annotation.attributes.attribute_map['test_str_key'],
486498
trace_pb2.AttributeValue(
@@ -492,6 +504,9 @@ def test_set_annotation_with_attributes(self):
492504
self.assertEqual(
493505
pb_event.annotation.attributes.attribute_map['test_bool_key'],
494506
trace_pb2.AttributeValue(bool_value=False))
507+
self.assertEqual(
508+
pb_event.annotation.attributes.attribute_map['test_double_key'],
509+
trace_pb2.AttributeValue(double_value=567.89))
495510

496511
def test_set_annotation_without_attributes(self):
497512
pb_span = trace_pb2.Span()

0 commit comments

Comments
 (0)