Skip to content

Commit

Permalink
Add starfish v1 attributes to span data/breadcrumbs (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric authored Aug 29, 2023
1 parent 542b6a2 commit b172a75
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 69 deletions.
8 changes: 4 additions & 4 deletions src/EventListener/TracingRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function handleKernelRequestEvent(RequestEvent $event): void
}

$context->setStartTimestamp($requestStartTime);
$context->setTags($this->getTags($request));
$context->setData($this->getData($request));

$this->hub->setSpan($this->hub->startTransaction($context));
}
Expand All @@ -76,19 +76,19 @@ public function handleKernelTerminateEvent(TerminateEvent $event): void
}

/**
* Gets the tags to attach to the transaction.
* Gets the data to attach to the transaction.
*
* @param Request $request The HTTP request
*
* @return array<string, string>
*/
private function getTags(Request $request): array
private function getData(Request $request): array
{
$client = $this->hub->getClient();
$httpFlavor = $this->getHttpFlavor($request);
$tags = [
'net.host.port' => (string) $request->getPort(),
'http.method' => $request->getMethod(),
'http.request.method' => $request->getMethod(),
'http.url' => $request->getUri(),
'route' => $this->getRouteName($request),
];
Expand Down
4 changes: 2 additions & 2 deletions src/EventListener/TracingSubRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public function handleKernelRequestEvent(RequestEvent $event): void
$spanContext = new SpanContext();
$spanContext->setOp('http.server');
$spanContext->setDescription(sprintf('%s %s%s%s', $request->getMethod(), $request->getSchemeAndHttpHost(), $request->getBaseUrl(), $request->getPathInfo()));
$spanContext->setTags([
'http.method' => $request->getMethod(),
$spanContext->setData([
'http.request.method' => $request->getMethod(),
'http.url' => $request->getUri(),
'route' => $this->getRouteName($request),
]);
Expand Down
14 changes: 7 additions & 7 deletions src/Tracing/Doctrine/DBAL/TracingDriverConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function __construct(
) {
$this->hub = $hub;
$this->decoratedConnection = $decoratedConnection;
$this->spanData = $this->getSpanTags($databasePlatform, $params);
$this->spanData = $this->getSpanData($databasePlatform, $params);
}

/**
Expand Down Expand Up @@ -258,7 +258,7 @@ private function traceFunction(string $spanOperation, string $spanDescription, \
*
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md
*/
private function getSpanTags(string $databasePlatform, array $params): array
private function getSpanData(string $databasePlatform, array $params): array
{
$data = ['db.system' => $databasePlatform];

Expand All @@ -272,20 +272,20 @@ private function getSpanTags(string $databasePlatform, array $params): array

if (isset($params['host']) && !empty($params['host']) && !isset($params['memory'])) {
if (false === filter_var($params['host'], \FILTER_VALIDATE_IP)) {
$data['net.peer.name'] = $params['host'];
$data['server.address'] = $params['host'];
} else {
$data['net.peer.ip'] = $params['host'];
$data['server.address'] = $params['host'];
}
}

if (isset($params['port'])) {
$data['net.peer.port'] = (string) $params['port'];
$data['server.port'] = (string) $params['port'];
}

if (isset($params['unix_socket'])) {
$data['net.transport'] = 'Unix';
$data['server.socket.address'] = 'Unix';
} elseif (isset($params['memory'])) {
$data['net.transport'] = 'inproc';
$data['server.socket.address'] = 'inproc';
}

return $data;
Expand Down
18 changes: 11 additions & 7 deletions src/Tracing/HttpClient/AbstractTraceableHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,18 @@ public function request(string $method, string $url, array $options = []): Respo
$context = new SpanContext();
$context->setOp('http.client');
$context->setDescription($method . ' ' . (string) $partialUri);
$context->setTags([
'http.method' => $method,

$contextData = [
'http.url' => (string) $partialUri,
]);
$context->setData([
'http.query' => $uri->getQuery(),
'http.fragment' => $uri->getFragment(),
]);
'http.request.method' => $method,
];
if ('' !== $uri->getQuery()) {
$contextData['http.query'] = $uri->getQuery();
}
if ('' !== $uri->getFragment()) {
$contextData['http.fragment'] = $uri->getFragment();
}
$context->setData($contextData);

$childSpan = $span->startChild($context);

Expand Down
48 changes: 24 additions & 24 deletions tests/EventListener/TracingRequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
$transactionContext->setData([
'net.host.port' => '80',
'http.method' => 'GET',
'http.request.method' => 'GET',
'http.url' => 'http://www.example.com/',
'http.flavor' => '1.1',
'route' => '<unknown>',
Expand Down Expand Up @@ -135,9 +135,9 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
$transactionContext->setData([
'net.host.port' => '80',
'http.method' => 'GET',
'http.request.method' => 'GET',
'http.url' => 'http://www.example.com/',
'http.flavor' => '1.1',
'route' => '<unknown>',
Expand Down Expand Up @@ -167,9 +167,9 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
$transactionContext->setData([
'net.host.port' => '80',
'http.method' => 'GET',
'http.request.method' => 'GET',
'http.url' => 'http://www.example.com/',
'http.flavor' => '1.1',
'route' => '<unknown>',
Expand All @@ -190,9 +190,9 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
$transactionContext->setData([
'net.host.port' => '80',
'http.method' => 'GET',
'http.request.method' => 'GET',
'http.url' => 'http://127.0.0.1/',
'http.flavor' => '1.1',
'route' => '<unknown>',
Expand Down Expand Up @@ -221,9 +221,9 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setSource(TransactionSource::route());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
$transactionContext->setData([
'net.host.port' => '80',
'http.method' => 'GET',
'http.request.method' => 'GET',
'http.url' => 'http://www.example.com/path',
'http.flavor' => '1.1',
'route' => 'app_homepage',
Expand All @@ -245,9 +245,9 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
$transactionContext->setData([
'net.host.port' => '80',
'http.method' => 'GET',
'http.request.method' => 'GET',
'http.url' => 'http://www.example.com/path',
'http.flavor' => '1.1',
'route' => '/path',
Expand All @@ -269,9 +269,9 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
$transactionContext->setData([
'net.host.port' => '80',
'http.method' => 'GET',
'http.request.method' => 'GET',
'http.url' => 'http://www.example.com/',
'http.flavor' => '1.1',
'route' => 'App\\Controller::indexAction',
Expand All @@ -293,9 +293,9 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
$transactionContext->setData([
'net.host.port' => '80',
'http.method' => 'GET',
'http.request.method' => 'GET',
'http.url' => 'http://www.example.com/',
'http.flavor' => '1.1',
'route' => 'App\\Controller::indexAction',
Expand All @@ -317,9 +317,9 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
$transactionContext->setData([
'net.host.port' => '80',
'http.method' => 'GET',
'http.request.method' => 'GET',
'http.url' => 'http://www.example.com/',
'http.flavor' => '1.1',
'route' => 'class@anonymous::indexAction',
Expand All @@ -341,9 +341,9 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
$transactionContext->setData([
'net.host.port' => '80',
'http.method' => 'GET',
'http.request.method' => 'GET',
'http.url' => 'http://www.example.com/',
'http.flavor' => '1.1',
'route' => '<unknown>',
Expand All @@ -365,9 +365,9 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
$transactionContext->setData([
'net.host.port' => '80',
'http.method' => 'GET',
'http.request.method' => 'GET',
'http.url' => 'http://www.example.com/',
'http.flavor' => '1.1',
'route' => '<unknown>',
Expand All @@ -389,9 +389,9 @@ public function handleKernelRequestEventDataProvider(): \Generator
$transactionContext->setSource(TransactionSource::url());
$transactionContext->setOp('http.server');
$transactionContext->setStartTimestamp(1613493597.010275);
$transactionContext->setTags([
$transactionContext->setData([
'net.host.port' => '',
'http.method' => 'GET',
'http.request.method' => 'GET',
'http.url' => 'http://:/',
'route' => '<unknown>',
'net.host.name' => '',
Expand Down
16 changes: 8 additions & 8 deletions tests/EventListener/TracingSubRequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public function handleKernelRequestEventDataProvider(): \Generator
$span = new Span();
$span->setOp('http.server');
$span->setDescription('GET http://www.example.com/path');
$span->setTags([
'http.method' => 'GET',
$span->setData([
'http.request.method' => 'GET',
'http.url' => 'http://www.example.com/path',
'route' => 'App\\Controller::indexAction',
]);
Expand All @@ -92,8 +92,8 @@ public function handleKernelRequestEventDataProvider(): \Generator
$span = new Span();
$span->setOp('http.server');
$span->setDescription('GET http://www.example.com/');
$span->setTags([
'http.method' => 'GET',
$span->setData([
'http.request.method' => 'GET',
'http.url' => 'http://www.example.com/',
'route' => 'App\\Controller::indexAction',
]);
Expand All @@ -109,8 +109,8 @@ public function handleKernelRequestEventDataProvider(): \Generator
$span = new Span();
$span->setOp('http.server');
$span->setDescription('GET http://www.example.com/');
$span->setTags([
'http.method' => 'GET',
$span->setData([
'http.request.method' => 'GET',
'http.url' => 'http://www.example.com/',
'route' => 'class@anonymous::indexAction',
]);
Expand All @@ -126,8 +126,8 @@ public function handleKernelRequestEventDataProvider(): \Generator
$span = new Span();
$span->setOp('http.server');
$span->setDescription('GET http://www.example.com/');
$span->setTags([
'http.method' => 'GET',
$span->setData([
'http.request.method' => 'GET',
'http.url' => 'http://www.example.com/',
'route' => '<unknown>',
]);
Expand Down
12 changes: 6 additions & 6 deletions tests/Tracing/Doctrine/DBAL/TracingDriverConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,8 @@ public function spanDataDataProvider(): \Generator
'db.system' => 'foo_platform',
'db.user' => 'root',
'db.name' => 'INFORMATION_SCHEMA',
'net.peer.port' => '3306',
'net.transport' => 'Unix',
'server.port' => '3306',
'server.socket.address' => 'Unix',
],
];

Expand All @@ -482,8 +482,8 @@ public function spanDataDataProvider(): \Generator
'db.system' => 'foo_platform',
'db.user' => 'root',
'db.name' => 'INFORMATION_SCHEMA',
'net.peer.port' => '3306',
'net.transport' => 'inproc',
'server.port' => '3306',
'server.socket.address' => 'inproc',
],
];

Expand All @@ -493,7 +493,7 @@ public function spanDataDataProvider(): \Generator
],
[
'db.system' => 'foo_platform',
'net.peer.name' => 'localhost',
'server.address' => 'localhost',
],
];

Expand All @@ -503,7 +503,7 @@ public function spanDataDataProvider(): \Generator
],
[
'db.system' => 'foo_platform',
'net.peer.ip' => '127.0.0.1',
'server.address' => '127.0.0.1',
],
];
}
Expand Down
Loading

0 comments on commit b172a75

Please sign in to comment.