-
-
Notifications
You must be signed in to change notification settings - Fork 466
feat(otel): add OTLP integration #2030
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5e56f27
feat(otel): add OTLP integration
Litarnus daa26ec
Merge branch 'master' into otlp-integration
Litarnus 91089e1
fix
Litarnus b274e34
Merge branch 'master' into otlp-integration
Litarnus 4612fea
Merge branch 'master' into otlp-integration
Litarnus aa64b36
fix
Litarnus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sentry\Integration; | ||
|
|
||
| use Psr\Log\LoggerInterface; | ||
| use Psr\Log\NullLogger; | ||
| use Sentry\Client; | ||
| use Sentry\Options; | ||
| use Sentry\SentrySdk; | ||
| use Sentry\State\Scope; | ||
| use Sentry\Util\Http; | ||
|
|
||
| final class OTLPIntegration implements OptionAwareIntegrationInterface | ||
| { | ||
| /** | ||
| * @var bool | ||
| */ | ||
| private $setupOtlpTracesExporter; | ||
|
|
||
| /** | ||
| * @var string|null | ||
| */ | ||
| private $collectorUrl; | ||
|
|
||
| /** | ||
| * @var Options|null | ||
| */ | ||
| private $options; | ||
|
|
||
| public function __construct(bool $setupOtlpTracesExporter = true, ?string $collectorUrl = null) | ||
| { | ||
| $this->setupOtlpTracesExporter = $setupOtlpTracesExporter; | ||
| $this->collectorUrl = $collectorUrl; | ||
| } | ||
|
|
||
| public function setOptions(Options $options): void | ||
| { | ||
| $this->options = $options; | ||
| } | ||
|
|
||
| public function setupOnce(): void | ||
| { | ||
| $options = $this->options; | ||
|
|
||
| if ($options === null) { | ||
| $this->logDebug('Skipping OTLPIntegration setup because client options were not provided.'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if ($options->isTracingEnabled()) { | ||
| $this->logDebug('Skipping OTLPIntegration because Sentry tracing is enabled. Disable "traces_sample_rate", "traces_sampler", and "enable_tracing" before using OTLPIntegration.'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| Scope::registerExternalPropagationContext(static function (): ?array { | ||
| $currentHub = SentrySdk::getCurrentHub(); | ||
| $integration = $currentHub->getIntegration(self::class); | ||
|
|
||
| if (!$integration instanceof self) { | ||
| return null; | ||
| } | ||
|
|
||
| return $integration->getCurrentOpenTelemetryPropagationContext(); | ||
| }); | ||
|
|
||
| if ($this->setupOtlpTracesExporter) { | ||
| $this->configureOtlpTracesExporter($options); | ||
| } | ||
| } | ||
|
|
||
| public function getCollectorUrl(): ?string | ||
| { | ||
| return $this->collectorUrl; | ||
| } | ||
|
|
||
| /** | ||
| * @return array{trace_id: string, span_id: string}|null | ||
| */ | ||
| private function getCurrentOpenTelemetryPropagationContext(): ?array | ||
| { | ||
| if (!class_exists(\OpenTelemetry\API\Trace\Span::class)) { | ||
| return null; | ||
| } | ||
|
|
||
| $spanContext = \OpenTelemetry\API\Trace\Span::getCurrent()->getContext(); | ||
|
|
||
| if (!$spanContext->isValid()) { | ||
| return null; | ||
| } | ||
|
|
||
| return [ | ||
| 'trace_id' => $spanContext->getTraceId(), | ||
| 'span_id' => $spanContext->getSpanId(), | ||
| ]; | ||
| } | ||
|
|
||
| private function configureOtlpTracesExporter(Options $options): void | ||
| { | ||
| $endpoint = $this->collectorUrl; | ||
| $headers = []; | ||
| $dsn = $options->getDsn(); | ||
|
|
||
| if ($endpoint === null && $dsn !== null) { | ||
| $endpoint = $dsn->getOtlpTracesEndpointUrl(); | ||
| $headers['X-Sentry-Auth'] = Http::getSentryAuthHeader($dsn, Client::SDK_IDENTIFIER, Client::SDK_VERSION); | ||
| } | ||
|
|
||
| if ($endpoint === null) { | ||
| $this->logDebug('Skipping automatic OTLP exporter setup because neither a DSN nor a collector URL is configured.'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (!$this->shouldConfigureOtlpTracesExporter()) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $transport = (new \OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory())->create( | ||
| $endpoint, | ||
| \OpenTelemetry\Contrib\Otlp\ContentTypes::PROTOBUF, | ||
| $headers | ||
| ); | ||
| $spanExporter = new \OpenTelemetry\Contrib\Otlp\SpanExporter($transport); | ||
| $batchSpanProcessor = new \OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor( | ||
| $spanExporter, | ||
| \OpenTelemetry\API\Common\Time\Clock::getDefault() | ||
| ); | ||
|
|
||
| (new \OpenTelemetry\SDK\SdkBuilder()) | ||
| ->setTracerProvider(new \OpenTelemetry\SDK\Trace\TracerProvider($batchSpanProcessor)) | ||
| ->buildAndRegisterGlobal(); | ||
| } catch (\Throwable $exception) { | ||
| $this->logDebug(\sprintf('Skipping automatic OTLP exporter setup because it could not be configured: %s', $exception->getMessage())); | ||
| } | ||
| } | ||
|
|
||
| private function shouldConfigureOtlpTracesExporter(): bool | ||
| { | ||
| if (\PHP_VERSION_ID < 80100) { | ||
| $this->logDebug('Skipping automatic OTLP exporter setup because it requires PHP 8.1 or newer.'); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| foreach ([ | ||
| \OpenTelemetry\API\Globals::class, | ||
| \OpenTelemetry\API\Common\Time\Clock::class, | ||
| \OpenTelemetry\SDK\SdkBuilder::class, | ||
| \OpenTelemetry\SDK\Trace\TracerProvider::class, | ||
| \OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor::class, | ||
| \OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory::class, | ||
| \OpenTelemetry\Contrib\Otlp\SpanExporter::class, | ||
| ] as $className) { | ||
| if (!class_exists($className)) { | ||
| $this->logDebug('Skipping automatic OTLP exporter setup because the required OpenTelemetry SDK/exporter classes are not available.'); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| if (!$this->isNoopTracerProvider(\OpenTelemetry\API\Globals::tracerProvider())) { | ||
| $this->logDebug('Skipping automatic OTLP exporter setup because the existing OpenTelemetry tracer provider cannot be modified after construction.'); | ||
|
|
||
| return false; | ||
| } | ||
| } catch (\Throwable $exception) { | ||
| $this->logDebug(\sprintf('Skipping automatic OTLP exporter setup because the current OpenTelemetry tracer provider could not be inspected: %s', $exception->getMessage())); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private function isNoopTracerProvider(?object $tracerProvider): bool | ||
| { | ||
| return $tracerProvider === null || $tracerProvider instanceof \OpenTelemetry\API\Trace\NoopTracerProvider; | ||
| } | ||
|
|
||
| private function logDebug(string $message): void | ||
| { | ||
| $this->getLogger()->debug($message); | ||
| } | ||
|
|
||
| private function getLogger(): LoggerInterface | ||
| { | ||
| if ($this->options !== null) { | ||
| return $this->options->getLoggerOrNullLogger(); | ||
| } | ||
|
|
||
| $currentHub = SentrySdk::getCurrentHub(); | ||
| $client = $currentHub->getClient(); | ||
|
|
||
| if ($client !== null) { | ||
| return $client->getOptions()->getLoggerOrNullLogger(); | ||
| } | ||
|
|
||
| return new NullLogger(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.