1313 */
1414final class TransactionSampler
1515{
16- /**
17- * @var Options
18- */
19- private $ options ;
20-
21- public function __construct (Options $ options )
16+ private function __construct ()
2217 {
23- $ this ->options = $ options ;
2418 }
2519
2620 /**
2721 * @param array<string, mixed> $customSamplingContext Additional context that will be passed to the {@see SamplingContext}
2822 */
29- public function startTransaction (Transaction $ transaction , TransactionContext $ context , array $ customSamplingContext = []): Transaction
23+ public static function startTransaction (Options $ options , TransactionContext $ context , array $ customSamplingContext = []): Transaction
3024 {
31- $ logger = $ this ->options ->getLoggerOrNullLogger ();
25+ $ transaction = new Transaction ($ context );
26+ $ logger = $ options ->getLoggerOrNullLogger ();
3227
33- if (!$ this -> options ->isTracingEnabled ()) {
28+ if (!$ options ->isTracingEnabled ()) {
3429 $ transaction ->setSampled (false );
3530
3631 $ logger ->warning (\sprintf ('Transaction [%s] was started but tracing is not enabled. ' , (string ) $ transaction ->getTraceId ()), ['context ' => $ context ]);
@@ -45,7 +40,7 @@ public function startTransaction(Transaction $transaction, TransactionContext $c
4540 $ sampleRand = $ context ->getMetadata ()->getSampleRand () ?? 0.0 ;
4641
4742 if ($ transaction ->getSampled () === null ) {
48- $ tracesSampler = $ this -> options ->getTracesSampler ();
43+ $ tracesSampler = $ options ->getTracesSampler ();
4944
5045 if ($ tracesSampler !== null ) {
5146 $ sampleRate = $ tracesSampler ($ samplingContext );
@@ -56,15 +51,15 @@ public function startTransaction(Transaction $transaction, TransactionContext $c
5651 $ sampleRate = $ parentSampleRate ;
5752 $ sampleSource = 'parent:sample_rate ' ;
5853 } else {
59- $ sampleRate = $ this -> getSampleRate (
54+ $ sampleRate = self :: getSampleRate (
6055 $ samplingContext ->getParentSampled (),
61- $ this -> options ->getTracesSampleRate () ?? 0
56+ $ options ->getTracesSampleRate () ?? 0
6257 );
6358 $ sampleSource = $ samplingContext ->getParentSampled () !== null ? 'parent:sampling_decision ' : 'config:traces_sample_rate ' ;
6459 }
6560 }
6661
67- if (!$ this -> isValidSampleRate ($ sampleRate )) {
62+ if (!self :: isValidSampleRate ($ sampleRate )) {
6863 $ transaction ->setSampled (false );
6964
7065 $ logger ->warning (\sprintf ('Transaction [%s] was started but not sampled because sample rate (decided by %s) is invalid. ' , (string ) $ transaction ->getTraceId (), $ sampleSource ), ['context ' => $ context ]);
@@ -102,31 +97,31 @@ public function startTransaction(Transaction $transaction, TransactionContext $c
10297 $ transaction ->initSpanRecorder ();
10398
10499 $ profilesSampleSource = 'config:profiles_sample_rate ' ;
105- $ profilesSampler = $ this -> options ->getProfilesSampler ();
100+ $ profilesSampler = $ options ->getProfilesSampler ();
106101
107102 if ($ profilesSampler !== null ) {
108103 $ profilesSampleRate = $ profilesSampler ($ samplingContext );
109104 $ profilesSampleSource = 'config:profiles_sampler ' ;
110105 } else {
111- $ profilesSampleRate = $ this -> options ->getProfilesSampleRate ();
106+ $ profilesSampleRate = $ options ->getProfilesSampleRate ();
112107 }
113108
114109 if ($ profilesSampleRate === null ) {
115110 $ logger ->info (\sprintf ('Transaction [%s] is not profiling because neither `profiles_sample_rate` nor `profiles_sampler` option is set. ' , (string ) $ transaction ->getTraceId ()));
116- } elseif (!$ this -> isValidSampleRate ($ profilesSampleRate )) {
111+ } elseif (!self :: isValidSampleRate ($ profilesSampleRate )) {
117112 $ logger ->warning (\sprintf ('Transaction [%s] is not profiling because profile sample rate (decided by %s) is invalid. ' , (string ) $ transaction ->getTraceId (), $ profilesSampleSource ));
118- } elseif ($ this -> sampleRate ($ profilesSampleRate )) {
113+ } elseif (self :: sampleRate ($ profilesSampleRate )) {
119114 $ logger ->info (\sprintf ('Transaction [%s] started profiling because it was sampled. ' , (string ) $ transaction ->getTraceId ()));
120115
121- $ transaction ->initProfiler ()->start ();
116+ $ transaction ->initProfiler ($ options )->start ();
122117 } else {
123118 $ logger ->info (\sprintf ('Transaction [%s] is not profiling because it was not sampled. ' , (string ) $ transaction ->getTraceId ()));
124119 }
125120
126121 return $ transaction ;
127122 }
128123
129- private function getSampleRate (?bool $ hasParentBeenSampled , float $ fallbackSampleRate ): float
124+ private static function getSampleRate (?bool $ hasParentBeenSampled , float $ fallbackSampleRate ): float
130125 {
131126 if ($ hasParentBeenSampled === true ) {
132127 return 1.0 ;
@@ -142,7 +137,7 @@ private function getSampleRate(?bool $hasParentBeenSampled, float $fallbackSampl
142137 /**
143138 * @param mixed $sampleRate
144139 */
145- private function sampleRate ($ sampleRate ): bool
140+ private static function sampleRate ($ sampleRate ): bool
146141 {
147142 if (!\is_float ($ sampleRate ) && !\is_int ($ sampleRate )) {
148143 return false ;
@@ -162,7 +157,7 @@ private function sampleRate($sampleRate): bool
162157 /**
163158 * @param mixed $sampleRate
164159 */
165- private function isValidSampleRate ($ sampleRate ): bool
160+ private static function isValidSampleRate ($ sampleRate ): bool
166161 {
167162 if (!\is_float ($ sampleRate ) && !\is_int ($ sampleRate )) {
168163 return false ;
0 commit comments