public function tryToAddToCompress()

in agent/php/ElasticApm/Impl/Span.php [310:352]


    public function tryToAddToCompress(Span $sibling): bool
    {
        $logTraceProxy = $this->logger->ifTraceLevelEnabledNoLine(__FUNCTION__);
        if ($this->logger->isTraceLevelEnabled()) {
            $logger = $this->logger->inherit()->addContext('sibling', $sibling);
            $logTraceProxy = $logger->ifTraceLevelEnabledNoLine(__FUNCTION__);
        }

        $logTraceProxy && $logTraceProxy->log(__LINE__, 'Entered');

        if ($this->composite === null) {
            $compressionStrategy = $this->canCompressFirstPair($sibling);
            if ($compressionStrategy === null) {
                $logTraceProxy && $logTraceProxy->log(__LINE__, 'Exiting - cannot compress first pair');
                return false;
            }
            if ($compressionStrategy === Constants::COMPRESSION_STRATEGY_SAME_KIND) {
                $this->name = (($serviceTarget = $this->getServiceTarget()) === null)
                    ? self::buildSameKindCompressedCompositeName(null, null)
                    : self::buildSameKindCompressedCompositeName($serviceTarget->type, $serviceTarget->name);
            }
            $this->composite = new SpanComposite($compressionStrategy, $this->duration);
        } else {
            if (!$this->canAddToCompositeToCompress($this->composite, $sibling)) {
                $logTraceProxy && $logTraceProxy->log(__LINE__, 'Exiting - cannot add sibling');
                return false;
            }
        }

        $this->composite->durationsSum += $sibling->duration;
        ++$this->composite->count;
        $this->recalcDurationForComposite($sibling);
        /**
         * When a span is compressed into a composite, span_count.reported should ONLY count the compressed composite as a single span.
         * Spans that have been compressed into the composite should not be counted.
         *
         * @link https://github.com/elastic/apm/blob/5e1bfbc95fa0358ef195cedba8cb1be281988227/specs/agents/handling-huge-traces/tracing-spans-compress.md#effects-on-span-count
         */
        --$this->containingTransaction->startedSpansCount;

        $logTraceProxy && $logTraceProxy->log(__LINE__, 'Exiting - added sibling');
        return true;
    }