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

Align OTEL http attributes with latest standard #121

Merged
merged 3 commits into from
Sep 4, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

## [1.2.1] - 2024-09-04

### Changed
- Align OTEL http attributes with latest standard

## [1.2.0]

### Changed
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
final class Constants
{
/** @var string The current version for this Library */
public const KIOTA_HTTP_CLIENT_VERSION = '1.2.0';
public const KIOTA_HTTP_CLIENT_VERSION = '1.2.1';
}
20 changes: 10 additions & 10 deletions src/GuzzleRequestAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,13 @@ public function getPsrRequestFromRequestInformation(RequestInformation $requestI
$scope = $psrRequestFromInfoSpan->activate();
try {
$requestInformation->pathParameters["baseurl"] = $this->getBaseUrl();
$span->setAttribute('http.method', $requestInformation->httpMethod);
$span->setAttribute('http.scheme', explode(':', $requestInformation->getUri())[0]);
$span->setAttribute('http.request.method', $requestInformation->httpMethod);
$span->setAttribute('server.address', explode(':', $requestInformation->getUri())[0]);
if ($this->getObservabilityOptionsFromRequest($requestInformation)->getIncludeEUIIAttributes()) {
$span->setAttribute('http.uri', $requestInformation->getUri());
$span->setAttribute('url.full', $requestInformation->getUri());
}
if (!empty($requestInformation->content)) {
$span->setAttribute('http.request_content_length?', $requestInformation->content->getSize());
$span->setAttribute('http.request.body.size?', $requestInformation->content->getSize());
}

$result = new Request(
Expand Down Expand Up @@ -718,9 +718,9 @@ private function startTracingSpan(RequestInformation $requestInfo, string $metho
$decUriTemplate = ParametersNameDecodingHandler::decodeUriEncodedString($requestInfo->urlTemplate, $parametersToDecode);
$telemetryPathValue = empty($decUriTemplate) ? '' : preg_replace($queryReg, '', $decUriTemplate);
$span = $this->tracer->spanBuilder("$methodName - $telemetryPathValue")->startSpan();
$span->setAttribute("http.uri_template", $decUriTemplate);
$span->setAttribute('http.method', $requestInfo->httpMethod);
$span->setAttribute('http.request_content_type?', $requestInfo->getHeaders()->get(RequestInformation::$contentTypeHeader));
$span->setAttribute("url.uri_template", $decUriTemplate);
$span->setAttribute('http.request.method', $requestInfo->httpMethod);
$span->setAttribute('http.request.header.content-type?', $requestInfo->getHeaders()->get(RequestInformation::$contentTypeHeader));
return $span;
}

Expand All @@ -731,9 +731,9 @@ private function startTracingSpan(RequestInformation $requestInfo, string $metho
*/
private function setHttpResponseAttributesInSpan(SpanInterface $span, ResponseInterface $response): void
{
$span->setAttribute('http.status_code', $response->getStatusCode());
$span->setAttribute('http.flavor', $response->getProtocolVersion());
$span->setAttribute('http.response_content_type?', $response->getHeaderLine('Content-Type'));
$span->setAttribute('http.response.status_code', $response->getStatusCode());
$span->setAttribute('network.protocol.version', $response->getProtocolVersion());
$span->setAttribute('http.response.header.content-type?', $response->getHeaderLine('Content-Type'));
}

/**
Expand Down
17 changes: 12 additions & 5 deletions src/Middleware/RetryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class RetryHandler
public const HANDLER_NAME = 'kiotaRetryHandler';
private const RETRY_AFTER_HEADER = "Retry-After";
private const RETRY_ATTEMPT_HEADER = "Retry-Attempt";

private const RESEND_COUNT_ATTRIBUTE = "http.request.resend_count";
private const RESEND_DELAY_ATTRIBUTE = "http.request.resend_delay";
private const STATUS_CODE_ATTRIBUTE = "http.response.status_code";

/**
* @var TracerInterface
*/
Expand Down Expand Up @@ -114,9 +117,11 @@ private function onFulfilled(RequestInterface $request, array $options, SpanInte
return $response;
}
$retries = $this->getRetries($request);
$span->setAttribute('retryCount', $retries);
$span->setAttribute(self::RESEND_COUNT_ATTRIBUTE, $retries);
$delaySecs = $this->calculateDelay($retries, $response);
$span->setAttribute('delaySeconds', $delaySecs);
$span->setAttribute(self::RESEND_DELAY_ATTRIBUTE, $delaySecs);
$statusCode = $response->getStatusCode();
$span->setAttribute(self::STATUS_CODE_ATTRIBUTE, $statusCode);
$fullFilledSpan->setStatus(StatusCode::STATUS_OK, 'RetryFullFilled');
if (!$this->shouldRetry($request, $retries, $delaySecs, $response, $span)
|| $this->exceedRetriesTimeLimit($delaySecs)) {
Expand Down Expand Up @@ -158,9 +163,11 @@ private function onRejected(RequestInterface $request, array $options, SpanInter
}

$retries = $this->getRetries($request);
$rejectedSpan->setAttribute('http.retry_count', $retries);
$rejectedSpan->setAttribute(self::RESEND_COUNT_ATTRIBUTE, $retries);
$delaySecs = $this->calculateDelay($retries, $reason->getResponse());
$rejectedSpan->setAttribute('delaySeconds', $delaySecs);
$rejectedSpan->setAttribute(self::RESEND_DELAY_ATTRIBUTE, $delaySecs);
$statusCode = $reason->getResponse()->getStatusCode();
$span->setAttribute(self::STATUS_CODE_ATTRIBUTE, $statusCode);
if (!$this->shouldRetry($request, $retries, $delaySecs, $reason->getResponse(), $span)
|| $this->exceedRetriesTimeLimit($delaySecs)) {
Create::rejectionFor($reason);
Expand Down