Add #[WithSpan] / #[SpanAttribute] native attribute-based instrumentation (#117)#118
Merged
Merged
Conversation
SergeyKleyman
approved these changes
Jun 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements native
#[WithSpan]/#[SpanAttribute]attribute-based auto-instrumentation, compatible with the officialopentelemetry-php-instrumentationextension.Users annotate methods or functions with PHP 8 attributes to create spans automatically, without writing instrumentation code:
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 ofInternalFunctionInstrumentationStorage). Metadata persists across requests because with opcacheregisterObserverHandlersis called only once per function per process. ZTS-safe version (mutex) deferred — same caveat as existing storage.Extension integration
registerObserverHandlers: detects#[WithSpan], stores metadata inAttrHooksStorage.observerFcallBeginHandler/observerFcallEndHandler: callsWithSpanHandler::pre/postfromopen-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$thisat call time.WithSpanHandler::postrecords exception and setsSTATUS_ERROR.New configuration option
OTEL_PHP_ATTR_HOOKS_ENABLED(opentelemetry_distro.attr_hooks_enabled) — boolean, defaultfalse.Tests
phpt tests in
phpbridge_extensioncovering: no attribute, basic#[WithSpan], custom name+kind,#[SpanAttribute]on parameters,#[SpanAttribute]on properties.Documentation
New page
docs/reference/attribute-instrumentation.mdwith full usage examples. Updatedconfiguration.mdandsupported-technologies.md.