Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TraceableHttpClient): span finishes too early #105

Merged
merged 2 commits into from
Dec 22, 2024
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
45 changes: 15 additions & 30 deletions src/Instrumentation/Symfony/HttpClient/TraceableHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\HttpClient;

use Nyholm\Psr7\Uri;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\API\Trace\TracerInterface;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SemConv\TraceAttributes;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\Response\ResponseStream;
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
Expand All @@ -38,38 +35,26 @@ public function request(string $method, string $url, array $options = []): Respo
} else {
$this->logger?->debug('No active scope');
}
$span = null;

try {
$uri = new Uri($url);
$uri = new Uri($url);

$spanBuilder = $this->tracer
->spanBuilder('http.client')
->setSpanKind(SpanKind::KIND_CLIENT)
->setParent($scope?->context())
->setAttribute(TraceAttributes::URL_FULL, $url)
->setAttribute(TraceAttributes::URL_SCHEME, $uri->getScheme())
->setAttribute(TraceAttributes::URL_PATH, $uri->getPath())
->setAttribute(TraceAttributes::URL_QUERY, $uri->getQuery())
->setAttribute(TraceAttributes::URL_FRAGMENT, $uri->getFragment())
->setAttribute(TraceAttributes::HTTP_REQUEST_METHOD, $method)
;
$spanBuilder = $this->tracer
->spanBuilder('http.client')
->setSpanKind(SpanKind::KIND_CLIENT)
->setParent($scope?->context())
->setAttribute(TraceAttributes::URL_FULL, $url)
->setAttribute(TraceAttributes::URL_SCHEME, $uri->getScheme())
->setAttribute(TraceAttributes::URL_PATH, $uri->getPath())
->setAttribute(TraceAttributes::URL_QUERY, $uri->getQuery())
->setAttribute(TraceAttributes::URL_FRAGMENT, $uri->getFragment())
->setAttribute(TraceAttributes::HTTP_REQUEST_METHOD, $method)
;

$span = $spanBuilder->startSpan();
$span = $spanBuilder->startSpan();

$this->logger?->debug(sprintf('Starting span "%s"', $span->getContext()->getSpanId()));
$this->logger?->debug(sprintf('Starting span "%s"', $span->getContext()->getSpanId()));

return new TraceableResponse($this->client, $this->client->request($method, $url, $options), $span);
} catch (ExceptionInterface $exception) {
$span->recordException($exception, [TraceAttributes::EXCEPTION_ESCAPED => true]);
$span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage());
throw $exception;
} finally {
if ($span instanceof SpanInterface) {
$this->logger?->debug(sprintf('Ending span "%s"', $span->getContext()->getSpanId()));
$span->end();
}
}
return new TraceableResponse($this->client, $this->client->request($method, $url, $options), $span);
}

public function stream(iterable|ResponseInterface $responses, ?float $timeout = null): ResponseStreamInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function testOk(): void
'url.query' => '',
'url.fragment' => '',
'http.request.method' => 'GET',
'http.response.status_code' => 200,
]);
self::assertSpanEventsCount($mainSpan, 0);
}
Expand Down Expand Up @@ -82,14 +83,15 @@ public function testFailure(): void

$mainSpan = self::getSpans()[0];
self::assertSpanName($mainSpan, 'http.client');
self::assertSpanStatus($mainSpan, StatusData::unset());
self::assertSpanStatus($mainSpan, StatusData::error());
self::assertSpanAttributes($mainSpan, [
'url.full' => 'http://localhost/failure',
'url.scheme' => 'http',
'url.path' => '/failure',
'url.query' => '',
'url.fragment' => '',
'http.request.method' => 'GET',
'http.response.status_code' => 500,
]);
self::assertSpanEventsCount($mainSpan, 0);
}
Expand Down Expand Up @@ -124,6 +126,7 @@ public function testException(): void
'url.query' => '',
'url.fragment' => '',
'http.request.method' => 'GET',
'http.response.status_code' => 200,
]);
self::assertSpanEventsCount($mainSpan, 0);
}
Expand Down Expand Up @@ -179,6 +182,7 @@ public function testStream(): void
'url.query' => '',
'url.fragment' => '',
'http.request.method' => 'GET',
'http.response.status_code' => 200,
]);
self::assertSpanEventsCount($mainSpan, 0);
}
Expand Down
Loading