Skip to content
Merged
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: 2 additions & 0 deletions src/sentry/publish/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
'cache' => env('SENTRY_BREADCRUMBS_CACHE', true),
'command' => env('SENTRY_BREADCRUMBS_COMMAND', true),
'command_input' => env('SENTRY_BREADCRUMBS_COMMAND_INPUT', true),
'filesystem' => env('SENTRY_BREADCRUMBS_FILESYSTEM', true),
'sql_queries' => env('SENTRY_BREADCRUMBS_SQL_QUERIES', true),
'sql_bindings' => env('SENTRY_BREADCRUMBS_SQL_BINDINGS', true),
'sql_transaction' => env('SENTRY_BREADCRUMBS_SQL_TRANSACTION', true),
Expand Down Expand Up @@ -106,6 +107,7 @@
'coroutine' => env('SENTRY_TRACING_SPANS_COROUTINE', true),
'db' => env('SENTRY_TRACING_SPANS_DB', true),
'elasticsearch' => env('SENTRY_TRACING_SPANS_ELASTICSEARCH', true),
'filesystem' => env('SENTRY_TRACING_SPANS_FILESYSTEM', true),
'guzzle' => env('SENTRY_TRACING_SPANS_GUZZLE', true),
'rpc' => env('SENTRY_TRACING_SPANS_RPC', true),
'grpc' => env('SENTRY_TRACING_SPANS_GRPC', true),
Expand Down
133 changes: 133 additions & 0 deletions src/sentry/src/Aspect/FilesystemAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/

namespace FriendsOfHyperf\Sentry\Aspect;

use FriendsOfHyperf\Sentry\Integration;
use FriendsOfHyperf\Sentry\Switcher;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Sentry\Breadcrumb;

class FilesystemAspect extends AbstractAspect
{
public array $classes = [
'League\Flysystem\*\*Adapter::fileExists',
'League\Flysystem\*\*Adapter::directoryExists',
'League\Flysystem\*\*Adapter::write',
'League\Flysystem\*\*Adapter::writeStream',
'League\Flysystem\*\*Adapter::read',
'League\Flysystem\*\*Adapter::readStream',
'League\Flysystem\*\*Adapter::delete',
'League\Flysystem\*\*Adapter::deleteDirectory',
'League\Flysystem\*\*Adapter::createDirectory',
'League\Flysystem\*\*Adapter::setVisibility',
'League\Flysystem\*\*Adapter::visibility',
'League\Flysystem\*\*Adapter::mimeType',
'League\Flysystem\*\*Adapter::lastModified',
'League\Flysystem\*\*Adapter::fileSize',
'League\Flysystem\*\*Adapter::listContents',
'League\Flysystem\*\*Adapter::move',
'League\Flysystem\*\*Adapter::copy',

// More adapter methods can be added here
];

public function __construct(protected Switcher $switcher)
{
}

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
if (! $this->switcher->isBreadcrumbEnabled('filesystem')) {
return $proceedingJoinPoint->process();
}

[$op, $description, $data] = $this->getSentryMetadata($proceedingJoinPoint);

Integration::addBreadcrumb(new Breadcrumb(
Breadcrumb::LEVEL_INFO,
Breadcrumb::TYPE_DEFAULT,
$op,
$description,
$data
));

return $proceedingJoinPoint->process();
}

/**
* @return array{0: string, 1: string, 2: array} [op, description, data]
*/
protected function getSentryMetadata(ProceedingJoinPoint $proceedingJoinPoint): array
{
$method = $proceedingJoinPoint->methodName;
$arguments = $proceedingJoinPoint->arguments['keys'] ?? [];
// See https://develop.sentry.dev/sdk/performance/span-operations/#web-server
$op = "file.{$method}";
$description = match ($method) {
'move', 'copy' => sprintf(
'from "%s" to "%s"',
$arguments['source'] ?? '',
$arguments['destination'] ?? ''
),
'write', 'writeStream',
'read', 'readStream',
'setVisibility', 'visibility',
'delete', 'deleteDirectory', 'createDirectory',
'fileExists', 'directoryExists',
'listContents',
'lastModified',
'fileSize',
'mimeType', => $arguments['path'] ?? '',
default => '',
};

$config = null;

if (isset($arguments['config'])) {
if (is_object($arguments['config']) && method_exists($arguments['config'], 'toArray')) {
$config = $arguments['config']->toArray();
} else {
$config = $arguments['config'];
}
}

$data = match ($method) {
'move', 'copy' => [
'from' => $arguments['source'] ?? '',
'to' => $arguments['destination'] ?? '',
'config' => $config,
],
'write', 'writeStream', 'createDirectory' => [
'path' => $arguments['path'] ?? '',
'config' => $config,
],
'setVisibility' => [
'path' => $arguments['path'] ?? '',
'visibility' => $arguments['visibility'] ?? '',
],
'listContents' => [
'path' => $arguments['path'] ?? '',
'deep' => $arguments['deep'] ?? false,
],
'read', 'readStream',
'visibility', 'delete', 'deleteDirectory',
'fileExists', 'directoryExists',
'lastModified', 'fileSize', 'mimeType' => [
'path' => $arguments['path'] ?? '',
],
default => [],
};

return [$op, $description, $data];
}
}
2 changes: 2 additions & 0 deletions src/sentry/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __invoke(): array
Aspect\BreadcrumbAspect::class,
Aspect\CacheAspect::class,
Aspect\CoroutineAspect::class,
Aspect\FilesystemAspect::class,
Aspect\GuzzleHttpClientAspect::class,
Aspect\LoggerAspect::class,
Aspect\RedisAspect::class,
Expand All @@ -33,6 +34,7 @@ public function __invoke(): array
Tracing\Aspect\CoroutineAspect::class,
Tracing\Aspect\DbAspect::class,
Tracing\Aspect\ElasticsearchAspect::class,
Tracing\Aspect\FilesystemAspect::class,
Tracing\Aspect\GrpcAspect::class,
Tracing\Aspect\GuzzleHttpClientAspect::class,
Tracing\Aspect\KafkaProducerAspect::class,
Expand Down
41 changes: 41 additions & 0 deletions src/sentry/src/Tracing/Aspect/FilesystemAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/

namespace FriendsOfHyperf\Sentry\Tracing\Aspect;

use FriendsOfHyperf\Sentry\Aspect\FilesystemAspect as BaseFilesystemAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Override;
use Sentry\Tracing\SpanContext;

use function Sentry\trace;

class FilesystemAspect extends BaseFilesystemAspect
{
#[Override]
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
if (! $this->switcher->isTracingEnabled('filesystem')) {
return $proceedingJoinPoint->process();
}

[$op, $description, $data] = $this->getSentryMetadata($proceedingJoinPoint);

return trace(
fn () => $proceedingJoinPoint->process(),
SpanContext::make()
->setOp($op)
->setData($data)
->setOrigin('auto.filesystem')
->setDescription($description)
);
}
}