From b5c414102fdc9404b75a0088a2d27250cd7fba45 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Fri, 27 Oct 2023 16:34:32 +0800 Subject: [PATCH] Set request Op as route (#383) Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/sentry/publish/sentry.php | 6 ++- .../Tracing/Middleware/TraceMiddleware.php | 44 +++++++++++++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/sentry/publish/sentry.php b/src/sentry/publish/sentry.php index a0cc61119..b944918b8 100644 --- a/src/sentry/publish/sentry.php +++ b/src/sentry/publish/sentry.php @@ -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' => [ diff --git a/src/sentry/src/Tracing/Middleware/TraceMiddleware.php b/src/sentry/src/Tracing/Middleware/TraceMiddleware.php index 4148c91cb..2af21f916 100644 --- a/src/sentry/src/Tracing/Middleware/TraceMiddleware.php +++ b/src/sentry/src/Tracing/Middleware/TraceMiddleware.php @@ -11,6 +11,7 @@ namespace FriendsOfHyperf\Sentry\Tracing\Middleware; +use Closure; use FriendsOfHyperf\Sentry\SentryContext; use FriendsOfHyperf\Sentry\Switcher; use FriendsOfHyperf\Sentry\Tracing\TagManager; @@ -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); @@ -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);