Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions ext/priority_sampling/priority_sampling.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,30 @@ static void dd_update_decision_maker_tag(ddtrace_root_span_data *root_span,
static void dd_update_knuth_sampling_rate_tag(ddtrace_root_span_data *root_span, double sample_rate) {
zend_array *meta = ddtrace_property_array(&root_span->property_meta);

// Round to 6 decimal places at integer level to avoid IEEE 754 precision issues,
// then format with fixed-point notation (never scientific notation).
double rounded = floor(sample_rate * 1e6 + 0.5) / 1e6;
char buf[32];
snprintf(buf, sizeof(buf), "%.6f", rounded);
#if PHP_VERSION_ID < 80100
int is_negative;
#else
bool is_negative;
#endif
size_t len;
php_conv_fp('F', sample_rate, false, 6, '.', &is_negative, buf, &len); // F for fixed point vs scientific notation

// Strip trailing zeros and optional decimal point
size_t len = strlen(buf);
while (len > 1 && buf[len - 1] == '0') {
buf[--len] = '\0';
--len;
}
if (len > 1 && buf[len - 1] == '.') {
buf[--len] = '\0';
--len;
}

// Skip update if already set to the same value
zval *existing = zend_hash_str_find(meta, ZEND_STRL("_dd.p.ksr"));
if (existing && Z_TYPE_P(existing) == IS_STRING && strcmp(Z_STRVAL_P(existing), buf) == 0) {
if (existing && Z_TYPE_P(existing) == IS_STRING && zend_string_equals_cstr(Z_STR_P(existing), buf, len) == 0) {
return;
}

zval ksr;
ZVAL_STRING(&ksr, buf);
ZVAL_STRINGL(&ksr, buf, len);
zend_hash_str_update(meta, ZEND_STRL("_dd.p.ksr"), &ksr);
zend_hash_str_add_empty_element(ddtrace_property_array(&root_span->property_propagated_tags), ZEND_STRL("_dd.p.ksr"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ _dd.p.ksr propagated tag is set for rule-based sampling
--ENV--
DD_TRACE_SAMPLING_RULES=[{"sample_rate": 0.3}]
DD_TRACE_GENERATE_ROOT_SPAN=1
LOCALE=de_DE
--FILE--
<?php
$root = \DDTrace\root_span();
Expand Down
Loading