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
6061ZEND_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 )
6364ZEND_END_ARG_INFO ()
6465
6566ZEND_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 */
425433PHP_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 );
0 commit comments