/
TracingDriverConnection.php
287 lines (247 loc) · 7.86 KB
/
TracingDriverConnection.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
declare(strict_types=1);
namespace Sentry\SentryBundle\Tracing\Doctrine\DBAL;
use Doctrine\DBAL\Driver\Connection as DriverConnectionInterface;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\ParameterType;
use Sentry\State\HubInterface;
use Sentry\Tracing\SpanContext;
/**
* This implementation wraps a driver connection and adds distributed tracing
* capabilities to Doctrine DBAL. This implementation IS and MUST be compatible
* with all versions of Doctrine DBAL >= 2.10.
*
* @phpstan-import-type Params from \Doctrine\DBAL\DriverManager as ConnectionParams
*/
final class TracingDriverConnection implements TracingDriverConnectionInterface
{
/**
* @internal
*/
public const SPAN_OP_CONN_PREPARE = 'db.sql.prepare';
/**
* @internal
*/
public const SPAN_OP_CONN_QUERY = 'db.sql.query';
/**
* @internal
*/
public const SPAN_OP_CONN_EXEC = 'db.sql.exec';
/**
* @internal
*/
public const SPAN_OP_CONN_BEGIN_TRANSACTION = 'db.sql.transaction.begin';
/**
* @internal
*/
public const SPAN_OP_TRANSACTION_COMMIT = 'db.sql.transaction.commit';
/**
* @internal
*/
public const SPAN_OP_TRANSACTION_ROLLBACK = 'db.sql.transaction.rollback';
/**
* @var HubInterface The current hub
*/
private $hub;
/**
* @var DriverConnectionInterface The decorated connection
*/
private $decoratedConnection;
/**
* @var array<string, string> The tags to attach to the span
*/
private $spanTags;
/**
* Constructor.
*
* @param HubInterface $hub The current hub
* @param DriverConnectionInterface $decoratedConnection The connection to decorate
* @param string $databasePlatform The name of the database platform
* @param array<string, mixed> $params The connection params
*
* @phpstan-param ConnectionParams $params
*/
public function __construct(
HubInterface $hub,
DriverConnectionInterface $decoratedConnection,
string $databasePlatform,
array $params
) {
$this->hub = $hub;
$this->decoratedConnection = $decoratedConnection;
$this->spanTags = $this->getSpanTags($databasePlatform, $params);
}
/**
* {@inheritdoc}
*/
public function prepare($sql): Statement
{
$statement = $this->traceFunction(self::SPAN_OP_CONN_PREPARE, $sql, function () use ($sql): Statement {
return $this->decoratedConnection->prepare($sql);
});
return new TracingStatement($this->hub, $statement, $sql, $this->spanTags);
}
/**
* {@inheritdoc}
*/
public function query(?string $sql = null, ...$args): Result
{
return $this->traceFunction(self::SPAN_OP_CONN_QUERY, $sql, function () use ($sql, $args): Result {
return $this->decoratedConnection->query($sql, ...$args);
});
}
/**
* {@inheritdoc}
*
* @return mixed
*/
public function quote($value, $type = ParameterType::STRING)
{
return $this->decoratedConnection->quote($value, $type);
}
/**
* {@inheritdoc}
*/
public function exec($sql): int
{
return $this->traceFunction(self::SPAN_OP_CONN_EXEC, $sql, function () use ($sql): int {
return $this->decoratedConnection->exec($sql);
});
}
/**
* {@inheritdoc}
*
* @return string|int|false
*/
public function lastInsertId($name = null)
{
return $this->decoratedConnection->lastInsertId($name);
}
/**
* {@inheritdoc}
*/
public function beginTransaction(): bool
{
return $this->traceFunction(self::SPAN_OP_CONN_BEGIN_TRANSACTION, 'BEGIN TRANSACTION', function (): bool {
return $this->decoratedConnection->beginTransaction();
});
}
/**
* {@inheritdoc}
*/
public function commit(): bool
{
return $this->traceFunction(self::SPAN_OP_TRANSACTION_COMMIT, 'COMMIT', function (): bool {
return $this->decoratedConnection->commit();
});
}
/**
* {@inheritdoc}
*/
public function rollBack(): bool
{
return $this->traceFunction(self::SPAN_OP_TRANSACTION_ROLLBACK, 'ROLLBACK', function (): bool {
return $this->decoratedConnection->rollBack();
});
}
/**
* {@inheritdoc}
*
* @return resource|object
*/
public function getNativeConnection()
{
if (!method_exists($this->decoratedConnection, 'getNativeConnection')) {
throw new \BadMethodCallException(sprintf('The connection "%s" does not support accessing the native connection.', \get_class($this->decoratedConnection)));
}
return $this->decoratedConnection->getNativeConnection();
}
/**
* {@inheritdoc}
*/
public function errorCode(): ?string
{
if (method_exists($this->decoratedConnection, 'errorCode')) {
return $this->decoratedConnection->errorCode();
}
throw new \BadMethodCallException(sprintf('The %s() method is not supported on Doctrine DBAL 3.0.', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function errorInfo(): array
{
if (method_exists($this->decoratedConnection, 'errorInfo')) {
return $this->decoratedConnection->errorInfo();
}
throw new \BadMethodCallException(sprintf('The %s() method is not supported on Doctrine DBAL 3.0.', __METHOD__));
}
public function getWrappedConnection(): DriverConnectionInterface
{
return $this->decoratedConnection;
}
/**
* @phpstan-template T
*
* @phpstan-param \Closure(): T $callback
*
* @phpstan-return T
*/
private function traceFunction(string $spanOperation, string $spanDescription, \Closure $callback)
{
$span = $this->hub->getSpan();
if (null !== $span) {
$spanContext = new SpanContext();
$spanContext->setOp($spanOperation);
$spanContext->setDescription($spanDescription);
$spanContext->setTags($this->spanTags);
$span = $span->startChild($spanContext);
}
try {
return $callback();
} finally {
if (null !== $span) {
$span->finish();
}
}
}
/**
* Gets a map of key-value pairs that will be set as tags of the span.
*
* @param string $databasePlatform The database platform
* @param array<string, mixed> $params The connection params
*
* @return array<string, string>
*
* @phpstan-param ConnectionParams $params
*
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md
*/
private function getSpanTags(string $databasePlatform, array $params): array
{
$tags = ['db.system' => $databasePlatform];
if (isset($params['user'])) {
$tags['db.user'] = $params['user'];
}
if (isset($params['dbname'])) {
$tags['db.name'] = $params['dbname'];
}
if (isset($params['host']) && !empty($params['host']) && !isset($params['memory'])) {
if (false === filter_var($params['host'], \FILTER_VALIDATE_IP)) {
$tags['net.peer.name'] = $params['host'];
} else {
$tags['net.peer.ip'] = $params['host'];
}
}
if (isset($params['port'])) {
$tags['net.peer.port'] = (string) $params['port'];
}
if (isset($params['unix_socket'])) {
$tags['net.transport'] = 'Unix';
} elseif (isset($params['memory'])) {
$tags['net.transport'] = 'inproc';
}
return $tags;
}
}