-
Notifications
You must be signed in to change notification settings - Fork 171
/
SpanExporterFactory.php
94 lines (77 loc) · 3.32 KB
/
SpanExporterFactory.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
<?php
declare(strict_types=1);
namespace OpenTelemetry\Contrib\Otlp;
use OpenTelemetry\API\Behavior\LogsMessagesTrait;
use OpenTelemetry\API\Signals;
use OpenTelemetry\SDK\Common\Configuration\Configuration;
use OpenTelemetry\SDK\Common\Configuration\Defaults;
use OpenTelemetry\SDK\Common\Configuration\Variables;
use OpenTelemetry\SDK\Common\Export\TransportFactoryInterface;
use OpenTelemetry\SDK\Common\Export\TransportInterface;
use OpenTelemetry\SDK\Registry;
use OpenTelemetry\SDK\Trace\SpanExporter\SpanExporterFactoryInterface;
use OpenTelemetry\SDK\Trace\SpanExporterInterface;
class SpanExporterFactory implements SpanExporterFactoryInterface
{
use LogsMessagesTrait;
private const DEFAULT_COMPRESSION = 'none';
public function __construct(private readonly ?TransportFactoryInterface $transportFactory = null)
{
}
/**
* @psalm-suppress ArgumentTypeCoercion
*/
public function create(): SpanExporterInterface
{
$transport = $this->buildTransport();
return new SpanExporter($transport);
}
/**
* @psalm-suppress ArgumentTypeCoercion
* @psalm-suppress UndefinedClass
*/
private function buildTransport(): TransportInterface
{
$protocol = $this->getProtocol();
$contentType = Protocols::contentType($protocol);
$endpoint = $this->getEndpoint($protocol);
$headers = OtlpUtil::getHeaders(Signals::TRACE);
$compression = $this->getCompression();
$timeout = $this->getTimeout();
$factoryClass = Registry::transportFactory($protocol);
$factory = $this->transportFactory ?: new $factoryClass();
return $factory->create($endpoint, $contentType, $headers, $compression, $timeout);
}
private function getProtocol(): string
{
return Configuration::has(Variables::OTEL_EXPORTER_OTLP_TRACES_PROTOCOL) ?
Configuration::getEnum(Variables::OTEL_EXPORTER_OTLP_TRACES_PROTOCOL) :
Configuration::getEnum(Variables::OTEL_EXPORTER_OTLP_PROTOCOL);
}
private function getEndpoint(string $protocol): string
{
if (Configuration::has(Variables::OTEL_EXPORTER_OTLP_TRACES_ENDPOINT)) {
return Configuration::getString(Variables::OTEL_EXPORTER_OTLP_TRACES_ENDPOINT);
}
$endpoint = Configuration::has(Variables::OTEL_EXPORTER_OTLP_ENDPOINT)
? Configuration::getString(Variables::OTEL_EXPORTER_OTLP_ENDPOINT)
: Defaults::OTEL_EXPORTER_OTLP_ENDPOINT;
if ($protocol === Protocols::GRPC) {
return $endpoint . OtlpUtil::method(Signals::TRACE);
}
return HttpEndpointResolver::create()->resolveToString($endpoint, Signals::TRACE);
}
private function getCompression(): string
{
return Configuration::has(Variables::OTEL_EXPORTER_OTLP_TRACES_COMPRESSION) ?
Configuration::getEnum(Variables::OTEL_EXPORTER_OTLP_TRACES_COMPRESSION) :
Configuration::getEnum(Variables::OTEL_EXPORTER_OTLP_COMPRESSION, self::DEFAULT_COMPRESSION);
}
private function getTimeout(): float
{
$value = Configuration::has(Variables::OTEL_EXPORTER_OTLP_TRACES_TIMEOUT) ?
Configuration::getInt(Variables::OTEL_EXPORTER_OTLP_TRACES_TIMEOUT) :
Configuration::getInt(Variables::OTEL_EXPORTER_OTLP_TIMEOUT);
return $value/1000;
}
}