Skip to content

Commit 0deb6e5

Browse files
committed
Fix fatal error when the SERVER_PROTOCOL header is missing
1 parent 107f60c commit 0deb6e5

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Fix the conditions to automatically enable the cache instrumentation when possible (#487)
6+
- Fix fatal error when the `SERVER_PROTOCOL` header is missing (#495)
67

78
## 4.1.0 (2021-04-19)
89

@@ -12,7 +13,7 @@
1213
- Add support for distributed tracing of SQL queries while using Doctrine DBAL (#426)
1314
- Add support for distributed tracing when running a console command (#455)
1415
- Add support for distributed tracing of cache pools (#477)
15-
- Add `Full command` to extras for CLI commands, which includes command with all arguments
16+
- Add the full CLI command string to the extra context (#352)
1617
- Deprecate the `Sentry\SentryBundle\EventListener\ConsoleCommandListener` class in favor of its parent class `Sentry\SentryBundle\EventListener\ConsoleListener` (#429)
1718
- Lower the required version of `symfony/psr-http-message-bridge` to allow installing it on a project that uses Symfony `3.4.x` components only (#480)
1819

src/EventListener/TracingRequestListener.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,18 @@ public function handleKernelTerminateEvent(RequestListenerTerminateEvent $event)
6868
private function getTags(Request $request): array
6969
{
7070
$client = $this->hub->getClient();
71+
$httpFlavor = $this->getHttpFlavor($request);
7172
$tags = [
7273
'net.host.port' => (string) $request->getPort(),
7374
'http.method' => $request->getMethod(),
7475
'http.url' => $request->getUri(),
75-
'http.flavor' => $this->getHttpFlavor($request),
7676
'route' => $this->getRouteName($request),
7777
];
7878

79+
if (null !== $httpFlavor) {
80+
$tags['http.flavor'] = $httpFlavor;
81+
}
82+
7983
if (false !== filter_var($request->getHost(), \FILTER_VALIDATE_IP)) {
8084
$tags['net.host.ip'] = $request->getHost();
8185
} else {
@@ -94,11 +98,11 @@ private function getTags(Request $request): array
9498
*
9599
* @param Request $request The HTTP request
96100
*/
97-
protected function getHttpFlavor(Request $request): string
101+
private function getHttpFlavor(Request $request): ?string
98102
{
99103
$protocolVersion = $request->getProtocolVersion();
100104

101-
if (str_starts_with($protocolVersion, 'HTTP/')) {
105+
if (null !== $protocolVersion && str_starts_with($protocolVersion, 'HTTP/')) {
102106
return substr($protocolVersion, \strlen('HTTP/'));
103107
}
104108

tests/EventListener/TracingRequestListenerTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testHandleKernelRequestEvent(Options $options, Request $request,
5252
$transaction = new Transaction(new TransactionContext());
5353

5454
$client = $this->createMock(ClientInterface::class);
55-
$client->expects($this->once())
55+
$client->expects($this->any())
5656
->method('getOptions')
5757
->willReturn($options);
5858

@@ -63,7 +63,7 @@ public function testHandleKernelRequestEvent(Options $options, Request $request,
6363
$this->hub->expects($this->once())
6464
->method('startTransaction')
6565
->with($this->callback(function (TransactionContext $context) use ($expectedTransactionContext): bool {
66-
$this->assertEquals($context, $expectedTransactionContext);
66+
$this->assertEquals($expectedTransactionContext, $context);
6767

6868
return true;
6969
}))
@@ -326,6 +326,27 @@ public function handleKernelRequestEventDataProvider(): \Generator
326326
$request,
327327
$transactionContext,
328328
];
329+
330+
$request = Request::createFromGlobals();
331+
$request->server->set('REQUEST_TIME_FLOAT', 1613493597.010275);
332+
333+
$transactionContext = new TransactionContext();
334+
$transactionContext->setName('GET http://:/');
335+
$transactionContext->setOp('http.server');
336+
$transactionContext->setStartTimestamp(1613493597.010275);
337+
$transactionContext->setTags([
338+
'net.host.port' => '',
339+
'http.method' => 'GET',
340+
'http.url' => 'http://:/',
341+
'route' => '<unknown>',
342+
'net.host.name' => '',
343+
]);
344+
345+
yield 'request.server.SERVER_PROTOCOL NOT EXISTS' => [
346+
new Options(),
347+
$request,
348+
$transactionContext,
349+
];
329350
}
330351

331352
public function testHandleKernelRequestEventDoesNothingIfRequestTypeIsSubRequest(): void

0 commit comments

Comments
 (0)