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

Commit b50c5bd

Browse files
authored
Span kind (#19)
* Add failing tests for handling span kind * Handle span kind. Use constants with int values for keeping track of span kind. Each reporter is responsible for mapping this constant to its representation of span kind. * Add failing test for defined span kind constants * Move constants to the Span class * Register span kind constants in span class * Add failing tests for implementing span kind in C * Handle span kind in the C extension * Update documentation on equivalent OpenCensus\Trace\Span class.
1 parent 30c80b9 commit b50c5bd

18 files changed

Lines changed: 333 additions & 17 deletions

ext/opencensus_trace.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ static opencensus_trace_span_t *opencensus_trace_begin(zend_string *function_nam
246246

247247
span->start = opencensus_now();
248248
span->name = zend_string_copy(function_name);
249+
span->kind = OPENCENSUS_TRACE_SPAN_KIND_UNKNOWN;
249250

250251
#if PHP_VERSION_ID < 70100
251252
if (!BG(mt_rand_is_seeded)) {
@@ -604,6 +605,7 @@ PHP_FUNCTION(opencensus_trace_list)
604605
zend_update_property_str(opencensus_trace_span_ce, &span, "name", sizeof("name") - 1, trace_span->name);
605606
zend_update_property_double(opencensus_trace_span_ce, &span, "startTime", sizeof("startTime") - 1, trace_span->start);
606607
zend_update_property_double(opencensus_trace_span_ce, &span, "endTime", sizeof("endTime") - 1, trace_span->stop);
608+
zend_update_property_long(opencensus_trace_span_ce, &span, "kind", sizeof("kind") - 1, trace_span->kind);
607609

608610
ZVAL_ARR(&label, trace_span->labels);
609611
zend_update_property(opencensus_trace_span_ce, &span, "labels", sizeof("labels") - 1, &label);

ext/opencensus_trace_span.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@
2121
* namespace OpenCensus\Trace;
2222
*
2323
* class Span {
24+
* const SPAN_KIND_UNKNOWN = 0;
25+
* const SPAN_KIND_CLIENT = 1;
26+
* const SPAN_KIND_SERVER = 2;
27+
* const SPAN_KIND_PRODUCER = 3;
28+
* const SPAN_KIND_CONSUMER = 4;
29+
*
2430
* protected $name = "unknown";
2531
* protected $spanId;
2632
* protected $parentSpanId;
2733
* protected $startTime;
2834
* protected $endTime;
2935
* protected $labels;
36+
* protected $kind;
3037
*
3138
* public function __construct(array $spanOptions)
3239
* {
@@ -45,7 +52,7 @@
4552
* return $this->spanId;
4653
* }
4754
*
48-
* public function spanId()
55+
* public function parentSpanId()
4956
* {
5057
* return $this->parentSpanId;
5158
* }
@@ -64,6 +71,11 @@
6471
* {
6572
* return $this->labels;
6673
* }
74+
*
75+
* public function kind()
76+
* {
77+
* return $this->kind;
78+
* }
6779
* }
6880
*/
6981

@@ -197,6 +209,23 @@ static PHP_METHOD(OpenCensusTraceSpan, endTime) {
197209
RETURN_ZVAL(val, 1, 0);
198210
}
199211

212+
/**
213+
* Fetch the span kind
214+
*
215+
* @return int
216+
*/
217+
static PHP_METHOD(OpenCensusTraceSpan, kind) {
218+
zval *val, rv;
219+
220+
if (zend_parse_parameters_none() == FAILURE) {
221+
return;
222+
}
223+
224+
val = zend_read_property(opencensus_trace_span_ce, getThis(), "kind", sizeof("kind") - 1, 1, &rv);
225+
226+
RETURN_ZVAL(val, 1, 0);
227+
}
228+
200229
/* Declare method entries for the OpenCensus\Trace\Span class */
201230
static zend_function_entry opencensus_trace_span_methods[] = {
202231
PHP_ME(OpenCensusTraceSpan, __construct, arginfo_OpenCensusTraceSpan_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
@@ -206,9 +235,12 @@ static zend_function_entry opencensus_trace_span_methods[] = {
206235
PHP_ME(OpenCensusTraceSpan, labels, NULL, ZEND_ACC_PUBLIC)
207236
PHP_ME(OpenCensusTraceSpan, startTime, NULL, ZEND_ACC_PUBLIC)
208237
PHP_ME(OpenCensusTraceSpan, endTime, NULL, ZEND_ACC_PUBLIC)
238+
PHP_ME(OpenCensusTraceSpan, kind, NULL, ZEND_ACC_PUBLIC)
209239
PHP_FE_END
210240
};
211241

242+
#define REGISTER_TRACE_SPAN_CONSTANT(id) zend_declare_class_constant_long(opencensus_trace_span_ce, "SPAN_" #id, sizeof("SPAN_" #id) - 1, OPENCENSUS_TRACE_SPAN_##id);
243+
212244
/* Module init handler for registering the OpenCensus\Trace\Span class */
213245
int opencensus_trace_span_minit(INIT_FUNC_ARGS) {
214246
zend_class_entry ce;
@@ -223,8 +255,15 @@ int opencensus_trace_span_minit(INIT_FUNC_ARGS) {
223255
zend_declare_property_null(opencensus_trace_span_ce, "parentSpanId", sizeof("parentSpanId") - 1, ZEND_ACC_PROTECTED TSRMLS_CC);
224256
zend_declare_property_null(opencensus_trace_span_ce, "startTime", sizeof("startTime") - 1, ZEND_ACC_PROTECTED TSRMLS_CC);
225257
zend_declare_property_null(opencensus_trace_span_ce, "endTime", sizeof("endTime") - 1, ZEND_ACC_PROTECTED TSRMLS_CC);
258+
zend_declare_property_null(opencensus_trace_span_ce, "kind", sizeof("kind") - 1, ZEND_ACC_PROTECTED TSRMLS_CC);
226259
zend_declare_property_null(opencensus_trace_span_ce, "labels", sizeof("labels") - 1, ZEND_ACC_PROTECTED TSRMLS_CC);
227260

261+
REGISTER_TRACE_SPAN_CONSTANT(KIND_UNKNOWN);
262+
REGISTER_TRACE_SPAN_CONSTANT(KIND_CLIENT);
263+
REGISTER_TRACE_SPAN_CONSTANT(KIND_SERVER);
264+
REGISTER_TRACE_SPAN_CONSTANT(KIND_PRODUCER);
265+
REGISTER_TRACE_SPAN_CONSTANT(KIND_CONSUMER);
266+
228267
return SUCCESS;
229268
}
230269

@@ -296,6 +335,8 @@ int opencensus_trace_span_apply_span_options(opencensus_trace_span_t *span, zval
296335
span->start = Z_DVAL_P(v);
297336
} else if (strcmp(ZSTR_VAL(k), "name") == 0) {
298337
span->name = zend_string_copy(Z_STR_P(v));
338+
} else if (strcmp(ZSTR_VAL(k), "kind") == 0) {
339+
span->kind = Z_LVAL_P(v);
299340
}
300341
} ZEND_HASH_FOREACH_END();
301342
return SUCCESS;

ext/opencensus_trace_span.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121

2222
extern zend_class_entry* opencensus_trace_span_ce;
2323

24+
#define OPENCENSUS_TRACE_SPAN_KIND_UNKNOWN 0
25+
#define OPENCENSUS_TRACE_SPAN_KIND_CLIENT 1
26+
#define OPENCENSUS_TRACE_SPAN_KIND_SERVER 2
27+
#define OPENCENSUS_TRACE_SPAN_KIND_PRODUCER 3
28+
#define OPENCENSUS_TRACE_SPAN_KIND_CONSUMER 4
2429

2530
// TraceSpan struct
2631
typedef struct opencensus_trace_span_t {
@@ -29,6 +34,7 @@ typedef struct opencensus_trace_span_t {
2934
double start;
3035
double stop;
3136
struct opencensus_trace_span_t *parent;
37+
zend_long kind;
3238

3339
// zend_string* => zval*
3440
HashTable *labels;

ext/package.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ This extension allows you to easily gather latency and other metadata by watchin
5757
<file name="function_callback_extra_arguments.phpt" role="test" />
5858
<file name="function_callback_wrong_return.phpt" role="test" />
5959
<file name="function_custom.phpt" role="test" />
60+
<file name="function_kind_default.phpt" role="test" />
61+
<file name="function_kind_specified.phpt" role="test" />
6062
<file name="inherit_context.phpt" role="test" />
6163
<file name="labels.phpt" role="test" />
6264
<file name="manual_spans.phpt" role="test" />
@@ -66,6 +68,8 @@ This extension allows you to easily gather latency and other metadata by watchin
6668
<file name="method_callback_arguments.phpt" role="test" />
6769
<file name="method_callback_scope.phpt" role="test" />
6870
<file name="method_custom.phpt" role="test" />
71+
<file name="method_kind_default.phpt" role="test" />
72+
<file name="method_kind_specified.phpt" role="test" />
6973
<file name="nested_spans.phpt" role="test" />
7074
<file name="non-string-labels-function-callback.phpt" role="test" />
7175
<file name="non-string-labels-function.phpt" role="test" />

ext/span.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@
2121
* This is the equivalent PHP class created by the opencensus C extension
2222
*/
2323
class Span {
24+
const SPAN_KIND_UNKNOWN = 0;
25+
const SPAN_KIND_CLIENT = 1;
26+
const SPAN_KIND_SERVER = 2;
27+
const SPAN_KIND_PRODUCER = 3;
28+
const SPAN_KIND_CONSUMER = 4;
29+
2430
protected $name = "unknown";
2531
protected $spanId;
2632
protected $parentSpanId;
2733
protected $startTime;
2834
protected $endTime;
2935
protected $labels;
36+
protected $kind;
3037

3138
public function __construct(array $spanOptions)
3239
{
@@ -45,7 +52,7 @@ public function spanId()
4552
return $this->spanId;
4653
}
4754

48-
public function spanId()
55+
public function parentSpanId()
4956
{
5057
return $this->parentSpanId;
5158
}
@@ -64,4 +71,9 @@ public function labels()
6471
{
6572
return $this->labels;
6673
}
74+
75+
public function kind()
76+
{
77+
return $this->kind;
78+
}
6779
}

ext/tests/constants_test.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
OpenCensus Trace: Test Constants Defined
3+
--FILE--
4+
<?php
5+
6+
var_dump(OpenCensus\Trace\Span::SPAN_KIND_UNKNOWN);
7+
var_dump(OpenCensus\Trace\Span::SPAN_KIND_CLIENT);
8+
var_dump(OpenCensus\Trace\Span::SPAN_KIND_SERVER);
9+
var_dump(OpenCensus\Trace\Span::SPAN_KIND_PRODUCER);
10+
var_dump(OpenCensus\Trace\Span::SPAN_KIND_CONSUMER);
11+
12+
?>
13+
--EXPECT--
14+
int(0)
15+
int(1)
16+
int(2)
17+
int(3)
18+
int(4)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
OpenCensus Trace: Default span kind
3+
--FILE--
4+
<?php
5+
6+
require_once(__DIR__ . '/common.php');
7+
8+
// 1: Sanity test a simple profile run
9+
opencensus_trace_function("bar", ['name' => 'foo', 'startTime' => 0.1, 'labels' => ['asdf' => 'qwer']]);
10+
bar();
11+
$traces = opencensus_trace_list();
12+
echo "Number of traces: " . count($traces) . "\n";
13+
$span = $traces[0];
14+
15+
echo "Span kind is '{$span->kind()}'";
16+
?>
17+
--EXPECT--
18+
Number of traces: 1
19+
Span kind is '0'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
OpenCensus Trace: Provided span kind
3+
--FILE--
4+
<?php
5+
6+
require_once(__DIR__ . '/common.php');
7+
8+
// 1: Sanity test a simple profile run
9+
opencensus_trace_function("bar", ['name' => 'foo', 'startTime' => 0.1, 'labels' => ['asdf' => 'qwer'], 'kind' => 1]);
10+
bar();
11+
$traces = opencensus_trace_list();
12+
echo "Number of traces: " . count($traces) . "\n";
13+
$span = $traces[0];
14+
15+
echo "Span kind is '{$span->kind()}'";
16+
?>
17+
--EXPECT--
18+
Number of traces: 1
19+
Span kind is '1'

ext/tests/manual_spans_default_options.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Array
2828
[parentSpanId:protected] =>%s
2929
[startTime:protected] => %d.%d
3030
[endTime:protected] => %d.%d
31+
[kind:protected] => %d
3132
[labels:protected] => Array
3233
(
3334
)
@@ -41,6 +42,7 @@ Array
4142
[parentSpanId:protected] => %d
4243
[startTime:protected] => %d.%d
4344
[endTime:protected] => %d.%d
45+
[kind:protected] => %d
4446
[labels:protected] => Array
4547
(
4648
)

ext/tests/method_kind_default.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
OpenCensus Trace: Default span kind
3+
--FILE--
4+
<?php
5+
6+
require_once(__DIR__ . '/common.php');
7+
8+
// 1: Sanity test a simple profile run
9+
opencensus_trace_method("Foo", "bar", ['name' => 'foo', 'startTime' => 0.1, 'labels' => ['asdf' => 'qwer']]);
10+
$f = new Foo();
11+
$f->bar();
12+
$traces = opencensus_trace_list();
13+
echo "Number of traces: " . count($traces) . "\n";
14+
$span = $traces[0];
15+
16+
echo "Span kind is '{$span->kind()}'";
17+
?>
18+
--EXPECT--
19+
Number of traces: 1
20+
Span kind is '0'

0 commit comments

Comments
 (0)