Skip to content

Commit

Permalink
Set request Op as route (#383)
Browse files Browse the repository at this point in the history
Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia committed Oct 27, 2023
1 parent cd4e972 commit b5c4141
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/sentry/publish/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,13 @@
'exception.stack_trace' => 'exception.stack_trace',
],
'request' => [
'http.path' => 'http.path',
'http.method' => 'http.method',
'header' => 'request.header',
'body' => 'request.body',
'query_params' => 'request.query_params',
'body' => 'request.body',
'route.params' => 'route.params',
'route.callback' => 'route.callback',
'exception.stack_trace' => 'exception.stack_trace',
],
'rpc' => [
Expand Down
44 changes: 35 additions & 9 deletions src/sentry/src/Tracing/Middleware/TraceMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace FriendsOfHyperf\Sentry\Tracing\Middleware;

use Closure;
use FriendsOfHyperf\Sentry\SentryContext;
use FriendsOfHyperf\Sentry\Switcher;
use FriendsOfHyperf\Sentry\Tracing\TagManager;
Expand Down Expand Up @@ -110,6 +111,22 @@ private function startTransaction(ServerRequestInterface $request, HubInterface
$sentryTrace = $request->getHeaderLine('sentry-trace', '');
$baggage = $request->getHeaderLine('baggage', '');

/** @var Dispatched|null $dispatched */
$dispatched = $request->getAttribute(Dispatched::class);
$route = $path;
$routeParams = [];
$routeCallback = '[unknown_callback]';
if ($dispatched->isFound()) {
$route = $dispatched->handler->route;
$routeParams = $dispatched->params;
$routeCallback = match (true) {
$dispatched->handler->callback instanceof Closure => 'closure',
is_array($dispatched->handler->callback) => implode('::', $dispatched->handler->callback),
is_string($dispatched->handler->callback) => $dispatched->handler->callback,
default => '[unknown_callback]',
};
}

if ($this->container->has(RpcContext::class)) {
$rpcContext = $this->container->get(RpcContext::class);
$carrier = $rpcContext->get(TraceContext::RPC_CARRIER);
Expand All @@ -120,27 +137,36 @@ private function startTransaction(ServerRequestInterface $request, HubInterface
}

$context = continueTrace($sentryTrace, $baggage);
$context->setName($path);
$context->setName($route);
$context->setOp(sprintf('%s.server', $server));
$context->setDescription(sprintf('request: %s %s', $request->getMethod(), $path));
$context->setSource(TransactionSource::url());
$context->setStartTimestamp($startTimestamp);

// Set data
$data = [
'url' => $path,
'http.method' => strtoupper($request->getMethod()),
];
if ($this->tagManager->has('request.query_params')) {
$data[$this->tagManager->get('request.query_params')] = $request->getQueryParams();
$data = [];
if ($this->tagManager->has('request.route.params') && $routeParams) {
$data[$this->tagManager->get('request.route.params')] = $routeParams;
}
if ($this->tagManager->has('request.body')) {
$data[$this->tagManager->get('request.body')] = $request->getParsedBody();
if ($this->tagManager->has('request.query_params') && $queryParams = $request->getQueryParams()) {
$data[$this->tagManager->get('request.query_params')] = $queryParams;
}
if ($this->tagManager->has('request.body') && $parsedBody = $request->getParsedBody()) {
$data[$this->tagManager->get('request.body')] = $parsedBody;
}
$context->setData($data);

// Set tags
$tags = [];
if ($this->tagManager->has('request.http.path')) {
$tags[$this->tagManager->get('request.http.path')] = $path;
}
if ($this->tagManager->has('request.http.method')) {
$tags[$this->tagManager->get('request.http.method')] = strtoupper($request->getMethod());
}
if ($this->tagManager->has('request.route.callback')) {
$tags[$this->tagManager->get('request.route.callback')] = $routeCallback;
}
if ($this->tagManager->has('request.header')) {
foreach ($request->getHeaders() as $key => $value) {
$tags[$this->tagManager->get('request.header') . '.' . $key] = implode(', ', $value);
Expand Down

0 comments on commit b5c4141

Please sign in to comment.