9
9
use Swoole \Http \Request ;
10
10
use Swoole \Http \Response ;
11
11
use SwooleBundle \SwooleBundle \Bridge \Symfony \HttpFoundation \RequestFactory ;
12
+ use SwooleBundle \SwooleBundle \Common \System \System ;
12
13
use Symfony \Component \HttpFoundation \Request as SymfonyRequest ;
13
14
14
- final readonly class RequestMonitoring
15
+ final class RequestMonitoring
15
16
{
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
+ ) {}
17
23
18
24
public function monitor (Closure $ fn , Request $ request , Response $ response ): void
19
25
{
@@ -29,11 +35,48 @@ public function monitor(Closure $fn, Request $request, Response $response): void
29
35
30
36
private function start (SymfonyRequest $ request ): void
31
37
{
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 ()));
33
63
}
34
64
35
65
private function stop (): void
36
66
{
67
+ if ($ this ->getBlackfireVersion () === '' ) {
68
+ return ;
69
+ }
70
+
37
71
BlackfireProbe::stopTransaction ();
38
72
}
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
+ }
39
82
}
0 commit comments