Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 6845ae1

Browse files
authored
Fix empty start time (#135)
* Failing test for default timestamp * Add phpt tests for extension when you provide invalid startTime options * Extension ignores invalid startTime options (only allow int/float) * Extension tracer sets default startTime * Fix test description
1 parent ac4be6d commit 6845ae1

File tree

5 files changed

+70
-5
lines changed

5 files changed

+70
-5
lines changed

ext/opencensus_trace_span.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
zend_class_entry* opencensus_trace_span_ce = NULL;
9696

9797
ZEND_BEGIN_ARG_INFO_EX(arginfo_OpenCensusTraceSpan_construct, 0, 0, 1)
98-
ZEND_ARG_ARRAY_INFO(0, spanOptions, 0)
98+
ZEND_ARG_ARRAY_INFO(0, spanOptions, 0)
9999
ZEND_END_ARG_INFO();
100100

101101
/**
@@ -468,7 +468,14 @@ int opencensus_trace_span_apply_span_options(opencensus_trace_span_t *span, zval
468468
if (strcmp(ZSTR_VAL(k), "attributes") == 0) {
469469
zend_hash_merge(span->attributes, Z_ARRVAL_P(v), zval_add_ref, 0);
470470
} else if (strcmp(ZSTR_VAL(k), "startTime") == 0) {
471-
span->start = Z_DVAL_P(v);
471+
switch (Z_TYPE_P(v)) {
472+
case IS_DOUBLE:
473+
span->start = Z_DVAL_P(v);
474+
break;
475+
case IS_LONG:
476+
span->start = (double) Z_LVAL_P(v);
477+
break;
478+
}
472479
} else if (strcmp(ZSTR_VAL(k), "name") == 0) {
473480
if (span->name) {
474481
zend_string_release(span->name);
@@ -504,7 +511,7 @@ static int opencensus_trace_update_time_events(opencensus_trace_span_t *span, zv
504511
opencensus_trace_time_event_to_zval(event, &zv);
505512
add_next_index_zval(return_value, &zv);
506513
} ZEND_HASH_FOREACH_END();
507-
return SUCCESS;
514+
return SUCCESS;
508515
}
509516

510517
static int opencensus_trace_update_links(opencensus_trace_span_t *span, zval *return_value)
@@ -515,7 +522,7 @@ static int opencensus_trace_update_links(opencensus_trace_span_t *span, zval *re
515522
opencensus_trace_link_to_zval(link, &zv);
516523
add_next_index_zval(return_value, &zv);
517524
} ZEND_HASH_FOREACH_END();
518-
return SUCCESS;
525+
return SUCCESS;
519526
}
520527

521528
/* Fill the provided span with the provided data from the internal span representation */

ext/tests/int_start_time.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
OpenCensus Trace: Bug 131: Providing integer start time should work
3+
--FILE--
4+
<?php
5+
6+
opencensus_trace_begin('foo', ['startTime' => (int) microtime(true)]);
7+
opencensus_trace_finish();
8+
$spans = opencensus_trace_list();
9+
10+
echo 'Number of spans: ' . count($spans) . PHP_EOL;
11+
$span = $spans[0];
12+
var_dump($span->startTime());
13+
14+
$test = microtime(true) - 1 < $span->startTime();
15+
echo 'Start time just happened: ' . $test;
16+
17+
?>
18+
--EXPECTF--
19+
Number of spans: 1
20+
float(%d)
21+
Start time just happened: 1

ext/tests/null_start_time.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
OpenCensus Trace: Bug 131: Providing null start time defaults to a current time
3+
--FILE--
4+
<?php
5+
6+
opencensus_trace_begin('foo', ['startTime' => null]);
7+
opencensus_trace_finish();
8+
$spans = opencensus_trace_list();
9+
10+
echo 'Number of spans: ' . count($spans) . PHP_EOL;
11+
$span = $spans[0];
12+
var_dump($span->startTime());
13+
14+
$test = microtime(true) - 1 < $span->startTime();
15+
echo 'Start time just happened: ' . $test;
16+
17+
?>
18+
--EXPECTF--
19+
Number of spans: 1
20+
float(%d.%d)
21+
Start time just happened: 1

src/Trace/Tracer/ExtensionTracer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function withSpan(Span $span)
8383
{
8484
$startTime = $span->startTime()
8585
? (float)($span->startTime()->format('U.u'))
86-
: null;
86+
: microtime(true);
8787
$info = [
8888
'spanId' => $span->spanId(),
8989
'parentSpanId' => $span->parentSpanId(),

tests/unit/Trace/Tracer/AbstractTracerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,22 @@ public function testAddsMessageEventToRootSpan()
242242
$this->assertCount(1, $span->timeEvents());
243243
}
244244

245+
public function testInSpanSetsDefaultStartTime()
246+
{
247+
$class = $this->getTracerClass();
248+
$tracer = new $class();
249+
$tracer->inSpan(['name' => 'foo'], function () {
250+
// do nothing
251+
});
252+
253+
$spans = $tracer->spans();
254+
$this->assertCount(1, $spans);
255+
$span = $spans[0];
256+
257+
// #131 - Span should be initialized with current time, not the epoch.
258+
$this->assertNotEquals(0, $span->startTime()->getTimestamp());
259+
}
260+
245261
private function assertEquivalentTimestamps($expected, $value)
246262
{
247263
$this->assertEquals((float)($expected->format('U.u')), (float)($expected->format('U.u')), '', 0.000001);

0 commit comments

Comments
 (0)