open-telemetry / opentelemetry-php

The OpenTelemetry PHP Library

Home Page:https://opentelemetry.io/docs/instrumentation/php/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

auto-instrumention support for `GuzzleHttp\ClientInterface` and `sendRequest()`

paulgrav opened this issue · comments

Is your feature request related to a problem?

There are no PHP auto-instrumentation libraries that support GuzzleHttp\ClientInterface and public function send(RequestInterface $request, array $options = []): ResponseInterface;

We can’t easily migrate to Psr18 because Psr\Http\Client\ClientInterface and public function sendRequest(RequestInterface $request): ResponseInterface; doesn’t support specifying options.

Describe the solution you'd like

There would be an auto-instrumentation library, very similar to https://github.com/opentelemetry-php/contrib-auto-psr18 that would support the use case described above.

Describe alternatives you've considered

We could create our own PHP library that is almost identical to https://github.com/opentelemetry-php/contrib-auto-psr18 but with a couple of line changes that would auto-instrument our GuzzleHttp calls. But should contrib-auto-psr18 support both scenarios? Should there be a separate library for non-psr18 implementations?

Additional context

The code change would look something like:

--- A/app.kaleidoscope.kaleidoscope_dynamic_file_7BF225D2-5172-4ECB-A7F0-FF0350CF33E7-64343-00000408697D7FDD.txt
+++ B/app.kaleidoscope.kaleidoscope_dynamic_file_73C56430-878D-4719-A22F-703297E718CE-64343-0000040871B329A1.txt
@@ -13,9 +13,10 @@
 use OpenTelemetry\Context\Context;
 use function OpenTelemetry\Instrumentation\hook;
 use OpenTelemetry\SemConv\TraceAttributes;
-use Psr\Http\Client\ClientInterface;
+use GuzzleHttp\ClientInterface;
 use Psr\Http\Message\RequestInterface;
 use Psr\Http\Message\ResponseInterface;
 use function sprintf;
 use function strtolower;
 use Throwable;
@@ -31,7 +32,7 @@
 
         hook(
             ClientInterface::class,
+            'send',
-            'sendRequest',
             pre: static function (ClientInterface $client, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($instrumentation): ?array {
                 $request = $params[0] ?? null;
                 if (!$request instanceof RequestInterface) {
@@ -118,4 +119,4 @@
             },
         );
     }
+}
-}
\ No newline at end of file

But should contrib-auto-psr18 support both scenarios? Should there be a separate library for non-psr18 implementations?

I think it should be a new library, rather than trying to hack guzzle support into psr-18. There are also other guzzle methods which could also be handled (get, post, etc) for a guzzle auto-instrumentation library to be as useful as possible.

@brettmc Thank you very much! It works as expected.