Skip to content
Draft
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"suggest": {
"ext-excimer": "Enable Sentry profiling with the Excimer PHP extension.",
"ext-sentry": "Enable automatic function and method tracing with the Sentry PHP tracer extension.",
"monolog/monolog": "Allow sending log messages to Sentry by using the included Monolog handler."
},
"conflict": {
Expand Down
23 changes: 23 additions & 0 deletions src/Integration/FunctionTracingIntegration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Sentry\Integration;

use Sentry\Tracing\FunctionTracingCallbacks;

/**
* Registers the Sentry PHP tracer extension callbacks.
*/
final class FunctionTracingIntegration implements IntegrationInterface
{
public function setupOnce(): void
{
if (!\function_exists('Sentry\\setStartCallback') || !\function_exists('Sentry\\setEndCallback')) {
return;
}

\call_user_func('Sentry\\setStartCallback', [FunctionTracingCallbacks::class, 'handleStart']);
\call_user_func('Sentry\\setEndCallback', [FunctionTracingCallbacks::class, 'handleEnd']);
}
}
4 changes: 4 additions & 0 deletions src/Integration/IntegrationRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ private function getDefaultIntegrations(Options $options): array
new ModulesIntegration(),
];

if (\function_exists('Sentry\\setStartCallback') && \function_exists('Sentry\\setEndCallback')) {
$integrations[] = new FunctionTracingIntegration();
}

if ($options->getDsn() !== null || $options->isSpotlightEnabled()) {
array_unshift($integrations, new ExceptionListenerIntegration(), new ErrorListenerIntegration(), new FatalErrorListenerIntegration());
}
Expand Down
117 changes: 117 additions & 0 deletions src/Tracing/FunctionTracingCallbacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

declare(strict_types=1);

namespace Sentry\Tracing;

use Sentry\Integration\FunctionTracingIntegration;
use Sentry\SentrySdk;

/**
* @internal
*/
final class FunctionTracingCallbacks
{
private function __construct()
{
}

/**
* @param array<string, mixed> $data
*
* @return array{Span, Span}|null
*/
public static function handleStart(array $data): ?array
{
$hub = SentrySdk::getCurrentHub();

if (!$hub->getIntegration(FunctionTracingIntegration::class) instanceof FunctionTracingIntegration) {
return null;
}

$parentSpan = $hub->getSpan();
if ($parentSpan === null || $parentSpan->getSampled() !== true) {
return null;
}

if (!isset($data['name']) || !\is_string($data['name'])) {
return null;
}

$context = self::createSpanContext($data['name'], $data);
$childSpan = $parentSpan->startChild($context);

$hub->setSpan($childSpan);

return [$childSpan, $parentSpan];
}

/**
* @param array<string, mixed> $data
* @param mixed $callbackState
*/
public static function handleEnd(array $data, $callbackState = null): void
{
if (!\is_array($callbackState)
|| !\array_key_exists(0, $callbackState)
|| !\array_key_exists(1, $callbackState)
|| !$callbackState[0] instanceof Span
|| !$callbackState[1] instanceof Span
) {
return;
}

if (!isset($data['end_time']) || (!\is_int($data['end_time']) && !\is_float($data['end_time']))) {
return;
}

$callbackState[0]->finish((float) $data['end_time']);
SentrySdk::getCurrentHub()->setSpan($callbackState[1]);
}

/**
* @param array<string, mixed> $data
*/
private static function createSpanContext(string $name, array $data): SpanContext
{
$metadata = [];
if (isset($data['metadata']) && \is_array($data['metadata'])) {
foreach (array_keys($data['metadata']) as $key) {
if (\is_string($key)) {
$metadata[$key] = $data['metadata'][$key];
}
}
}

$description = $name;
$op = 'function';
$origin = 'auto.function.sentry_php_tracer';

if (isset($metadata['sentry.description']) && \is_string($metadata['sentry.description'])) {
$description = $metadata['sentry.description'];
unset($metadata['sentry.description']);
}

if (isset($metadata['sentry.op']) && \is_string($metadata['sentry.op'])) {
$op = $metadata['sentry.op'];
unset($metadata['sentry.op']);
}

if (isset($metadata['sentry.origin']) && \is_string($metadata['sentry.origin'])) {
$origin = $metadata['sentry.origin'];
unset($metadata['sentry.origin']);
}

$context = SpanContext::make()
->setDescription($description)
->setOp($op)
->setOrigin($origin)
->setData($metadata);

if (isset($data['start_time']) && (\is_int($data['start_time']) || \is_float($data['start_time']))) {
$context->setStartTimestamp((float) $data['start_time']);
}

return $context;
}
}
36 changes: 36 additions & 0 deletions stubs/SentryTracer.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Sentry {
/**
* @param array<string, mixed> $extra_metadata
*/
function instrument(?string $class_name, string $function_name, array $extra_metadata = []): bool
{
}

/**
* @phpstan-param callable(array{name: string, start_time: float, end_time: float, duration: float, metadata: array<string, mixed>}, mixed): mixed $callback
*/
function setEndCallback(callable $callback): bool
{
}

/**
* @phpstan-param callable(array{name: string, start_time: float, metadata: array<string, mixed>}): mixed $callback
*/
function setStartCallback(callable $callback): bool
{
}

final class Trace
{
/**
* @param array<string, mixed> $metadata
*/
public function __construct(array $metadata = [])
{
}
}
}
16 changes: 9 additions & 7 deletions stubs/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

declare(strict_types=1);

if (extension_loaded('excimer')) {
return;
if (!extension_loaded('excimer')) {
require_once __DIR__ . '/ExcimerLog.stub';
require_once __DIR__ . '/ExcimerLogEntry.stub';
require_once __DIR__ . '/ExcimerProfiler.stub';
require_once __DIR__ . '/ExcimerTimer.stub';
require_once __DIR__ . '/globals.stub';
}

require_once __DIR__ . '/ExcimerLog.stub';
require_once __DIR__ . '/ExcimerLogEntry.stub';
require_once __DIR__ . '/ExcimerProfiler.stub';
require_once __DIR__ . '/ExcimerTimer.stub';
require_once __DIR__ . '/globals.stub';
if (!function_exists('Sentry\\instrument')) {
require_once __DIR__ . '/SentryTracer.stub';
}
118 changes: 65 additions & 53 deletions tests/Integration/IntegrationRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Sentry\Integration\ExceptionListenerIntegration;
use Sentry\Integration\FatalErrorListenerIntegration;
use Sentry\Integration\FrameContextifierIntegration;
use Sentry\Integration\FunctionTracingIntegration;
use Sentry\Integration\IntegrationInterface;
use Sentry\Integration\IntegrationRegistry;
use Sentry\Integration\ModulesIntegration;
Expand Down Expand Up @@ -77,16 +78,7 @@ public function setupOnce(): void
'dsn' => 'http://public@example.com/sentry/1',
'default_integrations' => true,
]),
[
ExceptionListenerIntegration::class => new ExceptionListenerIntegration(),
ErrorListenerIntegration::class => ErrorListenerIntegration::make($options),
FatalErrorListenerIntegration::class => new FatalErrorListenerIntegration(),
RequestIntegration::class => new RequestIntegration(),
TransactionIntegration::class => new TransactionIntegration(),
FrameContextifierIntegration::class => new FrameContextifierIntegration(),
EnvironmentIntegration::class => new EnvironmentIntegration(),
ModulesIntegration::class => new ModulesIntegration(),
],
self::getExpectedDefaultIntegrations($options, true),
];

yield 'No default integrations and some user integrations' => [
Expand All @@ -112,15 +104,7 @@ public function setupOnce(): void
$integration2,
],
]),
[
ExceptionListenerIntegration::class => new ExceptionListenerIntegration(),
ErrorListenerIntegration::class => ErrorListenerIntegration::make($options),
FatalErrorListenerIntegration::class => new FatalErrorListenerIntegration(),
RequestIntegration::class => new RequestIntegration(),
TransactionIntegration::class => new TransactionIntegration(),
FrameContextifierIntegration::class => new FrameContextifierIntegration(),
EnvironmentIntegration::class => new EnvironmentIntegration(),
ModulesIntegration::class => new ModulesIntegration(),
self::getExpectedDefaultIntegrations($options, true) + [
$integration1ClassName => $integration1,
$integration2ClassName => $integration2,
],
Expand All @@ -135,14 +119,7 @@ public function setupOnce(): void
$integration1,
],
]),
[
ExceptionListenerIntegration::class => new ExceptionListenerIntegration(),
ErrorListenerIntegration::class => ErrorListenerIntegration::make($options),
FatalErrorListenerIntegration::class => new FatalErrorListenerIntegration(),
RequestIntegration::class => new RequestIntegration(),
FrameContextifierIntegration::class => new FrameContextifierIntegration(),
EnvironmentIntegration::class => new EnvironmentIntegration(),
ModulesIntegration::class => new ModulesIntegration(),
self::getExpectedDefaultIntegrations($options, true, [TransactionIntegration::class]) + [
TransactionIntegration::class => new TransactionIntegration(),
$integration1ClassName => $integration1,
],
Expand All @@ -156,14 +133,7 @@ public function setupOnce(): void
new ModulesIntegration(),
],
]),
[
ExceptionListenerIntegration::class => new ExceptionListenerIntegration(),
ErrorListenerIntegration::class => ErrorListenerIntegration::make($options),
FatalErrorListenerIntegration::class => new FatalErrorListenerIntegration(),
RequestIntegration::class => new RequestIntegration(),
TransactionIntegration::class => new TransactionIntegration(),
FrameContextifierIntegration::class => new FrameContextifierIntegration(),
EnvironmentIntegration::class => new EnvironmentIntegration(),
self::getExpectedDefaultIntegrations($options, true, [ModulesIntegration::class]) + [
ModulesIntegration::class => new ModulesIntegration(),
],
];
Expand Down Expand Up @@ -199,34 +169,76 @@ public function setupOnce(): void
return $defaultIntegrations;
},
]),
[
ExceptionListenerIntegration::class => new ExceptionListenerIntegration(),
ErrorListenerIntegration::class => ErrorListenerIntegration::make($options),
FatalErrorListenerIntegration::class => new FatalErrorListenerIntegration(),
RequestIntegration::class => new RequestIntegration(),
TransactionIntegration::class => new TransactionIntegration(),
FrameContextifierIntegration::class => new FrameContextifierIntegration(),
EnvironmentIntegration::class => new EnvironmentIntegration(),
ModulesIntegration::class => new ModulesIntegration(),
],
self::getExpectedDefaultIntegrations($options, true),
];

yield 'Default integrations with DSN set to null' => [
new Options([
$options = new Options([
'dsn' => null,
'default_integrations' => true,
'integrations' => static function (array $defaultIntegrations): array {
return $defaultIntegrations;
},
]),
[
RequestIntegration::class => new RequestIntegration(),
TransactionIntegration::class => new TransactionIntegration(),
FrameContextifierIntegration::class => new FrameContextifierIntegration(),
EnvironmentIntegration::class => new EnvironmentIntegration(),
ModulesIntegration::class => new ModulesIntegration(),
],
self::getExpectedDefaultIntegrations($options, false),
];
}

/**
* @param class-string<IntegrationInterface>[] $excludedDefaultIntegrations
*
* @return array<class-string<IntegrationInterface>, IntegrationInterface>
*/
private static function getExpectedDefaultIntegrations(Options $options, bool $withErrorIntegrations, array $excludedDefaultIntegrations = []): array
{
$integrations = [
RequestIntegration::class => new RequestIntegration(),
TransactionIntegration::class => new TransactionIntegration(),
FrameContextifierIntegration::class => new FrameContextifierIntegration(),
EnvironmentIntegration::class => new EnvironmentIntegration(),
ModulesIntegration::class => new ModulesIntegration(),
];

if (\function_exists('Sentry\\setStartCallback') && \function_exists('Sentry\\setEndCallback')) {
$integrations[FunctionTracingIntegration::class] = new FunctionTracingIntegration();
}

foreach ($excludedDefaultIntegrations as $excludedDefaultIntegration) {
unset($integrations[$excludedDefaultIntegration]);
}

if ($withErrorIntegrations) {
$integrations = [
ExceptionListenerIntegration::class => new ExceptionListenerIntegration(),
ErrorListenerIntegration::class => ErrorListenerIntegration::make($options),
FatalErrorListenerIntegration::class => new FatalErrorListenerIntegration(),
] + $integrations;
}

return $integrations;
}

/**
* @runInSeparateProcess
*
* @preserveGlobalState disabled
*/
public function testDefaultIntegrationsIncludeFunctionTracingIntegrationWhenExtensionCallbacksExist(): void
{
if (!\function_exists('Sentry\\setStartCallback')) {
eval('namespace Sentry { function setStartCallback(callable $callback): bool { return true; } function setEndCallback(callable $callback): bool { return true; } }');
}

$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->once())
->method('debug');

$integrations = IntegrationRegistry::getInstance()->setupIntegrations(new Options([
'dsn' => null,
'default_integrations' => true,
]), $logger);

$this->assertArrayHasKey(FunctionTracingIntegration::class, $integrations);
}

/**
Expand Down
Loading
Loading