From cdd7ad020e165fe680703b6d3319b908b682fb7a Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 20 Oct 2023 17:12:29 +0300 Subject: jaeger-client -> opentelemetry --- .../open-telemetry/sdk/Resource/ResourceInfo.php | 125 +++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 vendor/open-telemetry/sdk/Resource/ResourceInfo.php (limited to 'vendor/open-telemetry/sdk/Resource/ResourceInfo.php') diff --git a/vendor/open-telemetry/sdk/Resource/ResourceInfo.php b/vendor/open-telemetry/sdk/Resource/ResourceInfo.php new file mode 100644 index 000000000..4210a6142 --- /dev/null +++ b/vendor/open-telemetry/sdk/Resource/ResourceInfo.php @@ -0,0 +1,125 @@ +attributes = $attributes; + $this->schemaUrl = $schemaUrl; + } + + public static function create(AttributesInterface $attributes, ?string $schemaUrl = null): self + { + return new ResourceInfo($attributes, $schemaUrl); + } + + public function getAttributes(): AttributesInterface + { + return $this->attributes; + } + + public function getSchemaUrl(): ?string + { + return $this->schemaUrl; + } + + public function serialize(): string + { + $copyOfAttributesAsArray = array_slice($this->attributes->toArray(), 0); //This may be overly cautious (in trying to avoid mutating the source array) + ksort($copyOfAttributesAsArray); //sort the associative array by keys since the serializer will consider equal arrays different otherwise + + //The exact return value doesn't matter, as long as it can distingusih between instances that represent the same/different resources + return serialize([ + 'schemaUrl' => $this->schemaUrl, + 'attributes' => $copyOfAttributesAsArray, + ]); + } + + /** + * Merge current resource with an updating resource, combining all attributes. If a key exists on both the old and updating + * resource, the value of the updating resource MUST be picked (even if the updated value is empty) + * + * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/resource/sdk.md#merge + */ + public function merge(ResourceInfo $updating): ResourceInfo + { + $schemaUrl = self::mergeSchemaUrl($this->getSchemaUrl(), $updating->getSchemaUrl()); + $attributes = $updating->getAttributes()->toArray() + $this->getAttributes()->toArray(); + + return ResourceInfo::create(Attributes::create($attributes), $schemaUrl); + } + + /** + * Merge the schema URLs from the old and updating resource. + * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/resource/sdk.md#merge + */ + private static function mergeSchemaUrl(?string $old, ?string $updating): ?string + { + if (empty($old)) { + return $updating; + } + if (empty($updating)) { + return $old; + } + if ($old === $updating) { + return $old; + } + + self::logWarning('Merging resources with different schema URLs', [ + 'old' => $old, + 'updating' => $updating, + ]); + + return null; + } + + /** + * @codeCoverageIgnore + */ + public static function defaultResource(): ResourceInfo + { + BcUtil::triggerMethodDeprecationNotice( + __METHOD__, + 'defaultResource', + ResourceInfoFactory::class + ); + + return ResourceInfoFactory::defaultResource(); + } + + /** + * @codeCoverageIgnore + */ + public static function emptyResource(): ResourceInfo + { + BcUtil::triggerMethodDeprecationNotice( + __METHOD__, + 'emptyResource', + ResourceInfoFactory::class + ); + + return ResourceInfoFactory::emptyResource(); + } +} -- cgit v1.2.3