77use Sentry \Options ;
88
99/**
10+ * Applies tracing and profiling sampling decisions to transactions.
11+ *
1012 * @internal
1113 */
1214final class TransactionSampler
1315{
14- private function __construct ()
16+ /**
17+ * @var Options
18+ */
19+ private $ options ;
20+
21+ public function __construct (Options $ options )
1522 {
23+ $ this ->options = $ options ;
1624 }
1725
1826 /**
1927 * @param array<string, mixed> $customSamplingContext Additional context that will be passed to the {@see SamplingContext}
2028 */
21- public static function startTransaction (Options $ options , TransactionContext $ context , array $ customSamplingContext = []): Transaction
29+ public function startTransaction (Transaction $ transaction , TransactionContext $ context , array $ customSamplingContext = []): Transaction
2230 {
23- $ transaction = new Transaction ($ context );
24- $ logger = $ options ->getLoggerOrNullLogger ();
31+ $ logger = $ this ->options ->getLoggerOrNullLogger ();
2532
26- if (!$ options ->isTracingEnabled ()) {
33+ if (!$ this -> options ->isTracingEnabled ()) {
2734 $ transaction ->setSampled (false );
2835
2936 $ logger ->warning (\sprintf ('Transaction [%s] was started but tracing is not enabled. ' , (string ) $ transaction ->getTraceId ()), ['context ' => $ context ]);
@@ -38,7 +45,7 @@ public static function startTransaction(Options $options, TransactionContext $co
3845 $ sampleRand = $ context ->getMetadata ()->getSampleRand () ?? 0.0 ;
3946
4047 if ($ transaction ->getSampled () === null ) {
41- $ tracesSampler = $ options ->getTracesSampler ();
48+ $ tracesSampler = $ this -> options ->getTracesSampler ();
4249
4350 if ($ tracesSampler !== null ) {
4451 $ sampleRate = $ tracesSampler ($ samplingContext );
@@ -49,15 +56,15 @@ public static function startTransaction(Options $options, TransactionContext $co
4956 $ sampleRate = $ parentSampleRate ;
5057 $ sampleSource = 'parent:sample_rate ' ;
5158 } else {
52- $ sampleRate = self :: getSampleRate (
59+ $ sampleRate = $ this -> getSampleRate (
5360 $ samplingContext ->getParentSampled (),
54- $ options ->getTracesSampleRate () ?? 0
61+ $ this -> options ->getTracesSampleRate () ?? 0
5562 );
5663 $ sampleSource = $ samplingContext ->getParentSampled () !== null ? 'parent:sampling_decision ' : 'config:traces_sample_rate ' ;
5764 }
5865 }
5966
60- if (!self :: isValidSampleRate ($ sampleRate )) {
67+ if (!$ this -> isValidSampleRate ($ sampleRate )) {
6168 $ transaction ->setSampled (false );
6269
6370 $ logger ->warning (\sprintf ('Transaction [%s] was started but not sampled because sample rate (decided by %s) is invalid. ' , (string ) $ transaction ->getTraceId (), $ sampleSource ), ['context ' => $ context ]);
@@ -95,20 +102,20 @@ public static function startTransaction(Options $options, TransactionContext $co
95102 $ transaction ->initSpanRecorder ();
96103
97104 $ profilesSampleSource = 'config:profiles_sample_rate ' ;
98- $ profilesSampler = $ options ->getProfilesSampler ();
105+ $ profilesSampler = $ this -> options ->getProfilesSampler ();
99106
100107 if ($ profilesSampler !== null ) {
101108 $ profilesSampleRate = $ profilesSampler ($ samplingContext );
102109 $ profilesSampleSource = 'config:profiles_sampler ' ;
103110 } else {
104- $ profilesSampleRate = $ options ->getProfilesSampleRate ();
111+ $ profilesSampleRate = $ this -> options ->getProfilesSampleRate ();
105112 }
106113
107114 if ($ profilesSampleRate === null ) {
108115 $ logger ->info (\sprintf ('Transaction [%s] is not profiling because neither `profiles_sample_rate` nor `profiles_sampler` option is set. ' , (string ) $ transaction ->getTraceId ()));
109- } elseif (!self :: isValidSampleRate ($ profilesSampleRate )) {
116+ } elseif (!$ this -> isValidSampleRate ($ profilesSampleRate )) {
110117 $ logger ->warning (\sprintf ('Transaction [%s] is not profiling because profile sample rate (decided by %s) is invalid. ' , (string ) $ transaction ->getTraceId (), $ profilesSampleSource ));
111- } elseif (self :: sampleRate ($ profilesSampleRate )) {
118+ } elseif ($ this -> sampleRate ($ profilesSampleRate )) {
112119 $ logger ->info (\sprintf ('Transaction [%s] started profiling because it was sampled. ' , (string ) $ transaction ->getTraceId ()));
113120
114121 $ transaction ->initProfiler ()->start ();
@@ -119,7 +126,7 @@ public static function startTransaction(Options $options, TransactionContext $co
119126 return $ transaction ;
120127 }
121128
122- private static function getSampleRate (?bool $ hasParentBeenSampled , float $ fallbackSampleRate ): float
129+ private function getSampleRate (?bool $ hasParentBeenSampled , float $ fallbackSampleRate ): float
123130 {
124131 if ($ hasParentBeenSampled === true ) {
125132 return 1.0 ;
@@ -135,7 +142,7 @@ private static function getSampleRate(?bool $hasParentBeenSampled, float $fallba
135142 /**
136143 * @param mixed $sampleRate
137144 */
138- private static function sampleRate ($ sampleRate ): bool
145+ private function sampleRate ($ sampleRate ): bool
139146 {
140147 if (!\is_float ($ sampleRate ) && !\is_int ($ sampleRate )) {
141148 return false ;
@@ -155,7 +162,7 @@ private static function sampleRate($sampleRate): bool
155162 /**
156163 * @param mixed $sampleRate
157164 */
158- private static function isValidSampleRate ($ sampleRate ): bool
165+ private function isValidSampleRate ($ sampleRate ): bool
159166 {
160167 if (!\is_float ($ sampleRate ) && !\is_int ($ sampleRate )) {
161168 return false ;
0 commit comments