This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpSender.php
130 lines (106 loc) · 3.9 KB
/
HttpSender.php
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
namespace OpenTelemetry\Contrib\Jaeger;
use Jaeger\Thrift\Process;
use OpenTelemetry\Contrib\Jaeger\BatchAdapter\BatchAdapterFactory;
use OpenTelemetry\Contrib\Jaeger\BatchAdapter\BatchAdapterFactoryInterface;
use OpenTelemetry\Contrib\Jaeger\BatchAdapter\BatchAdapterInterface;
use OpenTelemetry\Contrib\Jaeger\TagFactory\TagFactory;
use OpenTelemetry\SDK\Behavior\LogsMessagesTrait;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SemConv\ResourceAttributes;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Protocol\TProtocol;
class HttpSender
{
use LogsMessagesTrait;
private TProtocol $protocol;
private BatchAdapterFactoryInterface $batchAdapterFactory;
private string $defaultServiceName;
public function __construct(
ClientInterface $client,
RequestFactoryInterface $requestFactory,
StreamFactoryInterface $streamFactory,
ParsedEndpointUrl $parsedEndpoint,
BatchAdapterFactoryInterface $batchAdapterFactory = null
) {
$this->protocol = new TBinaryProtocol(
new ThriftHttpTransport(
$client,
$requestFactory,
$streamFactory,
$parsedEndpoint
)
);
$this->batchAdapterFactory = $batchAdapterFactory ?? new BatchAdapterFactory();
$this->defaultServiceName = ResourceInfoFactory::defaultResource()->getAttributes()->get(ResourceAttributes::SERVICE_NAME);
}
public function send(iterable $spans): void
{
$batches = $this->createBatchesPerResource(
self::groupSpansByResource($spans)
);
foreach ($batches as $batch) {
$this->sendBatch($batch);
}
}
private static function groupSpansByResource(iterable $spans): array
{
$spansGroupedByResource = [];
foreach ($spans as $span) {
/** @var ResourceInfo */
$resource = $span->getResource();
$resourceAsKey = $resource->serialize();
if (!isset($spansGroupedByResource[$resourceAsKey])) {
$spansGroupedByResource[$resourceAsKey] = [
'spans' => [],
'resource' => $resource,
];
}
$spansGroupedByResource[$resourceAsKey]['spans'][] = $span;
}
return $spansGroupedByResource;
}
private function createBatchesPerResource(array $spansGroupedByResource): array
{
$batches = [];
foreach ($spansGroupedByResource as $unused => $dataForBatch) {
$batch = $this->batchAdapterFactory->create([
'spans' => (new SpanConverter())->convert(
$dataForBatch['spans']
),
'process' => $this->createProcessFromResource(
$dataForBatch['resource']
),
]);
$batches[] = $batch;
}
return $batches;
}
private function createProcessFromResource(ResourceInfo $resource): Process
{
//Defaulting to (what should be) the default resource's service name
$serviceName = $this->defaultServiceName;
$tags = [];
foreach ($resource->getAttributes() as $key => $value) {
if ($key === ResourceAttributes::SERVICE_NAME) {
$serviceName = (string) $value;
continue;
}
$tags[] = TagFactory::create($key, $value);
}
return new Process([
'serviceName' => $serviceName,
'tags' => $tags,
]);
}
private function sendBatch(BatchAdapterInterface $batch): void
{
$batch->write($this->protocol);
$this->protocol->getTransport()->flush();
}
}