Skip to content

Commit d7c3a63

Browse files
committed
fix(blackfire): minor fixes to balckfire monitoring feature
1 parent 26f387b commit d7c3a63

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

phpstan.neon

+6
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@ parameters:
2626
-
2727
message: '#invalid (return )?type Doctrine\\Common\\Proxy\\Proxy#'
2828
path: src/Bridge/Doctrine/BlockingProxyFactory.php
29+
-
30+
message: '#Static method BlackfireProbe::startTransaction\(\) invoked with 1 parameter, 0 required\.#'
31+
path: src/Bridge/Upscale/Blackfire/Monitoring/RequestMonitoring.php
32+
-
33+
message: "#Call to function method_exists\\(\\) with 'BlackfireProbe' and 'setAttribute' will always evaluate to false.#"
34+
path: src/Bridge/Upscale/Blackfire/Monitoring/RequestMonitoring.php
2935
tmpDir: /tmp/phpstan_src

src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/BlackfireMonitoringPass.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use SwooleBundle\SwooleBundle\Bridge\Upscale\Blackfire\Monitoring\BlackfireMiddlewareFactory;
1111
use SwooleBundle\SwooleBundle\Bridge\Upscale\Blackfire\Monitoring\RequestMonitoring;
1212
use SwooleBundle\SwooleBundle\Bridge\Upscale\Blackfire\Monitoring\WithApm;
13+
use SwooleBundle\SwooleBundle\Common\System\System;
1314
use SwooleBundle\SwooleBundle\Server\Middleware\MiddlewareInjector;
1415
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -34,7 +35,8 @@ public function process(ContainerBuilder $container): void
3435
->setAutowired(false)
3536
->setAutoconfigured(false)
3637
->setPublic(false)
37-
->setArgument('$requestFactory', new Reference(RequestFactory::class));
38+
->setArgument('$requestFactory', new Reference(RequestFactory::class))
39+
->setArgument('$system', new Reference(System::class));
3840

3941
$container->register(BlackfireMiddlewareFactory::class)
4042
->setClass(BlackfireMiddlewareFactory::class)

src/Bridge/Upscale/Blackfire/Monitoring/RequestMonitoring.php

+46-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@
99
use Swoole\Http\Request;
1010
use Swoole\Http\Response;
1111
use SwooleBundle\SwooleBundle\Bridge\Symfony\HttpFoundation\RequestFactory;
12+
use SwooleBundle\SwooleBundle\Common\System\System;
1213
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
1314

14-
final readonly class RequestMonitoring
15+
final class RequestMonitoring
1516
{
16-
public function __construct(private RequestFactory $requestFactory) {}
17+
private string|null $blackfireVersion = null;
18+
19+
public function __construct(
20+
private RequestFactory $requestFactory,
21+
private System $system,
22+
) {}
1723

1824
public function monitor(Closure $fn, Request $request, Response $response): void
1925
{
@@ -29,11 +35,48 @@ public function monitor(Closure $fn, Request $request, Response $response): void
2935

3036
private function start(SymfonyRequest $request): void
3137
{
32-
BlackfireProbe::startTransaction();
38+
$blackfireVersion = $this->getBlackfireVersion();
39+
40+
if ($blackfireVersion === '') {
41+
return;
42+
}
43+
44+
$transactionName = $request->getMethod() . ' ' . $request->getPathInfo();
45+
46+
if (version_compare($blackfireVersion, '1.78.0', '>=')) {
47+
BlackfireProbe::startTransaction($transactionName);
48+
} else {
49+
BlackfireProbe::startTransaction();
50+
BlackfireProbe::setTransactionName($transactionName);
51+
}
52+
53+
if (!method_exists(BlackfireProbe::class, 'setAttribute')) {
54+
return;
55+
}
56+
57+
BlackfireProbe::setAttribute('http.target', $request->getPathInfo());
58+
BlackfireProbe::setAttribute('http.url', $request->getRequestUri());
59+
BlackfireProbe::setAttribute('http.method', $request->getMethod());
60+
BlackfireProbe::setAttribute('http.host', $request->getHost());
61+
BlackfireProbe::setAttribute('host', $request->getHost());
62+
BlackfireProbe::setAttribute('framework', sprintf('Symfony with %s', $this->system->extension()->toString()));
3363
}
3464

3565
private function stop(): void
3666
{
67+
if ($this->getBlackfireVersion() === '') {
68+
return;
69+
}
70+
3771
BlackfireProbe::stopTransaction();
3872
}
73+
74+
private function getBlackfireVersion(): string
75+
{
76+
if ($this->blackfireVersion === null) {
77+
$this->blackfireVersion = (($v = phpversion('blackfire')) === false ? '' : $v);
78+
}
79+
80+
return $this->blackfireVersion;
81+
}
3982
}

tests/Fixtures/Symfony/app/config/swoole.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
$parameters->set('env(HOST)', '0.0.0.0');
1313

14-
$parameters->set('env(TRUSTED_HOSTS)', 'localhost,127.0.0.1,.*.swoole-bundle.orb.local');
14+
$parameters->set('env(TRUSTED_HOSTS)', 'localhost,127.0.0.1,.*.swoole-bundle.orb.local,192.168.*.*');
1515

1616
$parameters->set('env(TRUSTED_PROXIES)', '*,192.168.0.0/16');
1717

0 commit comments

Comments
 (0)