Skip to content

Commit c016dc2

Browse files
authoredMay 5, 2021
Fix fatal error when the SERVER_PROTOCOL header is missing (#495)
1 parent ff0c7a3 commit c016dc2

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed
 

‎CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Fix the conditions to automatically enable the cache instrumentation when possible (#487)
66
- Fix deprecations triggered by Symfony 5.3 (#489)
7+
- Fix fatal error when the `SERVER_PROTOCOL` header is missing (#495)
78

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

@@ -13,7 +14,7 @@
1314
- Add support for distributed tracing of SQL queries while using Doctrine DBAL (#426)
1415
- Add support for distributed tracing when running a console command (#455)
1516
- Add support for distributed tracing of cache pools (#477)
16-
- Add `Full command` to extras for CLI commands, which includes command with all arguments
17+
- Add the full CLI command string to the extra context (#352)
1718
- Deprecate the `Sentry\SentryBundle\EventListener\ConsoleCommandListener` class in favor of its parent class `Sentry\SentryBundle\EventListener\ConsoleListener` (#429)
1819
- 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)
1920

‎phpstan-baseline.neon

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ parameters:
1515
count: 1
1616
path: src/EventListener/ErrorListener.php
1717

18+
-
19+
message: "#^Method Sentry\\\\SentryBundle\\\\EventListener\\\\TracingRequestListener\\:\\:getHttpFlavor\\(\\) never returns null so it can be removed from the return typehint\\.$#"
20+
count: 1
21+
path: src/EventListener/TracingRequestListener.php
22+
1823
-
1924
message: "#^Call to an undefined method Doctrine\\\\DBAL\\\\Driver\\|Sentry\\\\SentryBundle\\\\Tracing\\\\Doctrine\\\\DBAL\\\\Compatibility\\\\ExceptionConverterDriverInterface\\:\\:connect\\(\\)\\.$#"
2025
count: 1

‎src/EventListener/TracingRequestListener.php

+7-3
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

+23-2
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)
Please sign in to comment.