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

Commit 71b467b

Browse files
kornholic24t
authored andcommitted
Fix span batching to stackdriver (#425)
* Fix span batching to stackdriver * Lose FIXME comment
1 parent acc2d42 commit 71b467b

File tree

2 files changed

+44
-51
lines changed

2 files changed

+44
-51
lines changed

opencensus/trace/exporters/stackdriver_exporter.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,15 @@ def emit(self, span_datas):
216216
for sd in span_datas:
217217
trace_span_map[sd.context.trace_id] += [sd]
218218

219+
stackdriver_spans = []
219220
# Write spans to Stackdriver
220221
for _, sds in trace_span_map.items():
221222
# convert to the legacy trace json for easier refactoring
222223
# TODO: refactor this to use the span data directly
223224
trace = span_data.format_legacy_trace_json(sds)
224-
stackdriver_spans = self.translate_to_stackdriver(trace)
225-
self.client.batch_write_spans(project, stackdriver_spans)
225+
stackdriver_spans.extend(self.translate_to_stackdriver(trace))
226+
227+
self.client.batch_write_spans(project, {'spans': stackdriver_spans})
226228

227229
def export(self, span_datas):
228230
"""
@@ -248,7 +250,6 @@ def translate_to_stackdriver(self, trace):
248250
set_attributes(trace)
249251
spans_json = trace.get('spans')
250252
trace_id = trace.get('traceId')
251-
spans_list = []
252253

253254
for span in spans_json:
254255
span_name = 'projects/{}/traces/{}/spans/{}'.format(
@@ -273,10 +274,7 @@ def translate_to_stackdriver(self, trace):
273274
parent_span_id = str(span.get('parentSpanId'))
274275
span_json['parentSpanId'] = parent_span_id
275276

276-
spans_list.append(span_json)
277-
278-
spans = {'spans': spans_list}
279-
return spans
277+
yield span_json
280278

281279
def map_attributes(self, attribute_map):
282280
if attribute_map is None:

tests/unit/trace/exporters/test_stackdriver_exporter.py

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -195,54 +195,49 @@ def test_translate_to_stackdriver(self, monitor_resource_mock):
195195
client=client,
196196
project_id=project_id)
197197

198-
spans = exporter.translate_to_stackdriver(trace)
199-
200-
expected_traces = {
201-
'spans': [
202-
{
203-
'name': 'projects/{}/traces/{}/spans/{}'.format(
204-
project_id, trace_id, span_id),
205-
'displayName': {
206-
'value': span_name,
207-
'truncated_byte_count': 0
198+
spans = list(exporter.translate_to_stackdriver(trace))
199+
200+
expected_traces = [{
201+
'name': 'projects/{}/traces/{}/spans/{}'.format(
202+
project_id, trace_id, span_id),
203+
'displayName': {
204+
'value': span_name,
205+
'truncated_byte_count': 0
206+
},
207+
'attributes': {
208+
'attributeMap': {
209+
'g.co/agent': {
210+
'string_value': {
211+
'truncated_byte_count': 0,
212+
'value':
213+
'opencensus-python [{}]'.format(__version__)
214+
}
208215
},
209-
'attributes': {
210-
'attributeMap': {
211-
'g.co/agent': {
212-
'string_value': {
213-
'truncated_byte_count': 0,
214-
'value': 'opencensus-python [{}]'.format(
215-
__version__
216-
)
217-
}
218-
},
219-
'key': {
220-
'string_value': {
221-
'truncated_byte_count': 0,
222-
'value': 'value'
223-
}
224-
},
225-
'/http/host': {
226-
'string_value': {
227-
'truncated_byte_count': 0,
228-
'value': 'host'
229-
}
230-
}
216+
'key': {
217+
'string_value': {
218+
'truncated_byte_count': 0,
219+
'value': 'value'
231220
}
232221
},
233-
'spanId': str(span_id),
234-
'startTime': start_time,
235-
'endTime': end_time,
236-
'parentSpanId': str(parent_span_id),
237-
'status': None,
238-
'links': None,
239-
'stackTrace': None,
240-
'timeEvents': None,
241-
'childSpanCount': 0,
242-
'sameProcessAsParentSpan': None
222+
'/http/host': {
223+
'string_value': {
224+
'truncated_byte_count': 0,
225+
'value': 'host'
226+
}
227+
}
243228
}
244-
]
245-
}
229+
},
230+
'spanId': str(span_id),
231+
'startTime': start_time,
232+
'endTime': end_time,
233+
'parentSpanId': str(parent_span_id),
234+
'status': None,
235+
'links': None,
236+
'stackTrace': None,
237+
'timeEvents': None,
238+
'childSpanCount': 0,
239+
'sameProcessAsParentSpan': None
240+
}]
246241

247242
self.assertEqual(spans, expected_traces)
248243

0 commit comments

Comments
 (0)