1616#include "agent_info.h"
1717
1818/* Sampling constants */
19- const uint64_t KNUTH_FACTOR = 1111111111111111111ULL ;
20- const uint64_t MAX_TRACE_ID = ~0ULL ; // 2^64-1 - This represents the maximum value of a Trace ID
19+ static const uint64_t KNUTH_FACTOR = 1111111111111111111ULL ;
20+ static const uint64_t MAX_TRACE_ID = ~0ULL ; // 2^64-1 - This represents the maximum value of a Trace ID
2121
2222ZEND_EXTERN_MODULE_GLOBALS (ddtrace );
2323
@@ -62,6 +62,24 @@ static void dd_update_decision_maker_tag(ddtrace_root_span_data *root_span,
6262 }
6363}
6464
65+ static void dd_update_knuth_sampling_rate_tag (ddtrace_root_span_data * root_span , double sample_rate ) {
66+ zend_array * meta = ddtrace_property_array (& root_span -> property_meta );
67+
68+ char buf [32 ];
69+ snprintf (buf , sizeof (buf ), "%.6g" , sample_rate );
70+
71+ // Skip update if already set to the same value
72+ zval * existing = zend_hash_str_find (meta , ZEND_STRL ("_dd.p.ksr" ));
73+ if (existing && Z_TYPE_P (existing ) == IS_STRING && strcmp (Z_STRVAL_P (existing ), buf ) == 0 ) {
74+ return ;
75+ }
76+
77+ zval ksr ;
78+ ZVAL_STRING (& ksr , buf );
79+ zend_hash_str_update (meta , ZEND_STRL ("_dd.p.ksr" ), & ksr );
80+ zend_hash_str_add_empty_element (ddtrace_property_array (& root_span -> property_propagated_tags ), ZEND_STRL ("_dd.p.ksr" ));
81+ }
82+
6583static bool dd_check_sampling_rule (zend_array * rule , ddtrace_span_data * span ) {
6684 zval * service = & span -> property_service ;
6785 zval * resource = & span -> property_resource ;
@@ -293,6 +311,7 @@ static void dd_decide_on_sampling(ddtrace_root_span_data *span) {
293311 ZVAL_LONG (& priority_zv , PRIORITY_SAMPLING_AUTO_REJECT );
294312 ddtrace_assign_variable (& span -> property_sampling_priority , & priority_zv );
295313 }
314+ zend_hash_str_del (ddtrace_property_array (& span -> property_meta ), ZEND_STRL ("_dd.p.ksr" ));
296315 return ;
297316 } else {
298317 sample_rate = result .sampling_rate ;
@@ -318,8 +337,10 @@ static void dd_decide_on_sampling(ddtrace_root_span_data *span) {
318337
319338 if (mechanism == DD_MECHANISM_MANUAL ) {
320339 zend_hash_str_del (metrics , ZEND_STRL ("_dd.rule_psr" ));
340+ zend_hash_str_del (ddtrace_property_array (& span -> property_meta ), ZEND_STRL ("_dd.p.ksr" ));
321341 } else {
322342 zend_hash_str_update (metrics , ZEND_STRL ("_dd.rule_psr" ), & sample_rate_zv );
343+ dd_update_knuth_sampling_rate_tag (span , sample_rate );
323344 }
324345
325346 zend_hash_str_del (metrics , ZEND_STRL ("_dd.agent_psr" ));
@@ -329,6 +350,11 @@ static void dd_decide_on_sampling(ddtrace_root_span_data *span) {
329350 priority = sampling && !limited ? PRIORITY_SAMPLING_AUTO_KEEP : PRIORITY_SAMPLING_AUTO_REJECT ;
330351
331352 zend_hash_str_update (metrics , ZEND_STRL ("_dd.agent_psr" ), & sample_rate_zv );
353+ if (mechanism == DD_MECHANISM_AGENT_RATE ) {
354+ dd_update_knuth_sampling_rate_tag (span , sample_rate );
355+ } else {
356+ zend_hash_str_del (ddtrace_property_array (& span -> property_meta ), ZEND_STRL ("_dd.p.ksr" ));
357+ }
332358 }
333359
334360 if (limited ) {
0 commit comments