|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sentry\Integration; |
| 6 | + |
| 7 | +use Psr\Log\LoggerInterface; |
| 8 | +use Psr\Log\NullLogger; |
| 9 | +use Sentry\Client; |
| 10 | +use Sentry\Options; |
| 11 | +use Sentry\SentrySdk; |
| 12 | +use Sentry\State\Scope; |
| 13 | +use Sentry\Util\Http; |
| 14 | + |
| 15 | +final class OTLPIntegration implements OptionAwareIntegrationInterface |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var bool |
| 19 | + */ |
| 20 | + private $setupOtlpTracesExporter; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var string|null |
| 24 | + */ |
| 25 | + private $collectorUrl; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Options|null |
| 29 | + */ |
| 30 | + private $options; |
| 31 | + |
| 32 | + public function __construct(bool $setupOtlpTracesExporter = true, ?string $collectorUrl = null) |
| 33 | + { |
| 34 | + $this->setupOtlpTracesExporter = $setupOtlpTracesExporter; |
| 35 | + $this->collectorUrl = $collectorUrl; |
| 36 | + } |
| 37 | + |
| 38 | + public function setOptions(Options $options): void |
| 39 | + { |
| 40 | + $this->options = $options; |
| 41 | + } |
| 42 | + |
| 43 | + public function setupOnce(): void |
| 44 | + { |
| 45 | + $options = $this->options; |
| 46 | + |
| 47 | + if ($options === null) { |
| 48 | + $this->logDebug('Skipping OTLPIntegration setup because client options were not provided.'); |
| 49 | + |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if ($options->isTracingEnabled()) { |
| 54 | + $this->logDebug('Skipping OTLPIntegration because Sentry tracing is enabled. Disable "traces_sample_rate", "traces_sampler", and "enable_tracing" before using OTLPIntegration.'); |
| 55 | + |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + Scope::registerExternalPropagationContext(static function (): ?array { |
| 60 | + $currentHub = SentrySdk::getCurrentHub(); |
| 61 | + $integration = $currentHub->getIntegration(self::class); |
| 62 | + |
| 63 | + if (!$integration instanceof self) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + return $integration->getCurrentOpenTelemetryPropagationContext(); |
| 68 | + }); |
| 69 | + |
| 70 | + if ($this->setupOtlpTracesExporter) { |
| 71 | + $this->configureOtlpTracesExporter($options); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public function getCollectorUrl(): ?string |
| 76 | + { |
| 77 | + return $this->collectorUrl; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return array{trace_id: string, span_id: string}|null |
| 82 | + */ |
| 83 | + private function getCurrentOpenTelemetryPropagationContext(): ?array |
| 84 | + { |
| 85 | + if (!class_exists(\OpenTelemetry\API\Trace\Span::class)) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + $spanContext = \OpenTelemetry\API\Trace\Span::getCurrent()->getContext(); |
| 90 | + |
| 91 | + if (!$spanContext->isValid()) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + return [ |
| 96 | + 'trace_id' => $spanContext->getTraceId(), |
| 97 | + 'span_id' => $spanContext->getSpanId(), |
| 98 | + ]; |
| 99 | + } |
| 100 | + |
| 101 | + private function configureOtlpTracesExporter(Options $options): void |
| 102 | + { |
| 103 | + $endpoint = $this->collectorUrl; |
| 104 | + $headers = []; |
| 105 | + $dsn = $options->getDsn(); |
| 106 | + |
| 107 | + if ($endpoint === null && $dsn !== null) { |
| 108 | + $endpoint = $dsn->getOtlpTracesEndpointUrl(); |
| 109 | + $headers['X-Sentry-Auth'] = Http::getSentryAuthHeader($dsn, Client::SDK_IDENTIFIER, Client::SDK_VERSION); |
| 110 | + } |
| 111 | + |
| 112 | + if ($endpoint === null) { |
| 113 | + $this->logDebug('Skipping automatic OTLP exporter setup because neither a DSN nor a collector URL is configured.'); |
| 114 | + |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + if (!$this->shouldConfigureOtlpTracesExporter()) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + try { |
| 123 | + $transport = (new \OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory())->create( |
| 124 | + $endpoint, |
| 125 | + \OpenTelemetry\Contrib\Otlp\ContentTypes::PROTOBUF, |
| 126 | + $headers |
| 127 | + ); |
| 128 | + $spanExporter = new \OpenTelemetry\Contrib\Otlp\SpanExporter($transport); |
| 129 | + $batchSpanProcessor = new \OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor( |
| 130 | + $spanExporter, |
| 131 | + \OpenTelemetry\API\Common\Time\Clock::getDefault() |
| 132 | + ); |
| 133 | + |
| 134 | + (new \OpenTelemetry\SDK\SdkBuilder()) |
| 135 | + ->setTracerProvider(new \OpenTelemetry\SDK\Trace\TracerProvider($batchSpanProcessor)) |
| 136 | + ->buildAndRegisterGlobal(); |
| 137 | + } catch (\Throwable $exception) { |
| 138 | + $this->logDebug(\sprintf('Skipping automatic OTLP exporter setup because it could not be configured: %s', $exception->getMessage())); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private function shouldConfigureOtlpTracesExporter(): bool |
| 143 | + { |
| 144 | + if (\PHP_VERSION_ID < 80100) { |
| 145 | + $this->logDebug('Skipping automatic OTLP exporter setup because it requires PHP 8.1 or newer.'); |
| 146 | + |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + foreach ([ |
| 151 | + \OpenTelemetry\API\Globals::class, |
| 152 | + \OpenTelemetry\API\Common\Time\Clock::class, |
| 153 | + \OpenTelemetry\SDK\SdkBuilder::class, |
| 154 | + \OpenTelemetry\SDK\Trace\TracerProvider::class, |
| 155 | + \OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor::class, |
| 156 | + \OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory::class, |
| 157 | + \OpenTelemetry\Contrib\Otlp\SpanExporter::class, |
| 158 | + ] as $className) { |
| 159 | + if (!class_exists($className)) { |
| 160 | + $this->logDebug('Skipping automatic OTLP exporter setup because the required OpenTelemetry SDK/exporter classes are not available.'); |
| 161 | + |
| 162 | + return false; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + try { |
| 167 | + if (!$this->isNoopTracerProvider(\OpenTelemetry\API\Globals::tracerProvider())) { |
| 168 | + $this->logDebug('Skipping automatic OTLP exporter setup because the existing OpenTelemetry tracer provider cannot be modified after construction.'); |
| 169 | + |
| 170 | + return false; |
| 171 | + } |
| 172 | + } catch (\Throwable $exception) { |
| 173 | + $this->logDebug(\sprintf('Skipping automatic OTLP exporter setup because the current OpenTelemetry tracer provider could not be inspected: %s', $exception->getMessage())); |
| 174 | + |
| 175 | + return false; |
| 176 | + } |
| 177 | + |
| 178 | + return true; |
| 179 | + } |
| 180 | + |
| 181 | + private function isNoopTracerProvider(?object $tracerProvider): bool |
| 182 | + { |
| 183 | + return $tracerProvider === null || $tracerProvider instanceof \OpenTelemetry\API\Trace\NoopTracerProvider; |
| 184 | + } |
| 185 | + |
| 186 | + private function logDebug(string $message): void |
| 187 | + { |
| 188 | + $this->getLogger()->debug($message); |
| 189 | + } |
| 190 | + |
| 191 | + private function getLogger(): LoggerInterface |
| 192 | + { |
| 193 | + if ($this->options !== null) { |
| 194 | + return $this->options->getLoggerOrNullLogger(); |
| 195 | + } |
| 196 | + |
| 197 | + $currentHub = SentrySdk::getCurrentHub(); |
| 198 | + $client = $currentHub->getClient(); |
| 199 | + |
| 200 | + if ($client !== null) { |
| 201 | + return $client->getOptions()->getLoggerOrNullLogger(); |
| 202 | + } |
| 203 | + |
| 204 | + return new NullLogger(); |
| 205 | + } |
| 206 | +} |
0 commit comments