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

Commit 55f0f8c

Browse files
authored
Implement TraceContext header (#44)
* Implement TraceContext header * Put parsing test back * spanId and parentSpanId should be stored internally as hex values * Fix TraceContextFormatterTest to not convert to decimal * CloudTrace formatter needs to convert to hex * BinaryFormatter should store hex spanId in TraceContext * Extension should generate span ids as hex strings * Fix handoff of spanId to the extension * Allow uppercase hex when deserializing trace context
1 parent 11f6a2d commit 55f0f8c

37 files changed

Lines changed: 301 additions & 93 deletions

ext/opencensus_trace.c

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "Zend/zend_compile.h"
2222
#include "Zend/zend_closures.h"
2323
#include "zend_extensions.h"
24+
#include "standard/php_math.h"
2425

2526
#if PHP_VERSION_ID < 70100
2627
#include "standard/php_rand.h"
@@ -59,7 +60,7 @@ ZEND_END_ARG_INFO()
5960

6061
ZEND_BEGIN_ARG_INFO_EX(arginfo_opencensus_trace_set_context, 0, 0, 1)
6162
ZEND_ARG_TYPE_INFO(0, traceId, IS_STRING, 0)
62-
ZEND_ARG_TYPE_INFO(0, parentSpanId, IS_STRING, 0)
63+
ZEND_ARG_TYPE_INFO(0, parentSpanId, IS_STRING, 1)
6364
ZEND_END_ARG_INFO()
6465

6566
ZEND_BEGIN_ARG_INFO_EX(arginfo_opencensus_trace_add_label, 0, 0, 2)
@@ -245,6 +246,25 @@ static void opencensus_trace_execute_callback(opencensus_trace_span_t *span, zen
245246
}
246247
}
247248

249+
/**
250+
* Force the random span id to be positive. php_mt_rand() generates 32 bits
251+
* of randomness. On 32-bit systems, we must cast to an unsigned int before
252+
* bitshifting to force a positive number. We're ok to lose on bit of
253+
* randomness because previous versions of mt_rand only generated 31 bits.
254+
*/
255+
static zend_string *generate_span_id()
256+
{
257+
zval zv;
258+
#if PHP_VERSION_ID < 70100
259+
if (!BG(mt_rand_is_seeded)) {
260+
php_mt_srand(GENERATE_SEED());
261+
}
262+
#endif
263+
264+
ZVAL_LONG(&zv, ((uint32_t) php_mt_rand()) >> 1);
265+
return _php_math_longtobase(&zv, 16);
266+
}
267+
248268
/**
249269
* Start a new trace span. Inherit the parent span id from the current trace
250270
* context
@@ -258,19 +278,7 @@ static opencensus_trace_span_t *opencensus_trace_begin(zend_string *function_nam
258278
span->start = opencensus_now();
259279
span->name = zend_string_copy(function_name);
260280
span->kind = OPENCENSUS_TRACE_SPAN_KIND_UNKNOWN;
261-
262-
#if PHP_VERSION_ID < 70100
263-
if (!BG(mt_rand_is_seeded)) {
264-
php_mt_srand(GENERATE_SEED());
265-
}
266-
#endif
267-
/**
268-
* Force the random span id to be positive. php_mt_rand() generates 32 bits
269-
* of randomness. On 32-bit systems, we must cast to an unsigned int before
270-
* bitshifting to force a positive number. We're ok to lose on bit of
271-
* randomness because previous versions of mt_rand only generated 31 bits.
272-
*/
273-
span->span_id = ((uint32_t) php_mt_rand()) >> 1;
281+
span->span_id = generate_span_id();
274282

275283
if (OPENCENSUS_TRACE_G(current_span)) {
276284
span->parent = OPENCENSUS_TRACE_G(current_span);
@@ -424,14 +432,13 @@ PHP_FUNCTION(opencensus_trace_clear)
424432
*/
425433
PHP_FUNCTION(opencensus_trace_set_context)
426434
{
427-
zend_string *trace_id;
428-
long parent_span_id;
429-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|L", &trace_id, &parent_span_id) == FAILURE) {
435+
zend_string *trace_id, *parent_span_id;
436+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|S", &trace_id, &parent_span_id) == FAILURE) {
430437
RETURN_FALSE;
431438
}
432439

433440
OPENCENSUS_TRACE_G(trace_id) = zend_string_copy(trace_id);
434-
OPENCENSUS_TRACE_G(trace_parent_span_id) = parent_span_id;
441+
OPENCENSUS_TRACE_G(trace_parent_span_id) = zend_string_copy(parent_span_id);
435442

436443
RETURN_TRUE;
437444
}
@@ -447,9 +454,9 @@ PHP_FUNCTION(opencensus_trace_context)
447454
object_init_ex(return_value, opencensus_trace_context_ce);
448455

449456
if (span) {
450-
zend_update_property_long(opencensus_trace_context_ce, return_value, "spanId", sizeof("spanId") - 1, span->span_id);
457+
zend_update_property_str(opencensus_trace_context_ce, return_value, "spanId", sizeof("spanId") - 1, span->span_id);
451458
} else if (OPENCENSUS_TRACE_G(trace_parent_span_id)) {
452-
zend_update_property_long(opencensus_trace_context_ce, return_value, "spanId", sizeof("spanId") - 1, OPENCENSUS_TRACE_G(trace_parent_span_id));
459+
zend_update_property_str(opencensus_trace_context_ce, return_value, "spanId", sizeof("spanId") - 1, OPENCENSUS_TRACE_G(trace_parent_span_id));
453460
}
454461
if (OPENCENSUS_TRACE_G(trace_id)) {
455462
zend_update_property_str(opencensus_trace_context_ce, return_value, "traceId", sizeof("traceId") - 1, OPENCENSUS_TRACE_G(trace_id));
@@ -607,11 +614,11 @@ PHP_FUNCTION(opencensus_trace_list)
607614

608615
ZEND_HASH_FOREACH_PTR(OPENCENSUS_TRACE_G(spans), trace_span) {
609616
object_init_ex(&span, opencensus_trace_span_ce);
610-
zend_update_property_long(opencensus_trace_span_ce, &span, "spanId", sizeof("spanId") - 1, trace_span->span_id);
617+
zend_update_property_str(opencensus_trace_span_ce, &span, "spanId", sizeof("spanId") - 1, trace_span->span_id);
611618
if (trace_span->parent) {
612-
zend_update_property_long(opencensus_trace_span_ce, &span, "parentSpanId", sizeof("parentSpanId") - 1, trace_span->parent->span_id);
619+
zend_update_property_str(opencensus_trace_span_ce, &span, "parentSpanId", sizeof("parentSpanId") - 1, trace_span->parent->span_id);
613620
} else if (OPENCENSUS_TRACE_G(trace_parent_span_id)) {
614-
zend_update_property_long(opencensus_trace_span_ce, &span, "parentSpanId", sizeof("parentSpanId") - 1, OPENCENSUS_TRACE_G(trace_parent_span_id));
621+
zend_update_property_str(opencensus_trace_span_ce, &span, "parentSpanId", sizeof("parentSpanId") - 1, OPENCENSUS_TRACE_G(trace_parent_span_id));
615622
}
616623
zend_update_property_str(opencensus_trace_span_ce, &span, "name", sizeof("name") - 1, trace_span->name);
617624
zend_update_property_double(opencensus_trace_span_ce, &span, "startTime", sizeof("startTime") - 1, trace_span->start);

ext/opencensus_trace_span.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extern zend_class_entry* opencensus_trace_span_ce;
3030
// TraceSpan struct
3131
typedef struct opencensus_trace_span_t {
3232
zend_string *name;
33-
uint32_t span_id;
33+
zend_string *span_id;
3434
double start;
3535
double stop;
3636
struct opencensus_trace_span_t *parent;

ext/php_opencensus.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ ZEND_BEGIN_MODULE_GLOBALS(opencensus)
4848
// Trace context
4949
opencensus_trace_span_t *current_span;
5050
zend_string *trace_id;
51-
long trace_parent_span_id;
51+
zend_string *trace_parent_span_id;
5252

5353
// List of collected spans
5454
HashTable *spans;

ext/tests/basic_class_function.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var_dump($span->labels());
2929
?>
3030
--EXPECT--
3131
Number of traces: 1
32-
Span id is a integer
32+
Span id is a string
3333
Span name is: 'Foo::bar'
3434
Span startTime is a double: 1
3535
Span endTime is a double: 1

ext/tests/basic_function.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var_dump($span->labels());
2727
?>
2828
--EXPECT--
2929
Number of traces: 1
30-
Span id is a integer
30+
Span id is a string
3131
Span name is: 'bar'
3232
Span startTime is a double: 1
3333
Span endTime is a double: 1

ext/tests/basic_method.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ echo "Span endTime is a double: $test\n";
2727
?>
2828
--EXPECT--
2929
Number of traces: 1
30-
Span id is a integer
30+
Span id is a string
3131
Span name is: 'Foo::bar'
3232
Span startTime is a double: 1
3333
Span endTime is a double: 1

ext/tests/function_callback.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ print_r($span->labels());
3131
?>
3232
--EXPECT--
3333
Number of traces: 1
34-
Span id is a integer
34+
Span id is a string
3535
Span name is: 'foo'
3636
Span startTime is a double: 1
3737
Span startTime is: '0.1'

ext/tests/function_callback_arguments.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ print_r($span->labels());
3131
?>
3232
--EXPECT--
3333
Number of traces: 1
34-
Span id is a integer
34+
Span id is a string
3535
Span name is: 'foo'
3636
Span startTime is a double: 1
3737
Span startTime is: '0.1'

ext/tests/function_callback_array.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ print_r($span->labels());
3737
?>
3838
--EXPECT--
3939
Number of traces: 1
40-
Span id is a integer
40+
Span id is a string
4141
Span name is: 'foo'
4242
Span startTime is a double: 1
4343
Span startTime is: '0.1'

ext/tests/function_callback_static_string.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ print_r($span->labels());
3737
?>
3838
--EXPECT--
3939
Number of traces: 1
40-
Span id is a integer
40+
Span id is a string
4141
Span name is: 'foo'
4242
Span startTime is a double: 1
4343
Span startTime is: '0.1'

0 commit comments

Comments
 (0)