Skip to content

Commit

Permalink
Send span origin (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive authored Aug 14, 2024
1 parent 198ce50 commit 707eb33
Show file tree
Hide file tree
Showing 22 changed files with 97 additions and 66 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"php": "^7.2||^8.0",
"guzzlehttp/psr7": "^2.1.1",
"jean85/pretty-package-versions": "^1.5||^2.0",
"sentry/sentry": "^4.6.1",
"sentry/sentry": "^4.9.0",
"symfony/cache-contracts": "^1.1||^2.4||^3.0",
"symfony/config": "^4.4.20||^5.0.11||^6.0||^7.0",
"symfony/console": "^4.4.20||^5.0.11||^6.0||^7.0",
Expand Down
24 changes: 13 additions & 11 deletions src/EventListener/TracingConsoleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,20 @@ public function handleConsoleCommandEvent(ConsoleCommandEvent $event): void
$currentSpan = $this->hub->getSpan();

if (null === $currentSpan) {
$transactionContext = new TransactionContext();
$transactionContext->setOp('console.command');
$transactionContext->setName($this->getSpanName($command));
$transactionContext->setSource(TransactionSource::task());

$span = $this->hub->startTransaction($transactionContext);
$span = $this->hub->startTransaction(
TransactionContext::make()
->setOp('console.command')
->setOrigin('auto.console')
->setName($this->getSpanName($command))
->setSource(TransactionSource::task())
);
} else {
$spanContext = new SpanContext();
$spanContext->setOp('console.command');
$spanContext->setDescription($this->getSpanName($command));

$span = $currentSpan->startChild($spanContext);
$span = $currentSpan->startChild(
SpanContext::make()
->setOp('console.command')
->setOrigin('auto.console')
->setDescription($this->getSpanName($command))
);
}

$this->hub->setSpan($span);
Expand Down
2 changes: 2 additions & 0 deletions src/EventListener/TracingRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public function handleKernelRequestEvent(RequestEvent $event): void
$request->headers->get('sentry-trace') ?? $request->headers->get('traceparent', ''),
$request->headers->get('baggage', '')
);

$context->setOp('http.server');
$context->setOrigin('auto.http.server');

$routeName = $request->attributes->get('_route');
if (null !== $routeName && \is_string($routeName)) {
Expand Down
23 changes: 13 additions & 10 deletions src/EventListener/TracingSubRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ public function handleKernelRequestEvent(RequestEvent $event): void
return;
}

$spanContext = new SpanContext();
$spanContext->setOp('http.server');
$spanContext->setDescription(\sprintf('%s %s%s%s', $request->getMethod(), $request->getSchemeAndHttpHost(), $request->getBaseUrl(), $request->getPathInfo()));
$spanContext->setData([
'http.request.method' => $request->getMethod(),
'http.url' => $request->getUri(),
'route' => $this->getRouteName($request),
]);

$this->hub->setSpan($span->startChild($spanContext));
$this->hub->setSpan(
$span->startChild(
SpanContext::make()
->setOp('http.server')
->setData([
'http.request.method' => $request->getMethod(),
'http.url' => $request->getUri(),
'route' => $this->getRouteName($request),
])
->setOrigin('auto.http.server')
->setDescription(\sprintf('%s %s%s%s', $request->getMethod(), $request->getSchemeAndHttpHost(), $request->getBaseUrl(), $request->getPathInfo()))
)
);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Tracing/Cache/TraceableCacheAdapterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,10 @@ private function traceFunction(string $spanOperation, \Closure $callback, string
$span = $this->hub->getSpan();

if (null !== $span) {
$spanContext = new SpanContext();
$spanContext->setOp($spanOperation);
$spanContext = SpanContext::make()
->setOp($spanOperation)
->setOrigin('auto.cache');

if (null !== $spanDescription) {
$spanContext->setDescription(urldecode($spanDescription));
}
Expand Down
13 changes: 7 additions & 6 deletions src/Tracing/Doctrine/DBAL/TracingDriverConnectionForV2V3.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,13 @@ private function traceFunction(string $spanOperation, string $spanDescription, \
$span = $this->hub->getSpan();

if (null !== $span) {
$spanContext = new SpanContext();
$spanContext->setOp($spanOperation);
$spanContext->setDescription($spanDescription);
$spanContext->setData($this->spanData);

$span = $span->startChild($spanContext);
$span = $span->startChild(
SpanContext::make()
->setOp($spanOperation)
->setData($this->spanData)
->setOrigin('auto.db')
->setDescription($spanDescription)
);
}

try {
Expand Down
13 changes: 7 additions & 6 deletions src/Tracing/Doctrine/DBAL/TracingDriverConnectionForV4.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,13 @@ private function traceFunction(string $spanOperation, string $spanDescription, \
$span = $this->hub->getSpan();

if (null !== $span) {
$spanContext = new SpanContext();
$spanContext->setOp($spanOperation);
$spanContext->setDescription($spanDescription);
$spanContext->setData($this->spanData);

$span = $span->startChild($spanContext);
$span = $span->startChild(
SpanContext::make()
->setOp($spanOperation)
->setData($this->spanData)
->setOrigin('auto.db')
->setDescription($spanDescription)
);
}

try {
Expand Down
9 changes: 5 additions & 4 deletions src/Tracing/Doctrine/DBAL/TracingStatementForV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le
*/
public function execute($params = null): bool
{
$spanContext = new SpanContext();
$spanContext->setOp(self::SPAN_OP_STMT_EXECUTE);
$spanContext->setDescription($this->sqlQuery);
$spanContext->setData($this->spanData);
$spanContext = SpanContext::make()
->setOp(self::SPAN_OP_STMT_EXECUTE)
->setData($this->spanData)
->setOrigin('auto.db')
->setDescription($this->sqlQuery);

return $this->traceFunction($spanContext, [$this->decoratedStatement, 'execute'], $params);
}
Expand Down
9 changes: 5 additions & 4 deletions src/Tracing/Doctrine/DBAL/TracingStatementForV3.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le
*/
public function execute($params = null): Result
{
$spanContext = new SpanContext();
$spanContext->setOp(self::SPAN_OP_STMT_EXECUTE);
$spanContext->setDescription($this->sqlQuery);
$spanContext->setData($this->spanData);
$spanContext = SpanContext::make()
->setOp(self::SPAN_OP_STMT_EXECUTE)
->setData($this->spanData)
->setOrigin('auto.db')
->setDescription($this->sqlQuery);

return $this->traceFunction($spanContext, [$this->decoratedStatement, 'execute'], $params);
}
Expand Down
9 changes: 5 additions & 4 deletions src/Tracing/Doctrine/DBAL/TracingStatementForV4.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public function bindValue(int|string $param, mixed $value, ParameterType $type):
*/
public function execute(): Result
{
$spanContext = new SpanContext();
$spanContext->setOp(self::SPAN_OP_STMT_EXECUTE);
$spanContext->setDescription($this->sqlQuery);
$spanContext->setData($this->spanData);
$spanContext = SpanContext::make()
->setOp(self::SPAN_OP_STMT_EXECUTE)
->setData($this->spanData)
->setOrigin('auto.db')
->setDescription($this->sqlQuery);

return $this->traceFunction($spanContext, [$this->decoratedStatement, 'execute']);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Tracing/HttpClient/AbstractTraceableHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ public function request(string $method, string $url, array $options = []): Respo
'path' => $uri->getPath(),
]);

$context = new SpanContext();
$context->setOp('http.client');
$context->setDescription($method . ' ' . (string) $partialUri);
$context = SpanContext::make()
->setOp('http.client')
->setOrigin('auto.http.client')
->setDescription($method . ' ' . (string) $partialUri);

$contextData = [
'http.url' => (string) $partialUri,
Expand Down
11 changes: 6 additions & 5 deletions src/Tracing/Twig/TwigTracingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ public function enter(Profile $profile): void
return;
}

$spanContext = new SpanContext();
$spanContext->setOp('view.render');
$spanContext->setDescription($this->getSpanDescription($profile));

$this->spans[$profile] = $transaction->startChild($spanContext);
$this->spans[$profile] = $transaction->startChild(
SpanContext::make()
->setOp('view.render')
->setOrigin('auto.view')
->setDescription($this->getSpanDescription($profile))
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/End2End/App/Messenger/FooMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class FooMessageHandler
public function __invoke(FooMessage $message): void
{
if (!$message->shouldRetry()) {
throw new class() extends \Exception implements UnrecoverableExceptionInterface { };
throw new class extends \Exception implements UnrecoverableExceptionInterface { };
}

throw new \Exception('This is an intentional failure while handling a message of class ' . \get_class($message));
Expand Down
4 changes: 2 additions & 2 deletions tests/EventListener/LoginListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public function authenticationTokenForSymfonyVersionLowerThan54DataProvider(): \

if (version_compare(Kernel::VERSION, '5.0', '<')) {
yield 'If the user is an object implementing the Stringable interface, then the __toString() method is invoked' => [
new LegacyAuthenticatedTokenStub(new class() implements \Stringable {
new LegacyAuthenticatedTokenStub(new class implements \Stringable {
public function __toString(): string
{
return 'foo_user';
Expand All @@ -286,7 +286,7 @@ public function __toString(): string
];
} else {
yield 'If the user is an object implementing the Stringable interface, then the __toString() method is invoked' => [
new AuthenticatedTokenStub(new class() implements \Stringable {
new AuthenticatedTokenStub(new class implements \Stringable {
public function __toString(): string
{
return 'foo_user';
Expand Down
2 changes: 2 additions & 0 deletions tests/EventListener/TracingConsoleListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function handleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHubData
$transactionContext = new TransactionContext();
$transactionContext->setOp('console.command');
$transactionContext->setName('<unnamed command>');
$transactionContext->setOrigin('auto.console');
$transactionContext->setSource(TransactionSource::task());

yield [
Expand All @@ -81,6 +82,7 @@ public function handleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHubData
$transactionContext = new TransactionContext();
$transactionContext->setOp('console.command');
$transactionContext->setName('app:command');
$transactionContext->setOrigin('auto.console');
$transactionContext->setSource(TransactionSource::task());

yield [
Expand Down
15 changes: 14 additions & 1 deletion tests/EventListener/TracingRequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setOrigin('auto.http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setData([
'net.host.port' => '80',
Expand Down Expand Up @@ -134,6 +135,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setOrigin('auto.http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setData([
'net.host.port' => '80',
Expand Down Expand Up @@ -171,6 +173,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setOrigin('auto.http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setData([
'net.host.port' => '80',
Expand Down Expand Up @@ -203,6 +206,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setOrigin('auto.http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setData([
'net.host.port' => '80',
Expand All @@ -226,6 +230,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setName('GET http://127.0.0.1/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setOrigin('auto.http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setData([
'net.host.port' => '80',
Expand Down Expand Up @@ -257,6 +262,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setName('GET app_homepage');
$transactionContext->setSource(TransactionSource::route());
$transactionContext->setOp('http.server');
$transactionContext->setOrigin('auto.http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setData([
'net.host.port' => '80',
Expand All @@ -281,6 +287,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setName('GET http://www.example.com/path');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setOrigin('auto.http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setData([
'net.host.port' => '80',
Expand All @@ -305,6 +312,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setOrigin('auto.http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setData([
'net.host.port' => '80',
Expand All @@ -329,6 +337,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setOrigin('auto.http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setData([
'net.host.port' => '80',
Expand All @@ -347,12 +356,13 @@ public function handleKernelRequestEventDataProvider(): \Generator

$request = Request::create('http://www.example.com/');
$request->server->set('REQUEST_TIME_FLOAT', 1613493597.010275);
$request->attributes->set('_controller', [new class() {}, 'indexAction']);
$request->attributes->set('_controller', [new class {}, 'indexAction']);

$transactionContext = new TransactionContext();
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setOrigin('auto.http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setData([
'net.host.port' => '80',
Expand All @@ -377,6 +387,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setOrigin('auto.http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setData([
'net.host.port' => '80',
Expand All @@ -401,6 +412,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setName('GET http://www.example.com/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setOrigin('auto.http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setData([
'net.host.port' => '80',
Expand All @@ -425,6 +437,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setName('GET http://:/');
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setOrigin('auto.http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setData([
'net.host.port' => '',
Expand Down
2 changes: 1 addition & 1 deletion tests/EventListener/TracingSubRequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
];

$request = Request::create('http://www.example.com/');
$request->attributes->set('_controller', [new class() {}, 'indexAction']);
$request->attributes->set('_controller', [new class {}, 'indexAction']);

$span = new Span();
$span->setOp('http.server');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/IntegrationConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function integrationsDataProvider(): iterable
$environmentIntegration = new EnvironmentIntegration();
$modulesIntegration = new ModulesIntegration();

$userIntegration1 = new class() implements IntegrationInterface {
$userIntegration1 = new class implements IntegrationInterface {
public function setupOnce(): void
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public function testGetWrappedConnection(): void

public function testGetNativeConnection(): void
{
$nativeConnection = new class() {
$nativeConnection = new class {
};

$decoratedConnection = $this->createMock(NativeDriverConnectionInterfaceStub::class);
Expand Down
Loading

0 comments on commit 707eb33

Please sign in to comment.