Skip to content

Commit f788b2a

Browse files
authored
fix: Sanatize HTTP client spans (#690)
1 parent 9be2ecf commit f788b2a

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/Tracing/HttpClient/AbstractTraceableHttpClient.php

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

5454
$uri = new Uri($url);
55+
$partialUri = Uri::fromParts([
56+
'scheme' => $uri->getScheme(),
57+
'host' => $uri->getHost(),
58+
'port' => $uri->getPort(),
59+
'path' => $uri->getPath(),
60+
]);
5561

5662
// Check if the request destination is allow listed in the trace_propagation_targets option.
5763
$client = $this->hub->getClient();
@@ -65,14 +71,16 @@ public function request(string $method, string $url, array $options = []): Respo
6571

6672
$options['headers'] = $headers;
6773

68-
$formattedUri = $this->formatUri($uri);
69-
7074
$context = new SpanContext();
7175
$context->setOp('http.client');
72-
$context->setDescription($method . ' ' . $formattedUri);
76+
$context->setDescription($method . ' ' . (string) $partialUri);
7377
$context->setTags([
7478
'http.method' => $method,
75-
'http.url' => $formattedUri,
79+
'http.url' => (string) $partialUri,
80+
]);
81+
$context->setData([
82+
'http.query' => $uri->getQuery(),
83+
'http.fragment' => $uri->getFragment(),
7684
]);
7785

7886
$span = $parent->startChild($context);
@@ -111,10 +119,4 @@ public function setLogger(LoggerInterface $logger): void
111119
$this->client->setLogger($logger);
112120
}
113121
}
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-
}
120122
}

tests/Tracing/HttpClient/TraceableHttpClientTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,17 @@ public function testRequest(): void
7878
'http.method' => 'GET',
7979
'http.url' => 'https://www.example.com/test-page',
8080
];
81+
$expectedData = [
82+
'http.query' => 'foo=bar',
83+
'http.fragment' => 'baz',
84+
];
8185

8286
$this->assertCount(2, $spans);
8387
$this->assertNull($spans[1]->getEndTimestamp());
8488
$this->assertSame('http.client', $spans[1]->getOp());
8589
$this->assertSame('GET https://www.example.com/test-page', $spans[1]->getDescription());
8690
$this->assertSame($expectedTags, $spans[1]->getTags());
91+
$this->assertSame($expectedData, $spans[1]->getData());
8792
}
8893

8994
public function testRequestDoesNotContainBaggageHeader(): void

0 commit comments

Comments
 (0)