From dadd91369a798779b9bd07f84cdb05f1ae211d92 Mon Sep 17 00:00:00 2001 From: Filip Benco Date: Tue, 30 Jul 2019 19:49:44 +0200 Subject: [PATCH] Use before_send callback to set opentracing data --- src/Client.php | 55 +++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/src/Client.php b/src/Client.php index 87b0c79..ab10d07 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,6 +2,7 @@ namespace Websupport\YiiSentry; +use Sentry\Event; use Sentry\Severity; use Sentry\State\Hub; use Sentry\State\Scope; @@ -66,9 +67,6 @@ class Client extends CApplicationComponent */ private $userContext = []; - /** @var \Websupport\OpenTracing\OpenTracing */ - private $opentracing; - /** * Initializes the SentryClient component. * @return void @@ -85,10 +83,6 @@ public function init() if ($this->isJsErrorReportingEnabled()) { $this->installJsErrorReporting(); } - - if ($this->opentracingId && Yii::app()->hasComponent($this->opentracingId)) { - $this->opentracing = Yii::app()->getComponent($this->opentracingId); - } } /** @@ -102,9 +96,6 @@ public function init() */ public function captureMessage(string $message, ?Severity $level = null, ?Scope $scope = null): ?string { - if ($scope !== null) { - $this->injectOpenTracingIntoScope($scope); - } return Hub::getCurrent()->getClient()->captureMessage($message, $level, $scope); } @@ -118,23 +109,9 @@ public function captureMessage(string $message, ?Severity $level = null, ?Scope */ public function captureException(\Throwable $exception, ?Scope $scope = null): ?string { - if ($scope !== null) { - $this->injectOpenTracingIntoScope($scope); - } return Hub::getCurrent()->getClient()->captureException($exception, $scope); } - private function injectOpenTracingIntoScope(Scope &$scope) - { - if ($this->opentracing) { - $spanContext = $this->opentracing->getTracer()->getActiveSpan()->getContext(); - if ($spanContext instanceof \Jaeger\SpanContext) { - $scope->setTag('opentracing.trace_id', $spanContext->getTraceId()); - $scope->setTag('opentracing.span_id', $spanContext->getSpanId()); - } - } - } - /** * Return the last captured event's ID or null if none available. * @@ -184,13 +161,41 @@ public function setUserContext($context) private function installPhpErrorReporting() : void { - \Sentry\init(array_merge(['dsn' => $this->dsn], $this->options)); + $options = $this->options; + + $this->injectOpentracingBeforeSendCallback($options); + + \Sentry\init(array_merge(['dsn' => $this->dsn], $options)); \Sentry\configureScope(function (Scope $scope): void { $scope->setUser($this->getInitialPhpUserContext()); }); } + private function injectOpentracingBeforeSendCallback(array &$options):void + { + if ($this->opentracingId && Yii::app()->hasComponent($this->opentracingId)) { + $opentracing = Yii::app()->getComponent($this->opentracingId); + $originalCallback = $options['before_send'] ?? null; + $options['before_send'] = function (Event $event) use($opentracing, $originalCallback): ?Event { + $spanContext = $opentracing->getTracer()->getActiveSpan()->getContext(); + + if ($spanContext instanceof \Jaeger\SpanContext) { + $event->getTagsContext()->setData([ + 'opentracing.trace_id' => $spanContext->getTraceId(), + 'opentracing.span_id' => $spanContext->getSpanId() + ]); + } + + if (is_callable($originalCallback)) { + return $originalCallback($event); + } + + return $event; + }; + } + } + private function getInitialPhpUserContext(): array { if (!function_exists('session_id') || !session_id()) {