Skip to content

Commit

Permalink
chore: fix traceparent header in tracing client
Browse files Browse the repository at this point in the history
  • Loading branch information
cdaguerre committed Nov 6, 2024
1 parent c7d305a commit 4840d57
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/Http/TracingHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,13 @@ public function request(string $method, string $url, array $options = []): Respo
$headers = $options['headers'] ?? [];
$operationName = $options['extra']['operation_name'] ?? $this->operationNameResolver->getOperationName($method, $url);

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

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

try {
if (\in_array('request.body', $options['user_data']['span_attributes'])) {
$attributes['request.body'] = self::getRequestBody($options);
}
if (\in_array('request.headers', $options['user_data']['span_attributes'])) {
$attributes['request.headers'] = HttpMessageHelper::formatHeadersForSpanAttribute($headers);
}
} catch (\Throwable) {
}

$span = Tracing::getTracer()
->spanBuilder($operationName)
->setSpanKind(SpanKind::KIND_CLIENT)
->setAttributes($attributes)
->startSpan();

$span->activate();

$options = array_merge($options, [
'on_progress' => function ($dlNow, $dlSize, $info) use ($onProgress, $span, $options) {
static $dlStarted = false;
Expand All @@ -130,8 +116,8 @@ public function request(string $method, string $url, array $options = []): Respo
$span->updateName($operationName);
}

$span->setAttribute(TraceAttributes::HTTP_STATUS_CODE, $info['http_code']);
$span->setAttribute(TraceAttributes::HTTP_URL, $info['url']);
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_STATUS_CODE, $info['http_code']);
$span->setAttribute(TraceAttributes::URL_FULL, $info['url']);

if (\array_key_exists('total_time', $info)) {
$timestamp = (int) (($info['start_time'] + $info['total_time']) * ClockInterface::NANOS_PER_SECOND);
Expand All @@ -149,6 +135,23 @@ public function request(string $method, string $url, array $options = []): Respo
),
]);

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

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

try {
if (\in_array('request.body', $options['user_data']['span_attributes'])) {
$attributes['request.body'] = self::getRequestBody($options);
}
if (\in_array('request.headers', $options['user_data']['span_attributes'])) {
$attributes['request.headers'] = HttpMessageHelper::formatHeadersForSpanAttribute($options['headers']);

Check failure on line 148 in src/Http/TracingHttpClient.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3)

Parameter #1 $headers of static method Instrumentation\Http\HttpMessageHelper::formatHeadersForSpanAttribute() expects array<string, array<string>>, array<string, array<string>|string> given.
}
} catch (\Throwable) {
}

$span->setAttributes($attributes);

Check failure on line 153 in src/Http/TracingHttpClient.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3)

Parameter #1 $attributes of method OpenTelemetry\API\Trace\SpanInterface::setAttributes() expects iterable<non-empty-string, array|bool|float|int|string|null>, array<string, array<string>|Closure|resource|string> given.

return new TracedResponse($this->client->request($method, $url, $options), $span);
}

Expand Down

0 comments on commit 4840d57

Please sign in to comment.