Skip to content

Commit c3d2317

Browse files
cleptricstayallive
andauthored
Add starfish v1 attributes to span data/breadcrumbs (#764)
Co-authored-by: Alex Bouma <[email protected]>
1 parent bf155b8 commit c3d2317

File tree

5 files changed

+128
-14
lines changed

5 files changed

+128
-14
lines changed

src/Sentry/Laravel/EventHandler.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ protected function queryExecutedHandler(DatabaseEvents\QueryExecuted $query): vo
239239
Integration::addBreadcrumb(new Breadcrumb(
240240
Breadcrumb::LEVEL_INFO,
241241
Breadcrumb::TYPE_DEFAULT,
242-
'sql.query',
242+
'db.sql.query',
243243
$query->sql,
244244
$data
245245
));
@@ -287,12 +287,12 @@ protected function httpClientResponseReceivedHandler(HttpClientEvents\ResponseRe
287287
null,
288288
[
289289
'url' => $this->getPartialUri($fullUri),
290-
'method' => $event->request->method(),
291-
'status_code' => $event->response->status(),
290+
'http.request.method' => $event->request->method(),
291+
'http.response.status_code' => $event->response->status(),
292292
'http.query' => $fullUri->getQuery(),
293293
'http.fragment' => $fullUri->getFragment(),
294-
'request_body_size' => $event->request->toPsrRequest()->getBody()->getSize(),
295-
'response_body_size' => $event->response->toPsrResponse()->getBody()->getSize(),
294+
'http.request.body.size' => $event->request->toPsrRequest()->getBody()->getSize(),
295+
'http.response.body.size' => $event->response->toPsrResponse()->getBody()->getSize(),
296296
]
297297
));
298298
}
@@ -312,10 +312,10 @@ protected function httpClientConnectionFailedHandler(HttpClientEvents\Connection
312312
null,
313313
[
314314
'url' => $this->getPartialUri($fullUri),
315-
'method' => $event->request->method(),
315+
'http.request.method' => $event->request->method(),
316316
'http.query' => $fullUri->getQuery(),
317317
'http.fragment' => $fullUri->getFragment(),
318-
'request_body_size' => $event->request->toPsrRequest()->getBody()->getSize(),
318+
'http.request.body.size' => $event->request->toPsrRequest()->getBody()->getSize(),
319319
]
320320
));
321321
}

src/Sentry/Laravel/Tracing/EventHandler.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,22 @@ protected function queryExecutedHandler(DatabaseEvents\QueryExecuted $query): vo
182182
$context = new SpanContext();
183183
$context->setOp('db.sql.query');
184184
$context->setDescription($query->sql);
185+
$context->setData([
186+
'db.name' => $query->connection->getDatabaseName(),
187+
'db.system' => $query->connection->getDriverName(),
188+
'server.address' => $query->connection->getConfig('host'),
189+
'server.port' => $query->connection->getConfig('port'),
190+
]);
185191
$context->setStartTimestamp(microtime(true) - $query->time / 1000);
186192
$context->setEndTimestamp($context->getStartTimestamp() + $query->time / 1000);
187193

188194
if ($this->traceSqlQueryOrigins) {
189195
$queryOrigin = $this->resolveQueryOriginFromBacktrace();
190196

191197
if ($queryOrigin !== null) {
192-
$context->setData(['db.sql.origin' => $queryOrigin]);
198+
$context->setData(array_merge($context->getData(), [
199+
'db.sql.origin' => $queryOrigin
200+
]));
193201
}
194202
}
195203

@@ -302,7 +310,7 @@ protected function httpClientRequestSendingHandler(HttpClientEvents\RequestSendi
302310
$context->setDescription($event->request->method() . ' ' . $partialUri);
303311
$context->setData([
304312
'url' => $partialUri,
305-
'method' => $event->request->method(),
313+
'http.request.method' => $event->request->method(),
306314
'http.query' => $fullUri->getQuery(),
307315
'http.fragment' => $fullUri->getFragment(),
308316
]);

src/Sentry/Laravel/Tracing/Middleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private function startTransaction(Request $request, HubInterface $sentry): void
176176

177177
$context->setData([
178178
'url' => $requestPath,
179-
'method' => strtoupper($request->method()),
179+
'http.request.method' => strtoupper($request->method()),
180180
]);
181181

182182
$transaction = $sentry->startTransaction($context);
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Sentry\Features;
4+
5+
use Illuminate\Database\Events\QueryExecuted;
6+
use Illuminate\Support\Facades\DB;
7+
use Sentry\Laravel\Tests\TestCase;
8+
use Sentry\Tracing\Span;
9+
use Sentry\Tracing\TransactionContext;
10+
11+
class DatabaseIntegrationTest extends TestCase
12+
{
13+
protected function usesMySQL($app): void
14+
{
15+
$app['config']->set('database.default', 'mysql');
16+
$app['config']->set('database.connections.mysql', [
17+
'driver' => 'mysql',
18+
'host' => 'host-mysql',
19+
'port' => 3306,
20+
'username' => 'user-mysql',
21+
'password' => 'password',
22+
'database' => 'db-mysql',
23+
]);
24+
}
25+
26+
protected function usesMySQLFromUrl($app): void
27+
{
28+
$app['config']->set('database.default', 'mysqlurl');
29+
$app['config']->set('database.connections.mysqlurl', [
30+
'driver' => 'mysql',
31+
'url' => 'mysql://user-mysqlurl:password@host-mysqlurl:3307/db-mysqlurl',
32+
]);
33+
}
34+
35+
protected function usesInMemorySqlite($app): void
36+
{
37+
$app['config']->set('database.default', 'inmemory');
38+
$app['config']->set('database.connections.inmemory', [
39+
'driver' => 'sqlite',
40+
'database' => ':memory:',
41+
]);
42+
}
43+
44+
/**
45+
* @define-env usesMySQL
46+
*/
47+
public function testSpanIsCreatedForMySQLConnectionQuery(): void
48+
{
49+
$span = $this->executeQueryAndRetrieveSpan(
50+
$query = 'SELECT "mysql"'
51+
);
52+
53+
$this->assertEquals($query, $span->getDescription());
54+
$this->assertEquals('db.sql.query', $span->getOp());
55+
$this->assertEquals('host-mysql', $span->getData()['server.address']);
56+
$this->assertEquals(3306, $span->getData()['server.port']);
57+
}
58+
59+
/**
60+
* @define-env usesMySQLFromUrl
61+
*/
62+
public function testSpanIsCreatedForMySQLUrlConnectionQuery(): void
63+
{
64+
$span = $this->executeQueryAndRetrieveSpan(
65+
$query = 'SELECT "mysqlurl"'
66+
);
67+
68+
$this->assertEquals($query, $span->getDescription());
69+
$this->assertEquals('db.sql.query', $span->getOp());
70+
$this->assertEquals('host-mysqlurl', $span->getData()['server.address']);
71+
$this->assertEquals(3307, $span->getData()['server.port']);
72+
}
73+
74+
/**
75+
* @define-env usesInMemorySqlite
76+
*/
77+
public function testSpanIsCreatedForSqliteConnectionQuery(): void
78+
{
79+
$span = $this->executeQueryAndRetrieveSpan(
80+
$query = 'SELECT "inmemory"'
81+
);
82+
83+
$this->assertEquals($query, $span->getDescription());
84+
$this->assertEquals('db.sql.query', $span->getOp());
85+
$this->assertNull($span->getData()['server.address']);
86+
$this->assertNull($span->getData()['server.port']);
87+
}
88+
89+
private function executeQueryAndRetrieveSpan(string $query): Span
90+
{
91+
$hub = $this->getHubFromContainer();
92+
93+
$transaction = $hub->startTransaction(new TransactionContext);
94+
$transaction->initSpanRecorder();
95+
96+
$this->getCurrentScope()->setSpan($transaction);
97+
98+
$this->dispatchLaravelEvent(new QueryExecuted($query, [], 123, DB::connection()));
99+
100+
$spans = $transaction->getSpanRecorder()->getSpans();
101+
102+
$this->assertCount(2, $spans);
103+
104+
return $spans[1];
105+
}
106+
}

test/Sentry/Features/HttpClientIntegrationTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public function testHttpClientBreadcrumbIsRecordedForResponseReceivedEvent(): vo
3131

3232
$metadata = $this->getLastBreadcrumb()->getMetadata();
3333

34-
$this->assertEquals('GET', $metadata['method']);
34+
$this->assertEquals('GET', $metadata['http.request.method']);
3535
$this->assertEquals('https://example.com', $metadata['url']);
36-
$this->assertEquals(200, $metadata['status_code']);
37-
$this->assertEquals(7, $metadata['request_body_size']);
38-
$this->assertEquals(8, $metadata['response_body_size']);
36+
$this->assertEquals(200, $metadata['http.response.status_code']);
37+
$this->assertEquals(7, $metadata['http.request.body.size']);
38+
$this->assertEquals(8, $metadata['http.response.body.size']);
3939
}
4040

4141
public function testHttpClientBreadcrumbDoesntConsumeBodyStream(): void

0 commit comments

Comments
 (0)