Skip to content

Commit 448243a

Browse files
committed
Fix locale breaking ksr
Signed-off-by: Bob Weinand <bob.weinand@datadoghq.com>
1 parent ad5a7e6 commit 448243a

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

ext/priority_sampling/priority_sampling.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,26 @@ 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+
int is_negative;
71+
size_t len;
72+
php_conv_fp('F', sample_rate, false, 6, '.', &is_negative, buf, &len); // F for fixed point vs scientific notation
7473

75-
// Strip trailing zeros and optional decimal point
76-
size_t len = strlen(buf);
7774
while (len > 1 && buf[len - 1] == '0') {
78-
buf[--len] = '\0';
75+
--len;
7976
}
8077
if (len > 1 && buf[len - 1] == '.') {
81-
buf[--len] = '\0';
78+
--len;
8279
}
8380

8481
// Skip update if already set to the same value
8582
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) {
83+
if (existing && Z_TYPE_P(existing) == IS_STRING && zend_string_equals_cstr(Z_STR_P(existing), buf, len) == 0) {
8784
return;
8885
}
8986

9087
zval ksr;
91-
ZVAL_STRING(&ksr, buf);
88+
ZVAL_STRINGL(&ksr, buf, len);
9289
zend_hash_str_update(meta, ZEND_STRL("_dd.p.ksr"), &ksr);
9390
zend_hash_str_add_empty_element(ddtrace_property_array(&root_span->property_propagated_tags), ZEND_STRL("_dd.p.ksr"));
9491
}

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)