Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit d186438

Browse files
authored
Use Stackdriver V2 from PR branch (#75)
1 parent df3e55e commit d186438

3 files changed

Lines changed: 32 additions & 82 deletions

File tree

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@
2020
"require-dev": {
2121
"phpunit/phpunit": "4.8.*",
2222
"squizlabs/php_codesniffer": "2.*",
23-
"google/cloud-trace": "^0.3",
23+
"google/cloud": "dev-trace-v2",
2424
"twig/twig": "~2.0",
2525
"symfony/yaml": "~3.3"
2626
},
27+
"repositories": [
28+
{
29+
"type": "git",
30+
"url": "https://github.com/chingor13/google-cloud-php"
31+
}
32+
],
2733
"conflict": {
2834
"ext-opencensus": ">= 0.1.0"
2935
},

src/Trace/Exporter/StackdriverExporter.php

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
use Google\Cloud\Core\Batch\BatchRunner;
2121
use Google\Cloud\Core\Batch\BatchTrait;
2222
use Google\Cloud\Trace\TraceClient;
23-
use Google\Cloud\Trace\TraceSpan;
23+
use Google\Cloud\Trace\Span;
24+
use Google\Cloud\Trace\Trace;
2425
use OpenCensus\Trace\Tracer\TracerInterface;
25-
use OpenCensus\Trace\Span;
2626

2727
/**
2828
* This implementation of the ExporterInterface use the BatchRunner to provide
@@ -88,7 +88,6 @@ class StackdriverExporter implements ExporterInterface
8888
const HTTP_URL = '/http/url';
8989
const HTTP_USER_AGENT = '/http/user_agent';
9090
const PID = '/pid';
91-
const STACKTRACE = '/stacktrace';
9291
const TID = '/tid';
9392

9493
const GAE_APPLICATION_ERROR = 'g.co/gae/application_error';
@@ -157,16 +156,16 @@ public function __construct(array $options = [])
157156
public function report(TracerInterface $tracer)
158157
{
159158
$this->processSpans($tracer);
160-
$spans = $this->convertSpans($tracer);
159+
$trace = self::$client->trace(
160+
$tracer->spanContext()->traceId()
161+
);
162+
$spans = $this->convertSpans($tracer, $trace);
161163

162164
if (empty($spans)) {
163165
return false;
164166
}
165167

166168
// build a Trace object and assign Spans
167-
$trace = self::$client->trace(
168-
$tracer->spanContext()->traceId()
169-
);
170169
$trace->setSpans($spans);
171170

172171
try {
@@ -196,53 +195,28 @@ public function processSpans(TracerInterface $tracer, $headers = null)
196195
/**
197196
* Convert spans into Zipkin's expected JSON output format.
198197
*
199-
* @param TracerInterface $tracer
198+
* @param TracerInterface $tracer
199+
* @param Trace $trace
200200
* @return array Representation of the collected trace spans ready for serialization
201201
*/
202202
public function convertSpans(TracerInterface $tracer)
203203
{
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, [
209209
'name' => $span->name(),
210210
'startTime' => $span->startTime(),
211211
'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()
215216
]);
216-
$span->info();
217217
}, $tracer->spans());
218218
}
219219

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-
246220
/**
247221
* Returns an array representation of a callback which will be used to write
248222
* batch items.

tests/unit/Trace/Exporter/StackdriverExporterTest.php

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
use OpenCensus\Trace\Span as OpenCensusSpan;
2727
use Prophecy\Argument;
2828
use Google\Cloud\Trace\Trace;
29-
use Google\Cloud\Trace\TraceSpan;
29+
use Google\Cloud\Trace\Span;
3030
use Google\Cloud\Trace\TraceClient;
3131

3232
/**
@@ -55,11 +55,11 @@ public function testFormatsTrace()
5555

5656
$this->assertCount(3, $spans);
5757
foreach ($spans as $span) {
58-
$this->assertInstanceOf(TraceSpan::class, $span);
58+
$this->assertInstanceOf(Span::class, $span);
5959
$this->assertInternalType('string', $span->name());
60-
$this->assertInternalType('int', $span->spanId());
61-
$this->assertRegExp('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}Z/', $span->info()['startTime']);
62-
$this->assertRegExp('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}Z/', $span->info()['endTime']);
60+
$this->assertInternalType('string', $span->spanId());
61+
$this->assertRegExp('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}Z/', $span->jsonSerialize()['startTime']);
62+
$this->assertRegExp('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}Z/', $span->jsonSerialize()['endTime']);
6363
}
6464
}
6565

@@ -82,23 +82,6 @@ public function testReportWithAnExceptionErrorLog()
8282
$this->assertFalse($reporter->report($tracer));
8383
}
8484

85-
public function testHandlesKind()
86-
{
87-
$tracer = new ContextTracer(new SpanContext('testtraceid'));
88-
$tracer->inSpan(['name' => 'main'], function () use ($tracer) {
89-
$tracer->inSpan(['name' => 'span1'], 'usleep', [1]);
90-
$tracer->inSpan(['name' => 'span2'], 'usleep', [1]);
91-
$tracer->inSpan(['name' => 'span3'], 'usleep', [1]);
92-
$tracer->inSpan(['name' => 'span4'], 'usleep', [1]);
93-
});
94-
95-
$reporter = new StackdriverExporter(['client' => $this->client->reveal()]);
96-
$spans = $reporter->convertSpans($tracer);
97-
98-
$this->assertCount(5, $spans);
99-
$this->assertEquals(TraceSpan::SPAN_KIND_UNSPECIFIED, $spans[0]->info()['kind']);
100-
}
101-
10285
/**
10386
* @dataProvider attributeHeaders
10487
*/
@@ -131,7 +114,7 @@ public function attributeHeaders()
131114
];
132115
}
133116

134-
public function testStacktraceAttribute()
117+
public function testStacktrace()
135118
{
136119
$stackTrace = [
137120
[
@@ -148,20 +131,7 @@ public function testStacktraceAttribute()
148131
$reporter = new StackdriverExporter(['client' => $this->client->reveal()]);
149132
$spans = $reporter->convertSpans($tracer);
150133

151-
$attributes = $spans[0]->info()['labels'];
152-
$this->assertArrayHasKey('/stacktrace', $attributes);
153-
154-
$expected = [
155-
'stack_frame' => [
156-
[
157-
'file_name' => '/path/to/file.php',
158-
'line_number' => 1234,
159-
'method_name' => 'asdf',
160-
'class_name' => 'Foo'
161-
]
162-
]
163-
];
164-
$data = json_decode($attributes['/stacktrace'], true);
165-
$this->assertEquals($expected, $data);
134+
$data = $spans[0]->jsonSerialize();
135+
$this->assertArrayHasKey('stackTrace', $data);
166136
}
167137
}

0 commit comments

Comments
 (0)