Skip to content

Commit cba74ae

Browse files
committed
operations and tracer reset api
1 parent 5c69592 commit cba74ae

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

src/Metrics/Operations.php

+5
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@ public function count()
4343
{
4444
return count($this->data);
4545
}
46+
47+
public function reset()
48+
{
49+
$this->data = [];
50+
}
4651
}

src/Tracing/Tracer.php

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ class Tracer
1212

1313
public function __construct(SpanContext $context = null)
1414
{
15+
$this->reset($context);
16+
}
17+
18+
public function reset(SpanContext $context = null)
19+
{
20+
$this->spans = [];
21+
$this->tail = [];
1522
$context = $context ?: SpanContext::generate();
1623
$this->active = $this->generateSpanInstance('tracer', $context);
1724
}

tests/Metrics/OperationsTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@ public function testBasics(): void
3232

3333
$this->assertSame(2, $registry->get('request_counter', [ 'hostname' => 1 ]));
3434
$this->assertSame(42 * 2, $registry->get('request_timing', [ 'hostname' => 1 ]));
35+
36+
$operations->reset();
37+
$this->assertSame(0, $operations->count());
3538
}
3639
}

tests/Tracing/TracingTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@
1111

1212
class TracingTest extends TestCase
1313
{
14+
public function testReset()
15+
{
16+
$tracer = new Tracer();
17+
$this->assertCount(1, $tracer->getSpans());
18+
$active = $tracer->getActiveSpan();
19+
20+
$tracer->createSpan('a');
21+
$tracer->createSpan('b');
22+
$this->assertCount(3, $tracer->getSpans());
23+
24+
$tracer->reset();
25+
$this->assertCount(1, $tracer->getSpans());
26+
27+
$this->assertNotSame(
28+
$active->getSpanContext()->getSpanId(),
29+
$tracer->getActiveSpan()->getSpanContext()->getSpanId(),
30+
);
31+
}
32+
1433
public function testContextGenerationAndRestore()
1534
{
1635
$spanContext = SpanContext::generate();

0 commit comments

Comments
 (0)