Skip to content

Commit

Permalink
增加链路追踪中间件,需安装think-tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Jan 29, 2021
1 parent bb72a5e commit fa922b7
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"topthink/framework": "^6.0"
},
"require-dev": {
"symfony/var-dumper": "^4.3|^5.1"
"symfony/var-dumper": "^4.3|^5.1",
"topthink/think-tracing": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
46 changes: 46 additions & 0 deletions src/middleware/TraceRpcClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace think\swoole\middleware;

use think\swoole\rpc\Protocol;
use think\tracing\Tracer;
use Throwable;
use const OpenTracing\Formats\TEXT_MAP;
use const OpenTracing\Tags\ERROR;
use const OpenTracing\Tags\SPAN_KIND;
use const OpenTracing\Tags\SPAN_KIND_RPC_CLIENT;

class TraceRpcClient
{
protected $tracer;

public function __construct(Tracer $tracer)
{
$this->tracer = $tracer;
}

public function handle(Protocol $protocol, $next)
{
$scope = $this->tracer->startActiveSpan(
'rpc.client:' . $protocol->getInterface() . '@' . $protocol->getMethod(),
[
'tags' => [
SPAN_KIND => SPAN_KIND_RPC_CLIENT,
],
]
);
$span = $scope->getSpan();
$context = $protocol->getContext();
$this->tracer->inject($span->getContext(), TEXT_MAP, $context);
$protocol->setContext($context);

try {
return $next($protocol);
} catch (Throwable $e) {
$span->setTag(ERROR, $e);
throw $e;
} finally {
$scope->close();
}
}
}
46 changes: 46 additions & 0 deletions src/middleware/TraceRpcServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace think\swoole\middleware;

use think\swoole\rpc\Protocol;
use think\tracing\Tracer;
use Throwable;
use const OpenTracing\Formats\TEXT_MAP;
use const OpenTracing\Tags\ERROR;
use const OpenTracing\Tags\SPAN_KIND;
use const OpenTracing\Tags\SPAN_KIND_RPC_SERVER;

class TraceRpcServer
{
protected $tracer;

public function __construct(Tracer $tracer)
{
$this->tracer = $tracer;
}

public function handle(Protocol $protocol, $next)
{
$context = $this->tracer->extract(TEXT_MAP, $protocol->getContext());
$scope = $this->tracer->startActiveSpan(
'rpc.server:' . $protocol->getInterface() . '@' . $protocol->getMethod(),
[
'child_of' => $context,
'tags' => [
SPAN_KIND => SPAN_KIND_RPC_SERVER,
],
]
);
$span = $scope->getSpan();

try {
return $next($protocol);
} catch (Throwable $e) {
$span->setTag(ERROR, $e);
throw $e;
} finally {
$scope->close();
$this->tracer->flush();
}
}
}
2 changes: 1 addition & 1 deletion src/rpc/client/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function encodeData(Protocol $protocol)
}
}

$protocol = Protocol::make($protocol->getInterface(), $protocol->getMethod(), $params);
$protocol->setParams($params);

$data = $this->parser->encode($protocol);

Expand Down

0 comments on commit fa922b7

Please sign in to comment.