99use Swoole \Http \Request ;
1010use Swoole \Http \Response ;
1111use SwooleBundle \SwooleBundle \Bridge \Symfony \HttpFoundation \RequestFactory ;
12+ use SwooleBundle \SwooleBundle \Common \System \System ;
1213use 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}
0 commit comments