|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sentry\Laravel\Features\Storage; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Filesystem\Filesystem; |
| 6 | +use Sentry\Breadcrumb; |
| 7 | +use Sentry\Laravel\Integration; |
| 8 | +use Sentry\Laravel\Util\Filesize; |
| 9 | +use Sentry\Tracing\SpanContext; |
| 10 | +use function Sentry\trace; |
| 11 | + |
| 12 | +/** |
| 13 | + * Decorates the underlying filesystem by wrapping all calls to it with tracing. |
| 14 | + * |
| 15 | + * Parameters such as paths, directories or options are attached to the span as data, |
| 16 | + * parameters that contain file contents are omitted due to potential problems with |
| 17 | + * payload size or sensitive data. |
| 18 | + */ |
| 19 | +class SentryFilesystem implements Filesystem |
| 20 | +{ |
| 21 | + /** @var Filesystem */ |
| 22 | + protected $filesystem; |
| 23 | + |
| 24 | + /** @var array */ |
| 25 | + protected $defaultData; |
| 26 | + |
| 27 | + /** @var bool */ |
| 28 | + protected $recordSpans; |
| 29 | + |
| 30 | + /** @var bool */ |
| 31 | + protected $recordBreadcrumbs; |
| 32 | + |
| 33 | + public function __construct(Filesystem $filesystem, array $defaultData, bool $recordSpans, bool $recordBreadcrumbs) |
| 34 | + { |
| 35 | + $this->filesystem = $filesystem; |
| 36 | + $this->defaultData = $defaultData; |
| 37 | + $this->recordSpans = $recordSpans; |
| 38 | + $this->recordBreadcrumbs = $recordBreadcrumbs; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Execute the method on the underlying filesystem and wrap it with tracing and log a breadcrumb. |
| 43 | + * |
| 44 | + * @param list<mixed> $args |
| 45 | + * @param array<string, mixed> $data |
| 46 | + * |
| 47 | + * @return mixed |
| 48 | + */ |
| 49 | + protected function withSentry(string $method, array $args, string $description, array $data) |
| 50 | + { |
| 51 | + $op = "file.{$method}"; // See https://develop.sentry.dev/sdk/performance/span-operations/#web-server |
| 52 | + $data = array_merge($data, $this->defaultData); |
| 53 | + |
| 54 | + if ($this->recordBreadcrumbs) { |
| 55 | + Integration::addBreadcrumb(new Breadcrumb( |
| 56 | + Breadcrumb::LEVEL_INFO, |
| 57 | + Breadcrumb::TYPE_DEFAULT, |
| 58 | + $op, |
| 59 | + $description, |
| 60 | + $data |
| 61 | + )); |
| 62 | + } |
| 63 | + |
| 64 | + if ($this->recordSpans) { |
| 65 | + $spanContext = new SpanContext; |
| 66 | + $spanContext->setOp($op); |
| 67 | + $spanContext->setData($data); |
| 68 | + $spanContext->setDescription($description); |
| 69 | + |
| 70 | + return trace(function () use ($method, $args) { |
| 71 | + return $this->filesystem->{$method}(...$args); |
| 72 | + }, $spanContext); |
| 73 | + } |
| 74 | + |
| 75 | + return $this->filesystem->{$method}(...$args); |
| 76 | + } |
| 77 | + |
| 78 | + /** @see \Illuminate\Filesystem\FilesystemAdapter::assertExists() */ |
| 79 | + public function assertExists($path, $content = null) |
| 80 | + { |
| 81 | + return $this->withSentry(__FUNCTION__, func_get_args(), $path, compact('path')); |
| 82 | + } |
| 83 | + |
| 84 | + /** @see \Illuminate\Filesystem\FilesystemAdapter::assertMissing() */ |
| 85 | + public function assertMissing($path) |
| 86 | + { |
| 87 | + return $this->withSentry(__FUNCTION__, func_get_args(), $path, compact('path')); |
| 88 | + } |
| 89 | + |
| 90 | + /** @see \Illuminate\Filesystem\FilesystemAdapter::assertDirectoryEmpty() */ |
| 91 | + public function assertDirectoryEmpty($path) |
| 92 | + { |
| 93 | + return $this->withSentry(__FUNCTION__, func_get_args(), $path, compact('path')); |
| 94 | + } |
| 95 | + |
| 96 | + public function exists($path) |
| 97 | + { |
| 98 | + return $this->withSentry(__FUNCTION__, func_get_args(), $path, compact('path')); |
| 99 | + } |
| 100 | + |
| 101 | + public function get($path) |
| 102 | + { |
| 103 | + return $this->withSentry(__FUNCTION__, func_get_args(), $path, compact('path')); |
| 104 | + } |
| 105 | + |
| 106 | + public function readStream($path) |
| 107 | + { |
| 108 | + return $this->withSentry(__FUNCTION__, func_get_args(), $path, compact('path')); |
| 109 | + } |
| 110 | + |
| 111 | + public function put($path, $contents, $options = []) |
| 112 | + { |
| 113 | + $description = is_string($contents) ? sprintf('%s (%s)', $path, Filesize::toHuman(strlen($contents))) : $path; |
| 114 | + |
| 115 | + return $this->withSentry(__FUNCTION__, func_get_args(), $description, compact('path', 'options')); |
| 116 | + } |
| 117 | + |
| 118 | + public function writeStream($path, $resource, array $options = []) |
| 119 | + { |
| 120 | + return $this->withSentry(__FUNCTION__, func_get_args(), $path, compact('path', 'options')); |
| 121 | + } |
| 122 | + |
| 123 | + public function getVisibility($path) |
| 124 | + { |
| 125 | + return $this->withSentry(__FUNCTION__, func_get_args(), $path, compact('path')); |
| 126 | + } |
| 127 | + |
| 128 | + public function setVisibility($path, $visibility) |
| 129 | + { |
| 130 | + return $this->withSentry(__FUNCTION__, func_get_args(), $path, compact('path', 'visibility')); |
| 131 | + } |
| 132 | + |
| 133 | + public function prepend($path, $data) |
| 134 | + { |
| 135 | + $description = is_string($data) ? sprintf('%s (%s)', $path, Filesize::toHuman(strlen($data))) : $path; |
| 136 | + |
| 137 | + return $this->withSentry(__FUNCTION__, func_get_args(), $description, compact('path')); |
| 138 | + } |
| 139 | + |
| 140 | + public function append($path, $data) |
| 141 | + { |
| 142 | + $description = is_string($data) ? sprintf('%s (%s)', $path, Filesize::toHuman(strlen($data))) : $path; |
| 143 | + |
| 144 | + return $this->withSentry(__FUNCTION__, func_get_args(), $description, compact('path')); |
| 145 | + } |
| 146 | + |
| 147 | + public function delete($paths) |
| 148 | + { |
| 149 | + if (is_array($paths)) { |
| 150 | + $data = compact('paths'); |
| 151 | + $description = sprintf('%s paths', count($paths)); |
| 152 | + } else { |
| 153 | + $data = ['path' => $paths]; |
| 154 | + $description = $paths; |
| 155 | + } |
| 156 | + |
| 157 | + return $this->withSentry(__FUNCTION__, func_get_args(), $description, $data); |
| 158 | + } |
| 159 | + |
| 160 | + public function copy($from, $to) |
| 161 | + { |
| 162 | + return $this->withSentry(__FUNCTION__, func_get_args(), sprintf('from "%s" to "%s"', $from, $to), compact('from', 'to')); |
| 163 | + } |
| 164 | + |
| 165 | + public function move($from, $to) |
| 166 | + { |
| 167 | + return $this->withSentry(__FUNCTION__, func_get_args(), sprintf('from "%s" to "%s"', $from, $to), compact('from', 'to')); |
| 168 | + } |
| 169 | + |
| 170 | + public function size($path) |
| 171 | + { |
| 172 | + return $this->withSentry(__FUNCTION__, func_get_args(), $path, compact('path')); |
| 173 | + } |
| 174 | + |
| 175 | + public function lastModified($path) |
| 176 | + { |
| 177 | + return $this->withSentry(__FUNCTION__, func_get_args(), $path, compact('path')); |
| 178 | + } |
| 179 | + |
| 180 | + public function files($directory = null, $recursive = false) |
| 181 | + { |
| 182 | + return $this->withSentry(__FUNCTION__, func_get_args(), $directory, compact('directory', 'recursive')); |
| 183 | + } |
| 184 | + |
| 185 | + public function allFiles($directory = null) |
| 186 | + { |
| 187 | + return $this->withSentry(__FUNCTION__, func_get_args(), $directory, compact('directory')); |
| 188 | + } |
| 189 | + |
| 190 | + public function directories($directory = null, $recursive = false) |
| 191 | + { |
| 192 | + return $this->withSentry(__FUNCTION__, func_get_args(), $directory, compact('directory', 'recursive')); |
| 193 | + } |
| 194 | + |
| 195 | + public function allDirectories($directory = null) |
| 196 | + { |
| 197 | + return $this->withSentry(__FUNCTION__, func_get_args(), $directory, compact('directory')); |
| 198 | + } |
| 199 | + |
| 200 | + public function makeDirectory($path) |
| 201 | + { |
| 202 | + return $this->withSentry(__FUNCTION__, func_get_args(), $path, compact('path')); |
| 203 | + } |
| 204 | + |
| 205 | + public function deleteDirectory($directory) |
| 206 | + { |
| 207 | + return $this->withSentry(__FUNCTION__, func_get_args(), $directory, compact('directory')); |
| 208 | + } |
| 209 | + |
| 210 | + public function __call($name, $arguments) |
| 211 | + { |
| 212 | + return $this->filesystem->{$name}(...$arguments); |
| 213 | + } |
| 214 | +} |
0 commit comments