-
Notifications
You must be signed in to change notification settings - Fork 161
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
base: master
Are you sure you want to change the base?
Changes from all commits
e0e1466
ef9f82e
ac782dd
b4e33d9
028caa6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,6 +17,7 @@ static ddtrace_distributed_tracing_result dd_init_empty_result(void) { | |||||||||||||||||||||||||||
zend_hash_init(&result.tracestate_unknown_dd_keys, 8, unused, ZVAL_PTR_DTOR, 0); | ||||||||||||||||||||||||||||
zend_hash_init(&result.propagated_tags, 8, unused, ZVAL_PTR_DTOR, 0); | ||||||||||||||||||||||||||||
zend_hash_init(&result.meta_tags, 8, unused, ZVAL_PTR_DTOR, 0); | ||||||||||||||||||||||||||||
zend_hash_init(&result.baggage, 8, unused, ZVAL_PTR_DTOR, 0); | ||||||||||||||||||||||||||||
return result; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
@@ -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) { | ||||||||||||||||||||||||||||
char *key_start = baggage_ptr; | ||||||||||||||||||||||||||||
while (baggage_ptr < baggage_end && *baggage_ptr != '=') { | ||||||||||||||||||||||||||||
++baggage_ptr; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
if (baggage_ptr >= baggage_end || *baggage_ptr != '=') break; | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This will never be |
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
size_t key_len = baggage_ptr - key_start; | ||||||||||||||||||||||||||||
++baggage_ptr; // skip '=' | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
char *value_start = baggage_ptr; | ||||||||||||||||||||||||||||
while (baggage_ptr < baggage_end && *baggage_ptr != ',') { | ||||||||||||||||||||||||||||
++baggage_ptr; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
size_t value_len = baggage_ptr - value_start; | ||||||||||||||||||||||||||||
if (key_len > 0 && value_len > 0) { | ||||||||||||||||||||||||||||
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); | ||||||||||||||||||||||||||||
Comment on lines
+64
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if (baggage_ptr < baggage_end && *baggage_ptr == ',') { | ||||||||||||||||||||||||||||
++baggage_ptr; // skip ',' | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
Comment on lines
+71
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We can skip this unconditionally (the offset is going to be checked at the start of the loop) |
||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
static ddtrace_distributed_tracing_result ddtrace_read_distributed_baggage(ddtrace_read_header *read_header, void *data) { | ||||||||||||||||||||||||||||
zend_string *baggage_header; | ||||||||||||||||||||||||||||
ddtrace_distributed_tracing_result result = dd_init_empty_result(); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if (!read_header((zai_str)ZAI_STRL("BAGGAGE"), "baggage", &baggage_header, data)) { | ||||||||||||||||||||||||||||
return result; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
char *baggage_ptr = ZSTR_VAL(baggage_header); | ||||||||||||||||||||||||||||
char *baggage_end = baggage_ptr + ZSTR_LEN(baggage_header); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
ddtrace_deserialize_baggage(baggage_ptr, baggage_end, &result.baggage); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
zend_string_release(baggage_header); | ||||||||||||||||||||||||||||
return result; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
static ddtrace_distributed_tracing_result ddtrace_read_distributed_tracing_ids_datadog(ddtrace_read_header *read_header, void *data) { | ||||||||||||||||||||||||||||
zend_string *trace_id_str, *parent_id_str, *priority_str, *propagated_tags; | ||||||||||||||||||||||||||||
ddtrace_distributed_tracing_result result = dd_init_empty_result(); | ||||||||||||||||||||||||||||
|
@@ -359,12 +408,16 @@ ddtrace_distributed_tracing_result ddtrace_read_distributed_tracing_ids(ddtrace_ | |||||||||||||||||||||||||||
func = ddtrace_read_distributed_tracing_ids_b3; | ||||||||||||||||||||||||||||
} else if (!has_trace && zend_string_equals_literal(extraction_style, "b3 single header")) { | ||||||||||||||||||||||||||||
func = ddtrace_read_distributed_tracing_ids_b3_single_header; | ||||||||||||||||||||||||||||
} else if (zend_string_equals_literal(extraction_style, "baggage")) { | ||||||||||||||||||||||||||||
func = ddtrace_read_distributed_baggage; | ||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if (!has_trace) { | ||||||||||||||||||||||||||||
zend_string *existing_origin = result.origin; | ||||||||||||||||||||||||||||
zend_array existing_baggage = result.baggage; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if (result.meta_tags.arData) { | ||||||||||||||||||||||||||||
zend_hash_destroy(&result.meta_tags); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
@@ -388,8 +441,24 @@ ddtrace_distributed_tracing_result ddtrace_read_distributed_tracing_ids(ddtrace_ | |||||||||||||||||||||||||||
result.origin = existing_origin; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if (existing_baggage.arData) { | ||||||||||||||||||||||||||||
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); | ||||||||||||||||||||||||||||
Comment on lines
+446
to
+457
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
... this whole stuff is a bit weird. There's no reason for baggage being in this loop. Let's talk about it. |
||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||
ddtrace_distributed_tracing_result new_result = func(read_header, data); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if (result.trace_id.low == new_result.trace_id.low && result.trace_id.high == new_result.trace_id.high) { | ||||||||||||||||||||||||||||
if (!result.tracestate && new_result.tracestate) { | ||||||||||||||||||||||||||||
result.tracestate = new_result.tracestate; | ||||||||||||||||||||||||||||
|
@@ -415,12 +484,27 @@ ddtrace_distributed_tracing_result ddtrace_read_distributed_tracing_ids(ddtrace_ | |||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if (new_result.baggage.arData) { | ||||||||||||||||||||||||||||
zend_string *key; | ||||||||||||||||||||||||||||
zval *value; | ||||||||||||||||||||||||||||
ZEND_HASH_FOREACH_STR_KEY_VAL(&new_result.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(); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if (new_result.tracestate) { | ||||||||||||||||||||||||||||
zend_string_release(new_result.tracestate); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
if (new_result.origin) { | ||||||||||||||||||||||||||||
zend_string_release(new_result.origin); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
zend_hash_destroy(&new_result.baggage); | ||||||||||||||||||||||||||||
zend_hash_destroy(&new_result.meta_tags); | ||||||||||||||||||||||||||||
zend_hash_destroy(&new_result.propagated_tags); | ||||||||||||||||||||||||||||
zend_hash_destroy(&new_result.tracestate_unknown_dd_keys); | ||||||||||||||||||||||||||||
|
@@ -448,6 +532,10 @@ void ddtrace_apply_distributed_tracing_result(ddtrace_distributed_tracing_result | |||||||||||||||||||||||||||
*Z_ARR(zv) = result->propagated_tags; | ||||||||||||||||||||||||||||
ddtrace_assign_variable(&span->property_propagated_tags, &zv); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
ZVAL_ARR(&zv, emalloc(sizeof(HashTable))); | ||||||||||||||||||||||||||||
*Z_ARR(zv) = result->baggage; | ||||||||||||||||||||||||||||
ddtrace_assign_variable(&span->property_baggage, &zv); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
zend_hash_copy(root_meta, &result->meta_tags, NULL); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if (result->origin) { | ||||||||||||||||||||||||||||
|
@@ -474,6 +562,8 @@ void ddtrace_apply_distributed_tracing_result(ddtrace_distributed_tracing_result | |||||||||||||||||||||||||||
DDTRACE_G(propagated_root_span_tags) = result->propagated_tags; | ||||||||||||||||||||||||||||
zend_hash_destroy(&DDTRACE_G(tracestate_unknown_dd_keys)); | ||||||||||||||||||||||||||||
DDTRACE_G(tracestate_unknown_dd_keys) = result->tracestate_unknown_dd_keys; | ||||||||||||||||||||||||||||
zend_hash_destroy(&DDTRACE_G(baggage)); | ||||||||||||||||||||||||||||
DDTRACE_G(baggage) = result->baggage; | ||||||||||||||||||||||||||||
zend_hash_copy(&DDTRACE_G(root_span_tags_preset), &result->meta_tags, NULL); | ||||||||||||||||||||||||||||
if (DDTRACE_G(dd_origin)) { | ||||||||||||||||||||||||||||
zend_string_release(DDTRACE_G(dd_origin)); | ||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -797,6 +797,7 @@ void ddtrace_set_root_span_properties(ddtrace_root_span_data *span) { | |
if (parent_root) { | ||
ddtrace_inherit_span_properties(&span->span, &parent_root->span); | ||
ZVAL_COPY(&span->property_origin, &parent_root->property_origin); | ||
ZVAL_COPY(&span->property_baggage, &parent_root->property_baggage); | ||
} else { | ||
zval *prop_type = &span->property_type; | ||
zval *prop_name = &span->property_name; | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some tests fail with:
Have a look at ddtrace_root_span_data_create. |
||
|
||
if (DDTRACE_G(propagated_priority_sampling) != DDTRACE_PRIORITY_SAMPLING_UNSET) { | ||
ZVAL_LONG(&span->property_propagated_sampling_priority, DDTRACE_G(propagated_priority_sampling)); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redundant loop condition, we eventually check anyway in the first loop below here.