Skip to content

Commit 8d5205b

Browse files
committed
Move span tags to span data
1 parent 6a89399 commit 8d5205b

9 files changed

+85
-63
lines changed

src/Tracing/Doctrine/DBAL/AbstractTracingStatement.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ abstract class AbstractTracingStatement
3232
protected $sqlQuery;
3333

3434
/**
35-
* @var array<string, string> The span tags
35+
* @var array<string, string> The span data
3636
*/
37-
protected $spanTags;
37+
protected $spanData;
3838

3939
/**
4040
* Constructor.
4141
*
4242
* @param HubInterface $hub The current hub
4343
* @param Statement $decoratedStatement The decorated statement
4444
* @param string $sqlQuery The SQL query executed by the decorated statement
45-
* @param array<string, string> $spanTags The span tags
45+
* @param array<string, string> $spanData The span data
4646
*/
47-
public function __construct(HubInterface $hub, Statement $decoratedStatement, string $sqlQuery, array $spanTags)
47+
public function __construct(HubInterface $hub, Statement $decoratedStatement, string $sqlQuery, array $spanData)
4848
{
4949
$this->hub = $hub;
5050
$this->decoratedStatement = $decoratedStatement;
5151
$this->sqlQuery = $sqlQuery;
52-
$this->spanTags = $spanTags;
52+
$this->spanData = $spanData;
5353
}
5454

5555
/**

src/Tracing/Doctrine/DBAL/TracingDriverConnection.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ final class TracingDriverConnection implements TracingDriverConnectionInterface
6161
private $decoratedConnection;
6262

6363
/**
64-
* @var array<string, string> The tags to attach to the span
64+
* @var array<string, string> The data to attach to the span
6565
*/
66-
private $spanTags;
66+
private $spanData;
6767

6868
/**
6969
* Constructor.
@@ -83,7 +83,7 @@ public function __construct(
8383
) {
8484
$this->hub = $hub;
8585
$this->decoratedConnection = $decoratedConnection;
86-
$this->spanTags = $this->getSpanTags($databasePlatform, $params);
86+
$this->spanData = $this->getSpanTags($databasePlatform, $params);
8787
}
8888

8989
/**
@@ -95,7 +95,7 @@ public function prepare($sql): Statement
9595
return $this->decoratedConnection->prepare($sql);
9696
});
9797

98-
return new TracingStatement($this->hub, $statement, $sql, $this->spanTags);
98+
return new TracingStatement($this->hub, $statement, $sql, $this->spanData);
9999
}
100100

101101
/**
@@ -226,7 +226,7 @@ private function traceFunction(string $spanOperation, string $spanDescription, \
226226
$spanContext = new SpanContext();
227227
$spanContext->setOp($spanOperation);
228228
$spanContext->setDescription($spanDescription);
229-
$spanContext->setTags($this->spanTags);
229+
$spanContext->setData($this->spanData);
230230

231231
$span = $span->startChild($spanContext);
232232
}
@@ -241,10 +241,9 @@ private function traceFunction(string $spanOperation, string $spanDescription, \
241241
}
242242

243243
/**
244-
* Gets a map of key-value pairs that will be set as tags of the span.
244+
* Gets a map of key-value pairs that will be set as the span data.
245245
*
246-
* @param string $databasePlatform The database platform
247-
* @param array<string, mixed> $params The connection params
246+
* @param array<string, mixed> $params The connection params
248247
*
249248
* @return array<string, string>
250249
*
@@ -254,34 +253,34 @@ private function traceFunction(string $spanOperation, string $spanDescription, \
254253
*/
255254
private function getSpanTags(string $databasePlatform, array $params): array
256255
{
257-
$tags = ['db.system' => $databasePlatform];
256+
$data = ['db.system' => $databasePlatform];
258257

259258
if (isset($params['user'])) {
260-
$tags['db.user'] = $params['user'];
259+
$data['db.user'] = $params['user'];
261260
}
262261

263262
if (isset($params['dbname'])) {
264-
$tags['db.name'] = $params['dbname'];
263+
$data['db.name'] = $params['dbname'];
265264
}
266265

267266
if (isset($params['host']) && !empty($params['host']) && !isset($params['memory'])) {
268267
if (false === filter_var($params['host'], \FILTER_VALIDATE_IP)) {
269-
$tags['net.peer.name'] = $params['host'];
268+
$data['net.peer.name'] = $params['host'];
270269
} else {
271-
$tags['net.peer.ip'] = $params['host'];
270+
$data['net.peer.ip'] = $params['host'];
272271
}
273272
}
274273

275274
if (isset($params['port'])) {
276-
$tags['net.peer.port'] = (string) $params['port'];
275+
$data['net.peer.port'] = (string) $params['port'];
277276
}
278277

279278
if (isset($params['unix_socket'])) {
280-
$tags['net.transport'] = 'Unix';
279+
$data['net.transport'] = 'Unix';
281280
} elseif (isset($params['memory'])) {
282-
$tags['net.transport'] = 'inproc';
281+
$data['net.transport'] = 'inproc';
283282
}
284283

285-
return $tags;
284+
return $data;
286285
}
287286
}

src/Tracing/Doctrine/DBAL/TracingDriverConnectionFactory.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66

77
use Doctrine\DBAL\Driver\Connection;
88
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
9+
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
910
use Doctrine\DBAL\Platforms\AbstractPlatform;
11+
use Doctrine\DBAL\Platforms\DB2Platform;
12+
use Doctrine\DBAL\Platforms\OraclePlatform;
13+
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
14+
use Doctrine\DBAL\Platforms\SqlitePlatform;
15+
use Doctrine\DBAL\Platforms\SQLServerPlatform;
1016
use Sentry\State\HubInterface;
1117

1218
/**
@@ -37,7 +43,7 @@ public function create(Connection $connection, AbstractPlatform $databasePlatfor
3743
$tracingDriverConnection = new TracingDriverConnection(
3844
$this->hub,
3945
$connection,
40-
$databasePlatform->getName(),
46+
$this->getDatabasePlatform($databasePlatform),
4147
$params
4248
);
4349

@@ -47,4 +53,29 @@ public function create(Connection $connection, AbstractPlatform $databasePlatfor
4753

4854
return $tracingDriverConnection;
4955
}
56+
57+
private function getDatabasePlatform(AbstractPlatform $databasePlatform): string
58+
{
59+
// https://github.com/open-telemetry/opentelemetry-specification/blob/33113489fb5a1b6da563abb4ffa541447b87f515/specification/trace/semantic_conventions/database.md#connection-level-attributes
60+
if ($databasePlatform instanceof AbstractMySQLPlatform) {
61+
return 'mysql';
62+
}
63+
if ($databasePlatform instanceof DB2Platform) {
64+
return 'db2';
65+
}
66+
if ($databasePlatform instanceof OraclePlatform) {
67+
return 'oracle';
68+
}
69+
if ($databasePlatform instanceof PostgreSQLPlatform) {
70+
return 'postgresql';
71+
}
72+
if ($databasePlatform instanceof SqlitePlatform) {
73+
return 'sqlite';
74+
}
75+
if ($databasePlatform instanceof SQLServerPlatform) {
76+
return 'mssql';
77+
}
78+
79+
return 'other_sql';
80+
}
5081
}

src/Tracing/Doctrine/DBAL/TracingStatementForV2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function execute($params = null): bool
119119
$spanContext = new SpanContext();
120120
$spanContext->setOp(self::SPAN_OP_STMT_EXECUTE);
121121
$spanContext->setDescription($this->sqlQuery);
122-
$spanContext->setTags($this->spanTags);
122+
$spanContext->setData($this->spanData);
123123

124124
return $this->traceFunction($spanContext, [$this->decoratedStatement, 'execute'], $params);
125125
}

src/Tracing/Doctrine/DBAL/TracingStatementForV3.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function execute($params = null): Result
3838
$spanContext = new SpanContext();
3939
$spanContext->setOp(self::SPAN_OP_STMT_EXECUTE);
4040
$spanContext->setDescription($this->sqlQuery);
41-
$spanContext->setTags($this->spanTags);
41+
$spanContext->setData($this->spanData);
4242

4343
return $this->traceFunction($spanContext, [$this->decoratedStatement, 'execute'], $params);
4444
}

tests/Tracing/Doctrine/DBAL/TracingDriverConnectionFactoryTest.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,9 @@ protected function setUp(): void
4747

4848
public function testCreate(): void
4949
{
50-
$this->databasePlatform->expects($this->once())
51-
->method('getName')
52-
->willReturn('foo_platform');
53-
5450
$connection = $this->createMock(Connection::class);
5551
$driverConnection = $this->tracingDriverConnectionFactory->create($connection, $this->databasePlatform, []);
56-
$expectedDriverConnection = new TracingDriverConnection($this->hub, $connection, 'foo_platform', []);
52+
$expectedDriverConnection = new TracingDriverConnection($this->hub, $connection, 'other_sql', []);
5753

5854
$this->assertEquals($expectedDriverConnection, $driverConnection);
5955
}
@@ -64,13 +60,9 @@ public function testCreateWithServerInfoAwareConnection(): void
6460
self::markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be >= 3.0.');
6561
}
6662

67-
$this->databasePlatform->expects($this->once())
68-
->method('getName')
69-
->willReturn('foo_platform');
70-
7163
$connection = $this->createMock(ServerInfoAwareConnectionStub::class);
7264
$driverConnection = $this->tracingDriverConnectionFactory->create($connection, $this->databasePlatform, []);
73-
$expectedDriverConnection = new TracingServerInfoAwareDriverConnection(new TracingDriverConnection($this->hub, $connection, 'foo_platform', []));
65+
$expectedDriverConnection = new TracingServerInfoAwareDriverConnection(new TracingDriverConnection($this->hub, $connection, 'other_sql', []));
7466

7567
$this->assertEquals($expectedDriverConnection, $driverConnection);
7668
}

0 commit comments

Comments
 (0)