Commit d0e1aa8a authored by wanjilong's avatar wanjilong

add: request 增加 traceId

parent 94791b89
...@@ -21,6 +21,7 @@ namespace Api\PhpUtils\Http; ...@@ -21,6 +21,7 @@ namespace Api\PhpUtils\Http;
use Api\PhpUtils\Log\FileLog; use Api\PhpUtils\Log\FileLog;
use Api\PhpUtils\Log\Tracer;
use Api\PhpUtils\Mon\MonUtil; use Api\PhpUtils\Mon\MonUtil;
class HttpUtil class HttpUtil
...@@ -58,6 +59,10 @@ class HttpUtil ...@@ -58,6 +59,10 @@ class HttpUtil
*/ */
public static function get(string $url,array $params = [], int $timeout = 10000,int $retries = 1, array $headers = [], string $proxy = '',array $curl_opts = []): array public static function get(string $url,array $params = [], int $timeout = 10000,int $retries = 1, array $headers = [], string $proxy = '',array $curl_opts = []): array
{ {
// 增加traceId
if(is_array($headers)) {
$headers['X-TRACE-ID'] = Tracer::getTraceId();
}
$url = $url ."?". http_build_query($params); $url = $url ."?". http_build_query($params);
return self::curl($url, $timeout, $retries, $headers, [], $proxy, 'GET', $curl_opts); return self::curl($url, $timeout, $retries, $headers, [], $proxy, 'GET', $curl_opts);
} }
...@@ -90,6 +95,10 @@ class HttpUtil ...@@ -90,6 +95,10 @@ class HttpUtil
*/ */
public static function post(string $url, $post,int $timeout = 10000,int $retries = 1,array $headers = [], string $proxy = '',array $curl_opts = []): array public static function post(string $url, $post,int $timeout = 10000,int $retries = 1,array $headers = [], string $proxy = '',array $curl_opts = []): array
{ {
// 增加traceId
if(is_array($headers)) {
$headers['X-TRACE-ID'] = Tracer::getTraceId();
}
return self::curl($url, $timeout, $retries, $headers, $post, $proxy, 'POST', $curl_opts); return self::curl($url, $timeout, $retries, $headers, $post, $proxy, 'POST', $curl_opts);
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Api\PhpUtils\Http; namespace Api\PhpUtils\Http;
use Api\PhpUtils\Log\FileLog; use Api\PhpUtils\Log\FileLog;
use Api\PhpUtils\Log\Tracer;
use Api\PhpUtils\Mon\MonUtil; use Api\PhpUtils\Mon\MonUtil;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\TransferException; use GuzzleHttp\Exception\TransferException;
...@@ -64,6 +65,12 @@ class Request ...@@ -64,6 +65,12 @@ class Request
if (!empty($timeout)) { if (!empty($timeout)) {
$options['timeout'] = $timeout / 1000; $options['timeout'] = $timeout / 1000;
} }
// 增加traceId
if(is_array($headers)) {
$headers['X-TRACE-ID'] = Tracer::getTraceId();
}
if (!empty($headers)) { if (!empty($headers)) {
$options['headers'] = $headers; $options['headers'] = $headers;
} }
...@@ -140,6 +147,12 @@ class Request ...@@ -140,6 +147,12 @@ class Request
if (!empty($timeout)) { if (!empty($timeout)) {
$options['timeout'] = $timeout / 1000; $options['timeout'] = $timeout / 1000;
} }
// 增加traceId
if(is_array($headers)) {
$headers['X-TRACE-ID'] = Tracer::getTraceId();
}
if (!empty($headers)) { if (!empty($headers)) {
$options['headers'] = $headers; $options['headers'] = $headers;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment