|
19 | 19 |
|
20 | 20 | from google.cloud.trace.client import Client |
21 | 21 |
|
| 22 | + |
22 | 23 | # Environment variable set in App Engine when vm:true is set. |
23 | 24 | _APPENGINE_FLEXIBLE_ENV_VM = 'GAE_APPENGINE_HOSTNAME' |
24 | 25 |
|
@@ -90,54 +91,67 @@ class StackdriverExporter(base.Exporter): |
90 | 91 | """ |
91 | 92 | def __init__(self, client=None, project_id=None, |
92 | 93 | transport=sync.SyncTransport): |
93 | | - # The client will handler the case when project_id is None |
| 94 | + # The client will handle the case when project_id is None |
94 | 95 | if client is None: |
95 | 96 | client = Client(project=project_id) |
96 | 97 |
|
97 | 98 | self.client = client |
98 | 99 | self.project_id = client.project |
99 | 100 | self.transport = transport(self) |
100 | 101 |
|
101 | | - def emit(self, trace): |
| 102 | + def emit(self, spans): |
102 | 103 | """ |
103 | | - :type trace: dict |
104 | | - :param trace: Trace collected. |
| 104 | + :type spans: dict |
| 105 | + :param spans: Spans collected. |
105 | 106 | """ |
106 | | - stackdriver_traces = self.translate_to_stackdriver(trace) |
107 | | - self.client.patch_traces(stackdriver_traces) |
| 107 | + name = 'projects/{}'.format(self.project_id) |
| 108 | + stackdriver_spans = self.translate_to_stackdriver(spans) |
| 109 | + self.client.batch_write_spans(name, stackdriver_spans) |
108 | 110 |
|
109 | 111 | def export(self, trace): |
110 | 112 | self.transport.export(trace) |
111 | 113 |
|
112 | | - def translate_to_stackdriver(self, trace): |
113 | | - """ |
114 | | - :type trace: dict |
115 | | - :param trace: Trace collected. |
| 114 | + def translate_to_stackdriver(self, spans): |
| 115 | + """Translate the spans json to Stackdriver format. |
| 116 | +
|
| 117 | + See: https://cloud.google.com/trace/docs/reference/v2/rest/v2/ |
| 118 | + projects.traces/batchWrite |
| 119 | +
|
| 120 | + :type spans: dict |
| 121 | + :param spans: Spans collected. |
116 | 122 |
|
117 | 123 | :rtype: dict |
118 | | - :returns: Traces in Google Cloud StackDriver Trace format. |
| 124 | + :returns: Spans in Google Cloud StackDriver Trace format. |
119 | 125 | """ |
120 | | - set_attributes(trace) |
121 | | - spans = trace.get('spans') |
122 | | - trace_id = trace.get('traceId') |
123 | | - spans_json = [] |
| 126 | + set_attributes(spans) |
| 127 | + spans_json = spans.get('spans') |
| 128 | + trace_id = spans.get('traceId') |
| 129 | + spans_list = [] |
| 130 | + |
| 131 | + for span in spans_json: |
| 132 | + span_name = 'projects/{}/traces/{}/spans/{}'.format( |
| 133 | + self.project_id, trace_id, span.get('spanId')) |
124 | 134 |
|
125 | | - for span in spans: |
126 | 135 | span_json = { |
127 | | - 'name': span.get('name'), |
| 136 | + 'name': span_name, |
| 137 | + 'displayName': span.get('displayName'), |
128 | 138 | 'startTime': span.get('startTime'), |
129 | 139 | 'endTime': span.get('endTime'), |
130 | | - 'spanId': span.get('spanId'), |
131 | | - 'parentSpanId': span.get('parentSpanId'), |
132 | | - 'labels': span.get('attributes') |
| 140 | + 'spanId': str(span.get('spanId')), |
| 141 | + 'attributes': span.get('attributes'), |
| 142 | + 'links': span.get('links'), |
| 143 | + 'status': span.get('status'), |
| 144 | + 'stackTrace': span.get('stackTrace'), |
| 145 | + 'timeEvents': span.get('timeEvents'), |
| 146 | + 'sameProcessAsParentSpan': span.get('sameProcessAsParentSpan'), |
| 147 | + 'childSpanCount': span.get('childSpanCount') |
133 | 148 | } |
134 | | - spans_json.append(span_json) |
135 | 149 |
|
136 | | - trace_json = { |
137 | | - 'projectId': self.project_id, |
138 | | - 'traceId': trace_id, |
139 | | - 'spans': spans_json |
140 | | - } |
| 150 | + if span.get('parentSpanId') is not None: |
| 151 | + parent_span_id = str(span.get('parentSpanId')) |
| 152 | + span_json['parentSpanId'] = parent_span_id |
| 153 | + |
| 154 | + spans_list.append(span_json) |
141 | 155 |
|
142 | | - traces = {'traces': [trace_json]} |
143 | | - return traces |
| 156 | + spans = {'spans': spans_list} |
| 157 | + return spans |
0 commit comments