Skip to content

Add #[WithSpan] / #[SpanAttribute] native attribute-based instrumentation (#117)#118

Merged
intuibase merged 4 commits into
open-telemetry:mainfrom
intuibase:with_span_attribute
Jun 5, 2026
Merged

Add #[WithSpan] / #[SpanAttribute] native attribute-based instrumentation (#117)#118
intuibase merged 4 commits into
open-telemetry:mainfrom
intuibase:with_span_attribute

Conversation

@intuibase

Copy link
Copy Markdown
Contributor

Implements native #[WithSpan] / #[SpanAttribute] attribute-based auto-instrumentation, compatible with the official opentelemetry-php-instrumentation extension.

Users annotate methods or functions with PHP 8 attributes to create spans automatically, without writing instrumentation code:

use OpenTelemetry\API\Instrumentation\WithSpan;
use OpenTelemetry\API\Instrumentation\SpanAttribute;
use OpenTelemetry\API\Trace\SpanKind;

class OrderService
{
    #[WithSpan('order.process', SpanKind::KIND_SERVER, ['service.component' => 'orders'])]
    public function processOrder(
        #[SpanAttribute] int    $orderId,
        #[SpanAttribute('order.customer')] string $customerId,
    ): void {
        // span created automatically, parameters captured as attributes
    }
}

Disabled by default — enable with OTEL_PHP_ATTR_HOOKS_ENABLED=true.

What's new

Native layer (libphpbridge)

  • WithSpanAttributes — reads #[WithSpan] and #[SpanAttribute] attributes from PHP op_array via Zend attribute API at observer registration time. Supports: custom span name, span kind, static attributes, #[SpanAttribute] on parameters (by name or alias), #[SpanAttribute] on class properties.
  • AttrHooksStorage — per-process singleton (pattern of InternalFunctionInstrumentationStorage). Metadata persists across requests because with opcache registerObserverHandlers is called only once per function per process. ZTS-safe version (mutex) deferred — same caveat as existing storage.

Extension integration

  • Hooks into registerObserverHandlers: detects #[WithSpan], stores metadata in AttrHooksStorage.
  • observerFcallBeginHandler / observerFcallEndHandler: calls WithSpanHandler::pre/post from open-telemetry/api (distro's scoped vendor) with 8 and 4 arguments respectively - same handler as the official extension.
  • #[SpanAttribute] on parameters: values read from call stack at call time.
  • #[SpanAttribute] on class properties: values read from $this at call time.
  • Exception path: WithSpanHandler::post records exception and sets STATUS_ERROR.

New configuration option

OTEL_PHP_ATTR_HOOKS_ENABLED (opentelemetry_distro.attr_hooks_enabled) — boolean, default false.

Tests

phpt tests in phpbridge_extension covering: no attribute, basic #[WithSpan], custom name+kind, #[SpanAttribute] on parameters, #[SpanAttribute] on properties.

Documentation

New page docs/reference/attribute-instrumentation.md with full usage examples. Updated configuration.md and supported-technologies.md.

@intuibase
intuibase requested a review from a team as a code owner June 3, 2026 12:47
@intuibase
intuibase requested a review from SergeyKleyman June 3, 2026 12:47
@intuibase intuibase self-assigned this Jun 3, 2026
@intuibase
intuibase merged commit 2180101 into open-telemetry:main Jun 5, 2026
67 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add #[WithSpan] / #[SpanAttribute] attribute-based auto-instrumentation

2 participants