forked from open-telemetry/opentelemetry-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleInstrumentation.php
More file actions
52 lines (41 loc) · 1.62 KB
/
ExampleInstrumentation.php
File metadata and controls
52 lines (41 loc) · 1.62 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
<?php
declare(strict_types=1);
namespace OpenTelemetry\Example;
use Exception;
use OpenTelemetry\API\Globals;
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\ConfigurationRegistry;
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager;
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Instrumentation;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\Context\ContextStorageInterface;
final class ExampleInstrumentation implements Instrumentation
{
public function register(HookManager $hookManager, ConfigurationRegistry $configuration, ContextStorageInterface $storage): void
{
$config = $configuration->get(ExampleConfig::class) ?? throw new Exception('example instrumentation must be configured');
if (!$config->enabled) {
return;
}
$tracer = Globals::tracerProvider()->getTracer('example-instrumentation');
$hookManager->hook(
Example::class,
'test',
static function () use ($tracer, $config, $storage): void {
$context = $storage->current();
$span = $tracer
->spanBuilder($config->spanName)
->setParent($context)
->startSpan();
$storage->attach($span->storeInContext($context));
},
static function () use ($storage): void {
if (!$scope = $storage->scope()) {
return;
}
$scope->detach();
$span = Span::fromContext($scope->context());
$span->end();
}
);
}
}