Skip to content

Commit

Permalink
Use before_send callback to set opentracing data
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Benco committed Jul 30, 2019
1 parent 6b9c576 commit dadd913
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Websupport\YiiSentry;

use Sentry\Event;
use Sentry\Severity;
use Sentry\State\Hub;
use Sentry\State\Scope;
Expand Down Expand Up @@ -66,9 +67,6 @@ class Client extends CApplicationComponent
*/
private $userContext = [];

/** @var \Websupport\OpenTracing\OpenTracing */
private $opentracing;

/**
* Initializes the SentryClient component.
* @return void
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}

Expand All @@ -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.
*
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit dadd913

Please sign in to comment.