Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Integration/EnvironmentIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class EnvironmentIntegration implements IntegrationInterface
public function setupOnce(): void
{
Scope::addGlobalEventProcessor(static function (Event $event): Event {
$integration = SentrySdk::getCurrentHub()->getIntegration(self::class);
$integration = SentrySdk::getClient()->getIntegration(self::class);

if ($integration !== null) {
$event->setRuntimeContext($integration->updateRuntimeContext($event->getRuntimeContext()));
Expand Down
2 changes: 1 addition & 1 deletion src/Integration/FrameContextifierIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(?LoggerInterface $logger = null)
public function setupOnce(): void
{
Scope::addGlobalEventProcessor(static function (Event $event): Event {
$client = SentrySdk::getCurrentHub()->getClient();
$client = SentrySdk::getClient();

$maxContextLines = $client->getOptions()->getContextLines();
$integration = $client->getIntegration(self::class);
Expand Down
2 changes: 1 addition & 1 deletion src/Integration/ModulesIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class ModulesIntegration implements IntegrationInterface
public function setupOnce(): void
{
Scope::addGlobalEventProcessor(static function (Event $event): Event {
$integration = SentrySdk::getCurrentHub()->getIntegration(self::class);
$integration = SentrySdk::getClient()->getIntegration(self::class);

// The integration could be bound to a client that is not the one
// attached to the current hub. If this is the case, bail out
Expand Down
8 changes: 2 additions & 6 deletions src/Integration/OTLPIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public function setupOnce(): void
}

Scope::registerExternalPropagationContext(static function (): ?array {
$currentHub = SentrySdk::getCurrentHub();
$integration = $currentHub->getIntegration(self::class);
$integration = SentrySdk::getClient()->getIntegration(self::class);

if (!$integration instanceof self) {
return null;
Expand Down Expand Up @@ -193,9 +192,6 @@ private function getLogger(): LoggerInterface
return $this->options->getLoggerOrNullLogger();
}

$currentHub = SentrySdk::getCurrentHub();
$client = $currentHub->getClient();

return $client->getOptions()->getLoggerOrNullLogger();
return SentrySdk::getClient()->getOptions()->getLoggerOrNullLogger();
}
}
6 changes: 2 additions & 4 deletions src/Integration/RequestIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,13 @@ public function __construct(?RequestFetcherInterface $requestFetcher = null, arr
public function setupOnce(): void
{
Scope::addGlobalEventProcessor(function (Event $event): Event {
$currentHub = SentrySdk::getCurrentHub();
$integration = $currentHub->getIntegration(self::class);
$client = SentrySdk::getClient();
$integration = $client->getIntegration(self::class);

if ($integration === null) {
return $event;
}

$client = $currentHub->getClient();

$this->processEvent($event, $client->getOptions());

return $event;
Expand Down
2 changes: 1 addition & 1 deletion src/Integration/TransactionIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class TransactionIntegration implements IntegrationInterface
public function setupOnce(): void
{
Scope::addGlobalEventProcessor(static function (Event $event, EventHint $hint): Event {
$integration = SentrySdk::getCurrentHub()->getIntegration(self::class);
$integration = SentrySdk::getClient()->getIntegration(self::class);

// The client bound to the current hub, if any, could not have this
// integration enabled. If this is the case, bail out
Expand Down
66 changes: 27 additions & 39 deletions src/Logs/LogsAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Sentry\Event;
use Sentry\EventId;
use Sentry\SentrySdk;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Sentry\Util\Arr;
use Sentry\Util\Str;
Expand Down Expand Up @@ -41,8 +40,9 @@ public function add(
): void {
$timestamp = microtime(true);

$hub = SentrySdk::getCurrentHub();
$client = $hub->getClient();
$isolationScope = SentrySdk::getIsolationScope();
$client = SentrySdk::getClient($isolationScope);
$scope = Scope::mergeScopes(SentrySdk::getGlobalScope(), $isolationScope);

$options = $client->getOptions();
$sdkLogger = $options->getLogger();
Expand Down Expand Up @@ -71,7 +71,7 @@ public function add(
$formattedMessage = $message;
}

$traceData = $this->getTraceData($hub);
$traceData = $this->getTraceData($scope);
$traceId = $traceData['trace_id'];
$parentSpanId = $traceData['parent_span_id'];

Expand All @@ -86,20 +86,18 @@ public function add(
$log->setAttribute('sentry.sdk.version', $client->getSdkVersion());
}

$hub->configureScope(static function (Scope $scope) use ($log) {
$user = $scope->getUser();
if ($user !== null) {
if ($user->getId() !== null) {
$log->setAttribute('user.id', $user->getId());
}
if ($user->getEmail() !== null) {
$log->setAttribute('user.email', $user->getEmail());
}
if ($user->getUsername() !== null) {
$log->setAttribute('user.name', $user->getUsername());
}
$user = $scope->getUser();
if ($user !== null) {
if ($user->getId() !== null) {
$log->setAttribute('user.id', $user->getId());
}
if ($user->getEmail() !== null) {
$log->setAttribute('user.email', $user->getEmail());
}
});
if ($user->getUsername() !== null) {
$log->setAttribute('user.name', $user->getUsername());
}
}

if (\count($values)) {
$log->setAttribute('sentry.message.template', $message);
Expand Down Expand Up @@ -190,9 +188,9 @@ public function all(): array
/**
* @return array{trace_id: string, parent_span_id: string|null}
*/
private function getTraceData(HubInterface $hub): array
private function getTraceData(Scope $scope): array
{
$span = $hub->getSpan();
$span = $scope->getSpan();

if ($span !== null) {
return [
Expand All @@ -201,28 +199,18 @@ private function getTraceData(HubInterface $hub): array
];
}

$traceData = null;

$hub->configureScope(static function (Scope $scope) use (&$traceData): void {
$externalPropagationContext = Scope::getExternalPropagationContext();

if ($externalPropagationContext !== null) {
$traceData = [
'trace_id' => $externalPropagationContext['trace_id'],
'parent_span_id' => $externalPropagationContext['span_id'],
];

return;
}

$traceData = [
'trace_id' => (string) $scope->getPropagationContext()->getTraceId(),
'parent_span_id' => null,
$externalPropagationContext = Scope::getExternalPropagationContext();
if ($externalPropagationContext !== null) {
return [
'trace_id' => $externalPropagationContext['trace_id'],
'parent_span_id' => $externalPropagationContext['span_id'],
];
});
}

/** @var array{trace_id: string, parent_span_id: string|null} $traceData */
return $traceData;
return [
'trace_id' => (string) $scope->getPropagationContext()->getTraceId(),
'parent_span_id' => null,
];
}

/**
Expand Down
98 changes: 43 additions & 55 deletions src/Metrics/MetricsAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Sentry\Metrics\Types\GaugeMetric;
use Sentry\Metrics\Types\Metric;
use Sentry\SentrySdk;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Sentry\Tracing\SpanId;
use Sentry\Tracing\TraceId;
Expand Down Expand Up @@ -52,73 +51,64 @@ public function add(
array $attributes,
?Unit $unit
): void {
$hub = SentrySdk::getCurrentHub();
$client = $hub->getClient();
$metricFlushThreshold = null;
$isolationScope = SentrySdk::getIsolationScope();
$client = SentrySdk::getClient($isolationScope);
$scope = Scope::mergeScopes(SentrySdk::getGlobalScope(), $isolationScope);
$options = $client->getOptions();
$metricFlushThreshold = $options->getMetricFlushThreshold();

if (!\is_int($value) && !\is_float($value)) {
if ($client !== null) {
$client->getOptions()->getLoggerOrNullLogger()->debug('Metrics value is neither int nor float. Metric will be discarded');
}
$options->getLoggerOrNullLogger()->debug('Metrics value is neither int nor float. Metric will be discarded');

return;
}

if ($client !== null) {
$options = $client->getOptions();
$metricFlushThreshold = $options->getMetricFlushThreshold();
if ($options->getEnableMetrics() === false) {
return;
}

if ($options->getEnableMetrics() === false) {
return;
}
$defaultAttributes = [
'sentry.environment' => $options->getEnvironment() ?? Event::DEFAULT_ENVIRONMENT,
'server.address' => $options->getServerName(),
];

$defaultAttributes = [
'sentry.environment' => $options->getEnvironment() ?? Event::DEFAULT_ENVIRONMENT,
'server.address' => $options->getServerName(),
];
if ($client instanceof Client) {
$defaultAttributes['sentry.sdk.name'] = $client->getSdkIdentifier();
$defaultAttributes['sentry.sdk.version'] = $client->getSdkVersion();
}

if ($client instanceof Client) {
$defaultAttributes['sentry.sdk.name'] = $client->getSdkIdentifier();
$defaultAttributes['sentry.sdk.version'] = $client->getSdkVersion();
$user = $scope->getUser();
if ($user !== null) {
if ($user->getId() !== null) {
$defaultAttributes['user.id'] = $user->getId();
}

$hub->configureScope(static function (Scope $scope) use (&$defaultAttributes) {
$user = $scope->getUser();
if ($user !== null) {
if ($user->getId() !== null) {
$defaultAttributes['user.id'] = $user->getId();
}
if ($user->getEmail() !== null) {
$defaultAttributes['user.email'] = $user->getEmail();
}
if ($user->getUsername() !== null) {
$defaultAttributes['user.name'] = $user->getUsername();
}
}
});

$release = $options->getRelease();
if ($release !== null) {
$defaultAttributes['sentry.release'] = $release;
if ($user->getEmail() !== null) {
$defaultAttributes['user.email'] = $user->getEmail();
}
if ($user->getUsername() !== null) {
$defaultAttributes['user.name'] = $user->getUsername();
}
}

$attributes += $defaultAttributes;
$release = $options->getRelease();
if ($release !== null) {
$defaultAttributes['sentry.release'] = $release;
}

$traceContext = $this->getTraceContext($hub);
$attributes += $defaultAttributes;

$traceContext = $this->getTraceContext($scope);
$traceId = new TraceId($traceContext['trace_id']);
$spanId = new SpanId($traceContext['span_id']);

$metricTypeClass = self::METRIC_TYPES[$type];
/** @var Metric $metric */
$metric = new $metricTypeClass($name, $value, $traceId, $spanId, $attributes, microtime(true), $unit);

if ($client !== null) {
$beforeSendMetric = $client->getOptions()->getBeforeSendMetricCallback();
$metric = $beforeSendMetric($metric);
if ($metric === null) {
return;
}
$beforeSendMetric = $options->getBeforeSendMetricCallback();
$metric = $beforeSendMetric($metric);
if ($metric === null) {
return;
}

$metrics = $this->getStorage($metricFlushThreshold);
Expand Down Expand Up @@ -147,16 +137,14 @@ public function flush(?ClientInterface $client = null, ?Scope $isolationScope =
/**
* @return array{trace_id: string, span_id: string}
*/
private function getTraceContext(HubInterface $hub): array
private function getTraceContext(Scope $scope): array
{
$traceContext = null;

$hub->configureScope(static function (Scope $scope) use (&$traceContext): void {
$traceContext = $scope->getTraceContext();
});
$traceContext = $scope->getTraceContext();

/** @var array{trace_id: string, span_id: string} $traceContext */
return $traceContext;
return [
'trace_id' => $traceContext['trace_id'],
'span_id' => $traceContext['span_id'],
];
}

/**
Expand Down
11 changes: 4 additions & 7 deletions src/Tracing/PropagationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Sentry\Tracing;

use Sentry\SentrySdk;
use Sentry\State\Scope;
use Sentry\Tracing\Traits\TraceHeaderParserTrait;

final class PropagationContext
Expand Down Expand Up @@ -84,12 +83,10 @@ public function toTraceparent(): string
public function toBaggage(): string
{
if ($this->dynamicSamplingContext === null) {
$hub = SentrySdk::getCurrentHub();
$options = $hub->getClient()->getOptions();

$hub->configureScope(function (Scope $scope) use ($options) {
$this->dynamicSamplingContext = DynamicSamplingContext::fromOptions($options, $scope);
});
$this->dynamicSamplingContext = DynamicSamplingContext::fromOptions(
SentrySdk::getClient()->getOptions(),
SentrySdk::getIsolationScope()
);
}

return (string) $this->dynamicSamplingContext;
Expand Down
9 changes: 3 additions & 6 deletions src/Tracing/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Sentry\EventId;
use Sentry\SentrySdk;
use Sentry\State\Scope;

/**
* This class stores all the information about a span.
Expand Down Expand Up @@ -299,11 +298,9 @@ public function setStatus(?SpanStatus $status)
*/
public function setHttpStatus(int $statusCode)
{
SentrySdk::getCurrentHub()->configureScope(static function (Scope $scope) use ($statusCode) {
$scope->setContext('response', [
'status_code' => $statusCode,
]);
});
SentrySdk::getIsolationScope()->setContext('response', [
'status_code' => $statusCode,
]);

$status = SpanStatus::createFromHttpStatusCode($statusCode);

Expand Down
6 changes: 2 additions & 4 deletions src/Tracing/Traits/TraceHeaderParserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ private static function parseSampleRand(DynamicSamplingContext $samplingContext)
}
}

$hub = SentrySdk::getCurrentHub();
$client = $hub->getClient();
$client = SentrySdk::getClient();
$client->getOptions()->getLoggerOrNullLogger()->debug(
'Ignoring invalid sentry-sample_rand baggage value because it must be a numeric value in the range [0, 1).',
['sample_rand' => $sampleRand]
Expand All @@ -139,8 +138,7 @@ private static function parseSampleRand(DynamicSamplingContext $samplingContext)

private static function shouldContinueTrace(DynamicSamplingContext $samplingContext): bool
{
$hub = SentrySdk::getCurrentHub();
$client = $hub->getClient();
$client = SentrySdk::getClient();

$options = $client->getOptions();
$clientOrgId = $options->getOrgId();
Expand Down
Loading
Loading