PHP code example of open-telemetry / ext-opentelemetry

1. Go to this page and download the library: Download open-telemetry/ext-opentelemetry library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

open-telemetry / ext-opentelemetry example snippets




$tracer = new Tracer(...);

OpenTelemetry\Instrumentation\hook(
    DemoClass::class,
    'run',
    pre: static function (DemoClass $demo, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) {
        $span = $tracer->spanBuilder($class)
            ->startSpan();
        Context::storage()->attach($span->storeInContext(Context::getCurrent()));
    },
    post: static function (DemoClass $demo, array $params, $returnValue, ?Throwable $exception) use ($tracer) {
        $scope = Context::storage()->scope();
        $scope?->detach();
        $span = Span::fromContext($scope->context());
        $exception && $span->recordException($exception);
        $span->setStatus($exception ? StatusCode::STATUS_ERROR : StatusCode::STATUS_OK);
        $span->end();
    }
);


OpenTelemetry\Instrumentation\hook(
    null,
    'hello',
     function($obj, array $params) {
        return [
          0 => null,  //make first param null
          2 => 'baz', //replace 3rd param
          3 => 'bat', //add 4th param
        ];
    }
);
function hello($one = null, $two = null, $three = null, $four = null) {
  var_dump(func_get_args());
}

hello('a', 'b', 'c');


\OpenTelemetry\Instrumentation\hook(null, 'hello', post: fn(mixed $object, array $params, string $return): int => ++$return);

function hello(int $val) {
    return $val;
}

var_dump(hello(1));


\OpenTelemetry\Instrumentation\hook(null, 'hello', post: function(mixed $object, array $params, mixed $return, ?Throwable $throwable) {
    throw new Exception('new', previous: $throwable);
});

function hello() {
    throw new Exception('original');
}

try {
    hello();
} catch (\Throwable $t) {
    var_dump($t->getMessage());
    var_dump($t->getPrevious()?->getMessage());
}

string(3) "new"
string(8) "original"

use OpenTelemetry\API\Instrumentation\WithSpan

class MyClass
{
    #[WithSpan]
    public function trace_me(): void
    {
        /* ... */
    }

    #[WithSpan('custom_span_name', SpanKind::KIND_INTERNAL, ['my-attr' => 'value'])]
    public function trace_me_with_customization(): void
    {
        /* ... */
    }
}

#[WithSpan]
function my_function(): void
{
    /* ... */
}

use OpenTelemetry\API\Instrumentation\WithSpan
use OpenTelemetry\API\Instrumentation\SpanAttribute

class MyClass
{
    #[WithSpan]
    public function add_user(
        #[SpanAttribute] string $username,
        string $password,
        #[SpanAttribute('a_better_attribute_name')] string $foo_bar_baz,
    ): void
    {
        /* ... */
    }
shell
install-php-extensions opentelemetry-php/ext-opentelemetry@main
shell
install-php-extensions opentelemetry[-beta|-stable|-latest]
shell
php --ri  opentelemetry