Skip to content

Commit 23b1ac3

Browse files
authored
Fix locale breaking ksr (#3797)
Signed-off-by: Bob Weinand <bob.weinand@datadoghq.com>
1 parent a939e94 commit 23b1ac3

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

ext/priority_sampling/priority_sampling.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,30 @@ static void dd_update_decision_maker_tag(ddtrace_root_span_data *root_span,
6666
static 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
}

tests/ext/priority_sampling/025-ksr-tag-rule-sampling.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ _dd.p.ksr propagated tag is set for rule-based sampling
33
--ENV--
44
DD_TRACE_SAMPLING_RULES=[{"sample_rate": 0.3}]
55
DD_TRACE_GENERATE_ROOT_SPAN=1
6+
LOCALE=de_DE
67
--FILE--
78
<?php
89
$root = \DDTrace\root_span();

0 commit comments

Comments
 (0)