-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add grpc request and response to entry (#507)
* add grpc request and response to entry * Formatting code * add request and response when using middleware * Formatting code * rename response method to getResponsePayload * add grpc request and response to entry * Formatting code * add request and response when using middleware * Formatting code * rename response method to getResponsePayload * Formatting code * Optimize code * Optimize code * Optimize code * Optimize code * Optimize * Fix return statement in TelescopeMiddleware * Refactor TelescopeMiddleware to return payload instead of an empty string * Refactor TelescopeMiddleware to simplify getRequestPayload method * Refactor getRequestPayload method in RequestHandledListener * Refactor GrpcCoreMiddlewareAspect to handle request and response payloads --------- Co-authored-by: guandeng <[email protected]> Co-authored-by: Deeka Wong <[email protected]>
- Loading branch information
1 parent
491f8b4
commit fca1658
Showing
5 changed files
with
192 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of friendsofhyperf/components. | ||
* | ||
* @link https://github.com/friendsofhyperf/components | ||
* @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
* @contact [email protected] | ||
*/ | ||
|
||
namespace FriendsOfHyperf\Telescope\Aspect; | ||
|
||
use FriendsOfHyperf\Telescope\TelescopeConfig; | ||
use FriendsOfHyperf\Telescope\TelescopeContext; | ||
use Google\Protobuf\Internal\Message; | ||
use Hyperf\Di\Aop\AbstractAspect; | ||
use Hyperf\Di\Aop\ProceedingJoinPoint; | ||
use Hyperf\GrpcServer\CoreMiddleware; | ||
use Throwable; | ||
|
||
use function Hyperf\Tappable\tap; | ||
|
||
class GrpcCoreMiddlewareAspect extends AbstractAspect | ||
{ | ||
public array $classes = [ | ||
CoreMiddleware::class . '::parseMethodParameters', | ||
CoreMiddleware::class . '::handleResponse', | ||
]; | ||
|
||
public function __construct(protected TelescopeConfig $telescopeConfig) | ||
{ | ||
} | ||
|
||
public function process(ProceedingJoinPoint $proceedingJoinPoint) | ||
{ | ||
return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) { | ||
if (! $this->telescopeConfig->isEnable('grpc')) { | ||
return; | ||
} | ||
|
||
match ($proceedingJoinPoint->methodName) { | ||
'parseMethodParameters' => $this->setRequestPayload($result[0] ?? null), | ||
'handleResponse' => $this->setResponsePayload($proceedingJoinPoint->arguments['keys']['message'] ?? null), | ||
default => null, | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
* @param Message|null $message | ||
*/ | ||
protected function setRequestPayload($message) | ||
{ | ||
if (! $message instanceof Message) { | ||
return; | ||
} | ||
|
||
try { | ||
$payload = json_decode($message->serializeToJsonString(), true); | ||
} catch (Throwable $e) { | ||
return; | ||
} | ||
|
||
TelescopeContext::setGrpcRequestPayload($payload); | ||
} | ||
|
||
/** | ||
* @param Message|null $message | ||
*/ | ||
protected function setResponsePayload($message) | ||
{ | ||
if (! $message instanceof Message) { | ||
return; | ||
} | ||
|
||
try { | ||
$payload = json_decode($message->serializeToJsonString(), true); | ||
} catch (Throwable $e) { | ||
return; | ||
} | ||
|
||
TelescopeContext::setGrpcResponsePayload($payload); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters