Skip to content

Commit 795376f

Browse files
authored
ref: Add proper HTTP client span descriptions (#680)
1 parent 38f70b7 commit 795376f

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
## Unreleased
44

5-
- feat: Add support for tracing of the Symfony HTTP client requests (#606)
5+
- feat: Add support for tracing of Symfony HTTP client requests (#606)
6+
- feat: Add support for HTTP client baggage propagation (#663)
7+
- ref: Add proper HTTP client span descriptions (#680)
68
- feat: Support logging the impersonator user, if any (#647)
79
- ref: Use constant for the SDK version (#662)
8-
- Add support for HTTP client baggage propagation (#663)
910

1011
## 4.4.0 (2022-10-20)
1112

src/Tracing/HttpClient/AbstractTraceableHttpClient.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ public function request(string $method, string $url, array $options = []): Respo
5151
$headers = $options['headers'] ?? [];
5252
$headers['sentry-trace'] = $parent->toTraceparent();
5353

54+
$uri = new Uri($url);
55+
5456
// Check if the request destination is allow listed in the trace_propagation_targets option.
5557
$client = $this->hub->getClient();
5658
if (null !== $client) {
5759
$sdkOptions = $client->getOptions();
58-
$uri = new Uri($url);
5960

6061
if (\in_array($uri->getHost(), $sdkOptions->getTracePropagationTargets())) {
6162
$headers['baggage'] = $parent->toBaggage();
@@ -64,12 +65,14 @@ public function request(string $method, string $url, array $options = []): Respo
6465

6566
$options['headers'] = $headers;
6667

68+
$formattedUri = $this->formatUri($uri);
69+
6770
$context = new SpanContext();
6871
$context->setOp('http.client');
69-
$context->setDescription('HTTP ' . $method);
72+
$context->setDescription($method . ' ' . $formattedUri);
7073
$context->setTags([
7174
'http.method' => $method,
72-
'http.url' => $url,
75+
'http.url' => $formattedUri,
7376
]);
7477

7578
$span = $parent->startChild($context);
@@ -108,4 +111,10 @@ public function setLogger(LoggerInterface $logger): void
108111
$this->client->setLogger($logger);
109112
}
110113
}
114+
115+
private function formatUri(Uri $uri): string
116+
{
117+
// Instead of relying on Uri::__toString, we only use a sub set of the URI
118+
return Uri::composeComponents($uri->getScheme(), $uri->getHost(), $uri->getPath(), null, null);
119+
}
111120
}

tests/Tracing/HttpClient/TraceableHttpClientTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ public function testRequest(): void
6363
$mockResponse = new MockResponse();
6464
$decoratedHttpClient = new MockHttpClient($mockResponse);
6565
$httpClient = new TraceableHttpClient($decoratedHttpClient, $this->hub);
66-
$response = $httpClient->request('GET', 'https://www.example.com/test-page');
66+
$response = $httpClient->request('GET', 'https://username:password@www.example.com/test-page?foo=bar#baz');
6767

6868
$this->assertInstanceOf(AbstractTraceableResponse::class, $response);
6969
$this->assertSame(200, $response->getStatusCode());
7070
$this->assertSame('GET', $response->getInfo('http_method'));
71-
$this->assertSame('https://www.example.com/test-page', $response->getInfo('url'));
71+
$this->assertSame('https://username:password@www.example.com/test-page?foo=bar#baz', $response->getInfo('url'));
7272
$this->assertSame(['sentry-trace: ' . $transaction->toTraceparent()], $mockResponse->getRequestOptions()['normalized_headers']['sentry-trace']);
7373
$this->assertArrayNotHasKey('baggage', $mockResponse->getRequestOptions()['normalized_headers']);
7474
$this->assertNotNull($transaction->getSpanRecorder());
@@ -82,7 +82,7 @@ public function testRequest(): void
8282
$this->assertCount(2, $spans);
8383
$this->assertNull($spans[1]->getEndTimestamp());
8484
$this->assertSame('http.client', $spans[1]->getOp());
85-
$this->assertSame('HTTP GET', $spans[1]->getDescription());
85+
$this->assertSame('GET https://www.example.com/test-page', $spans[1]->getDescription());
8686
$this->assertSame($expectedTags, $spans[1]->getTags());
8787
}
8888

@@ -130,7 +130,7 @@ public function testRequestDoesNotContainBaggageHeader(): void
130130
$this->assertCount(2, $spans);
131131
$this->assertNull($spans[1]->getEndTimestamp());
132132
$this->assertSame('http.client', $spans[1]->getOp());
133-
$this->assertSame('HTTP PUT', $spans[1]->getDescription());
133+
$this->assertSame('PUT https://www.example.com/test-page', $spans[1]->getDescription());
134134
$this->assertSame($expectedTags, $spans[1]->getTags());
135135
}
136136

@@ -178,7 +178,7 @@ public function testRequestDoesContainBaggageHeader(): void
178178
$this->assertCount(2, $spans);
179179
$this->assertNull($spans[1]->getEndTimestamp());
180180
$this->assertSame('http.client', $spans[1]->getOp());
181-
$this->assertSame('HTTP POST', $spans[1]->getDescription());
181+
$this->assertSame('POST https://www.example.com/test-page', $spans[1]->getDescription());
182182
$this->assertSame($expectedTags, $spans[1]->getTags());
183183
}
184184

@@ -214,7 +214,7 @@ public function testStream(): void
214214
$this->assertCount(2, $spans);
215215
$this->assertNotNull($spans[1]->getEndTimestamp());
216216
$this->assertSame('http.client', $spans[1]->getOp());
217-
$this->assertSame('HTTP GET', $spans[1]->getDescription());
217+
$this->assertSame('GET https://www.example.com/test-page', $spans[1]->getDescription());
218218
$this->assertSame($expectedTags, $spans[1]->getTags());
219219

220220
$loopIndex = 0;

0 commit comments

Comments
 (0)