-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathhandlers_http.h
More file actions
495 lines (430 loc) · 18.1 KB
/
handlers_http.h
File metadata and controls
495 lines (430 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include "asm_event.h"
#include "trace_source.h"
#include "configuration.h"
#include "ddtrace.h"
#include "priority_sampling/priority_sampling.h"
#include "tracer_tag_propagation/tracer_tag_propagation.h"
#include "span.h"
#include <Zend/zend_smart_str.h>
#include <components/log/log.h>
ZEND_EXTERN_MODULE_GLOBALS(ddtrace);
static inline zend_string *ddtrace_format_tracestate(zend_string *tracestate, uint64_t span_id, zend_string *origin, zend_long sampling_priority, zend_string *propagated_tags, zend_array *tracestate_unknown_dd_keys) {
smart_str str = {0};
if (span_id) {
smart_str_append_printf(&str, "p:%016" PRIx64, span_id);
}
if (origin) {
if (str.s) {
smart_str_appendc(&str, ';');
}
smart_str_appends(&str, "o:");
signed char *cur = (signed char *)ZSTR_VAL(str.s) + ZSTR_LEN(str.s);
smart_str_append(&str, origin);
for (signed char *end = (signed char *)ZSTR_VAL(str.s) + ZSTR_LEN(str.s); cur < end; ++cur) {
if (*cur == '=') {
*cur = '~';
} else if (*cur < 0x20 || *cur == ',' || *cur == ';' || *cur == '~') {
*cur = '_';
}
}
}
if (sampling_priority != DDTRACE_PRIORITY_SAMPLING_UNKNOWN && sampling_priority != 0 && sampling_priority != 1) {
if (str.s) {
smart_str_appendc(&str, ';');
}
smart_str_append_printf(&str, "s:" ZEND_LONG_FMT, sampling_priority);
}
if (propagated_tags && ZSTR_LEN(propagated_tags) > 0) {
bool last_separator = true;
bool is_first_iteration = true;
char next_equals;
for (char *cur = ZSTR_VAL(propagated_tags), *end = cur + ZSTR_LEN(propagated_tags); cur < end; ++cur) {
if (last_separator) {
next_equals = ':';
cur += strlen("_dd.p");
// drop the tid from otel tracestate
if (cur + strlen(".tid=") + 16 /* 16 byte tid */ <= end && memcmp(cur, ".tid=", strlen(".tid=")) == 0) {
cur += strlen(".tid=") + 16;
continue;
}
if (str.s && is_first_iteration) {
smart_str_appendc(&str, ';');
}
is_first_iteration = false;
smart_str_appendc(&str, 't');
}
signed char chr = *cur;
if (chr < 0x20 || chr == ';' || chr == '~') {
chr = '_';
} else if (chr == ',') {
chr = ';';
} else if (chr == '=') {
chr = next_equals;
next_equals = '~';
}
smart_str_appendc(&str, chr);
last_separator = chr == ';';
}
}
zend_string *unknown_key;
zval *unknown_val;
ZEND_HASH_FOREACH_STR_KEY_VAL(tracestate_unknown_dd_keys, unknown_key, unknown_val) {
if (str.s) {
smart_str_appendc(&str, ';');
}
smart_str_append(&str, unknown_key);
smart_str_appendc(&str, ':');
smart_str_append(&str, Z_STR_P(unknown_val));
} ZEND_HASH_FOREACH_END();
bool hasdd = str.s != NULL;
if (tracestate && ZSTR_LEN(tracestate) > 0) {
if (str.s) {
smart_str_appendc(&str, ',');
}
smart_str_append(&str, tracestate);
}
if (str.s && ZSTR_VAL(str.s)[ZSTR_LEN(str.s) - 1] == ',') {
ZSTR_VAL(str.s)[ZSTR_LEN(str.s) - 1] = '\0';
ZSTR_LEN(str.s)--;
}
if (str.s) {
zend_string *full_tracestate = zend_strpprintf(0, "%s%.*s", hasdd ? "dd=" : "", (int)ZSTR_LEN(str.s), ZSTR_VAL(str.s));
smart_str_free(&str);
return full_tracestate;
}
return NULL;
}
static inline zend_string *ddtrace_percent_encode(zend_string *string, bool is_key) {
smart_str encoded = {0};
char *str = ZSTR_VAL(string);
size_t len = ZSTR_LEN(string);
for (size_t i = 0; i < len; i++) {
char c = str[i];
bool encode;
if (is_key) {
// Encode all characters that are NOT in RFC7230 allowed set for keys
encode = !(isalnum(c) || strchr("!#$%&'*+-.^_`|~", c));
} else {
// Encode all non-ASCII characters and special disallowed characters
encode = c < 0x20 || c > 0x7E || c == ' ' || c == '"' || c == ',' || c == ';' || c == '\\';
}
if (!encoded.s) {
if (!encode) {
continue;
}
smart_str_appendl(&encoded, str, i);
}
if (encode) {
smart_str_append_printf(&encoded, "%%%02X", (unsigned char)c);
} else {
smart_str_appendc(&encoded, c);
}
}
if (!encoded.s) {
return zend_string_copy(string);
}
smart_str_0(&encoded); // Null-terminate string
return encoded.s;
}
static inline zend_string *ddtrace_serialize_baggage(HashTable *baggage) {
smart_str serialized_baggage = {0};
zend_string *key;
zend_long numkey;
zval *value;
uint64_t max_bytes = get_DD_TRACE_BAGGAGE_MAX_BYTES();
uint64_t max_items = get_DD_TRACE_BAGGAGE_MAX_ITEMS();
size_t size = 0;
size_t item_count = 0;
ZEND_HASH_FOREACH_KEY_VAL(baggage, numkey, key, value) {
if ((key && ZSTR_LEN(key) == 0) || Z_TYPE_P(value) != IS_STRING || Z_STRLEN_P(value) == 0) {
continue; // Skip invalid entries
}
zend_string *encoded_key = key ? ddtrace_percent_encode(key, true) : zend_long_to_str(numkey);
zend_string *encoded_value = ddtrace_percent_encode(Z_STR_P(value), false);
if (item_count++ >= max_items) {
LOG(WARN, "Baggage item limit of %ld exceeded, dropping excess items.", max_items);
zend_string_release(encoded_key);
zend_string_release(encoded_value);
DDTRACE_G(baggage_max_item_count) += 1;
break;
}
size += (serialized_baggage.s ? 1 : 0) + ZSTR_LEN(encoded_key) + 1 + ZSTR_LEN(encoded_value);
if (size > max_bytes) {
LOG(WARN, "Baggage header size of %ld bytes exceeded, dropping excess items.", max_bytes);
zend_string_release(encoded_key);
zend_string_release(encoded_value);
DDTRACE_G(baggage_max_byte_count) += 1;
break;
}
if (serialized_baggage.s) {
smart_str_appendc(&serialized_baggage, ',');
}
// Append key=value pair
smart_str_appendl(&serialized_baggage, ZSTR_VAL(encoded_key), ZSTR_LEN(encoded_key));
smart_str_appendc(&serialized_baggage, '=');
smart_str_appendl(&serialized_baggage, ZSTR_VAL(encoded_value), ZSTR_LEN(encoded_value));
zend_string_release(encoded_key);
zend_string_release(encoded_value);
} ZEND_HASH_FOREACH_END();
if (serialized_baggage.s) {
smart_str_0(&serialized_baggage); // Null-terminate
}
if (item_count != 0) {
DDTRACE_G(baggage_inject_count) += 1;
}
return serialized_baggage.s;
}
typedef enum {
HEADER_MODE_ARRAY, // ["Header: value", ...]
HEADER_MODE_KV_PAIRS, // [key => value]
HEADER_MODE_CONTEXT // "Header1: value\r\nHeader2: value"
} header_format;
static inline zend_string *ddtrace_extract_header_key(char *header) {
const char *colon = strchr(header, ':');
if (!colon) {
return NULL;
}
size_t key_len = (size_t)(colon - header);
zend_string *key = zend_string_init(header, key_len, 0);
zend_str_tolower(ZSTR_VAL(key), key_len);
return key;
}
static inline void ddtrace_init_header_keys_set(zend_array *array, header_format format, HashTable *header_keys) {
if (format == HEADER_MODE_KV_PAIRS) {
return;
}
zend_hash_init(header_keys, 8, NULL, NULL, 0);
if (format != HEADER_MODE_ARRAY) {
return;
}
uint32_t idx = 0;
ZEND_HASH_FOREACH_VAL(array, zval *entry) {
if (Z_TYPE_P(entry) != IS_STRING) {
idx++;
continue;
}
zend_string *header_key = ddtrace_extract_header_key(Z_STRVAL_P(entry));
if (!header_key) {
idx++;
continue;
}
zval index_zv;
ZVAL_LONG(&index_zv, idx);
zend_hash_update(header_keys, header_key, &index_zv);
zend_string_release(header_key);
idx++;
} ZEND_HASH_FOREACH_END();
}
static inline void ddtrace_add_or_update_header_array(zend_array *array, HashTable *keys, const char *header_key, size_t header_key_len, zend_string *header) {
// Check for duplicates
if (zend_hash_num_elements(keys) > 0) {
zval *existing_idx_zv = zend_hash_str_find(keys, header_key, header_key_len);
if (existing_idx_zv) {
zend_hash_index_del(array, Z_LVAL_P(existing_idx_zv));
}
}
// Insert header
zval zv;
ZVAL_STR(&zv, header);
zend_hash_next_index_insert(array, &zv);
}
static inline void ddtrace_inject_distributed_headers_config(zend_array *array, header_format format, zend_array *inject) {
ddtrace_root_span_data *root = DDTRACE_G(active_stack) && DDTRACE_G(active_stack)->active ? SPANDATA(DDTRACE_G(active_stack)->active)->root : NULL;
zend_string *origin = DDTRACE_G(dd_origin);
zend_array *tracestate_unknown_dd_keys = &DDTRACE_G(tracestate_unknown_dd_keys);
zend_string *tracestate = DDTRACE_G(tracestate);
zend_array *baggage = &DDTRACE_G(baggage);
if (root) {
SPANDATA(DDTRACE_G(active_stack)->active)->flags |= DDTRACE_SPAN_FLAG_NOT_DROPPABLE;
if (Z_TYPE(root->property_origin) == IS_STRING && Z_STRLEN(root->property_origin)) {
origin = Z_STR(root->property_origin);
} else {
origin = NULL;
}
if (Z_TYPE(root->property_tracestate) == IS_STRING && Z_STRLEN(root->property_tracestate)) {
tracestate = Z_STR(root->property_tracestate);
} else {
tracestate = NULL;
}
tracestate_unknown_dd_keys = ddtrace_property_array(&root->property_tracestate_tags);
}
if (DDTRACE_G(active_stack) && DDTRACE_G(active_stack)->active) {
baggage = ddtrace_property_array(&SPANDATA(DDTRACE_G(active_stack)->active)->property_baggage);
}
zval headers;
ZVAL_ARR(&headers, array);
smart_str stream_header = {0};
bool send_datadog = zend_hash_str_exists(inject, ZEND_STRL("datadog"));
bool send_tracestate = zend_hash_str_exists(inject, ZEND_STRL("tracecontext"));
bool send_b3 = zend_hash_str_exists(inject, ZEND_STRL("b3")) || zend_hash_str_exists(inject, ZEND_STRL("b3multi"));
bool send_b3single = zend_hash_str_exists(inject, ZEND_STRL("b3 single header"));
bool send_baggage = zend_hash_str_exists(inject, ZEND_STRL("baggage"));
zend_long sampling_priority = ddtrace_fetch_priority_sampling_from_root();
if (!get_DD_APM_TRACING_ENABLED() && ddtrace_asm_event_emitted()) {
sampling_priority = PRIORITY_SAMPLING_USER_KEEP;
}
ddtrace_root_span_data *root_span = DDTRACE_G(active_stack) ? DDTRACE_G(active_stack)->root_span : NULL;
zend_array *meta = root_span ? ddtrace_property_array(&root_span->property_meta) : &DDTRACE_G(root_span_tags_preset);
if (!get_DD_APM_TRACING_ENABLED() && !ddtrace_asm_event_emitted() && !ddtrace_trace_source_is_meta_asm_sourced(meta)) {
return;
}
HashTable header_keys;
ddtrace_init_header_keys_set(array, format, &header_keys);
#define ADD_HEADER(header, ...) \
if (format == HEADER_MODE_KV_PAIRS) { \
add_assoc_str_ex(&headers, ZEND_STRL(header), zend_strpprintf(0, __VA_ARGS__)); \
} else if (format == HEADER_MODE_ARRAY) { \
ddtrace_add_or_update_header_array(Z_ARRVAL(headers), &header_keys, ZEND_STRL(header), zend_strpprintf(0, header ": " __VA_ARGS__)); \
} else { \
if (stream_header.s) { \
smart_str_appendl(&stream_header, "\r\n", 2); \
} \
zend_string *formatted = zend_strpprintf(0, header ": " __VA_ARGS__); \
smart_str_append(&stream_header, formatted); \
\
zend_string *key = zend_string_init(ZEND_STRL(header), 0); \
zend_str_tolower(ZSTR_VAL(key), ZSTR_LEN(key)); \
zend_hash_add_empty_element(&header_keys, key); \
zend_string_release(key); \
\
zend_string_release(formatted); \
}
if (sampling_priority != DDTRACE_PRIORITY_SAMPLING_UNKNOWN) {
if (send_datadog) {
ADD_HEADER("x-datadog-sampling-priority", ZEND_LONG_FMT, sampling_priority);
}
if (send_b3) {
if (sampling_priority <= 0) {
ADD_HEADER("x-b3-sampled", "0");
} else if (sampling_priority == PRIORITY_SAMPLING_USER_KEEP) {
ADD_HEADER("x-b3-flags", "1");
} else {
ADD_HEADER("x-b3-sampled", "1");
}
}
}
zend_string *propagated_tags = ddtrace_format_root_propagated_tags();
if (send_datadog || send_b3 || send_b3single) {
if (propagated_tags) {
ADD_HEADER("x-datadog-tags", "%s", ZSTR_VAL(propagated_tags));
}
if (origin) {
ADD_HEADER("x-datadog-origin", "%s", ZSTR_VAL(origin));
}
}
ddtrace_trace_id trace_id = ddtrace_peek_trace_id();
uint64_t span_id = ddtrace_peek_span_id();
if (trace_id.low || trace_id.high) {
if (send_datadog) {
ADD_HEADER("x-datadog-trace-id", "%" PRIu64, trace_id.low);
}
if (send_b3) {
if (trace_id.high) {
ADD_HEADER("x-b3-traceid", "%016" PRIx64 "%016" PRIx64, trace_id.high, trace_id.low);
} else {
ADD_HEADER("x-b3-traceid", "%016" PRIx64, trace_id.low);
}
}
if (span_id) {
if (send_datadog) {
ADD_HEADER("x-datadog-parent-id", "%" PRIu64, span_id);
}
if (send_b3) {
ADD_HEADER("x-b3-spanid", "%016" PRIx64, span_id);
}
if (send_tracestate) {
ADD_HEADER("traceparent", "00-%016" PRIx64 "%016" PRIx64 "-%016" PRIx64 "-%02" PRIx8,
trace_id.high,
trace_id.low,
span_id,
sampling_priority > 0);
uint64_t propagated_span_id = 0;
zval *old_parent_id;
if (root) {
propagated_span_id = span_id;
} else if ((old_parent_id = zend_hash_str_find(&DDTRACE_G(root_span_tags_preset), ZEND_STRL("_dd.parent_id")))) {
propagated_span_id = ddtrace_parse_hex_span_id(old_parent_id);
}
zend_string *full_tracestate = ddtrace_format_tracestate(tracestate, propagated_span_id, origin, sampling_priority, propagated_tags, tracestate_unknown_dd_keys);
if (full_tracestate) {
ADD_HEADER("tracestate", "%.*s", (int)ZSTR_LEN(full_tracestate), ZSTR_VAL(full_tracestate));
zend_string_release(full_tracestate);
}
}
}
}
if (send_b3single) {
char *b3_sampling_decision = NULL;
if (sampling_priority != DDTRACE_PRIORITY_SAMPLING_UNKNOWN) {
if (sampling_priority <= 0) {
b3_sampling_decision = "0";
} else if (sampling_priority == PRIORITY_SAMPLING_USER_KEEP) {
b3_sampling_decision = "d";
} else {
b3_sampling_decision = "1";
}
}
if ((trace_id.low || trace_id.high) && span_id) {
char trace_id_buf[DD_TRACE_MAX_ID_LEN];
if (trace_id.high) {
sprintf(trace_id_buf, "%016" PRIx64 "%016" PRIx64, trace_id.high, trace_id.low);
} else {
sprintf(trace_id_buf, "%016" PRIx64, trace_id.low);
}
ADD_HEADER("b3", "%s-%016" PRIx64 "%s%s",
trace_id_buf,
span_id,
b3_sampling_decision ? "-" : "", b3_sampling_decision ? b3_sampling_decision : "");
} else if (b3_sampling_decision) {
ADD_HEADER("b3", "%s", b3_sampling_decision);
}
}
if (send_baggage) {
zend_string *full_baggage = ddtrace_serialize_baggage(baggage);
if (full_baggage) {
ADD_HEADER("baggage", "%.*s", (int)ZSTR_LEN(full_baggage), ZSTR_VAL(full_baggage));
zend_string_release(full_baggage);
}
}
if (propagated_tags) {
zend_string_release(propagated_tags);
}
if (format == HEADER_MODE_CONTEXT && stream_header.s) {
zval *existing_header_zv = zend_hash_str_find(array, ZEND_STRL("header"));
if (existing_header_zv && Z_TYPE_P(existing_header_zv) == IS_STRING) {
char *cursor = ZSTR_VAL(Z_STR_P(existing_header_zv));
char *end = cursor + ZSTR_LEN(Z_STR_P(existing_header_zv));
while (cursor < end) {
const char *eol = strstr(cursor, "\r\n");
size_t len = eol ? (size_t)(eol - cursor) : (size_t)(end - cursor);
if (len == 0) {
cursor += eol ? 2 : 0;
continue;
}
zend_string *key = ddtrace_extract_header_key(cursor);
if (key) {
if (!zend_hash_exists(&header_keys, key)) {
smart_str_appendl(&stream_header, "\r\n", 2);
smart_str_appendl(&stream_header, cursor, len);
}
zend_string_release(key);
}
cursor += len + (eol ? 2 : 0);
}
}
smart_str_0(&stream_header);
zval result;
ZVAL_STR(&result, stream_header.s);
zend_hash_str_update(array, ZEND_STRL("header"), &result);
}
if (format != HEADER_MODE_KV_PAIRS) {
zend_hash_destroy(&header_keys);
}
#undef ADD_HEADER
}
static inline void ddtrace_inject_distributed_headers(zend_array *array, header_format format) {
zend_array *inject = zai_config_is_modified(DDTRACE_CONFIG_DD_TRACE_PROPAGATION_STYLE)
&& !zai_config_is_modified(DDTRACE_CONFIG_DD_TRACE_PROPAGATION_STYLE_INJECT)
? get_DD_TRACE_PROPAGATION_STYLE() : get_DD_TRACE_PROPAGATION_STYLE_INJECT();
ddtrace_inject_distributed_headers_config(array, format, inject);
}