Skip to content

Commit

Permalink
Merge pull request #11 from JimChenWYU/support-ipv6
Browse files Browse the repository at this point in the history
chore(): support ipv6
  • Loading branch information
stallman-cui authored Nov 14, 2022
2 parents 62aad20 + 1bed752 commit 2b0c2ec
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
composer.phar
composer.lock
/.idea/
/vendor/

Expand Down
6 changes: 3 additions & 3 deletions src/EasyKin.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public static function app() {

/**
* @param string $serviceName
* @param string $ipv4
* @param string $ip
* @param int $port
*/
public static function setEndpoint($serviceName, $ipv4, $port)
public static function setEndpoint($serviceName, $ip, $port)
{
\easyops\easykin\core\Endpoint::init($serviceName, $ipv4, $port);
\easyops\easykin\core\Endpoint::init($serviceName, $ip, $port);
}
}
6 changes: 3 additions & 3 deletions src/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ public function getLogger()
/**
* @param string $name
* @param string|null $serviceName
* @param string|null $ipv4
* @param string|null $ip
* @param int|null $port
* @return ClientSpan
*/
public function newSpan($name, $serviceName = null, $ipv4 = null, $port = null)
public function newSpan($name, $serviceName = null, $ip = null, $port = null)
{
return $this->trace->newSpan($name, $serviceName, $ipv4, $port);
return $this->trace->newSpan($name, $serviceName, $ip, $port);
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/core/ClientSpan.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

namespace easyops\easykin\core;


/**
* Class ClientSpan
* @package anyclouds\easykin
Expand Down Expand Up @@ -65,17 +64,19 @@ protected function valueOfEnd()

/**
* @param string $serviceName
* @param string $ipv4
* @param string $ip
* @param int $port
*/
public function setSA($serviceName, $ipv4, $port)
public function setSA($serviceName, $ip, $port)
{
$isIPv6 = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
$this->sa = [
'key' => 'sa',
'value' => true,
'endpoint' => [
'serviceName' => $serviceName,
'ipv4' => $ipv4,
'ipv4' => !$isIPv6 ? $ip : null,
'ipv6' => $isIPv6 ? $ip : null,
'port' => $port
]
];
Expand Down
19 changes: 15 additions & 4 deletions src/core/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace easyops\easykin\core;

use InvalidArgumentException;

/**
* Class Endpoint
Expand All @@ -45,14 +46,24 @@ private function __construct()

/**
* @param string $serviceName
* @param string $ipv4
* @param string $ip
* @param int $port
*/
public static function init($serviceName, $ipv4, $port)
public static function init($serviceName, $ip, $port)
{
$isIPv6 = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
if (! $isIPv6) {
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
throw new InvalidArgumentException(
sprintf('Invalid IPv4. Expected something in the range 0.0.0.0 and 255.255.255.255, got %s', $ip)
);
}
}

static::$endpoint = [
'serviceName' => $serviceName,
'ipv4' => $ipv4,
'ipv4' => !$isIPv6 ? $ip : null,
'ipv6' => $isIPv6 ? $ip : null,
'port' => $port,
];
}
Expand All @@ -63,7 +74,7 @@ public static function init($serviceName, $ipv4, $port)
public static function toArray()
{
if (!isset(static::$endpoint['serviceName']) ||
!isset(static::$endpoint['ipv4']) ||
(!isset(static::$endpoint['ipv4']) && !isset(static::$endpoint['ipv6'])) ||
!isset(static::$endpoint['port'])) {
throw new \BadMethodCallException('Endpoint not initialized.');
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/Trace.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ public function __construct($name, $sampled = null, $traceId = null, $parentSpan
/**
* @param string $name
* @param string|null $serviceName
* @param string|null $ipv4
* @param string|null $ip
* @param int|null $port
* @return ClientSpan
*/
public function newSpan($name, $serviceName = null, $ipv4 = null, $port = null)
public function newSpan($name, $serviceName = null, $ip = null, $port = null)
{
$span = new ClientSpan($name, $this->serverSpan->traceId, $this->serverSpan->id);
!is_null($serviceName) && !is_null($ipv4) && !is_null($port) && $span->setSA($serviceName, $ipv4, $port);
!is_null($serviceName) && !is_null($ip) && !is_null($port) && $span->setSA($serviceName, $ip, $port);
$this->spans[] = $span;
return $span;
}
Expand Down

0 comments on commit 2b0c2ec

Please sign in to comment.