forked from getsentry/sentry-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTraceableCacheAdapterTrait.php
206 lines (182 loc) · 5.32 KB
/
TraceableCacheAdapterTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
declare(strict_types=1);
namespace Sentry\SentryBundle\Tracing\Cache;
use Psr\Cache\CacheItemInterface;
use Sentry\State\HubInterface;
use Sentry\Tracing\SpanContext;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Contracts\Cache\CacheInterface;
/**
* @internal
*
* @phpstan-template T of AdapterInterface
*/
trait TraceableCacheAdapterTrait
{
/**
* @var HubInterface The current hub
*/
private $hub;
/**
* @var AdapterInterface|TagAwareAdapterInterface The decorated adapter
*
* @phpstan-var T
*/
private $decoratedAdapter;
/**
* {@inheritdoc}
*/
public function getItem($key): CacheItem
{
return $this->traceFunction('cache.get_item', function () use ($key): CacheItem {
return $this->decoratedAdapter->getItem($key);
}, $key);
}
/**
* {@inheritdoc}
*/
public function getItems(array $keys = []): iterable
{
return $this->traceFunction('cache.get_items', function () use ($keys): iterable {
return $this->decoratedAdapter->getItems($keys);
});
}
/**
* {@inheritdoc}
*/
public function clear(string $prefix = ''): bool
{
return $this->traceFunction('cache.clear', function () use ($prefix): bool {
return $this->decoratedAdapter->clear($prefix);
}, $prefix);
}
/**
* {@inheritdoc}
*/
public function delete(string $key): bool
{
return $this->traceFunction('cache.delete_item', function () use ($key): bool {
if (!$this->decoratedAdapter instanceof CacheInterface) {
throw new \BadMethodCallException(sprintf('The %s::delete() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, CacheInterface::class));
}
return $this->decoratedAdapter->delete($key);
}, $key);
}
/**
* {@inheritdoc}
*/
public function hasItem($key): bool
{
return $this->traceFunction('cache.has_item', function () use ($key): bool {
return $this->decoratedAdapter->hasItem($key);
}, $key);
}
/**
* {@inheritdoc}
*/
public function deleteItem($key): bool
{
return $this->traceFunction('cache.delete_item', function () use ($key): bool {
return $this->decoratedAdapter->deleteItem($key);
}, $key);
}
/**
* {@inheritdoc}
*/
public function deleteItems(array $keys): bool
{
return $this->traceFunction('cache.delete_items', function () use ($keys): bool {
return $this->decoratedAdapter->deleteItems($keys);
});
}
/**
* {@inheritdoc}
*/
public function save(CacheItemInterface $item): bool
{
return $this->traceFunction('cache.save', function () use ($item): bool {
return $this->decoratedAdapter->save($item);
});
}
/**
* {@inheritdoc}
*/
public function saveDeferred(CacheItemInterface $item): bool
{
return $this->traceFunction('cache.save_deferred', function () use ($item): bool {
return $this->decoratedAdapter->saveDeferred($item);
});
}
/**
* {@inheritdoc}
*/
public function commit(): bool
{
return $this->traceFunction('cache.commit', function (): bool {
return $this->decoratedAdapter->commit();
});
}
/**
* {@inheritdoc}
*/
public function prune(): bool
{
return $this->traceFunction('cache.prune', function (): bool {
if (!$this->decoratedAdapter instanceof PruneableInterface) {
return false;
}
return $this->decoratedAdapter->prune();
});
}
/**
* {@inheritdoc}
*/
public function reset(): void
{
if ($this->decoratedAdapter instanceof ResettableInterface) {
$this->decoratedAdapter->reset();
}
}
/**
* @phpstan-template TResult
*
* @phpstan-param \Closure(): TResult $callback
*
* @phpstan-return TResult
*/
private function traceFunction(string $spanOperation, \Closure $callback, string $spanDescription = null)
{
$span = $this->hub->getSpan();
if (null !== $span) {
$spanContext = new SpanContext();
$spanContext->setOp($spanOperation);
if (null !== $spanDescription) {
$spanContext->setDescription(urldecode($spanDescription));
}
$span = $span->startChild($spanContext);
}
try {
return $callback();
} finally {
if (null !== $span) {
$span->finish();
}
}
}
/**
* @phpstan-param \Closure(CacheItem): CacheItem $callback
* @phpstan-param string $key
*
* @phpstan-return callable(): CacheItem
*/
private function setCallbackWrapper(callable $callback, string $key): callable
{
return function () use ($callback, $key): CacheItem {
return $callback($this->decoratedAdapter->getItem($key));
};
}
}