Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. AttributesBuilder.php

class AttributesBuilder

@internal

Hierarchy

  • class \OpenTelemetry\SDK\Common\Attribute\AttributesBuilder implements \OpenTelemetry\SDK\Common\Attribute\AttributesBuilderInterface uses \OpenTelemetry\API\Behavior\LogsMessagesTrait

Expanded class hierarchy of AttributesBuilder

File

vendor/open-telemetry/sdk/Common/Attribute/AttributesBuilder.php, line 17

Namespace

OpenTelemetry\SDK\Common\Attribute
View source
final class AttributesBuilder implements AttributesBuilderInterface {
    use LogsMessagesTrait;
    public function __construct(array $attributes, ?int $attributeCountLimit, ?int $attributeValueLengthLimit, int $droppedAttributesCount, AttributeValidatorInterface $attributeValidator = new AttributeValidator()) {
    }
    public function build() : AttributesInterface {
        return new Attributes($this->attributes, $this->droppedAttributesCount);
    }
    public function merge(AttributesInterface $old, AttributesInterface $updating) : AttributesInterface {
        $new = $old->toArray();
        $dropped = $old->getDroppedAttributesCount() + $updating->getDroppedAttributesCount();
        foreach ($updating->toArray() as $key => $value) {
            if (count($new) === $this->attributeCountLimit && !array_key_exists($key, $new)) {
                $dropped++;
            }
            else {
                $new[$key] = $value;
            }
        }
        return new Attributes($new, $dropped);
    }
    public function offsetExists($offset) : bool {
        return array_key_exists($offset, $this->attributes);
    }
    
    /**
     * @phan-suppress PhanUndeclaredClassAttribute
     */
    public function offsetGet($offset) : mixed {
        return $this->attributes[$offset] ?? null;
    }
    
    /**
     * @phan-suppress PhanUndeclaredClassAttribute
     */
    public function offsetSet($offset, $value) : void {
        if ($offset === null) {
            return;
        }
        if ($value === null) {
            unset($this->attributes[$offset]);
            return;
        }
        if (!$this->attributeValidator
            ->validate($value)) {
            self::logWarning($this->attributeValidator
                ->getInvalidMessage() . ': ' . $offset);
            $this->droppedAttributesCount++;
            return;
        }
        if (count($this->attributes) === $this->attributeCountLimit && !array_key_exists($offset, $this->attributes)) {
            $this->droppedAttributesCount++;
            return;
        }
        $this->attributes[$offset] = $this->normalizeValue($value);
        
        //@todo "There SHOULD be a message printed in the SDK's log to indicate to the user that an attribute was
        //       discarded due to such a limit. To prevent excessive logging, the message MUST be printed at most
        //       once per <thing> (i.e., not per discarded attribute)."
    }
    
    /**
     * @phan-suppress PhanUndeclaredClassAttribute
     */
    public function offsetUnset($offset) : void {
        unset($this->attributes[$offset]);
    }
    private function normalizeValue($value) {
        if (is_string($value) && $this->attributeValueLengthLimit !== null) {
            return mb_substr($value, 0, $this->attributeValueLengthLimit);
        }
        if (is_array($value)) {
            foreach ($value as $k => $v) {
                $processed = $this->normalizeValue($v);
                if ($processed !== $v) {
                    $value[$k] = $processed;
                }
            }
            return $value;
        }
        return $value;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AttributesBuilder::build public function Overrides AttributesBuilderInterface::build
AttributesBuilder::merge public function Overrides AttributesBuilderInterface::merge
AttributesBuilder::normalizeValue private function
AttributesBuilder::offsetExists public function
AttributesBuilder::offsetGet public function @phan-suppress PhanUndeclaredClassAttribute
AttributesBuilder::offsetSet public function @phan-suppress PhanUndeclaredClassAttribute
AttributesBuilder::offsetUnset public function @phan-suppress PhanUndeclaredClassAttribute
AttributesBuilder::__construct public function
LogsMessagesTrait::doLog private static function
LogsMessagesTrait::logDebug protected static function
LogsMessagesTrait::logError protected static function
LogsMessagesTrait::logInfo protected static function
LogsMessagesTrait::logNotice protected static function
LogsMessagesTrait::logWarning protected static function
LogsMessagesTrait::shouldLog private static function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal