|
14 | 14 |
|
15 | 15 | import os |
16 | 16 |
|
17 | | -from opencensus.trace.attributes import Attributes |
| 17 | +from google.cloud.trace.client import Client |
| 18 | + |
18 | 19 | from opencensus.trace import attributes_helper |
| 20 | +from opencensus.trace import span_data |
| 21 | +from opencensus.trace.attributes import Attributes |
19 | 22 | from opencensus.trace.exporters import base |
20 | 23 | from opencensus.trace.exporters.transports import sync |
21 | 24 |
|
22 | | -from google.cloud.trace.client import Client |
23 | | - |
24 | | - |
25 | 25 | # OpenCensus Version |
26 | 26 | VERSION = '0.1.3' |
27 | 27 |
|
@@ -132,33 +132,46 @@ def __init__(self, client=None, project_id=None, |
132 | 132 | self.project_id = client.project |
133 | 133 | self.transport = transport(self) |
134 | 134 |
|
135 | | - def emit(self, spans): |
| 135 | + def emit(self, span_datas): |
136 | 136 | """ |
137 | | - :type spans: dict |
138 | | - :param spans: Spans collected. |
| 137 | + :type span_datas: list of :class: |
| 138 | + `~opencensus.trace.span_data.SpanData` |
| 139 | + :param list of opencensus.trace.span_data.SpanData span_datas: |
| 140 | + SpanData tuples to emit |
139 | 141 | """ |
140 | 142 | name = 'projects/{}'.format(self.project_id) |
141 | | - stackdriver_spans = self.translate_to_stackdriver(spans) |
| 143 | + |
| 144 | + # convert to the legacy trace json for easier refactoring |
| 145 | + # TODO: refactor this to use the span data directly |
| 146 | + trace = span_data.format_legacy_trace_json(span_datas) |
| 147 | + |
| 148 | + stackdriver_spans = self.translate_to_stackdriver(trace) |
142 | 149 | self.client.batch_write_spans(name, stackdriver_spans) |
143 | 150 |
|
144 | | - def export(self, trace): |
145 | | - self.transport.export(trace) |
| 151 | + def export(self, span_datas): |
| 152 | + """ |
| 153 | + :type span_datas: list of :class: |
| 154 | + `~opencensus.trace.span_data.SpanData` |
| 155 | + :param list of opencensus.trace.span_data.SpanData span_datas: |
| 156 | + SpanData tuples to export |
| 157 | + """ |
| 158 | + self.transport.export(span_datas) |
146 | 159 |
|
147 | | - def translate_to_stackdriver(self, spans): |
| 160 | + def translate_to_stackdriver(self, trace): |
148 | 161 | """Translate the spans json to Stackdriver format. |
149 | 162 |
|
150 | 163 | See: https://cloud.google.com/trace/docs/reference/v2/rest/v2/ |
151 | 164 | projects.traces/batchWrite |
152 | 165 |
|
153 | | - :type spans: dict |
154 | | - :param spans: Spans collected. |
| 166 | + :type trace: dict |
| 167 | + :param trace: Trace dictionary |
155 | 168 |
|
156 | 169 | :rtype: dict |
157 | 170 | :returns: Spans in Google Cloud StackDriver Trace format. |
158 | 171 | """ |
159 | | - set_attributes(spans) |
160 | | - spans_json = spans.get('spans') |
161 | | - trace_id = spans.get('traceId') |
| 172 | + set_attributes(trace) |
| 173 | + spans_json = trace.get('spans') |
| 174 | + trace_id = trace.get('traceId') |
162 | 175 | spans_list = [] |
163 | 176 |
|
164 | 177 | for span in spans_json: |
|
0 commit comments