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

Commit 2a7788a

Browse files
Olivier Cervelloliyanhui1228
authored andcommitted
Correct association of spans with trace for StackdriverExporter - Fixes #212 (#291)
1 parent 5524eb8 commit 2a7788a

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

opencensus/trace/exporters/stackdriver_exporter.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import os
1616

17+
from collections import defaultdict
1718
from google.cloud.trace.client import Client
1819

1920
from opencensus.trace import attributes_helper
@@ -143,14 +144,20 @@ def emit(self, span_datas):
143144
:param list of opencensus.trace.span_data.SpanData span_datas:
144145
SpanData tuples to emit
145146
"""
146-
name = 'projects/{}'.format(self.project_id)
147-
148-
# convert to the legacy trace json for easier refactoring
149-
# TODO: refactor this to use the span data directly
150-
trace = span_data.format_legacy_trace_json(span_datas)
151-
152-
stackdriver_spans = self.translate_to_stackdriver(trace)
153-
self.client.batch_write_spans(name, stackdriver_spans)
147+
project = 'projects/{}'.format(self.project_id)
148+
149+
# Map each span data to it's corresponding trace id
150+
trace_span_map = defaultdict(list)
151+
for sd in span_datas:
152+
trace_span_map[sd.context.trace_id] += [sd]
153+
154+
# Write spans to Stackdriver
155+
for _, sds in trace_span_map.items():
156+
# convert to the legacy trace json for easier refactoring
157+
# TODO: refactor this to use the span data directly
158+
trace = span_data.format_legacy_trace_json(sds)
159+
stackdriver_spans = self.translate_to_stackdriver(trace)
160+
self.client.batch_write_spans(project, stackdriver_spans)
154161

155162
def export(self, span_datas):
156163
"""

0 commit comments

Comments
 (0)