1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Basis\Provider; |
4
|
|
|
|
5
|
|
|
use League\Container\ServiceProvider\AbstractServiceProvider; |
6
|
|
|
use OpenTelemetry\Exporter; |
7
|
|
|
use OpenTelemetry\Exporter\BasisExporter; |
8
|
|
|
use OpenTelemetry\Tracing\SpanContext; |
9
|
|
|
use OpenTelemetry\Tracing\Tracer; |
10
|
|
|
use OpenTelemetry\Transport; |
11
|
|
|
use OpenTelemetry\Transport\TarantoolQueueTransport; |
12
|
|
|
|
13
|
|
|
class OpenTelemetryProvider extends AbstractServiceProvider |
14
|
|
|
{ |
15
|
|
|
protected $provides = [ |
16
|
|
|
BasisExporter::class, |
17
|
|
|
Exporter::class, |
18
|
|
|
TarantoolQueueTransport::class, |
19
|
|
|
Tracer::class, |
20
|
|
|
Transport::class, |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
public function register() |
24
|
|
|
{ |
25
|
52 |
|
$this->container->share(BasisExporter::class, function () { |
|
|
|
|
26
|
|
|
return new BasisExporter(); |
27
|
52 |
|
}); |
28
|
|
|
|
29
|
52 |
|
$this->container->share(Exporter::class, function () { |
|
|
|
|
30
|
|
|
return $this->getContainer()->get(BasisExporter::class); |
31
|
52 |
|
}); |
32
|
|
|
|
33
|
52 |
|
$this->container->share(TarantoolQueueTransport::class, function () { |
|
|
|
|
34
|
|
|
return (new TarantoolQueueTransport()) |
35
|
|
|
->setQueue( |
36
|
|
|
$this->getContainer()->getQueue('audit.tracing') |
|
|
|
|
37
|
|
|
); |
38
|
52 |
|
}); |
39
|
|
|
|
40
|
52 |
|
$this->container->share(Tracer::class, function () { |
|
|
|
|
41
|
52 |
|
if (array_key_exists('rpc', $_REQUEST)) { |
42
|
|
|
$data = json_decode($_REQUEST['rpc']); |
43
|
|
|
if (property_exists($data, 'span')) { |
44
|
|
|
$traceId = $data->span->traceId; |
45
|
|
|
$spanId = $data->span->spanId; |
46
|
|
|
$context = SpanContext::restore($traceId, $spanId); |
47
|
|
|
return new Tracer($context); |
48
|
|
|
} |
49
|
|
|
} |
50
|
52 |
|
return new Tracer(); |
51
|
52 |
|
}); |
52
|
|
|
|
53
|
52 |
|
$this->container->share(Transport::class, function () { |
|
|
|
|
54
|
|
|
return $this->getContainer()->get(TarantoolQueueTransport::class); |
55
|
52 |
|
}); |
56
|
52 |
|
} |
57
|
|
|
} |
58
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: