|
20 | 20 | use Google\Cloud\Core\Batch\BatchRunner; |
21 | 21 | use Google\Cloud\Core\Batch\BatchTrait; |
22 | 22 | use Google\Cloud\Trace\TraceClient; |
23 | | -use Google\Cloud\Trace\TraceSpan; |
| 23 | +use Google\Cloud\Trace\Span; |
| 24 | +use Google\Cloud\Trace\Trace; |
24 | 25 | use OpenCensus\Trace\Tracer\TracerInterface; |
25 | | -use OpenCensus\Trace\Span; |
26 | 26 |
|
27 | 27 | /** |
28 | 28 | * This implementation of the ExporterInterface use the BatchRunner to provide |
@@ -88,7 +88,6 @@ class StackdriverExporter implements ExporterInterface |
88 | 88 | const HTTP_URL = '/http/url'; |
89 | 89 | const HTTP_USER_AGENT = '/http/user_agent'; |
90 | 90 | const PID = '/pid'; |
91 | | - const STACKTRACE = '/stacktrace'; |
92 | 91 | const TID = '/tid'; |
93 | 92 |
|
94 | 93 | const GAE_APPLICATION_ERROR = 'g.co/gae/application_error'; |
@@ -157,16 +156,16 @@ public function __construct(array $options = []) |
157 | 156 | public function report(TracerInterface $tracer) |
158 | 157 | { |
159 | 158 | $this->processSpans($tracer); |
160 | | - $spans = $this->convertSpans($tracer); |
| 159 | + $trace = self::$client->trace( |
| 160 | + $tracer->spanContext()->traceId() |
| 161 | + ); |
| 162 | + $spans = $this->convertSpans($tracer, $trace); |
161 | 163 |
|
162 | 164 | if (empty($spans)) { |
163 | 165 | return false; |
164 | 166 | } |
165 | 167 |
|
166 | 168 | // build a Trace object and assign Spans |
167 | | - $trace = self::$client->trace( |
168 | | - $tracer->spanContext()->traceId() |
169 | | - ); |
170 | 169 | $trace->setSpans($spans); |
171 | 170 |
|
172 | 171 | try { |
@@ -196,53 +195,28 @@ public function processSpans(TracerInterface $tracer, $headers = null) |
196 | 195 | /** |
197 | 196 | * Convert spans into Zipkin's expected JSON output format. |
198 | 197 | * |
199 | | - * @param TracerInterface $tracer |
| 198 | + * @param TracerInterface $tracer |
| 199 | + * @param Trace $trace |
200 | 200 | * @return array Representation of the collected trace spans ready for serialization |
201 | 201 | */ |
202 | 202 | public function convertSpans(TracerInterface $tracer) |
203 | 203 | { |
204 | | - // transform OpenCensus Spans to Google\Cloud\Spans |
205 | | - return array_map(function ($span) { |
206 | | - $attributes = $span->attributes(); |
207 | | - $attributes[self::STACKTRACE] = $this->formatBacktrace($span->stackTrace()); |
208 | | - return new TraceSpan([ |
| 204 | + $traceId = $tracer->spanContext()->traceId(); |
| 205 | + |
| 206 | + // transform OpenCensus Spans to Google\Cloud\Trace\Spans |
| 207 | + return array_map(function ($span) use ($traceId) { |
| 208 | + return new Span($traceId, [ |
209 | 209 | 'name' => $span->name(), |
210 | 210 | 'startTime' => $span->startTime(), |
211 | 211 | 'endTime' => $span->endTime(), |
212 | | - 'spanId' => hexdec($span->spanId()), |
213 | | - 'parentSpanId' => $span->parentSpanId() ? hexdec($span->parentSpanId()) : null, |
214 | | - 'labels' => $attributes |
| 212 | + 'spanId' => $span->spanId(), |
| 213 | + 'parentSpanId' => $span->parentSpanId(), |
| 214 | + 'attributes' => $span->attributes(), |
| 215 | + 'stackTrace' => $span->stackTrace() |
215 | 216 | ]); |
216 | | - $span->info(); |
217 | 217 | }, $tracer->spans()); |
218 | 218 | } |
219 | 219 |
|
220 | | - private function formatBacktrace($st) |
221 | | - { |
222 | | - return json_encode([ |
223 | | - 'stack_frame' => array_map([$this, 'mapStackframe'], $st) |
224 | | - ]); |
225 | | - } |
226 | | - |
227 | | - private function mapStackframe($sf) |
228 | | - { |
229 | | - // file and line should always be set |
230 | | - $data = []; |
231 | | - if (isset($sf['line'])) { |
232 | | - $data['line_number'] = $sf['line']; |
233 | | - } |
234 | | - if (isset($sf['file'])) { |
235 | | - $data['file_name'] = $sf['file']; |
236 | | - } |
237 | | - if (isset($sf['function'])) { |
238 | | - $data['method_name'] = $sf['function']; |
239 | | - } |
240 | | - if (isset($sf['class'])) { |
241 | | - $data['class_name'] = $sf['class']; |
242 | | - } |
243 | | - return $data; |
244 | | - } |
245 | | - |
246 | 220 | /** |
247 | 221 | * Returns an array representation of a callback which will be used to write |
248 | 222 | * batch items. |
|
0 commit comments