Refactored ResourceInfo and ResourceInfoFactory Unit and Integration tests (#664)

This commit is contained in:
Kishan Sangani 2022-04-20 23:17:52 +05:30 committed by GitHub
parent 1d5283b9e4
commit 5de0986f86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 359 additions and 288 deletions

View File

@ -0,0 +1,187 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\Tests\Integration\SDK\Resource;
use AssertWell\PHPUnitGlobalState\EnvironmentVariables;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SemConv\ResourceAttributes;
use PHPUnit\Framework\TestCase;
/**
* @coversNothing
*/
class ResourceInfoFactoryTest extends TestCase
{
use EnvironmentVariables;
public function tearDown(): void
{
$this->restoreEnvironmentVariables();
}
public function test_all_default_resources(): void
{
$resource = ResourceInfoFactory::defaultResource();
$this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl());
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::HOST_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_TYPE));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_DESCRIPTION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_VERSION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_PID));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_EXECUTABLE_PATH));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND_ARGS));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_VERSION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_VERSION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
$this->assertEquals('opentelemetry', $resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertEquals('php', $resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertEquals('unknown_service', $resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
}
public function test_none_default_resources(): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'none');
$resource = ResourceInfoFactory::defaultResource();
$this->assertNull($resource->getSchemaUrl());
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_TYPE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_DESCRIPTION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_PID));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_EXECUTABLE_PATH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND_ARGS));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
}
public function test_env_default_resources(): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'env');
$this->setEnvironmentVariable('OTEL_SERVICE_NAME', 'test-service');
$resource = ResourceInfoFactory::defaultResource();
$this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl());
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_TYPE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_DESCRIPTION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_PID));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_EXECUTABLE_PATH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND_ARGS));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_VERSION));
$this->assertEquals('test-service', $resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
}
public function test_os_and_host_default_resources(): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'os,host');
$resource = ResourceInfoFactory::defaultResource();
$this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl());
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::HOST_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_TYPE));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_DESCRIPTION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_PID));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_EXECUTABLE_PATH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND_ARGS));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
}
public function test_process_and_process_runtime_default_resources(): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'process,process_runtime');
$resource = ResourceInfoFactory::defaultResource();
$this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl());
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_TYPE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_DESCRIPTION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_VERSION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_PID));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_EXECUTABLE_PATH));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND_ARGS));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
}
public function test_sdk_and_sdk_provided_default_resources(): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'sdk,sdk_provided');
$resource = ResourceInfoFactory::defaultResource();
$this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl());
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_TYPE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_DESCRIPTION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_PID));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_EXECUTABLE_PATH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND_ARGS));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_VERSION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_VERSION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
$this->assertEquals('opentelemetry', $resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertEquals('php', $resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertEquals('unknown_service', $resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
}
}

View File

@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\Tests\Integration\SDK\Resource;
use AssertWell\PHPUnitGlobalState\EnvironmentVariables;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Resource\Detectors;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use PHPUnit\Framework\TestCase;
/**
* @coversNothing
*/
class ResourceInfoTest extends TestCase
{
use EnvironmentVariables;
public function tearDown(): void
{
$this->restoreEnvironmentVariables();
}
/**
* @dataProvider environmentResourceProvider
* @group compliance
*/
public function test_resource_from_environment(string $envAttributes, array $userAttributes, array $expected): void
{
$this->setEnvironmentVariable('OTEL_RESOURCE_ATTRIBUTES', $envAttributes);
$resource = (new Detectors\Composite([
new Detectors\Constant(ResourceInfo::create(new Attributes($userAttributes))),
new Detectors\Environment(),
]))->getResource();
foreach ($expected as $name => $value) {
$this->assertSame($value, $resource->getAttributes()->get($name));
}
}
public function environmentResourceProvider()
{
return [
'attributes from env var' => [
'foo=foo,bar=bar',
[],
['foo' => 'foo', 'bar' => 'bar'],
],
'user attributes have higher priority' => [
'foo=env-foo,bar=env-bar,baz=env-baz',
['foo' => 'user-foo', 'bar' => 'user-bar'],
['foo' => 'user-foo', 'bar' => 'user-bar', 'baz' => 'env-baz'],
],
];
}
}

View File

@ -0,0 +1,116 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\SDK\Resource;
use AssertWell\PHPUnitGlobalState\EnvironmentVariables;
use InvalidArgumentException;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use PHPUnit\Framework\TestCase;
/**
* @covers OpenTelemetry\SDK\Resource\ResourceInfoFactory
*/
class ResourceInfoFactoryTest extends TestCase
{
use EnvironmentVariables;
public function tearDown(): void
{
$this->restoreEnvironmentVariables();
}
public function test_empty_resource(): void
{
$resource = ResourceInfoFactory::emptyResource();
$this->assertEmpty($resource->getAttributes());
}
public function test_merge(): void
{
$primary = ResourceInfo::create(new Attributes(['name' => 'primary', 'empty' => '']));
$secondary = ResourceInfo::create(new Attributes(['version' => '1.0.0', 'empty' => 'value']));
$result = ResourceInfoFactory::merge($primary, $secondary);
$name = $result->getAttributes()->get('name');
$version = $result->getAttributes()->get('version');
$empty = $result->getAttributes()->get('empty');
$this->assertCount(3, $result->getAttributes());
$this->assertEquals('primary', $name);
$this->assertEquals('1.0.0', $version);
$this->assertEquals('', $empty);
}
/**
* @dataProvider schemaUrlsToMergeProvider
*/
public function test_merge_schema_url(array $schemaUrlsToMerge, ?string $expectedSchemaUrl): void
{
$resourcesToMerge = [];
foreach ($schemaUrlsToMerge as $schemaUrl) {
$resourcesToMerge[] = ResourceInfo::create(new Attributes([]), $schemaUrl);
}
$result = ResourceInfoFactory::merge(...$resourcesToMerge);
$this->assertEquals($expectedSchemaUrl, $result->getSchemaUrl());
}
public function schemaUrlsToMergeProvider()
{
yield 'Should keep old schemaUrl when the updating one is empty' => [['http://url', null], 'http://url'];
yield 'Should override empty old schemaUrl with non-empty updating one' => [[null, 'http://url'], 'http://url'];
yield 'Should keep matching schemaUrls' => [['http://url', 'http://url'], 'http://url'];
yield 'Should resolve an empty schemaUrl when the old and the updating are not equal' => [['http://url-1', 'http://url-2'], null];
yield 'Should keep empty schemaUrl when not equal schemas have been merged before' => [['http://url-1', 'http://url-2', 'http://url-2'], null];
}
public function test_resource_service_name_default(): void
{
$resource = ResourceInfoFactory::defaultResource();
$this->assertEquals('unknown_service', $resource->getAttributes()->get('service.name'));
}
/**
* @group compliance
*/
public function test_resource_with_empty_environment_variable(): void
{
$this->setEnvironmentVariable('OTEL_RESOURCE_ATTRIBUTES', '');
$this->assertInstanceOf(ResourceInfo::class, ResourceInfoFactory::defaultResource());
}
/**
* @group compliance
*/
public function test_resource_with_invalid_environment_variable(): void
{
$this->setEnvironmentVariable('OTEL_RESOURCE_ATTRIBUTES', 'foo');
$this->expectException(InvalidArgumentException::class);
$this->assertInstanceOf(ResourceInfo::class, ResourceInfoFactory::defaultResource());
}
/**
* @group compliance
*/
public function test_resource_from_environment_service_name_takes_precedence_over_resource_attribute(): void
{
$this->setEnvironmentVariable('OTEL_RESOURCE_ATTRIBUTES', 'service.name=bar');
$this->setEnvironmentVariable('OTEL_SERVICE_NAME', 'foo');
$resource = ResourceInfoFactory::defaultResource();
$this->assertEquals('foo', $resource->getAttributes()->get('service.name'));
}
/**
* @group compliance
*/
public function test_resource_from_environment_resource_attribute_takes_precedence_over_default(): void
{
$this->setEnvironmentVariable('OTEL_RESOURCE_ATTRIBUTES', 'service.name=foo');
$resource = ResourceInfoFactory::defaultResource();
$this->assertEquals('foo', $resource->getAttributes()->get('service.name'));
}
}

View File

@ -6,7 +6,6 @@ namespace OpenTelemetry\Tests\Unit\SDK\Resource;
use AssertWell\PHPUnitGlobalState\EnvironmentVariables;
use Composer\InstalledVersions;
use InvalidArgumentException;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Resource\Detectors;
use OpenTelemetry\SDK\Resource\ResourceInfo;
@ -26,12 +25,6 @@ class ResourceInfoTest extends TestCase
$this->restoreEnvironmentVariables();
}
public function test_empty_resource(): void
{
$resource = ResourceInfoFactory::emptyResource();
$this->assertEmpty($resource->getAttributes());
}
public function test_get_attributes(): void
{
$attributes = new Attributes();
@ -62,209 +55,6 @@ class ResourceInfoTest extends TestCase
$this->assertSame('test', $name);
}
public function test_all_default_resources(): void
{
$resource = ResourceInfoFactory::defaultResource();
$this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl());
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::HOST_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_TYPE));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_DESCRIPTION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_VERSION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_PID));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_EXECUTABLE_PATH));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND_ARGS));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_VERSION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_VERSION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
$this->assertEquals('opentelemetry', $resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertEquals('php', $resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertEquals('unknown_service', $resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
}
public function test_none_default_resources(): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'none');
$resource = ResourceInfoFactory::defaultResource();
$this->assertNull($resource->getSchemaUrl());
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_TYPE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_DESCRIPTION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_PID));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_EXECUTABLE_PATH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND_ARGS));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
}
public function test_env_default_resources(): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'env');
$this->setEnvironmentVariable('OTEL_SERVICE_NAME', 'test-service');
$resource = ResourceInfoFactory::defaultResource();
$this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl());
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_TYPE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_DESCRIPTION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_PID));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_EXECUTABLE_PATH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND_ARGS));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_VERSION));
$this->assertEquals('test-service', $resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
}
public function test_os_and_host_default_resources(): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'os,host');
$resource = ResourceInfoFactory::defaultResource();
$this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl());
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::HOST_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_TYPE));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_DESCRIPTION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::OS_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_PID));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_EXECUTABLE_PATH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND_ARGS));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
}
public function test_process_and_process_runtime_default_resources(): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'process,process_runtime');
$resource = ResourceInfoFactory::defaultResource();
$this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl());
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_TYPE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_DESCRIPTION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_VERSION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_PID));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_EXECUTABLE_PATH));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND_ARGS));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
}
public function test_sdk_and_sdk_provided_default_resources(): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'sdk,sdk_provided');
$resource = ResourceInfoFactory::defaultResource();
$this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl());
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_TYPE));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_DESCRIPTION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::OS_VERSION));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_PID));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_EXECUTABLE_PATH));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_COMMAND_ARGS));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_NAME));
$this->assertNull($resource->getAttributes()->get(ResourceAttributes::PROCESS_RUNTIME_VERSION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_VERSION));
$this->assertNotNull($resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
$this->assertEquals('opentelemetry', $resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME));
$this->assertEquals('php', $resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_LANGUAGE));
$this->assertEquals('unknown_service', $resource->getAttributes()->get(ResourceAttributes::SERVICE_NAME));
}
public function test_merge(): void
{
$primary = ResourceInfo::create(new Attributes(['name' => 'primary', 'empty' => '']));
$secondary = ResourceInfo::create(new Attributes(['version' => '1.0.0', 'empty' => 'value']));
$result = ResourceInfoFactory::merge($primary, $secondary);
$name = $result->getAttributes()->get('name');
$version = $result->getAttributes()->get('version');
$empty = $result->getAttributes()->get('empty');
$this->assertCount(3, $result->getAttributes());
$this->assertEquals('primary', $name);
$this->assertEquals('1.0.0', $version);
$this->assertEquals('', $empty);
}
/**
* @dataProvider schemaUrlsToMergeProvider
*/
public function test_merge_schema_url(array $schemaUrlsToMerge, ?string $expectedSchemaUrl): void
{
$resourcesToMerge = [];
foreach ($schemaUrlsToMerge as $schemaUrl) {
$resourcesToMerge[] = ResourceInfo::create(new Attributes([]), $schemaUrl);
}
$result = ResourceInfoFactory::merge(...$resourcesToMerge);
$this->assertEquals($expectedSchemaUrl, $result->getSchemaUrl());
}
public function schemaUrlsToMergeProvider()
{
yield 'Should keep old schemaUrl when the updating one is empty' => [['http://url', null], 'http://url'];
yield 'Should override empty old schemaUrl with non-empty updating one' => [[null, 'http://url'], 'http://url'];
yield 'Should keep matching schemaUrls' => [['http://url', 'http://url'], 'http://url'];
yield 'Should resolve an empty schemaUrl when the old and the updating are not equal' => [['http://url-1', 'http://url-2'], null];
yield 'Should keep empty schemaUrl when not equal schemas have been merged before' => [['http://url-1', 'http://url-2', 'http://url-2'], null];
}
public function test_immutable_create(): void
{
$attributes = new Attributes();
@ -280,84 +70,6 @@ class ResourceInfoTest extends TestCase
$this->assertEquals('1.0.0', $version);
}
/**
* @dataProvider environmentResourceProvider
* @group compliance
*/
public function test_resource_from_environment(string $envAttributes, array $userAttributes, array $expected): void
{
$this->setEnvironmentVariable('OTEL_RESOURCE_ATTRIBUTES', $envAttributes);
$resource = (new Detectors\Composite([
new Detectors\Constant(ResourceInfo::create(new Attributes($userAttributes))),
new Detectors\Environment(),
]))->getResource();
foreach ($expected as $name => $value) {
$this->assertSame($value, $resource->getAttributes()->get($name));
}
}
public function environmentResourceProvider()
{
return [
'attributes from env var' => [
'foo=foo,bar=bar',
[],
['foo' => 'foo', 'bar' => 'bar'],
],
'user attributes have higher priority' => [
'foo=env-foo,bar=env-bar,baz=env-baz',
['foo' => 'user-foo', 'bar' => 'user-bar'],
['foo' => 'user-foo', 'bar' => 'user-bar', 'baz' => 'env-baz'],
],
];
}
public function test_resource_service_name_default(): void
{
$resource = ResourceInfoFactory::defaultResource();
$this->assertEquals('unknown_service', $resource->getAttributes()->get('service.name'));
}
/**
* @group compliance
*/
public function test_resource_with_empty_environment_variable(): void
{
$this->setEnvironmentVariable('OTEL_RESOURCE_ATTRIBUTES', '');
$this->assertInstanceOf(ResourceInfo::class, ResourceInfoFactory::defaultResource());
}
/**
* @group compliance
*/
public function test_resource_with_invalid_environment_variable(): void
{
$this->setEnvironmentVariable('OTEL_RESOURCE_ATTRIBUTES', 'foo');
$this->expectException(InvalidArgumentException::class);
$this->assertInstanceOf(ResourceInfo::class, ResourceInfoFactory::defaultResource());
}
/**
* @group compliance
*/
public function test_resource_from_environment_service_name_takes_precedence_over_resource_attribute(): void
{
$this->setEnvironmentVariable('OTEL_RESOURCE_ATTRIBUTES', 'service.name=bar');
$this->setEnvironmentVariable('OTEL_SERVICE_NAME', 'foo');
$resource = ResourceInfoFactory::defaultResource();
$this->assertEquals('foo', $resource->getAttributes()->get('service.name'));
}
/**
* @group compliance
*/
public function test_resource_from_environment_resource_attribute_takes_precedence_over_default(): void
{
$this->setEnvironmentVariable('OTEL_RESOURCE_ATTRIBUTES', 'service.name=foo');
$resource = ResourceInfoFactory::defaultResource();
$this->assertEquals('foo', $resource->getAttributes()->get('service.name'));
}
/**
* @dataProvider sameResourcesProvider
*/