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

Commit 1b4d307

Browse files
authored
Add span kind to extension and Span (#151)
* Add span kind to extension and Span * Add span kind extension test and fix memory leak * Dump out extension test failures * ZipkinExporter respects span kind * Set span kind for known integrations
1 parent f4e3c41 commit 1b4d307

31 files changed

+328
-64
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RUN phpize && \
3838
./configure --enable-opencensus && \
3939
make clean && \
4040
make && \
41-
make test && \
41+
make test || ((find . -name '*.diff' | xargs cat) && false) && \
4242
make install
4343

4444
WORKDIR /build

ext/opencensus_trace_span.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* protected $stackTrace;
3131
* protected $links;
3232
* protected $timeEvents;
33+
* protected $kind;
3334
*
3435
* public function __construct(array $spanOptions)
3536
* {
@@ -82,6 +83,11 @@
8283
* {
8384
* return $this->timeEvents;
8485
* }
86+
*
87+
* public function kind()
88+
* {
89+
* return $this->kind;
90+
* }
8591
* }
8692
*/
8793

@@ -297,6 +303,23 @@ static PHP_METHOD(OpenCensusTraceSpan, stackTrace) {
297303
RETURN_ZVAL(val, 1, 0);
298304
}
299305

306+
/**
307+
* Fetch the span kind
308+
*
309+
* @return string
310+
*/
311+
static PHP_METHOD(OpenCensusTraceSpan, kind) {
312+
zval *val, rv;
313+
314+
if (zend_parse_parameters_none() == FAILURE) {
315+
return;
316+
}
317+
318+
val = zend_read_property(opencensus_trace_span_ce, getThis(), "kind", sizeof("kind") - 1, 1, &rv);
319+
320+
RETURN_ZVAL(val, 1, 0);
321+
}
322+
300323
/* Declare method entries for the OpenCensus\Trace\Span class */
301324
static zend_function_entry opencensus_trace_span_methods[] = {
302325
PHP_ME(OpenCensusTraceSpan, __construct, arginfo_OpenCensusTraceSpan_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
@@ -310,6 +333,7 @@ static zend_function_entry opencensus_trace_span_methods[] = {
310333
PHP_ME(OpenCensusTraceSpan, stackTrace, NULL, ZEND_ACC_PUBLIC)
311334
PHP_ME(OpenCensusTraceSpan, links, NULL, ZEND_ACC_PUBLIC)
312335
PHP_ME(OpenCensusTraceSpan, timeEvents, NULL, ZEND_ACC_PUBLIC)
336+
PHP_ME(OpenCensusTraceSpan, kind, NULL, ZEND_ACC_PUBLIC)
313337
PHP_FE_END
314338
};
315339

@@ -331,6 +355,7 @@ int opencensus_trace_span_minit(INIT_FUNC_ARGS) {
331355
zend_declare_property_null(opencensus_trace_span_ce, "stackTrace", sizeof("stackTrace") - 1, ZEND_ACC_PROTECTED TSRMLS_CC);
332356
zend_declare_property_null(opencensus_trace_span_ce, "links", sizeof("links") - 1, ZEND_ACC_PROTECTED TSRMLS_CC);
333357
zend_declare_property_null(opencensus_trace_span_ce, "timeEvents", sizeof("timeEvents") - 1, ZEND_ACC_PROTECTED TSRMLS_CC);
358+
zend_declare_property_null(opencensus_trace_span_ce, "kind", sizeof("kind") - 1, ZEND_ACC_PROTECTED TSRMLS_CC);
334359

335360
return SUCCESS;
336361
}
@@ -380,6 +405,11 @@ opencensus_trace_span_t *opencensus_trace_span_alloc()
380405
span->name = NULL;
381406
span->parent = NULL;
382407
span->span_id = NULL;
408+
span->kind = zend_string_init(
409+
OPENCENSUS_TRACE_SPAN_KIND_UNSPECIFIED,
410+
strlen(OPENCENSUS_TRACE_SPAN_KIND_UNSPECIFIED),
411+
0
412+
);
383413
span->start = 0;
384414
span->stop = 0;
385415
ALLOC_HASHTABLE(span->attributes);
@@ -413,6 +443,9 @@ void opencensus_trace_span_free(opencensus_trace_span_t *span)
413443
if (span->span_id) {
414444
zend_string_release(span->span_id);
415445
}
446+
if (span->kind) {
447+
zend_string_release(span->kind);
448+
}
416449

417450
ZVAL_DESTRUCTOR(&span->stackTrace);
418451

@@ -502,6 +535,11 @@ int opencensus_trace_span_apply_span_options(opencensus_trace_span_t *span, zval
502535
zend_string_release(span->span_id);
503536
}
504537
span->span_id = zend_string_copy(Z_STR_P(v));
538+
} else if (strcmp(ZSTR_VAL(k), "kind") == 0) {
539+
if (span->kind) {
540+
zend_string_release(span->kind);
541+
}
542+
span->kind = zend_string_copy(Z_STR_P(v));
505543
}
506544
} ZEND_HASH_FOREACH_END();
507545
return SUCCESS;
@@ -570,5 +608,9 @@ int opencensus_trace_span_to_zval(opencensus_trace_span_t *trace_span, zval *spa
570608
opencensus_trace_update_time_events(trace_span, &time_events);
571609
zend_update_property(opencensus_trace_span_ce, span, "timeEvents", sizeof("timeEvents") - 1, &time_events);
572610

611+
if (trace_span->kind) {
612+
zend_update_property_str(opencensus_trace_span_ce, span, "kind", sizeof("kind") - 1, trace_span->kind);
613+
}
614+
573615
return SUCCESS;
574616
}

ext/opencensus_trace_span.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121

2222
extern zend_class_entry* opencensus_trace_span_ce;
2323

24+
#define OPENCENSUS_TRACE_SPAN_KIND_UNSPECIFIED "SPAN_KIND_UNSPECIFIED"
25+
2426
// TraceSpan struct
2527
typedef struct opencensus_trace_span_t {
2628
zend_string *name;
2729
zend_string *span_id;
30+
zend_string *kind;
2831
double start;
2932
double stop;
3033
struct opencensus_trace_span_t *parent;

ext/span.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Span {
3030
protected $stackTrace;
3131
protected $links;
3232
protected $timeEvents;
33+
protected $kind;
3334

3435
public function __construct(array $spanOptions)
3536
{
@@ -82,4 +83,9 @@ public function timeEvents()
8283
{
8384
return $this->timeEvents;
8485
}
86+
87+
public function kind()
88+
{
89+
return $this->kind;
90+
}
8591
}

ext/tests/manual_spans.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ echo "Span startTime is: '{$span->startTime()}'\n";
3030
$test = gettype($span->endTime()) == 'double';
3131
echo "Span endTime is a double: $test\n";
3232

33+
echo "Span kind is: " . $span->kind() . PHP_EOL;
34+
3335
print_r($span->attributes());
3436

3537
$span = $traces[1];
@@ -45,6 +47,8 @@ echo "Span startTime is a double: $test\n";
4547
$test = gettype($span->endTime()) == 'double';
4648
echo "Span endTime is a double: $test\n";
4749

50+
echo "Span kind is: " . $span->kind() . PHP_EOL;
51+
4852
print_r($span->attributes());
4953
?>
5054
--EXPECT--
@@ -54,6 +58,7 @@ Span name is: '/'
5458
Span startTime is a double: 1
5559
Span startTime is: '0.1'
5660
Span endTime is a double: 1
61+
Span kind is: SPAN_KIND_UNSPECIFIED
5762
Array
5863
(
5964
[asdf] => qwer
@@ -62,6 +67,7 @@ Span id is a string
6267
Span name is: 'inner-1'
6368
Span startTime is a double: 1
6469
Span endTime is a double: 1
70+
Span kind is: SPAN_KIND_UNSPECIFIED
6571
Array
6672
(
6773
[foo] => bar

ext/tests/manual_spans_default_options.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Array
4444
(
4545
)
4646

47+
[kind:protected] => SPAN_KIND_UNSPECIFIED
4748
)
4849

4950
[1] => OpenCensus\Trace\Ext\Span Object
@@ -69,6 +70,7 @@ Array
6970
(
7071
)
7172

73+
[kind:protected] => SPAN_KIND_UNSPECIFIED
7274
)
7375

7476
)

ext/tests/span_kind.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
OpenCensus Trace: Set span kind
3+
--FILE--
4+
<?php
5+
6+
require_once(__DIR__ . '/common.php');
7+
8+
opencensus_trace_begin('/', [
9+
'startTime' => 0.1,
10+
'kind' => 'SERVER'
11+
]);
12+
13+
opencensus_trace_begin('inner-1', [
14+
'kind' => 'CLIENT'
15+
]);
16+
17+
opencensus_trace_finish();
18+
19+
opencensus_trace_finish();
20+
21+
$traces = opencensus_trace_list();
22+
echo "Number of traces: " . count($traces) . "\n";
23+
echo "Span kind is " . $traces[0]->kind() . PHP_EOL;
24+
echo "Span kind is " . $traces[1]->kind() . PHP_EOL;
25+
26+
?>
27+
--EXPECT--
28+
Number of traces: 2
29+
Span kind is SERVER
30+
Span kind is CLIENT

src/Trace/Exporter/ZipkinExporter.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace OpenCensus\Trace\Exporter;
1919

2020
use OpenCensus\Trace\MessageEvent;
21+
use OpenCensus\Trace\Span;
2122
use OpenCensus\Trace\SpanData;
2223

2324
/**
@@ -38,6 +39,11 @@ class ZipkinExporter implements ExporterInterface
3839
const KIND_SERVER = 'SERVER';
3940
const KIND_CLIENT = 'CLIENT';
4041
const DEFAULT_ENDPOINT = 'http://localhost:9411/api/v2/spans';
42+
const KIND_MAP = [
43+
Span::KIND_UNSPECIFIED => null,
44+
Span::KIND_SERVER => self::KIND_SERVER,
45+
Span::KIND_CLIENT => self::KIND_CLIENT
46+
];
4147

4248
/**
4349
* @var string
@@ -184,6 +190,11 @@ public function convertSpans(array $spans, $headers = null)
184190

185191
private function spanKind(SpanData $span)
186192
{
193+
$kind = self::KIND_MAP[$span->kind()];
194+
if ($kind !== null) {
195+
return $kind;
196+
}
197+
187198
if (strpos($span->name(), 'Sent.') === 0) {
188199
return self::KIND_CLIENT;
189200
}

src/Trace/Integrations/Curl.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace OpenCensus\Trace\Integrations;
1919

20+
use OpenCensus\Trace\Span;
21+
2022
/**
2123
* This class handles instrumenting curl requests using the opencensus extension.
2224
*
@@ -56,7 +58,8 @@ public static function handleCurlResource($resource)
5658
return [
5759
'attributes' => [
5860
'uri' => curl_getinfo($resource, CURLINFO_EFFECTIVE_URL)
59-
]
61+
],
62+
'kind' => Span::KIND_CLIENT
6063
];
6164
}
6265
}

src/Trace/Integrations/Doctrine.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace OpenCensus\Trace\Integrations;
1919

2020
use Doctrine\ORM\Version;
21+
use OpenCensus\Trace\Span;
2122

2223
/**
2324
* This class handles instrumenting the Doctrine ORM queries using the opencensus extension.
@@ -52,23 +53,26 @@ public static function load()
5253
opencensus_trace_method($persisterClass, 'load', function ($bep) {
5354
return [
5455
'name' => 'doctrine/load',
55-
'attributes' => ['entity' => $bep->getClassMetadata()->name]
56+
'attributes' => ['entity' => $bep->getClassMetadata()->name],
57+
'kind' => Span::KIND_CLIENT
5658
];
5759
});
5860

5961
// public function loadAll(array $criteria = array(), array $orderBy = null, $limit = null, $offset = null)
6062
opencensus_trace_method($persisterClass, 'loadAll', function ($bep) {
6163
return [
6264
'name' => 'doctrine/loadAll',
63-
'attributes' => ['entity' => $bep->getClassMetadata()->name]
65+
'attributes' => ['entity' => $bep->getClassMetadata()->name],
66+
'kind' => Span::KIND_CLIENT
6467
];
6568
});
6669

6770
// public int PDOConnection::exec(string $query)
6871
opencensus_trace_method(PDOConnection::class, 'exec', function ($scope, $query) {
6972
return [
7073
'name' => 'doctrine/exec',
71-
'attributes' => ['query' => $query]
74+
'attributes' => ['query' => $query],
75+
'kind' => Span::KIND_CLIENT
7276
];
7377
});
7478
}

0 commit comments

Comments
 (0)