Skip to content

Commit e91ed73

Browse files
committed
test: Add sample failing test
1 parent 426c409 commit e91ed73

File tree

4 files changed

+67
-21
lines changed

4 files changed

+67
-21
lines changed

ext/ddtrace.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ class SpanData {
121121
public array $peerServiceSources = [];
122122

123123
/**
124-
* @var \Closure|null $endCallback A callback to be called when the span is finished, if any.
124+
* @var \Closure|string|null $endCallback A callback to be called when the span is finished, if any.
125125
*/
126-
public \Closure|null $endCallback = null;
126+
public \Closure|string|null $endCallback = null;
127127

128128
/**
129129
* @var SpanData|null The parent span, or 'null' if there is none

ext/ddtrace_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 55972d231789add1cf24ac96d74b532cd7f112f5 */
2+
* Stub hash: e001e68def98ce39cf3f6a11337dc109cca4868f */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_DDTrace_trace_method, 0, 3, _IS_BOOL, 0)
55
ZEND_ARG_TYPE_INFO(0, className, IS_STRING, 0)
@@ -573,7 +573,7 @@ static zend_class_entry *register_class_DDTrace_SpanData(void)
573573
ZVAL_NULL(&property_endCallback_default_value);
574574
zend_string *property_endCallback_name = zend_string_init("endCallback", sizeof("endCallback") - 1, 1);
575575
zend_string *property_endCallback_class_Closure = zend_string_init("Closure", sizeof("Closure")-1, 1);
576-
zend_declare_typed_property(class_entry, property_endCallback_name, &property_endCallback_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_CLASS(property_endCallback_class_Closure, 0, MAY_BE_NULL));
576+
zend_declare_typed_property(class_entry, property_endCallback_name, &property_endCallback_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_CLASS(property_endCallback_class_Closure, 0, MAY_BE_STRING|MAY_BE_NULL));
577577
zend_string_release(property_endCallback_name);
578578

579579
zval property_parent_default_value;

ext/span.c

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -410,29 +410,47 @@ bool ddtrace_span_alter_root_span_config(zval *old_value, zval *new_value) {
410410
}
411411
}
412412

413-
void dd_trace_stop_span_time(ddtrace_span_data *span) {
413+
bool execute_span_end_callback(ddtrace_span_data *span) {
414414
zval *end_closure_zv = &span->property_end_closure;
415415

416-
if (end_closure_zv
417-
&& Z_TYPE_P(end_closure_zv) == IS_OBJECT
418-
&& Z_OBJCE_P(end_closure_zv) == zend_ce_closure) {
419-
zval rv;
420-
zval span_zv;
421-
ZVAL_OBJ(&span_zv, &span->std);
422-
bool success = zai_symbol_call(
423-
ZAI_SYMBOL_SCOPE_GLOBAL,
424-
NULL,
425-
ZAI_SYMBOL_FUNCTION_CLOSURE,
426-
end_closure_zv,
427-
&rv,
428-
1,
429-
&span_zv
430-
);
416+
if (end_closure_zv && !ZVAL_IS_NULL(end_closure_zv)) {
417+
uint8_t type = Z_TYPE_P(end_closure_zv);
418+
bool success = false;
419+
420+
if (type == IS_OBJECT && Z_OBJCE_P(end_closure_zv) == zend_ce_closure) {
421+
zval rv;
422+
zval span_zv;
423+
ZVAL_OBJ(&span_zv, &span->std);
424+
425+
success = zai_symbol_call(
426+
ZAI_SYMBOL_SCOPE_GLOBAL,
427+
NULL,
428+
ZAI_SYMBOL_FUNCTION_CLOSURE,
429+
end_closure_zv,
430+
&rv,
431+
1,
432+
&span_zv
433+
);
434+
435+
zval_ptr_dtor(&rv);
436+
} // TODO: string, etc? TBD, else refactor
437+
431438
if (!success) {
432-
LOG(Warn, "Unable to call end closure");
439+
zend_string *hex_trace_id = ddtrace_trace_id_as_hex_string(span->root->trace_id);
440+
zend_string *hex_span_id = ddtrace_span_id_as_hex_string(span->span_id);
441+
LOG(Warn, "Error while calling end closure on span %s [dd.trace_id=%s dd.span_id=%s]", Z_STRVAL_P(&span->property_name), ZSTR_VAL(hex_trace_id), ZSTR_VAL(hex_span_id));
442+
zend_string_release(hex_trace_id);
443+
zend_string_release(hex_span_id);
433444
}
445+
446+
return success;
447+
} else {
448+
return true;
434449
}
450+
}
435451

452+
void dd_trace_stop_span_time(ddtrace_span_data *span) {
453+
execute_span_end_callback(span);
436454
span->duration = _get_nanoseconds(USE_MONOTONIC_CLOCK) - span->duration_start;
437455
}
438456

tests/ext/span_callback_static.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
End callback using a static method
3+
--ENV--
4+
DD_TRACE_DEBUG=1
5+
--FILE--
6+
<?php
7+
8+
$counter = 0;
9+
10+
class MyCounter
11+
{
12+
public static function increment()
13+
{
14+
global $counter;
15+
$counter++;
16+
}
17+
}
18+
19+
$closure = Closure::fromCallable('MyCounter::increment');
20+
21+
$span = \DDTrace\start_span();
22+
$span->endCallback = $closure;
23+
\DDTrace\close_span();
24+
25+
var_dump($counter);
26+
27+
?>
28+
--EXPECT--

0 commit comments

Comments
 (0)