Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Jun 2, 2023
1 parent 6373619 commit 3075351
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 28 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
"require": {
"php": ">=7.1.0",
"topthink/framework": "^6.0",
"opentracing/opentracing": "^1.0"
"opentracing/opentracing": "^1.0",
"guzzlehttp/guzzle": "^7.0"
},
"suggest": {
"jcchavezs/zipkin-opentracing": "zipkin",
"jonahgeorge/jaeger-client-php": "jaeger"
},
"require-dev": {
"jcchavezs/zipkin-opentracing": "^1.0||^2.0",
"jcchavezs/zipkin-opentracing": "^1.0 || ^2.0",
"jonahgeorge/jaeger-client-php": "^1.0"
},
"extra": {
Expand Down
44 changes: 44 additions & 0 deletions src/reporter/HttpClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace think\tracing\reporter;

use GuzzleHttp\Client;
use think\helper\Arr;
use Zipkin\Reporters\Http\ClientFactory;

class HttpClientFactory implements ClientFactory
{

private function __construct()
{
}

public static function create(): self
{
return new self();
}

public function build(array $options): callable
{
$client = new Client([
'timeout' => Arr::get($options, 'timeout', 10),
]);

return static function (string $payload) use ($client, $options): void {
$additionalHeaders = $options['headers'] ?? [];

$requiredHeaders = [
'Content-Type' => 'application/json',
'Content-Length' => strlen($payload),
'b3' => '0',
];

$headers = array_merge($additionalHeaders, $requiredHeaders);

$client->post($options['endpoint_url'], [
'body' => $payload,
'headers' => $headers,
]);
};
}
}
11 changes: 1 addition & 10 deletions src/reporter/RedisReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,6 @@ public function push(string $spans)

public function pop()
{
$key = $this->key();

$count = $this->redis->lLen($key);

if ($count > 0) {
$end = min(50, $count) - 1;
[$list] = $this->redis->multi()->lRange($key, 0, $end)->lTrim($key, $end + 1, -1)->exec();
return $list;
}
return [];
return $this->redis->lPop($this->key());
}
}
27 changes: 11 additions & 16 deletions src/reporter/ZipkinReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

namespace think\tracing\reporter;

use RuntimeException;
use Exception;
use Zipkin\Reporter;
use Zipkin\Reporters\Http\CurlFactory;
use Zipkin\Reporters\JsonV2Serializer;

class ZipkinReporter implements Reporter, AsyncReporter
{

protected $reporter;
protected $serializer;
protected $clientFactory;
Expand All @@ -20,32 +18,29 @@ public function __construct(RedisReporter $reporter, $options)
{
$this->reporter = $reporter;
$this->serializer = new JsonV2Serializer();
$this->clientFactory = CurlFactory::create();
$this->clientFactory = HttpClientFactory::create();
$this->options = $options;
}

public function report(array $spans): void
{
$this->reporter->push(serialize($spans));
$this->reporter->push($this->serializer->serialize($spans));
}

public function flush()
{
$client = $this->clientFactory->build($this->options);

while (true) {
$list = $this->reporter->pop();
$spans = array_reduce($list, function ($carry, $item) {
return array_merge($carry, unserialize($item));
}, []);

$payload = $this->serializer->serialize($spans);
try {
$client($payload);
} catch (RuntimeException $e) {

$spans = $this->reporter->pop();
if (!empty($spans)) {
try {
$client($spans);
} catch (Exception $e) {
}
} else {
sleep(5);
}
sleep(5);
}
}
}

0 comments on commit 3075351

Please sign in to comment.