-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds experimental service.instance.id resource detector (#1309)
* implement experimental service.instance.id attribute
- Loading branch information
1 parent
eebf23a
commit 6bf422c
Showing
10 changed files
with
113 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Resource\Detectors; | ||
|
||
use OpenTelemetry\SDK\Common\Attribute\Attributes; | ||
use OpenTelemetry\SDK\Resource\ResourceDetectorInterface; | ||
use OpenTelemetry\SDK\Resource\ResourceInfo; | ||
use OpenTelemetry\SemConv\ResourceAttributes; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
/** | ||
* @see https://github.com/open-telemetry/semantic-conventions/tree/main/docs/resource#service-experimental | ||
*/ | ||
final class Service implements ResourceDetectorInterface | ||
{ | ||
public function getResource(): ResourceInfo | ||
{ | ||
static $serviceInstanceId; | ||
$serviceInstanceId ??= Uuid::uuid4()->toString(); | ||
|
||
$attributes = [ | ||
ResourceAttributes::SERVICE_INSTANCE_ID => $serviceInstanceId, | ||
]; | ||
|
||
return ResourceInfo::create(Attributes::create($attributes), ResourceAttributes::SCHEMA_URL); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Tests\Unit\SDK\Resource\Detectors; | ||
|
||
use OpenTelemetry\SDK\Resource\Detectors; | ||
use OpenTelemetry\SemConv\ResourceAttributes; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(Detectors\Service::class)] | ||
class ServiceTest extends TestCase | ||
{ | ||
const UUID_REGEX = '/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i'; | ||
|
||
private Detectors\Service $detector; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->detector = new Detectors\Service(); | ||
} | ||
|
||
public function test_service_get_resource_with_default_service_instance_id(): void | ||
{ | ||
$resource = $this->detector->getResource(); | ||
|
||
$this->assertNotEmpty($resource->getAttributes()); | ||
|
||
$id = $resource->getAttributes()->get(ResourceAttributes::SERVICE_INSTANCE_ID); | ||
$this->assertMatchesRegularExpression(self::UUID_REGEX, $id); | ||
} | ||
|
||
public function test_service_get_resource_multiple_calls_same_service_instance_id(): void | ||
{ | ||
$resource1 = $this->detector->getResource(); | ||
$resource2 = $this->detector->getResource(); | ||
|
||
$this->assertSame($resource1->getAttributes()->get(ResourceAttributes::SERVICE_INSTANCE_ID), $resource2->getAttributes()->get(ResourceAttributes::SERVICE_INSTANCE_ID)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters