webgrip / telemetry-service
A simple telemetry service for PHP applications
1.0.2
2025-03-03 10:53 UTC
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.9
- monolog/monolog: ^3.7
- nyholm/psr7: ^1.8
- open-telemetry/exporter-otlp: ^1.1
- open-telemetry/opentelemetry-logger-monolog: ^1.0
- open-telemetry/sem-conv: ^1.27
- php-di/php-di: ^7.0
- symfony/http-client: ^7.1
- symfony/http-foundation: ^7.1
Requires (Dev)
- beberlei/assert: ^3.3
- ergebnis/composer-normalize: ^2.42
- jangregor/phpstan-prophecy: ^1.0
- php-mock/php-mock-prophecy: ^0.1.2
- phpmd/phpmd: ^2.15
- phpspec/prophecy: ^1.19
- phpstan/phpstan: ^1.10
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: *
- rector/rector: ^1.0
- squizlabs/php_codesniffer: ^3.9
- symfony/var-dumper: ^7.1
- thecodingmachine/phpstan-strict-rules: ^1.0
- vimeo/psalm: ^6.0@dev
README
Prerequisites
- PHP 8.2
- Composer
How to use
Serviceprovider
TelemetryServiceProvider::class
Using TelemetryService to log
use \Webgrip\TelemetryService\Core\Domain\Services\TelemetryServiceInterface; class Foo { public function __construct(private TelemetryServiceInterface $telemetryService) { } public function bar() { $this->telemetryService->logger()->debug('Hello World'); $this->telemetryService->logger()->info('Hello World'); $this->telemetryService->logger()->warning('Hello World'); // ... } }
Using TelemetryService to trace
use \Webgrip\TelemetryService\Core\Domain\Services\TelemetryServiceInterface; class Foo { public function __construct(private TelemetryServiceInterface $telemetryService) { } public function bar() { $tracer = $this->telemetryService->tracer(); $span = $tracer->spanBuilder('foo')->startSpan(); $span->addEvent('bar'); $span->setAttributes(['foo' => 'bar']); $span->recordException(new \Exception('Hello World')); $span->addLink('foo', 'bar'); // ... $span->end(); } }
Using attributes
You can add the attribute 'Webgrip\TelemetryService\Core\Domain\Services\Traceable' to your class to automatically trace all methods of the class You can also use this attribute to trace a single method
#[\Webgrip\TelemetryService\Core\Domain\Attributes\Traceable] class Foo { public function bar() { // ... } #[\Webgrip\TelemetryService\Core\Domain\Attributes\Traceable] public function baz() { // ... } }
// DI configuration return [ Foo::class => function (\DI\Container $container) { $factory = $container->get(\Webgrip\TelemetryService\Infrastructure\Factories\TracedClassFactory::class) $foo = new Foo(); return $factory->create($foo); } ];