Skip to content

fix: Sanatize HTTP client spans #690

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

Merged
merged 4 commits into from
Feb 14, 2023
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
22 changes: 12 additions & 10 deletions src/Tracing/HttpClient/AbstractTraceableHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public function request(string $method, string $url, array $options = []): Respo
$headers['sentry-trace'] = $parent->toTraceparent();

$uri = new Uri($url);
$partialUri = Uri::fromParts([
'scheme' => $uri->getScheme(),
'host' => $uri->getHost(),
'port' => $uri->getPort(),
'path' => $uri->getPath(),
]);

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

$options['headers'] = $headers;

$formattedUri = $this->formatUri($uri);

$context = new SpanContext();
$context->setOp('http.client');
$context->setDescription($method . ' ' . $formattedUri);
$context->setDescription($method . ' ' . (string) $partialUri);
$context->setTags([
'http.method' => $method,
'http.url' => $formattedUri,
'http.url' => (string) $partialUri,
]);
$context->setData([
'http.query' => $uri->getQuery(),
'http.fragment' => $uri->getFragment(),
]);

$span = $parent->startChild($context);
Expand Down Expand Up @@ -111,10 +119,4 @@ public function setLogger(LoggerInterface $logger): void
$this->client->setLogger($logger);
}
}

private function formatUri(Uri $uri): string
{
// Instead of relying on Uri::__toString, we only use a sub set of the URI
return Uri::composeComponents($uri->getScheme(), $uri->getHost(), $uri->getPath(), null, null);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature is Uri::composeComponents(?string $scheme, ?string $authority, string $path, ?string $query, ?string $fragment), so I feel like this was implemented in a wrong way.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's almost an internal method I think they chose not to bother with default arguments. It's also not part of the PSR-7 spec: https://www.php-fig.org/psr/psr-7/.

}
}
5 changes: 5 additions & 0 deletions tests/Tracing/HttpClient/TraceableHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ public function testRequest(): void
'http.method' => 'GET',
'http.url' => 'https://www.example.com/test-page',
];
$expectedData = [
'http.query' => 'foo=bar',
'http.fragment' => 'baz',
];

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

public function testRequestDoesNotContainBaggageHeader(): void
Expand Down