@@ -66,29 +66,30 @@ static void dd_update_decision_maker_tag(ddtrace_root_span_data *root_span,
6666static void dd_update_knuth_sampling_rate_tag (ddtrace_root_span_data * root_span , double sample_rate ) {
6767 zend_array * meta = ddtrace_property_array (& root_span -> property_meta );
6868
69- // Round to 6 decimal places at integer level to avoid IEEE 754 precision issues,
70- // then format with fixed-point notation (never scientific notation).
71- double rounded = floor (sample_rate * 1e6 + 0.5 ) / 1e6 ;
7269 char buf [32 ];
73- snprintf (buf , sizeof (buf ), "%.6f" , rounded );
70+ #if PHP_VERSION_ID < 80100
71+ int is_negative ;
72+ #else
73+ bool is_negative ;
74+ #endif
75+ size_t len ;
76+ php_conv_fp ('F' , sample_rate , false, 6 , '.' , & is_negative , buf , & len ); // F for fixed point vs scientific notation
7477
75- // Strip trailing zeros and optional decimal point
76- size_t len = strlen (buf );
7778 while (len > 1 && buf [len - 1 ] == '0' ) {
78- buf [ -- len ] = '\0' ;
79+ -- len ;
7980 }
8081 if (len > 1 && buf [len - 1 ] == '.' ) {
81- buf [ -- len ] = '\0' ;
82+ -- len ;
8283 }
8384
8485 // Skip update if already set to the same value
8586 zval * existing = zend_hash_str_find (meta , ZEND_STRL ("_dd.p.ksr" ));
86- if (existing && Z_TYPE_P (existing ) == IS_STRING && strcmp ( Z_STRVAL_P (existing ), buf ) == 0 ) {
87+ if (existing && Z_TYPE_P (existing ) == IS_STRING && zend_string_equals_cstr ( Z_STR_P (existing ), buf , len ) == 0 ) {
8788 return ;
8889 }
8990
9091 zval ksr ;
91- ZVAL_STRING (& ksr , buf );
92+ ZVAL_STRINGL (& ksr , buf , len );
9293 zend_hash_str_update (meta , ZEND_STRL ("_dd.p.ksr" ), & ksr );
9394 zend_hash_str_add_empty_element (ddtrace_property_array (& root_span -> property_propagated_tags ), ZEND_STRL ("_dd.p.ksr" ));
9495}
0 commit comments