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

Breadcrumb

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

class Span

Same name in this branch
  1. 11.1.x vendor/open-telemetry/api/Trace/Span.php \OpenTelemetry\API\Trace\Span
  2. 11.1.x vendor/open-telemetry/gen-otlp-protobuf/Opentelemetry/Proto/Trace/V1/Span.php \Opentelemetry\Proto\Trace\V1\Span

Hierarchy

  • class \OpenTelemetry\SDK\Trace\Span extends \API\Span implements \OpenTelemetry\SDK\Trace\ReadWriteSpanInterface uses \OpenTelemetry\API\Behavior\LogsMessagesTrait

Expanded class hierarchy of Span

4 files declare their use of Span
AlwaysOffSampler.php in vendor/open-telemetry/sdk/Trace/Sampler/AlwaysOffSampler.php
AlwaysOnSampler.php in vendor/open-telemetry/sdk/Trace/Sampler/AlwaysOnSampler.php
ParentBased.php in vendor/open-telemetry/sdk/Trace/Sampler/ParentBased.php
TraceIdRatioBasedSampler.php in vendor/open-telemetry/sdk/Trace/Sampler/TraceIdRatioBasedSampler.php
12 string references to 'Span'
FieldFilteredMarkup::allowedTags in core/lib/Drupal/Core/Field/FieldFilteredMarkup.php
Returns the allowed tag list.
FieldPluginBase::elementLabelType in core/modules/views/src/Plugin/views/field/FieldPluginBase.php
Returns an HTML element for the label based upon the field's element type.
FieldPluginBase::elementType in core/modules/views/src/Plugin/views/field/FieldPluginBase.php
Returns an HTML element based upon the field's element type.
FieldPluginBase::renderTrimText in core/modules/views/src/Plugin/views/field/FieldPluginBase.php
Trims the field down to the specified length.
locale_string_is_safe in core/modules/locale/locale.module
Check that a string is safe to be added or imported as a translation.

... See full list

File

vendor/open-telemetry/sdk/Trace/Span.php, line 19

Namespace

OpenTelemetry\SDK\Trace
View source
final class Span extends API\Span implements ReadWriteSpanInterface {
    use LogsMessagesTrait;
    
    /** @var list<EventInterface> */
    private array $events = [];
    private int $totalRecordedEvents = 0;
    private StatusDataInterface $status;
    private int $endEpochNanos = 0;
    private bool $hasEnded = false;
    
    /**
     * @param non-empty-string $name
     * @param list<LinkInterface> $links
     */
    private function __construct(string $name, API\SpanContextInterface $context, InstrumentationScopeInterface $instrumentationScope, int $kind, API\SpanContextInterface $parentSpanContext, SpanLimits $spanLimits, SpanProcessorInterface $spanProcessor, ResourceInfo $resource, AttributesBuilderInterface $attributesBuilder, array $links, int $totalRecordedLinks, int $startEpochNanos) {
        $this->status = StatusData::unset();
    }
    
    /**
     * This method _MUST_ not be used directly.
     * End users should use a {@see API\TracerInterface} in order to create spans.
     *
     * @param non-empty-string $name
     * @psalm-param API\SpanKind::KIND_* $kind
     * @param list<LinkInterface> $links
     *
     * @internal
     * @psalm-internal OpenTelemetry
     */
    public static function startSpan(string $name, API\SpanContextInterface $context, InstrumentationScopeInterface $instrumentationScope, int $kind, API\SpanInterface $parentSpan, ContextInterface $parentContext, SpanLimits $spanLimits, SpanProcessorInterface $spanProcessor, ResourceInfo $resource, AttributesBuilderInterface $attributesBuilder, array $links, int $totalRecordedLinks, int $startEpochNanos) : self {
        $span = new self($name, $context, $instrumentationScope, $kind, $parentSpan->getContext(), $spanLimits, $spanProcessor, $resource, $attributesBuilder, $links, $totalRecordedLinks, $startEpochNanos !== 0 ? $startEpochNanos : Clock::getDefault()->now());
        // Call onStart here to ensure the span is fully initialized.
        $spanProcessor->onStart($span, $parentContext);
        return $span;
    }
    
    /**
     * Backward compatibility methods
     *
     * @codeCoverageIgnore
     */
    public static function formatStackTrace(Throwable $e, ?array &$seen = null) : string {
        BcUtil::triggerMethodDeprecationNotice(__METHOD__, 'format', StackTraceFormatter::class);
        return StackTraceFormatter::format($e);
    }
    
    /** @inheritDoc */
    public function getContext() : API\SpanContextInterface {
        return $this->context;
    }
    
    /** @inheritDoc */
    public function isRecording() : bool {
        return !$this->hasEnded;
    }
    
    /** @inheritDoc */
    public function setAttribute(string $key, $value) : self {
        if ($this->hasEnded) {
            return $this;
        }
        $this->attributesBuilder[$key] = $value;
        return $this;
    }
    
    /** @inheritDoc */
    public function setAttributes(iterable $attributes) : self {
        foreach ($attributes as $key => $value) {
            $this->attributesBuilder[$key] = $value;
        }
        return $this;
    }
    public function addLink(SpanContextInterface $context, iterable $attributes = []) : self {
        if ($this->hasEnded) {
            return $this;
        }
        if (!$context->isValid()) {
            return $this;
        }
        if (++$this->totalRecordedLinks > $this->spanLimits
            ->getLinkCountLimit()) {
            return $this;
        }
        $this->links[] = new Link($context, $this->spanLimits
            ->getLinkAttributesFactory()
            ->builder($attributes)
            ->build());
        return $this;
    }
    
    /** @inheritDoc */
    public function addEvent(string $name, iterable $attributes = [], ?int $timestamp = null) : self {
        if ($this->hasEnded) {
            return $this;
        }
        if (++$this->totalRecordedEvents > $this->spanLimits
            ->getEventCountLimit()) {
            return $this;
        }
        $timestamp ??= Clock::getDefault()->now();
        $eventAttributesBuilder = $this->spanLimits
            ->getEventAttributesFactory()
            ->builder($attributes);
        $this->events[] = new Event($name, $timestamp, $eventAttributesBuilder->build());
        return $this;
    }
    
    /** @inheritDoc */
    public function recordException(Throwable $exception, iterable $attributes = [], ?int $timestamp = null) : self {
        if ($this->hasEnded) {
            return $this;
        }
        if (++$this->totalRecordedEvents > $this->spanLimits
            ->getEventCountLimit()) {
            return $this;
        }
        $timestamp ??= Clock::getDefault()->now();
        $eventAttributesBuilder = $this->spanLimits
            ->getEventAttributesFactory()
            ->builder([
            'exception.type' => $exception::class,
            'exception.message' => $exception->getMessage(),
            'exception.stacktrace' => StackTraceFormatter::format($exception),
        ]);
        foreach ($attributes as $key => $value) {
            $eventAttributesBuilder[$key] = $value;
        }
        $this->events[] = new Event('exception', $timestamp, $eventAttributesBuilder->build());
        return $this;
    }
    
    /** @inheritDoc */
    public function updateName(string $name) : self {
        if ($this->hasEnded) {
            return $this;
        }
        $this->name = $name;
        return $this;
    }
    
    /** @inheritDoc */
    public function setStatus(string $code, ?string $description = null) : self {
        if ($this->hasEnded) {
            return $this;
        }
        // An attempt to set value Unset SHOULD be ignored.
        if ($code === API\StatusCode::STATUS_UNSET) {
            return $this;
        }
        // When span status is set to Ok it SHOULD be considered final and any further attempts to change it SHOULD be ignored.
        if ($this->status
            ->getCode() === API\StatusCode::STATUS_OK) {
            return $this;
        }
        $this->status = StatusData::create($code, $description);
        return $this;
    }
    
    /** @inheritDoc */
    public function end(?int $endEpochNanos = null) : void {
        if ($this->hasEnded) {
            return;
        }
        $this->endEpochNanos = $endEpochNanos ?? Clock::getDefault()->now();
        $this->hasEnded = true;
        $this->checkForDroppedElements();
        $this->spanProcessor
            ->onEnd($this);
    }
    
    /** @inheritDoc */
    public function getName() : string {
        return $this->name;
    }
    public function getParentContext() : API\SpanContextInterface {
        return $this->parentSpanContext;
    }
    public function getInstrumentationScope() : InstrumentationScopeInterface {
        return $this->instrumentationScope;
    }
    public function hasEnded() : bool {
        return $this->hasEnded;
    }
    public function toSpanData() : SpanDataInterface {
        return new ImmutableSpan($this, $this->name, $this->links, $this->events, $this->attributesBuilder
            ->build(), $this->totalRecordedLinks, $this->totalRecordedEvents, $this->status, $this->endEpochNanos, $this->hasEnded);
    }
    
    /** @inheritDoc */
    public function getDuration() : int {
        return ($this->hasEnded ? $this->endEpochNanos : Clock::getDefault()->now()) - $this->startEpochNanos;
    }
    
    /** @inheritDoc */
    public function getKind() : int {
        return $this->kind;
    }
    
    /** @inheritDoc */
    public function getAttribute(string $key) {
        return $this->attributesBuilder[$key];
    }
    public function getStartEpochNanos() : int {
        return $this->startEpochNanos;
    }
    public function getTotalRecordedLinks() : int {
        return $this->totalRecordedLinks;
    }
    public function getTotalRecordedEvents() : int {
        return $this->totalRecordedEvents;
    }
    public function getResource() : ResourceInfo {
        return $this->resource;
    }
    private function checkForDroppedElements() : void {
        $spanData = $this->toSpanData();
        
        //@todo could be optimized to reduce overhead of multiple calls
        $droppedLinkAttributes = 0;
        $droppedEventAttributes = 0;
        array_map(function (EventInterface $event) use (&$droppedEventAttributes) {
            $droppedEventAttributes += $event->getAttributes()
                ->getDroppedAttributesCount();
        }, $spanData->getEvents());
        array_map(function (LinkInterface $link) use (&$droppedLinkAttributes) {
            $droppedLinkAttributes += $link->getAttributes()
                ->getDroppedAttributesCount();
        }, $spanData->getLinks());
        if ($spanData->getTotalDroppedLinks() || $spanData->getTotalDroppedEvents() || $spanData->getAttributes()
            ->getDroppedAttributesCount() || $droppedEventAttributes || $droppedLinkAttributes) {
            self::logWarning('Dropped span attributes, links or events', [
                'trace_id' => $spanData->getTraceId(),
                'span_id' => $spanData->getSpanId(),
                'attributes' => $spanData->getAttributes()
                    ->getDroppedAttributesCount(),
                'links' => $spanData->getTotalDroppedLinks(),
                'link_attributes' => $droppedLinkAttributes,
                'events' => $spanData->getTotalDroppedEvents(),
                'event_attributes' => $droppedEventAttributes,
            ]);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
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
Span::$endEpochNanos private property
Span::$events private property @var list&lt;EventInterface&gt;
Span::$hasEnded private property
Span::$status private property
Span::$totalRecordedEvents private property
Span::addEvent public function @inheritDoc
Span::addLink public function
Span::checkForDroppedElements private function
Span::end public function @inheritDoc
Span::formatStackTrace public static function Backward compatibility methods
Span::getAttribute public function @inheritDoc
Span::getContext public function @inheritDoc
Span::getDuration public function @inheritDoc
Span::getInstrumentationScope public function
Span::getKind public function @inheritDoc
Span::getName public function @inheritDoc
Span::getParentContext public function
Span::getResource public function
Span::getStartEpochNanos public function
Span::getTotalRecordedEvents public function
Span::getTotalRecordedLinks public function
Span::hasEnded public function
Span::isRecording public function @inheritDoc
Span::recordException public function @inheritDoc
Span::setAttribute public function @inheritDoc
Span::setAttributes public function @inheritDoc
Span::setStatus public function @inheritDoc
Span::startSpan public static function This method _MUST_ not be used directly.
End users should use a {
Span::toSpanData public function
Span::updateName public function @inheritDoc
Span::__construct private function

API Navigation

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