Skip to content

Commit 2b0c2ec

Browse files
authored
Merge pull request #11 from JimChenWYU/support-ipv6
chore(): support ipv6
2 parents 62aad20 + 1bed752 commit 2b0c2ec

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
composer.phar
2+
composer.lock
23
/.idea/
34
/vendor/
45

src/EasyKin.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ public static function app() {
6565

6666
/**
6767
* @param string $serviceName
68-
* @param string $ipv4
68+
* @param string $ip
6969
* @param int $port
7070
*/
71-
public static function setEndpoint($serviceName, $ipv4, $port)
71+
public static function setEndpoint($serviceName, $ip, $port)
7272
{
73-
\easyops\easykin\core\Endpoint::init($serviceName, $ipv4, $port);
73+
\easyops\easykin\core\Endpoint::init($serviceName, $ip, $port);
7474
}
7575
}

src/Tracer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ public function getLogger()
8989
/**
9090
* @param string $name
9191
* @param string|null $serviceName
92-
* @param string|null $ipv4
92+
* @param string|null $ip
9393
* @param int|null $port
9494
* @return ClientSpan
9595
*/
96-
public function newSpan($name, $serviceName = null, $ipv4 = null, $port = null)
96+
public function newSpan($name, $serviceName = null, $ip = null, $port = null)
9797
{
98-
return $this->trace->newSpan($name, $serviceName, $ipv4, $port);
98+
return $this->trace->newSpan($name, $serviceName, $ip, $port);
9999
}
100100

101101
/**

src/core/ClientSpan.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
namespace easyops\easykin\core;
2828

29-
3029
/**
3130
* Class ClientSpan
3231
* @package anyclouds\easykin
@@ -65,17 +64,19 @@ protected function valueOfEnd()
6564

6665
/**
6766
* @param string $serviceName
68-
* @param string $ipv4
67+
* @param string $ip
6968
* @param int $port
7069
*/
71-
public function setSA($serviceName, $ipv4, $port)
70+
public function setSA($serviceName, $ip, $port)
7271
{
72+
$isIPv6 = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
7373
$this->sa = [
7474
'key' => 'sa',
7575
'value' => true,
7676
'endpoint' => [
7777
'serviceName' => $serviceName,
78-
'ipv4' => $ipv4,
78+
'ipv4' => !$isIPv6 ? $ip : null,
79+
'ipv6' => $isIPv6 ? $ip : null,
7980
'port' => $port
8081
]
8182
];

src/core/Endpoint.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
namespace easyops\easykin\core;
2828

29+
use InvalidArgumentException;
2930

3031
/**
3132
* Class Endpoint
@@ -45,14 +46,24 @@ private function __construct()
4546

4647
/**
4748
* @param string $serviceName
48-
* @param string $ipv4
49+
* @param string $ip
4950
* @param int $port
5051
*/
51-
public static function init($serviceName, $ipv4, $port)
52+
public static function init($serviceName, $ip, $port)
5253
{
54+
$isIPv6 = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
55+
if (! $isIPv6) {
56+
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
57+
throw new InvalidArgumentException(
58+
sprintf('Invalid IPv4. Expected something in the range 0.0.0.0 and 255.255.255.255, got %s', $ip)
59+
);
60+
}
61+
}
62+
5363
static::$endpoint = [
5464
'serviceName' => $serviceName,
55-
'ipv4' => $ipv4,
65+
'ipv4' => !$isIPv6 ? $ip : null,
66+
'ipv6' => $isIPv6 ? $ip : null,
5667
'port' => $port,
5768
];
5869
}
@@ -63,7 +74,7 @@ public static function init($serviceName, $ipv4, $port)
6374
public static function toArray()
6475
{
6576
if (!isset(static::$endpoint['serviceName']) ||
66-
!isset(static::$endpoint['ipv4']) ||
77+
(!isset(static::$endpoint['ipv4']) && !isset(static::$endpoint['ipv6'])) ||
6778
!isset(static::$endpoint['port'])) {
6879
throw new \BadMethodCallException('Endpoint not initialized.');
6980
}

src/core/Trace.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ public function __construct($name, $sampled = null, $traceId = null, $parentSpan
6161
/**
6262
* @param string $name
6363
* @param string|null $serviceName
64-
* @param string|null $ipv4
64+
* @param string|null $ip
6565
* @param int|null $port
6666
* @return ClientSpan
6767
*/
68-
public function newSpan($name, $serviceName = null, $ipv4 = null, $port = null)
68+
public function newSpan($name, $serviceName = null, $ip = null, $port = null)
6969
{
7070
$span = new ClientSpan($name, $this->serverSpan->traceId, $this->serverSpan->id);
71-
!is_null($serviceName) && !is_null($ipv4) && !is_null($port) && $span->setSA($serviceName, $ipv4, $port);
71+
!is_null($serviceName) && !is_null($ip) && !is_null($port) && $span->setSA($serviceName, $ip, $port);
7272
$this->spans[] = $span;
7373
return $span;
7474
}

0 commit comments

Comments
 (0)