Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Tag Keys for DD_TRACE_HEADER_TAGS #2922

Merged
merged 8 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 3 additions & 5 deletions tests/Unit/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,9 @@ public function testHttpHeadersCanSetOne()
public function testHttpHeadersCanSetMultiple()
{
$this->putEnvAndReloadConfig([
'DD_TRACE_HEADER_TAGS=A-Header ,Any-Name , cOn7aining-!spe_cial?:ch/ars , Some-Heather:with-colon-Key',
'DD_TRACE_HEADER_TAGS=A-Header ,Any-Name , cOn7aining-!spe_cial?:ch/ars , valueless:, Some-Header:with-colon-Key',
]);
// Same behavior as python tracer:
// https://github.com/DataDog/dd-trace-py/blob/f1298cb8100f146059f978b58c88641bd7424af8/ddtrace/http/headers.py
$this->assertSame(['a-header', 'any-name', 'con7aining-!spe_cial?', 'some-heather'], array_keys(\dd_trace_env_config("DD_TRACE_HEADER_TAGS")));
$this->assertEquals(['a-header' => '', 'any-name' => '', 'con7aining-!spe_cial?' => 'ch/ars', 'some-heather' => 'with-colon-Key'], \dd_trace_env_config("DD_TRACE_HEADER_TAGS"));
$this->assertSame(['a-header', 'any-name', 'con7aining-!spe_cial?', 'valueless', 'some-header'], array_keys(\dd_trace_env_config("DD_TRACE_HEADER_TAGS")));
$this->assertEquals(['a-header' => '', 'any-name' => '', 'con7aining-!spe_cial?' => 'ch/ars', 'valueless' => '', 'some-header' => 'with-colon-Key'], \dd_trace_env_config("DD_TRACE_HEADER_TAGS"));
}
}
51 changes: 35 additions & 16 deletions zend_abstract_interface/config/config_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ static bool zai_config_decode_map(zai_str value, zval *decoded_value, bool persi
char *data = (char *)value.ptr;
if (data && *data) { // non-empty
const char *key_start, *key_end, *value_start, *value_end;

do {
while (*data == ',' || *data == ' ' || *data == '\t' || *data == '\n') {
data++;
Expand All @@ -127,59 +127,78 @@ static bool zai_config_decode_map(zai_str value, zval *decoded_value, bool persi
while (*data && *data != ',') {
if (*data == ':') {
has_colon = true;

data++;

while (*data == ' ' || *data == '\t' || *data == '\n') data++;

value_start = data;
value_end = value_start;

while (*data && *data != ',') {
if (*data != ' ' && *data != '\t' && *data != '\n') {
value_end = data;
if (*data == ',' || *data == '\0') {
value_start = value_end = NULL;
} else {
value_start = data;
value_end = value_start;

while (*data && *data != ',') {
if (*data != ' ' && *data != '\t' && *data != '\n') {
value_end = data;
}
data++;
}
data++;
}

size_t key_len = key_end - key_start + 1;
size_t value_len = value_end - value_start + 1;
if (key_end && key_start) {
size_t key_len = key_end - key_start + 1;
size_t value_len = (value_end && value_start) ? (value_end - value_start + 1) : 0;

if (key_len > 0 && value_len > 0) {
zend_string *key = zend_string_init(key_start, key_len, persistent);
if (lowercase) {
zend_str_tolower(ZSTR_VAL(key), ZSTR_LEN(key));
}

zval val;
ZVAL_NEW_STR(&val, zend_string_init(value_start, value_len, persistent));
if (value_len > 0) {
ZVAL_NEW_STR(&val, zend_string_init(value_start, value_len, persistent));
} else {
if (persistent) {
ZVAL_EMPTY_PSTRING(&val);
} else {
ZVAL_EMPTY_STRING(&val);
}
}
zend_hash_update(Z_ARRVAL(tmp), key, &val);
zend_string_release(key);
}

break;
}

// Set key_end to the last valid non-whitespace character of the key
if (*data != ' ' && *data != '\t' && *data != '\n') {
key_end = data;
}
data++;
}

// Handle case without a colon (standalone key) only when lowercase is true which is current use case
if (map_keyless && !has_colon && key_end) {
// Handle standalone keys (without a colon) if map_keyless is enabled
if (map_keyless && !has_colon && key_end && key_start) {
size_t key_len = key_end - key_start + 1;
zend_string *key = zend_string_init(key_start, key_len, persistent);
if (lowercase) {
zend_str_tolower(ZSTR_VAL(key), ZSTR_LEN(key));
}

zval val;
ZVAL_EMPTY_STRING(&val);
if (persistent) {
ZVAL_EMPTY_PSTRING(&val);
} else {
ZVAL_EMPTY_STRING(&val);
}
zend_hash_update(Z_ARRVAL(tmp), key, &val);
zend_string_release(key);
}
}
} while (*data);

// Check if the array has any elements; if not, cleanup
if (zend_hash_num_elements(Z_ARRVAL(tmp)) == 0) {
zend_hash_destroy(Z_ARRVAL(tmp));
pefree(Z_ARRVAL(tmp), persistent);
Expand Down
Loading