@@ -62,26 +62,29 @@ public function handle(Request $request, Closure $next)
6262 */
6363 public function terminate (Request $ request , $ response ): void
6464 {
65- if ($ this ->transaction !== null && app ()->bound (HubInterface::class)) {
66- // We stop here if a route has not been matched unless we are configured to trace missing routes
67- if (config ('sentry.tracing.missing_routes ' , false ) === false && $ request ->route () === null ) {
68- return ;
69- }
65+ // If there is no transaction or the HubInterface is not bound in the container there is nothing for us to do
66+ if ($ this ->transaction === null || !app ()->bound (HubInterface::class)) {
67+ return ;
68+ }
7069
71- if ($ this ->appSpan !== null ) {
72- $ this ->appSpan ->finish ();
73- }
70+ // We stop here if a route has not been matched unless we are configured to trace missing routes
71+ if (config ('sentry.tracing.missing_routes ' , false ) === false && $ request ->route () === null ) {
72+ return ;
73+ }
7474
75- // Make sure we set the transaction and not have a child span in the Sentry SDK
76- // If the transaction is not on the scope during finish, the trace.context is wrong
77- SentrySdk:: getCurrentHub ()-> setSpan ( $ this -> transaction );
75+ if ( $ this -> appSpan !== null ) {
76+ $ this -> appSpan -> finish ();
77+ }
7878
79- if ( $ response instanceof SymfonyResponse) {
80- $ this -> hydrateResponseData ( $ response );
81- }
79+ // Make sure we set the transaction and not have a child span in the Sentry SDK
80+ // If the transaction is not on the scope during finish, the trace.context is wrong
81+ SentrySdk:: getCurrentHub ()-> setSpan ( $ this -> transaction );
8282
83- $ this ->transaction ->finish ();
83+ if ($ response instanceof SymfonyResponse) {
84+ $ this ->hydrateResponseData ($ response );
8485 }
86+
87+ $ this ->transaction ->finish ();
8588 }
8689
8790 /**
@@ -119,7 +122,14 @@ private function startTransaction(Request $request, HubInterface $sentry): void
119122 'method ' => strtoupper ($ request ->method ()),
120123 ]);
121124
122- $ this ->transaction = $ sentry ->startTransaction ($ context );
125+ $ transaction = $ sentry ->startTransaction ($ context );
126+
127+ // If this transaction is not sampled, don't set it either and stop doing work from this point on
128+ if (!$ transaction ->getSampled ()) {
129+ return ;
130+ }
131+
132+ $ this ->transaction = $ transaction ;
123133
124134 // Setting the Transaction on the Hub
125135 SentrySdk::getCurrentHub ()->setSpan ($ this ->transaction );
0 commit comments