-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathWithSpanHandlerTest.php
More file actions
101 lines (87 loc) · 2.98 KB
/
WithSpanHandlerTest.php
File metadata and controls
101 lines (87 loc) · 2.98 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
<?php
declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\API\Instrumentation;
use ArrayObject;
use OpenTelemetry\API\Instrumentation\Configurator;
use OpenTelemetry\API\Instrumentation\WithSpanHandler;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\Context\ScopeInterface;
use OpenTelemetry\SDK\Trace\ImmutableSpan;
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
#[CoversClass(WithSpanHandler::class)]
class WithSpanHandlerTest extends TestCase
{
private ScopeInterface $scope;
private ArrayObject $storage;
#[\Override]
public function setUp(): void
{
$this->storage = new ArrayObject();
$tracerProvider = new TracerProvider(
new SimpleSpanProcessor(
new InMemoryExporter($this->storage)
)
);
$this->scope = Configurator::create()
->withTracerProvider($tracerProvider)
->activate();
}
#[\Override]
public function tearDown(): void
{
$this->scope->detach();
}
public function test_creates_span_with_all_values(): void
{
$name = 'foo';
$kind = SpanKind::KIND_CLIENT;
$attributes = ['foo' => 'bar'];
WithSpanHandler::pre(
null,
[],
'My\\Class',
'some_function',
'a_file.php',
99,
['name' => $name, 'span_kind' => $kind],
$attributes,
);
$this->assertCount(0, $this->storage);
WithSpanHandler::post(null, [], null, null);
$this->assertCount(1, $this->storage);
/** @var ImmutableSpan $span */
$span = $this->storage->offsetGet(0);
$this->assertSame($name, $span->getName());
$this->assertSame($kind, $span->getKind());
$this->assertSame([
'code.function.name' => 'My\Class::some_function',
'code.file.path' => 'a_file.php',
'code.line.number' => 99,
'foo' => 'bar',
], $span->getAttributes()->toArray());
}
#[DataProvider('defaultsProvider')]
public function test_defaults(string $class, string $function, string $expected): void
{
$this->assertCount(0, $this->storage);
WithSpanHandler::pre(null, [], $class, $function, null, null, [], []);
WithSpanHandler::post(null, [], null, null);
$this->assertCount(1, $this->storage);
/** @var ImmutableSpan $span */
$span = $this->storage->offsetGet(0);
$this->assertSame($expected, $span->getName());
$this->assertSame(SpanKind::KIND_INTERNAL, $span->getKind());
}
public static function defaultsProvider(): array
{
return [
['My\\Class', 'foo', 'My\\Class::foo'],
['', 'foo', 'foo'],
];
}
}