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

Baggage Header Propagation Support #3102

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft

Conversation

link04
Copy link
Contributor

@link04 link04 commented Feb 24, 2025

Description

This PR's goal is adding support for OTEL's baggage header OOTB propagation and API.

Reviewer checklist

  • Test coverage seems ok.
  • Appropriate labels assigned.

Comment on lines +446 to +457
zend_string *key;
zval *value;
ZEND_HASH_FOREACH_STR_KEY_VAL(&existing_baggage, key, value) {
zend_string *new_key = zend_string_dup(key, 0);
zval new_value;
ZVAL_DUP(&new_value, value);

zend_hash_update(&result.baggage, new_key, &new_value);
zend_string_release(new_key);
} ZEND_HASH_FOREACH_END();

zend_hash_destroy(&existing_baggage);
Copy link
Collaborator

@bwoebi bwoebi Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
zend_string *key;
zval *value;
ZEND_HASH_FOREACH_STR_KEY_VAL(&existing_baggage, key, value) {
zend_string *new_key = zend_string_dup(key, 0);
zval new_value;
ZVAL_DUP(&new_value, value);
zend_hash_update(&result.baggage, new_key, &new_value);
zend_string_release(new_key);
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(&existing_baggage);
result.baggage = existing_baggage;

... this whole stuff is a bit weird. There's no reason for baggage being in this loop. Let's talk about it.

while (baggage_ptr < baggage_end && *baggage_ptr != '=') {
++baggage_ptr;
}
if (baggage_ptr >= baggage_end || *baggage_ptr != '=') break;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (baggage_ptr >= baggage_end || *baggage_ptr != '=') break;
if (baggage_ptr >= baggage_end) break;

This will never be != '=' at this place

Comment on lines +64 to +68
zend_string *key = zend_string_init(key_start, key_len, 0);
zval value;
ZVAL_STRINGL(&value, value_start, value_len);
zend_hash_update(baggage, key, &value);
zend_string_release(key);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
zend_string *key = zend_string_init(key_start, key_len, 0);
zval value;
ZVAL_STRINGL(&value, value_start, value_len);
zend_hash_update(baggage, key, &value);
zend_string_release(key);
zval value;
ZVAL_STRINGL(&value, value_start, value_len);
zend_hash_str_update(baggage, key_start, key_len, &value);

Comment on lines +71 to +73
if (baggage_ptr < baggage_end && *baggage_ptr == ',') {
++baggage_ptr; // skip ','
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (baggage_ptr < baggage_end && *baggage_ptr == ',') {
++baggage_ptr; // skip ','
}
++baggage_ptr; // skip ','

We can skip this unconditionally (the offset is going to be checked at the start of the loop)

@@ -42,6 +43,54 @@ static void dd_check_tid(ddtrace_distributed_tracing_result *result) {
}
}

static void ddtrace_deserialize_baggage(char *baggage_ptr, char *baggage_end, HashTable *baggage) {
while (baggage_ptr < baggage_end) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while (baggage_ptr < baggage_end) {
for (;;) {

redundant loop condition, we eventually check anyway in the first loop below here.

@@ -840,6 +841,9 @@ void ddtrace_set_root_span_properties(ddtrace_root_span_data *span) {
zend_hash_copy(Z_ARR(span->property_propagated_tags), &DDTRACE_G(propagated_root_span_tags), zval_add_ref);
SEPARATE_ARRAY(&span->property_tracestate_tags);
zend_hash_copy(Z_ARR(span->property_tracestate_tags), &DDTRACE_G(tracestate_unknown_dd_keys), zval_add_ref);
SEPARATE_ARRAY(&span->property_baggage);
zend_hash_copy(Z_ARR(span->property_baggage), &DDTRACE_G(baggage), zval_add_ref);
Copy link
Collaborator

@bwoebi bwoebi Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some tests fail with:

==305==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7f3fdc8a7ddd bp 0x7ffd0bae01f0 sp 0x7ffd0bae01d0 T0)
==305==The signal is caused by a READ memory access.
==305==Hint: address points to the zero page.
    #0 0x7f3fdc8a7ddd in zend_gc_refcount /opt/php/debug-zts-asan/include/php/Zend/zend_types.h:1025:12
    #1 0x7f3fdc8a4fb7 in ddtrace_set_root_span_properties /home/circleci/datadog/tmp/build_extension/ext/serializer.c:844:9
    #2 0x7f3fdc8cc915 in ddtrace_open_span /home/circleci/datadog/tmp/build_extension/ext/span.c:206:9
    #3 0x7f3fdc8d1961 in ddtrace_push_root_span /home/circleci/datadog/tmp/build_extension/ext/span.c:372:31
    #4 0x7f3fdc7cbdbd in dd_initialize_request /home/circleci/datadog/tmp/build_extension/ext/ddtrace.c:1618:9
    #5 0x7f3fdc7ea07c in zm_activate_ddtrace /home/circleci/datadog/tmp/build_extension/ext/ddtrace.c:1640:9
    #6 0x559c53442bab in zend_activate_modules /usr/local/src/php/Zend/zend_API.c:2605:7
    #7 0x559c531b27dc in php_request_startup /usr/local/src/php/main/main.c:1881:3
    #8 0x559c537cab41 in do_cli /usr/local/src/php/sapi/cli/php_cli.c:929:7
    #9 0x559c537c880b in main /usr/local/src/php/sapi/cli/php_cli.c:1360:18

Have a look at ddtrace_root_span_data_create.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants