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

Commit 890c19e

Browse files
authored
JaegerExporter : handle annotations (#324)
* JaegerExporter : handle annotations * Minor fix * Add test case * Fix test case
1 parent 17b9279 commit 890c19e

2 files changed

Lines changed: 64 additions & 8 deletions

File tree

opencensus/trace/exporters/jaeger_exporter.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,10 @@ def _extract_logs_from_span(span):
280280
annotation = time_event.annotation
281281
if not annotation:
282282
continue
283-
fields = _extract_tags(annotation.attributes)
283+
284+
fields = []
285+
if annotation.attributes is not None:
286+
fields = _extract_tags(annotation.attributes.attributes)
284287

285288
fields.append(jaeger.Tag(
286289
key='message',
@@ -297,7 +300,7 @@ def _extract_logs_from_span(span):
297300

298301
def _extract_tags(attr):
299302
if attr is None:
300-
return None
303+
return []
301304
tags = []
302305
for attribute_key, attribute_value in attr.items():
303306
tag = _convert_attribute_to_tag(attribute_key, attribute_value)

tests/unit/trace/exporters/test_jaeger_exporter.py

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
import mock
1818

19-
from opencensus.trace import (link, span_context, span_data, status,
20-
time_event, trace_options)
19+
from opencensus.trace import (attributes, link, span_context, span_data,
20+
status, time_event)
2121
from opencensus.trace.exporters import jaeger_exporter
2222
from opencensus.trace.exporters.gen.jaeger import jaeger
2323

@@ -178,7 +178,7 @@ def test_translate_to_jaeger(self):
178178
span_id = '6e0c63257de34c92'
179179
parent_span_id = '1111111111111111'
180180

181-
attributes = {
181+
span_attributes = {
182182
'key_bool': False,
183183
'key_string': 'hello_world',
184184
'key_int': 3
@@ -200,7 +200,21 @@ def test_translate_to_jaeger(self):
200200
timestamp=time,
201201
annotation=time_event.Annotation(
202202
description='First Annotation',
203-
attributes=annotation_attributes)),
203+
attributes=attributes.Attributes(annotation_attributes))),
204+
time_event.TimeEvent(
205+
timestamp=time,
206+
message_event=time_event.MessageEvent(
207+
id='message-event-id',
208+
uncompressed_size_bytes=0,
209+
)),
210+
]
211+
212+
time_events2 = [
213+
time_event.TimeEvent(
214+
timestamp=time,
215+
annotation=time_event.Annotation(
216+
description='First Annotation',
217+
attributes=None)),
204218
time_event.TimeEvent(
205219
timestamp=time,
206220
message_event=time_event.MessageEvent(
@@ -238,7 +252,7 @@ def test_translate_to_jaeger(self):
238252
context=span_context.SpanContext(trace_id=trace_id),
239253
span_id=span_id,
240254
parent_span_id=parent_span_id,
241-
attributes=attributes,
255+
attributes=span_attributes,
242256
start_time=start_time,
243257
end_time=end_time,
244258
child_span_count=0,
@@ -259,6 +273,22 @@ def test_translate_to_jaeger(self):
259273
end_time=end_time,
260274
child_span_count=None,
261275
stack_trace=None,
276+
time_events=time_events2,
277+
links=None,
278+
status=None,
279+
same_process_as_parent_span=None,
280+
span_kind=None,
281+
),
282+
span_data.SpanData(
283+
name='test3',
284+
context=None,
285+
span_id=span_id,
286+
parent_span_id=None,
287+
attributes=None,
288+
start_time=start_time,
289+
end_time=end_time,
290+
child_span_count=None,
291+
stack_trace=None,
262292
time_events=None,
263293
links=None,
264294
status=None,
@@ -341,7 +371,28 @@ def test_translate_to_jaeger(self):
341371
spanId=7929822056569588882,
342372
parentSpanId=0,
343373
startTime=1502820146071158,
344-
duration=10000000)
374+
duration=10000000,
375+
logs=[
376+
jaeger.Log(
377+
timestamp=1502820146000,
378+
fields=[
379+
jaeger.Tag(
380+
key='message',
381+
vType=jaeger.TagType.STRING,
382+
vStr='First Annotation')
383+
])
384+
]
385+
),
386+
jaeger.Span(
387+
operationName="test3",
388+
traceIdHigh=7929822056569588882,
389+
traceIdLow=-4638992594902767826,
390+
spanId=7929822056569588882,
391+
parentSpanId=0,
392+
startTime=1502820146071158,
393+
duration=10000000,
394+
logs=[]
395+
)
345396
]
346397

347398
spans_json = [span.format_span_json() for span in spans]
@@ -376,6 +427,8 @@ def test_translate_to_jaeger(self):
376427
self.assertEqual(span.get('flags'), expected_span.get('flags'))
377428
self.assertEqual(spans_json[1], expected_spans_json[1])
378429

430+
self.assertEqual(spans_json[2], expected_spans_json[2])
431+
379432
def test_convert_hex_str_to_int(self):
380433
invalid_id = '990c63257de34c92'
381434
jaeger_exporter._convert_hex_str_to_int(invalid_id)

0 commit comments

Comments
 (0)