Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Instrumentation/Guzzle/src/GuzzleInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use function get_cfg_var;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Promise\PromiseInterface;
use OpenTelemetry\API\Globals;
use OpenTelemetry\API\Instrumentation\CachedInstrumentation;
Expand Down Expand Up @@ -120,6 +121,12 @@ public static function register(): void
return $response;
},
onRejected: function (\Throwable $t) use ($span) {
if ($t instanceof BadResponseException && $t->hasResponse()) {
$response = $t->getResponse();
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_STATUS_CODE, $response->getStatusCode());
$span->setAttribute(TraceAttributes::NETWORK_PROTOCOL_VERSION, $response->getProtocolVersion());
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_BODY_SIZE, $response->getBody()->getSize());
}
$span->recordException($t);
$span->setStatus(StatusCode::STATUS_ERROR, $t->getMessage());
$span->end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use ArrayObject;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Promise\PromiseInterface;
Expand Down Expand Up @@ -165,4 +166,47 @@ public function test_headers_propagation(): void
$this->client->get('/');

}

/**
* @dataProvider exceptionProvider
*/
public function test_exceptions_enabled_sets_response_attributes($response, ?int $expected = null): void
{
$client = new Client([
'handler' => $this->handlerStack,
'base_uri' => 'https://example.com/',
'http_errors' => true,
'exceptions' => true,
]);
$this->mock->append($response);
$this->assertCount(0, $this->storage);

try {
$client->send(new Request('GET', 'https://example.com/error'));
} catch (\Exception $e) {
// Expected exception
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if we could expectException here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not, because it would bail out if we didn't catch, and not run all the code after this point.

}
$this->assertCount(1, $this->storage);
$span = $this->storage->offsetGet(0);
$attributes = $span->getAttributes()->toArray();
if ($expected) {
$this->assertSame($expected, $attributes[TraceAttributes::HTTP_RESPONSE_STATUS_CODE]);
$this->assertGreaterThan(0, $attributes[TraceAttributes::HTTP_RESPONSE_BODY_SIZE]);
$this->assertArrayHasKey(TraceAttributes::NETWORK_PROTOCOL_VERSION, $attributes);
} else {
$this->assertArrayNotHasKey(TraceAttributes::HTTP_RESPONSE_STATUS_CODE, $attributes);
}
}

public static function exceptionProvider(): array
{
return [
'400 Bad Request' => [new Response(400, [], 'Bad Request'), 400],
'404 Not Found' => [new Response(404, [], 'Not Found'), 404],
'500 Internal Server Error' => [new Response(500, [], 'Internal Server Error'), 500],
'503 Service Unavailable' => [new Response(503, [], 'Service Unavailable'), 503],
'network connection error' => [new ConnectException('network error', new Request('GET', 'https://example.com/error'))],
'runtime exception' => [new \RuntimeException('runtime error')],
];
}
}
Loading