Skip to content

Commit

Permalink
Make library work with new sdk version (fix Attributes) (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalmas authored Jul 24, 2022
1 parent 780c40a commit dd99a20
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 76 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": "^7.4 || ^8.0",
"ext-json": "*",
"open-telemetry/opentelemetry": "^0.0.12",
"open-telemetry/opentelemetry": "^0.0.13",
"php-http/discovery": "^1.14",
"php-http/message": "^1.12"
},
Expand Down
26 changes: 13 additions & 13 deletions src/Aws/Ec2/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,50 +70,50 @@ public function getResource(): ResourceInfo
if ($token === null) {
return ResourceInfoFactory::emptyResource();
}

$hostName = $this->fetchHostname($token);

$identitiesJson = $this->fetchIdentity($token);

if (!$token || !$identitiesJson) {
return ResourceInfoFactory::emptyResource();
}
$attributes = new Attributes();

$attributes = [];

foreach ($identitiesJson as $key => $value) {
switch ($key) {
case 'instanceId':
$attributes->setAttribute(ResourceAttributes::HOST_ID, $value);
$attributes[ResourceAttributes::HOST_ID] = $value;

break;
case 'availabilityZone':
$attributes->setAttribute(ResourceAttributes::CLOUD_AVAILABILITY_ZONE, $value);
$attributes[ResourceAttributes::CLOUD_AVAILABILITY_ZONE] = $value;

break;
case 'instanceType':
$attributes->setAttribute(ResourceAttributes::HOST_TYPE, $value);
$attributes[ResourceAttributes::HOST_TYPE] = $value;

break;
case 'imageId':
$attributes->setAttribute(ResourceAttributes::HOST_IMAGE_ID, $value);
$attributes[ResourceAttributes::HOST_IMAGE_ID] = $value;

break;
case 'accountId':
$attributes->setAttribute(ResourceAttributes::CLOUD_ACCOUNT_ID, $value);
$attributes[ResourceAttributes::CLOUD_ACCOUNT_ID] = $value;

break;
case 'region':
$attributes->setAttribute(ResourceAttributes::CLOUD_REGION, $value);
$attributes[ResourceAttributes::CLOUD_REGION] = $value;

break;
}
}

$attributes->setAttribute(ResourceAttributes::HOST_NAME, $hostName);
$attributes->setAttribute(ResourceAttributes::CLOUD_PROVIDER, self::CLOUD_PROVIDER);
$attributes[ResourceAttributes::HOST_NAME] = $hostName;
$attributes[ResourceAttributes::CLOUD_PROVIDER] = self::CLOUD_PROVIDER;

return ResourceInfo::create(new Attributes($attributes), ResourceAttributes::SCHEMA_URL);
return ResourceInfo::create(Attributes::create($attributes), ResourceAttributes::SCHEMA_URL);
} catch (\Throwable $e) {
//TODO: add 'Process is not running on K8S when logging is added
return ResourceInfoFactory::emptyResource();
Expand Down Expand Up @@ -175,7 +175,7 @@ private function request(RequestInterface $request): ?string
if (!empty($body) && $responseCode < 300 && $responseCode >= 200) {
return $body;
}

return null;
} catch (Throwable $e) {
// TODO: add log for exception. The code below
Expand Down
6 changes: 3 additions & 3 deletions src/Aws/Ecs/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ class Detector implements ResourceDetectorInterface
private const ECS_METADATA_KEY_V3 = 'ECS_CONTAINER_METADATA_URI';

private const CONTAINER_ID_LENGTH = 64;

private DataProvider $processData;

public function __construct(DataProvider $processData)
{
$this->processData = $processData;
}

/**
* If running on ECS, runs getContainerId(), getClusterName(), and
* returns resource with valid extracted values
Expand All @@ -63,7 +63,7 @@ public function getResource(): ResourceInfo

return !$hostName && !$containerId
? ResourceInfoFactory::emptyResource()
: ResourceInfo::create(new Attributes([
: ResourceInfo::create(Attributes::create([
ResourceAttributes::CONTAINER_NAME => $hostName,
ResourceAttributes::CONTAINER_ID => $containerId,
]));
Expand Down
6 changes: 3 additions & 3 deletions src/Aws/Eks/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(
$this->client = $client;
$this->requestFactory = $requestFactory;
}

public function getResource(): ResourceInfo
{
try {
Expand All @@ -68,10 +68,10 @@ public function getResource(): ResourceInfo

$clusterName = $this->getClusterName();
$containerId = $this->getContainerId();

return !$clusterName && !$containerId
? ResourceInfoFactory::emptyResource()
: ResourceInfo::create(new Attributes([
: ResourceInfo::create(Attributes::create([
ResourceAttributes::CONTAINER_ID => $containerId,
ResourceAttributes::K8S_CLUSTER_NAME => $clusterName,
]));
Expand Down
4 changes: 2 additions & 2 deletions src/Aws/Lambda/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getResource(): ResourceInfo
$lambdaName = getenv(self::LAMBDA_NAME_ENV);
$functionVersion = getenv(self::LAMBDA_VERSION_ENV);
$awsRegion = getenv(self::AWS_REGION_ENV);

// The following ternary operations are created because
// the attributes class will only NOT create a variable
// when it is set to null. getenv returns false when unsuccessful
Expand All @@ -53,7 +53,7 @@ public function getResource(): ResourceInfo

return !$lambdaName && !$awsRegion && !$functionVersion
? ResourceInfoFactory::emptyResource()
: ResourceInfo::create(new Attributes([
: ResourceInfo::create(Attributes::create([
ResourceAttributes::FAAS_NAME => $lambdaName,
ResourceAttributes::FAAS_VERSION => $functionVersion,
ResourceAttributes::CLOUD_REGION => $awsRegion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace OpenTelemetry\Symfony\OtelSdkBundle\DependencyInjection;

use OpenTelemetry\SDK\Common\Attribute\AttributeLimits;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Trace;
use OpenTelemetry\Symfony\OtelSdkBundle\DependencyInjection\Configuration as Conf;
Expand Down Expand Up @@ -97,13 +96,11 @@ private function configureResourceInfo(): void
if (empty($config)) {
return;
}
// configure resource attribute limits
$limits = $this->getDefinitionByClass(AttributeLimits::class);
// configure resource attributes limits
if (isset($config[Conf::LIMITS_NODE])) {
$limits->setArguments([
$config[Conf::LIMITS_NODE][Conf::ATTR_COUNT_NODE],
$config[Conf::LIMITS_NODE][Conf::ATTR_VALUE_LENGTH_NODE],
]);
$this->getDefinitionByClass(Trace\SpanLimitsBuilder::class)
->addMethodCall('setAttributeCountLimit', [$config[Conf::LIMITS_NODE][Conf::ATTR_COUNT_NODE]])
->addMethodCall('setAttributeValueLengthLimit', [$config[Conf::LIMITS_NODE][Conf::ATTR_VALUE_LENGTH_NODE]]);
}
// configure resource attributes
$attributesParams = (array) $this->getContainer()->getParameter(Parameters::RESOURCE_ATTRIBUTES);
Expand All @@ -115,7 +112,7 @@ private function configureResourceInfo(): void
$attributes = $this->getDefinitionByClass(Attributes::class);
$attributes->setArguments([
'%' . Parameters::RESOURCE_ATTRIBUTES . '%',
self::createReferenceFromClass(AttributeLimits::class),
0,
]);

// reference service name for later use
Expand Down Expand Up @@ -403,7 +400,7 @@ private function resolveExporterClass(array $config): string
{
if (in_array($config[Conf::TYPE_NODE], Conf::EXPORTERS_NODE_VALUES, true)) {
return ConfigMappings::SPAN_EXPORTERS[
$config[Conf::TYPE_NODE]
$config[Conf::TYPE_NODE]
];
}
if ($config[Conf::TYPE_NODE] === Conf::CUSTOM_TYPE) {
Expand Down
5 changes: 1 addition & 4 deletions src/Symfony/OtelSdkBundle/Resources/config/sdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace OpenTelemetry\Symfony\OtelSdkBundle\Resources;

use OpenTelemetry\SDK\Common\Attribute\AttributeLimits;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Time\SystemClock;
use OpenTelemetry\SDK\Resource;
Expand Down Expand Up @@ -44,12 +43,10 @@

// RESOURCE

$helper->setService(AttributeLimits::class);

$helper->setService(Attributes::class)
->args([
ConfigHelper::wrapParameter(Parameters::RESOURCE_ATTRIBUTES),
ConfigHelper::createReferenceFromClass(AttributeLimits::class),
0,
]);

$helper->setService(Resource\ResourceInfo::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Exception;
use OpenTelemetry\SDK;
use OpenTelemetry\SDK\Common\Attribute\AttributeLimits;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Time\SystemClock;
use OpenTelemetry\SDK\Trace\SpanProcessor;
Expand Down Expand Up @@ -60,11 +59,14 @@ public function testResourceLimits(): void
{
$data = $this->loadTestData('resource');

$limits = $this->getDefinitionByClass(AttributeLimits::class);
$spanLimitsBuilder = $this->getDefinitionByClass(SDK\Trace\SpanLimitsBuilder::class);

$this->assertEquals(
array_values($data['resource']['limits']),
$limits->getArguments()
[
$spanLimitsBuilder->getMethodCalls()[0][1][0],
$spanLimitsBuilder->getMethodCalls()[1][1][0],
]
);
}

Expand All @@ -87,7 +89,7 @@ public function testResourceAttributes(): void
ConfigHelper::wrapParameter(Parameters::RESOURCE_ATTRIBUTES),
$arguments[0]
);
$this->assertReference(AttributeLimits::class, $arguments[1]);
$this->assertEquals(0, $arguments[1]);
}

public function testDefaultSampler(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace OpenTelemetry\Test\Integration\Symfony\OtelSdkBundle\Resources;

use Exception;
use OpenTelemetry\SDK\Common\Attribute\AttributeLimits;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Time\SystemClock;
use OpenTelemetry\SDK\Resource;
Expand Down Expand Up @@ -63,7 +62,6 @@ public function testUtil()
*/
public function testResource()
{
$this->assertServiceSetup(AttributeLimits::class);
$this->assertServiceSetup(Attributes::class);
$this->assertServiceSetup(Resource\ResourceInfo::class);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Aws/Ec2/DetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function TestValidEc2()
$detector = new Detector($client, $requestFactory);

$this->assertEquals(
new Attributes(
Attributes::create(
[
ResourceAttributes::HOST_ID => self::HOST_ID,
ResourceAttributes::CLOUD_AVAILABILITY_ZONE => self::CLOUD_ZONE,
Expand Down Expand Up @@ -147,7 +147,7 @@ public function TestInvalidHostName()
$detector = new Detector($client, $requestFactory);

$this->assertEquals(
new Attributes(
Attributes::create(
[
ResourceAttributes::HOST_ID => self::HOST_ID,
ResourceAttributes::CLOUD_AVAILABILITY_ZONE => self::CLOUD_ZONE,
Expand Down Expand Up @@ -206,7 +206,7 @@ public function TestInvalidIncompleteIdentities()
$detector = new Detector($client, $requestFactory);

$this->assertEquals(
new Attributes(
Attributes::create(
[
ResourceAttributes::HOST_ID => self::HOST_ID,
ResourceAttributes::CLOUD_AVAILABILITY_ZONE => self::CLOUD_ZONE,
Expand Down
16 changes: 8 additions & 8 deletions tests/Unit/Aws/Ecs/DetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function TestValidCgroupData()
$detector = new Detector($mockData);

$this->assertEquals(ResourceInfo::create(
new Attributes(
Attributes::create(
[
ResourceAttributes::CONTAINER_NAME => self::HOST_NAME,
ResourceAttributes::CONTAINER_ID => self::EXTRACTED_CONTAINER_ID,
Expand All @@ -56,7 +56,7 @@ public function TestValidCgroupData()
//unset environment variable
putenv(self::ECS_ENV_VAR_V4_KEY);
}

/**
* @test
*/
Expand All @@ -72,7 +72,7 @@ public function TestFirstValidCgroupData()
$detector = new Detector($mockData);

$this->assertEquals(ResourceInfo::create(
new Attributes(
Attributes::create(
[
ResourceAttributes::CONTAINER_NAME => self::HOST_NAME,
ResourceAttributes::CONTAINER_ID => self::EXTRACTED_CONTAINER_ID,
Expand Down Expand Up @@ -115,7 +115,7 @@ public function TestReturnOnlyHostnameWithoutCgroupFile()
$detector = new Detector($mockData);

$this->assertEquals(ResourceInfo::create(
new Attributes(
Attributes::create(
[
ResourceAttributes::CONTAINER_NAME => self::HOST_NAME,
]
Expand All @@ -138,14 +138,14 @@ public function TestReturnOnlyHostnameWithInvalidCgroupFile()
$mockData->method('getHostName')->willReturn(self::HOST_NAME);

$detector = new Detector($mockData);

$invalidCgroups = [self::INVALID_CGROUP_LENGTH, self::INVALID_CGROUP_EMPTY, self::INVALID_CGROUP_VALUES];

foreach ($invalidCgroups as $invalidCgroup) {
$mockData->method('getCgroupData')->willReturn($invalidCgroup);

$this->assertEquals(ResourceInfo::create(
new Attributes(
Attributes::create(
[
ResourceAttributes::CONTAINER_NAME => self::HOST_NAME,
]
Expand All @@ -172,7 +172,7 @@ public function TestReturnOnlyContainerIdWithoutHostname()
$detector = new Detector($mockData);

$this->assertEquals(ResourceInfo::create(
new Attributes(
Attributes::create(
[
ResourceAttributes::CONTAINER_ID => self::EXTRACTED_CONTAINER_ID,
]
Expand All @@ -195,7 +195,7 @@ public function TestReturnEmptyResourceInvalidContainerIdAndHostname()

$mockData->method('getCgroupData')->willReturn(self::INVALID_CGROUP_LENGTH);
$mockData->method('getHostName')->willReturn(null);

$detector = new Detector($mockData);

$this->assertEquals(ResourceInfoFactory::emptyResource(), $detector->getResource());
Expand Down
Loading

0 comments on commit dd99a20

Please sign in to comment.