Skip to content

Commit

Permalink
feat: allow passing request and response callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
cdaguerre committed Dec 20, 2024
1 parent 92669b9 commit 6b908fd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 39 deletions.
9 changes: 3 additions & 6 deletions src/Http/TracedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,14 @@ protected function endTracing(): void
}

try {
if (\in_array('response.headers', $info['user_data']['span_attributes'] ?? [])) {
$this->span->setAttribute('response.headers', HttpMessageHelper::formatHeadersForSpanAttribute($this->getHeaders(false)));
}

if (\in_array('response.body', $info['user_data']['span_attributes'] ?? [])) {
if (\is_callable($info['user_data']['on_response'] ?? null)) {
if (empty($this->content)) {
$stream = $this->toStream(false);
$this->content = stream_get_contents($stream) ?: null;
rewind($stream);
}
$this->span->setAttribute('response.body', $this->content);

\call_user_func($info['user_data']['on_response'], $this->getHeaders(false), $this->content, $this->span);
}
} catch (\Throwable) {
}
Expand Down
43 changes: 10 additions & 33 deletions src/Http/TracingHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Instrumentation\Semantics\OperationName\ClientRequestOperationNameResolver;
use Instrumentation\Semantics\OperationName\ClientRequestOperationNameResolverInterface;
use Instrumentation\Tracing\Tracing;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
Expand Down Expand Up @@ -56,34 +57,15 @@ public function __construct(
$this->attributeProvider = $attributeProvider ?: new ClientRequestAttributeProvider();
}

/**
* @param array<string> $attributes
*
* @return array<string>
*/
protected function getExtraSpanAttributes(array|null $attributes): array
{
$attributes = $attributes ?: $_SERVER['OTEL_PHP_HTTP_SPAN_ATTRIBUTES'] ?? [];

if (\is_string($attributes)) {
$attributes = explode(',', $attributes);
}

if (!\is_array($attributes)) {
throw new \RuntimeException(\sprintf('Extra span attributes must be a comma separated list of attributes or an array. %s given.', get_debug_type($attributes)));
}

return $attributes;
}

/**
* @param array{
* on_progress?: ?callable,
* headers?: array<string,array<string>>,
* extra?: array{
* operation_name: non-empty-string,
* span_attributes: array<non-empty-string>,
* extra_attributes: array<non-empty-string, string>
* extra_attributes: array<non-empty-string, string>,
* on_request: callable(array<string,array<string>>, string|null, SpanInterface): void,
* on_response: callable(array<string,array<string>>, string|null, SpanInterface): void,
* }
* } $options
*/
Expand All @@ -106,22 +88,17 @@ public function request(string $method, string $url, array $options = []): Respo

$attributes = $this->attributeProvider->getAttributes($method, $url, $headers);
$attributes += $options['extra']['extra_attributes'] ?? [];
$span->setAttributes($attributes);

$scope->detach();

$options['user_data']['span_attributes'] = $this->getExtraSpanAttributes($options['extra']['span_attributes'] ?? null);

try {
if (\in_array('request.body', $options['user_data']['span_attributes']) && $body = self::getRequestBody($options)) {
$attributes['request.body'] = $body;
}
if (\in_array('request.headers', $options['user_data']['span_attributes'])) {
$attributes['request.headers'] = HttpMessageHelper::formatHeadersForSpanAttribute($headers);
}
} catch (\Throwable) {
if (\is_callable($options['extra']['on_request'] ?? null)) {
\call_user_func($options['extra']['on_request'], $headers, self::getRequestBody($options), $span);
}

$span->setAttributes($attributes);
if (isset($options['extra']['on_response'])) {
$options['user_data']['on_response'] = $options['extra']['on_response'];
}

$options = array_merge($options, [
'on_progress' => function ($dlNow, $dlSize, $info) use ($onProgress, $span, $options) {
Expand Down

0 comments on commit 6b908fd

Please sign in to comment.