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

Commit b9d248e

Browse files
authored
Data model (#68)
* Make Span data model match the protos * Remove kind handling * Add test for Span/Status * RequestHandler sets http status on root span as span status * Rename stackTrace variable names to st
1 parent 8b921b2 commit b9d248e

17 files changed

Lines changed: 362 additions & 228 deletions

ext/opencensus_trace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ static opencensus_trace_span_t *opencensus_trace_begin(zend_string *function_nam
273273
{
274274
opencensus_trace_span_t *span = opencensus_trace_span_alloc();
275275

276-
zend_fetch_debug_backtrace(&span->backtrace, 1, DEBUG_BACKTRACE_IGNORE_ARGS, 0);
276+
zend_fetch_debug_backtrace(&span->stackTrace, 1, DEBUG_BACKTRACE_IGNORE_ARGS, 0);
277277

278278
span->start = opencensus_now();
279279
span->name = zend_string_copy(function_name);
@@ -628,7 +628,7 @@ PHP_FUNCTION(opencensus_trace_list)
628628
ZVAL_ARR(&attribute, trace_span->attributes);
629629
zend_update_property(opencensus_trace_span_ce, &span, "attributes", sizeof("attributes") - 1, &attribute);
630630

631-
zend_update_property(opencensus_trace_span_ce, &span, "backtrace", sizeof("backtrace") - 1, &trace_span->backtrace);
631+
zend_update_property(opencensus_trace_span_ce, &span, "stackTrace", sizeof("stackTrace") - 1, &trace_span->stackTrace);
632632

633633
add_next_index_zval(return_value, &span);
634634
} ZEND_HASH_FOREACH_END();

ext/opencensus_trace_span.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,18 @@ static PHP_METHOD(OpenCensusTraceSpan, endTime) {
210210
}
211211

212212
/**
213-
* Fetch the backtrace from the moment the span was started
213+
* Fetch the stackTrace from the moment the span was started
214214
*
215215
* @return array
216216
*/
217-
static PHP_METHOD(OpenCensusTraceSpan, backtrace) {
217+
static PHP_METHOD(OpenCensusTraceSpan, stackTrace) {
218218
zval *val, rv;
219219

220220
if (zend_parse_parameters_none() == FAILURE) {
221221
return;
222222
}
223223

224-
val = zend_read_property(opencensus_trace_span_ce, getThis(), "backtrace", sizeof("backtrace") - 1, 1, &rv);
224+
val = zend_read_property(opencensus_trace_span_ce, getThis(), "stackTrace", sizeof("stackTrace") - 1, 1, &rv);
225225

226226
RETURN_ZVAL(val, 1, 0);
227227
}
@@ -252,7 +252,7 @@ static zend_function_entry opencensus_trace_span_methods[] = {
252252
PHP_ME(OpenCensusTraceSpan, attributes, NULL, ZEND_ACC_PUBLIC)
253253
PHP_ME(OpenCensusTraceSpan, startTime, NULL, ZEND_ACC_PUBLIC)
254254
PHP_ME(OpenCensusTraceSpan, endTime, NULL, ZEND_ACC_PUBLIC)
255-
PHP_ME(OpenCensusTraceSpan, backtrace, NULL, ZEND_ACC_PUBLIC)
255+
PHP_ME(OpenCensusTraceSpan, stackTrace, NULL, ZEND_ACC_PUBLIC)
256256
PHP_ME(OpenCensusTraceSpan, kind, NULL, ZEND_ACC_PUBLIC)
257257
PHP_FE_END
258258
};
@@ -275,7 +275,7 @@ int opencensus_trace_span_minit(INIT_FUNC_ARGS) {
275275
zend_declare_property_null(opencensus_trace_span_ce, "endTime", sizeof("endTime") - 1, ZEND_ACC_PROTECTED TSRMLS_CC);
276276
zend_declare_property_null(opencensus_trace_span_ce, "kind", sizeof("kind") - 1, ZEND_ACC_PROTECTED TSRMLS_CC);
277277
zend_declare_property_null(opencensus_trace_span_ce, "attributes", sizeof("attributes") - 1, ZEND_ACC_PROTECTED TSRMLS_CC);
278-
zend_declare_property_null(opencensus_trace_span_ce, "backtrace", sizeof("backtrace") - 1, ZEND_ACC_PROTECTED TSRMLS_CC);
278+
zend_declare_property_null(opencensus_trace_span_ce, "stackTrace", sizeof("stackTrace") - 1, ZEND_ACC_PROTECTED TSRMLS_CC);
279279

280280
REGISTER_TRACE_SPAN_CONSTANT(KIND_UNKNOWN);
281281
REGISTER_TRACE_SPAN_CONSTANT(KIND_CLIENT);

ext/opencensus_trace_span.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ typedef struct opencensus_trace_span_t {
3434
double start;
3535
double stop;
3636
struct opencensus_trace_span_t *parent;
37-
zval backtrace;
37+
zval stackTrace;
3838
zend_long kind;
3939

4040
// zend_string* => zval*

ext/tests/backtrace_test.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ $traces = opencensus_trace_list();
2020
echo "Number of traces: " . count($traces) . "\n";
2121

2222
foreach ($traces as $span) {
23-
var_dump($span->backtrace());
23+
var_dump($span->stackTrace());
2424
}
2525
?>
2626
--EXPECTF--

ext/tests/manual_spans_default_options.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Array
3333
(
3434
)
3535

36-
[backtrace:protected] => Array
36+
[stackTrace:protected] => Array
3737
(
3838
)
3939

@@ -51,7 +51,7 @@ Array
5151
(
5252
)
5353

54-
[backtrace:protected] => Array
54+
[stackTrace:protected] => Array
5555
(
5656
)
5757

src/Trace/Exporter/StackdriverExporter.php

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -202,35 +202,26 @@ public function processSpans(TracerInterface $tracer, $headers = null)
202202
*/
203203
public function convertSpans(TracerInterface $tracer)
204204
{
205-
$spanKindMap = [
206-
Span::SPAN_KIND_CLIENT => TraceSpan::SPAN_KIND_RPC_CLIENT,
207-
Span::SPAN_KIND_SERVER => TraceSpan::SPAN_KIND_RPC_SERVER
208-
];
209-
210205
// transform OpenCensus Spans to Google\Cloud\Spans
211-
return array_map(function ($span) use ($spanKindMap) {
212-
$kind = array_key_exists($span->kind(), $spanKindMap)
213-
? $spanKindMap[$span->kind()]
214-
: TraceSpan::SPAN_KIND_UNSPECIFIED;
206+
return array_map(function ($span) {
215207
$attributes = $span->attributes();
216-
$attributes[self::STACKTRACE] = $this->formatBacktrace($span->backtrace());
208+
$attributes[self::STACKTRACE] = $this->formatBacktrace($span->stackTrace());
217209
return new TraceSpan([
218210
'name' => $span->name(),
219211
'startTime' => $span->startTime(),
220212
'endTime' => $span->endTime(),
221213
'spanId' => hexdec($span->spanId()),
222214
'parentSpanId' => $span->parentSpanId() ? hexdec($span->parentSpanId()) : null,
223-
'labels' => $attributes,
224-
'kind' => $kind,
215+
'labels' => $attributes
225216
]);
226217
$span->info();
227218
}, $tracer->spans());
228219
}
229220

230-
private function formatBacktrace($bt)
221+
private function formatBacktrace($st)
231222
{
232223
return json_encode([
233-
'stack_frame' => array_map([$this, 'mapStackframe'], $bt)
224+
'stack_frame' => array_map([$this, 'mapStackframe'], $st)
234225
]);
235226
}
236227

@@ -272,18 +263,6 @@ private function addCommonAttributes(&$tracer, $headers = null)
272263
{
273264
$headers = $headers ?: $_SERVER;
274265

275-
// If a redirect, add the HTTP_REDIRECTED_URL attribute to the main span
276-
$responseCode = http_response_code();
277-
if ($responseCode == 301 || $responseCode == 302) {
278-
foreach (headers_list() as $header) {
279-
if (substr($header, 0, 9) == 'Location:') {
280-
$tracer->addRootAttribute(self::HTTP_REDIRECTED_URL, substr($header, 10));
281-
break;
282-
}
283-
}
284-
}
285-
$tracer->addRootAttribute(self::HTTP_STATUS_CODE, $responseCode);
286-
287266
$attributeMap = [
288267
self::HTTP_URL => ['REQUEST_URI'],
289268
self::HTTP_METHOD => ['REQUEST_METHOD'],

src/Trace/Exporter/ZipkinExporter.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,6 @@ public function convertSpans(TracerInterface $tracer, $headers = null)
109109
$rootSpan = $spans[0];
110110
$traceId = $tracer->spanContext()->traceId();
111111

112-
$kindMap = [
113-
Span::SPAN_KIND_CLIENT => 'CLIENT',
114-
Span::SPAN_KIND_SERVER => 'SERVER',
115-
Span::SPAN_KIND_PRODUCER => 'PRODUCER',
116-
Span::SPAN_KIND_CONSUMER => 'CONSUMER'
117-
];
118-
119112
// True is a request to store this span even if it overrides sampling policy.
120113
// This is true when the X-B3-Flags header has a value of 1.
121114
$isDebug = array_key_exists('HTTP_X_B3_FLAGS', $headers) && $headers['HTTP_X_B3_FLAGS'] == '1';
@@ -150,9 +143,6 @@ public function convertSpans(TracerInterface $tracer, $headers = null)
150143
'localEndpoint' => $localEndpoint,
151144
'tags' => $span->attributes()
152145
];
153-
if (array_key_exists($span->kind(), $kindMap)) {
154-
$zipkinSpan['kind'] = $kindMap[$span->kind()];
155-
}
156146

157147
$zipkinSpans[] = $zipkinSpan;
158148
}

src/Trace/RequestHandler.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class RequestHandler
4949
*/
5050
private $tracer;
5151

52+
/**
53+
* @var Span The primary span for this request
54+
*/
55+
private $rootSpan;
56+
5257
/**
5358
* @var Scope
5459
*/
@@ -104,8 +109,8 @@ public function __construct(
104109
'name' => $this->nameFromHeaders($headers),
105110
'attributes' => []
106111
];
107-
$span = $this->tracer->startSpan($spanOptions);
108-
$this->scope = $this->tracer->withSpan($span);
112+
$this->rootSpan = $this->tracer->startSpan($spanOptions);
113+
$this->scope = $this->tracer->withSpan($this->rootSpan);
109114

110115
register_shutdown_function([$this, 'onExit']);
111116
}
@@ -117,6 +122,17 @@ public function __construct(
117122
*/
118123
public function onExit()
119124
{
125+
$responseCode = http_response_code();
126+
if ($responseCode == 301 || $responseCode == 302) {
127+
foreach (headers_list() as $header) {
128+
if (substr($header, 0, 9) == 'Location:') {
129+
$this->rootSpan->addAttribute(self::HTTP_REDIRECTED_URL, substr($header, 10));
130+
break;
131+
}
132+
}
133+
}
134+
$this->rootSpan->setStatus($responseCode, "HTTP status code: $responseCode");
135+
120136
$this->scope->close();
121137
$this->reporter->report($this->tracer);
122138
}

0 commit comments

Comments
 (0)