eerzho / opentelemetry-auto-class-symfony
Symfony bundle for automatic OpenTelemetry tracing via the #[Traceable] attribute
Package info
github.com/eerzho/opentelemetry-auto-class-symfony
Type:symfony-bundle
pkg:composer/eerzho/opentelemetry-auto-class-symfony
Requires
- php: >=8.2
- ext-opentelemetry: *
- eerzho/opentelemetry-auto-class: ^0.1
- open-telemetry/api: ^1.0
- symfony/dependency-injection: ^6.0 || ^7.0 || ^8.0
- symfony/http-kernel: ^6.0 || ^7.0 || ^8.0
README
Symfony integration for automatic OpenTelemetry tracing of PHP methods via the #[Traceable] attribute. All services with the attribute in the container are instrumented automatically using the ext-opentelemetry hook API.
This is a read-only sub-split. Please open issues and pull requests in the monorepo.
Installation
composer require eerzho/opentelemetry-auto-class-symfony
Register the bundle:
// config/bundles.php return [ // ... OpenTelemetry\Contrib\Instrumentation\Class\Symfony\TraceableBundle::class => ['all' => true], ];
Requirements:
- ext-opentelemetry
- PHP 8.2+
- Symfony 6+
Usage
Basic
Add #[Traceable] to a class registered in the service container — all public methods will be traced automatically:
namespace App\Service; use OpenTelemetry\Contrib\Instrumentation\Class\Attribute\Traceable; #[Traceable] class OrderService { public function create(array $items): void { // span "App\Service\OrderService::create" is created automatically } public function cancel(int $orderId): void { // span "App\Service\OrderService::cancel" is created automatically } }
For full details on how spans are created, argument serialization, and limitations, see opentelemetry-auto-class.
Exclude methods
Use the exclude parameter to skip specific methods from tracing:
namespace App\Service; use OpenTelemetry\Contrib\Instrumentation\Class\Attribute\Traceable; #[Traceable(exclude: ['healthCheck', 'getVersion'])] class PaymentService { public function charge(int $amount, string $currency): void { // traced } public function healthCheck(): bool { // NOT traced return true; } public function getVersion(): string { // NOT traced return '1.0.0'; } }
Exclude arguments
By default, all method arguments are captured as span attributes. Use #[Arguments(exclude: [...])] on a method to hide sensitive parameters:
namespace App\Service; use OpenTelemetry\Contrib\Instrumentation\Class\Attribute\Arguments; use OpenTelemetry\Contrib\Instrumentation\Class\Attribute\Traceable; #[Traceable] class AuthService { #[Arguments(exclude: ['password', 'token'])] public function login(string $email, string $password, string $token): void { // span captures "email" attribute only // "password" and "token" are excluded } public function logout(int $userId): void { // span captures "userId" attribute (no exclusions) } }
How it works
- During container compilation, the bundle scans all service definitions for
#[Traceable]attribute - Builds a method map and stores it as a container parameter
- On kernel boot, registers
ext-opentelemetryhooks for matched methods
Disabling instrumentation
To disable tracing at runtime, use the standard OpenTelemetry environment variable:
OTEL_PHP_DISABLED_INSTRUMENTATIONS=class