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
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ public function request(string $method, string $url, array $options = []): Respo

$span->recordException($exception, new DateTimeImmutable(), ['exception.message' => $message]);
$span->setStatus(SpanStatus::error($message));
$scope->detach();
$tracer->complete($span);

throw $exception;
} finally {
$scope->detach();
}

// base_uri and the query option make the effective URL differ from the $url argument.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use function get_debug_type;
use function is_int;
use function is_string;
use function method_exists;
use function sprintf;

/**
Expand All @@ -44,9 +43,13 @@ public function __construct(
public function __destruct()
{
try {
if (method_exists($this->response, '__destruct')) {
$this->response->__destruct();
if (!$this->completed && $this->response->getInfo('http_code') === 0) {
$this->response->getHeaders(true);
}
} catch (Throwable $exception) {
$this->failAndComplete($exception);

throw $exception;
} finally {
$this->recordStatusFromInfo();
$this->complete();
Expand Down Expand Up @@ -194,7 +197,13 @@ private function failAndComplete(Throwable $exception): void
: $exception->getMessage();

$this->span->recordException($exception, new DateTimeImmutable(), ['exception.message' => $message]);
$this->span->setStatus(SpanStatus::error($message));
$this->recordStatusFromInfo();

if ($this->span->status()?->isError() !== true) {
$this->span->setAttribute(SemConvAttributes::ERROR_TYPE, $exception::class);
$this->span->setStatus(SpanStatus::error($message));
}

$this->complete();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\TraceableHttpClient;

use function Flow\Telemetry\DSL\url_sanitizer;

Expand All @@ -27,6 +30,22 @@
#[CoversClass(ResponseStream::class)]
final class TracableHttpClientTest extends TestCase
{
public function test_consumed_response_behind_symfony_tracing_destructs_silently(): void
{
$spanProcessor = new MemorySpanProcessor(new MemoryExporter());
$tracable = new TracableHttpClient(new TraceableHttpClient(new MockHttpClient(new MockResponse('{}', [
'http_code' => 200,
]))), TelemetryMother::withSpanProcessor($spanProcessor), 'test.client');

$response = $tracable->request('GET', 'https://api.example.com/users');
$response->getContent();
unset($response);

$spans = $spanProcessor->endedSpans();
static::assertCount(1, $spans);
static::assertSame(200, $spans[0]->attributes()['http.response.status_code']);
}

public function test_emits_no_spans_when_tracing_is_suppressed(): void
{
$spanProcessor = new MemorySpanProcessor(new MemoryExporter());
Expand All @@ -45,6 +64,132 @@ public function test_emits_no_spans_when_tracing_is_suppressed(): void
);
}

public function test_get_content_on_4xx_records_status_before_completion(): void
{
$spanProcessor = new MemorySpanProcessor(new MemoryExporter());
$tracable = new TracableHttpClient(new MockHttpClient(new MockResponse('', [
'http_code' => 404,
])), TelemetryMother::withSpanProcessor($spanProcessor), 'test.client');

$response = $tracable->request('GET', 'https://api.example.com/missing');
$exceptionThrown = false;

try {
$response->getContent();
} catch (ClientException) {
$exceptionThrown = true;
}

static::assertTrue($exceptionThrown);

$spans = $spanProcessor->endedSpans();
static::assertCount(1, $spans);
static::assertSame(404, $spans[0]->attributes()['http.response.status_code']);
static::assertSame('404', $spans[0]->attributes()['error.type']);
static::assertCount(1, $spans[0]->events());
static::assertSame('HTTP 404', $spans[0]->status()?->description);
}

public function test_get_status_code_then_get_content_on_4xx_keeps_http_status(): void
{
$spanProcessor = new MemorySpanProcessor(new MemoryExporter());
$tracable = new TracableHttpClient(new MockHttpClient(new MockResponse('', [
'http_code' => 404,
])), TelemetryMother::withSpanProcessor($spanProcessor), 'test.client');

$response = $tracable->request('GET', 'https://api.example.com/missing');
static::assertSame(404, $response->getStatusCode());
$exceptionThrown = false;

try {
$response->getContent();
} catch (ClientException) {
$exceptionThrown = true;
}

static::assertTrue($exceptionThrown);

$spans = $spanProcessor->endedSpans();
static::assertCount(1, $spans);
static::assertSame(404, $spans[0]->attributes()['http.response.status_code']);
static::assertSame('404', $spans[0]->attributes()['error.type']);
static::assertSame('HTTP 404', $spans[0]->status()?->description);
}

public function test_never_consumed_4xx_behind_symfony_tracing_throws_client_exception(): void
{
$spanProcessor = new MemorySpanProcessor(new MemoryExporter());
$tracable = new TracableHttpClient(new TraceableHttpClient(new MockHttpClient(new MockResponse('', [
'http_code' => 404,
]))), TelemetryMother::withSpanProcessor($spanProcessor), 'test.client');

$response = $tracable->request('GET', 'https://api.example.com/missing');
$exceptionThrown = false;

try {
unset($response);
} catch (ClientException) {
$exceptionThrown = true;
}

static::assertTrue($exceptionThrown);

$spans = $spanProcessor->endedSpans();
static::assertCount(1, $spans);
static::assertSame(404, $spans[0]->attributes()['http.response.status_code']);
static::assertSame('404', $spans[0]->attributes()['error.type']);
static::assertCount(1, $spans[0]->events());

$status = $spans[0]->status();
static::assertNotNull($status);
static::assertTrue($status->isError());
static::assertSame('HTTP 404', $status->description);
}

public function test_never_consumed_response_behind_symfony_tracing_completes_span(): void
{
$spanProcessor = new MemorySpanProcessor(new MemoryExporter());
$tracable = new TracableHttpClient(new TraceableHttpClient(new MockHttpClient(new MockResponse('{}', [
'http_code' => 200,
]))), TelemetryMother::withSpanProcessor($spanProcessor), 'test.client');

$response = $tracable->request('GET', 'https://api.example.com/users');
unset($response);

$spans = $spanProcessor->endedSpans();
static::assertCount(1, $spans);
static::assertSame(200, $spans[0]->attributes()['http.response.status_code']);
}

public function test_never_consumed_transport_error_completes_span_with_error(): void
{
$spanProcessor = new MemorySpanProcessor(new MemoryExporter());
$tracable = new TracableHttpClient(new MockHttpClient(new MockResponse('', [
'error' => 'mock transport error',
])), TelemetryMother::withSpanProcessor($spanProcessor), 'test.client');

$response = $tracable->request('GET', 'https://api.example.com/users');
$transportException = null;

try {
unset($response);
} catch (TransportException $exception) {
$transportException = $exception;
}

static::assertInstanceOf(TransportException::class, $transportException);

$spans = $spanProcessor->endedSpans();
static::assertCount(1, $spans);
static::assertCount(1, $spans[0]->events());
static::assertSame(TransportException::class, $spans[0]->attributes()['error.type']);

$status = $spans[0]->status();
static::assertNotNull($status);
static::assertTrue($status->isError());
static::assertSame($transportException->getMessage(), $status->description);
}

public function test_request_defaults_host_to_unknown_when_missing(): void
{
$spanProcessor = new MemorySpanProcessor(new MemoryExporter());
Expand Down Expand Up @@ -300,6 +445,37 @@ public function test_request_sets_error_status_for_5xx_codes(): void
static::assertSame('HTTP 500', $status->description);
}

public function test_request_leaves_no_active_span_after_it_fails(): void
{
$telemetry = TelemetryMother::withSpanProcessor(new MemorySpanProcessor(new MemoryExporter()));
$tracable = new TracableHttpClient(new FailingHttpClient('Connection timeout'), $telemetry, 'test.client');

$exceptionThrown = false;

try {
$tracable->request('GET', 'https://api.example.com/users');
} catch (RuntimeException) {
$exceptionThrown = true;
}

static::assertTrue($exceptionThrown);
static::assertNull($telemetry->tracer('test')->activeSpan());
}

public function test_request_leaves_no_active_span_after_it_returns(): void
{
$telemetry = TelemetryMother::withSpanProcessor(new MemorySpanProcessor(new MemoryExporter()));
$tracable = new TracableHttpClient(new MockHttpClient(new MockResponse('{}')), $telemetry, 'test.client');

$response = $tracable->request('GET', 'https://api.example.com/users');

static::assertNull($telemetry->tracer('test')->activeSpan());

$response->getContent();

static::assertNull($telemetry->tracer('test')->activeSpan());
}

public function test_request_leaves_status_unset_for_2xx_codes(): void
{
$spanProcessor = new MemorySpanProcessor(new MemoryExporter());
Expand Down Expand Up @@ -425,6 +601,24 @@ public function test_stream_unwraps_tracable_response_and_completes_span_on_last
static::assertSame(200, $spans[0]->attributes()['http.response.status_code']);
}

public function test_two_requests_in_a_row_give_two_sibling_spans(): void
{
$spanProcessor = new MemorySpanProcessor(new MemoryExporter());
$tracable = new TracableHttpClient(
new MockHttpClient([new MockResponse('{}'), new MockResponse('{}')]),
TelemetryMother::withSpanProcessor($spanProcessor),
'test.client',
);

$tracable->request('GET', 'https://api.example.com/first')->getContent();
$tracable->request('GET', 'https://api.example.com/second')->getContent();

$spans = $spanProcessor->endedSpans();
static::assertCount(2, $spans);
static::assertNull($spans[0]->context()->parentSpanId);
static::assertNull($spans[1]->context()->parentSpanId);
}

public function test_with_options_creates_new_instance(): void
{
$spanProcessor = new MemorySpanProcessor(new MemoryExporter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,40 @@ public function test_destruct_completes_span_as_backstop(): void
$response = new TraceableResponse($tracer, $spy, $span);
unset($response);

static::assertTrue($spy->destructCalled);
static::assertFalse($spy->destructCalled);
static::assertFalse($spy->getHeadersCalled);
static::assertTrue($span->isEnded());
static::assertCount(1, $processor->endedSpans());
static::assertSame(200, $span->attributes()['http.response.status_code']);
}

public function test_destruct_resolves_response_whose_status_is_unknown(): void
{
$processor = new MemorySpanProcessor(new MemoryExporter());
$tracer = TelemetryMother::withSpanProcessor($processor)->tracer('test');
$span = $tracer->span('GET host', SpanKind::CLIENT);

$spy = new SpyResponse(0);
$response = new TraceableResponse($tracer, $spy, $span);
unset($response);

static::assertTrue($spy->getHeadersCalled);
static::assertTrue($span->isEnded());
static::assertCount(1, $processor->endedSpans());
static::assertArrayNotHasKey('http.response.status_code', $span->attributes());
}

public function test_destruct_skips_resolving_after_content_was_read(): void
{
$tracer = TelemetryMother::withSpanProcessor(new MemorySpanProcessor(new MemoryExporter()))->tracer('test');
$spy = new SpyResponse(0);
$response = new TraceableResponse($tracer, $spy, $tracer->span('GET host', SpanKind::CLIENT));
$response->getContent();
unset($response);

static::assertFalse($spy->getHeadersCalled);
}

public function test_get_content_completes_span_with_error_status_for_4xx(): void
{
$processor = new MemorySpanProcessor(new MemoryExporter());
Expand Down