@@ -834,6 +834,127 @@ public function testStartTransactionWithCustomSamplingContext(): void
834834 $ hub ->startTransaction (new TransactionContext (), $ customSamplingContext );
835835 }
836836
837+ public function testStartTransactionStartsProfilerWithProfilesSampler (): void
838+ {
839+ $ client = $ this ->createMock (ClientInterface::class);
840+ $ client ->expects ($ this ->exactly (2 ))
841+ ->method ('getOptions ' )
842+ ->willReturn (new Options ([
843+ 'traces_sample_rate ' => 1.0 ,
844+ 'profiles_sampler ' => static function (): float {
845+ return 1.0 ;
846+ },
847+ ]));
848+
849+ $ hub = new Hub ($ client );
850+ $ transaction = $ hub ->startTransaction (new TransactionContext ());
851+
852+ $ this ->assertTrue ($ transaction ->getSampled ());
853+ $ this ->assertNotNull ($ transaction ->getProfiler ());
854+ }
855+
856+ public function testStartTransactionDoesNotStartProfilerWhenProfilesSamplerReturnsZero (): void
857+ {
858+ $ client = $ this ->createMock (ClientInterface::class);
859+ $ client ->expects ($ this ->once ())
860+ ->method ('getOptions ' )
861+ ->willReturn (new Options ([
862+ 'traces_sample_rate ' => 1.0 ,
863+ 'profiles_sampler ' => static function (): float {
864+ return 0.0 ;
865+ },
866+ ]));
867+
868+ $ hub = new Hub ($ client );
869+ $ transaction = $ hub ->startTransaction (new TransactionContext ());
870+
871+ $ this ->assertTrue ($ transaction ->getSampled ());
872+ $ this ->assertNull ($ transaction ->getProfiler ());
873+ }
874+
875+ public function testStartTransactionPrefersProfilesSamplerOverProfilesSampleRate (): void
876+ {
877+ $ client = $ this ->createMock (ClientInterface::class);
878+ $ client ->expects ($ this ->once ())
879+ ->method ('getOptions ' )
880+ ->willReturn (new Options ([
881+ 'traces_sample_rate ' => 1.0 ,
882+ 'profiles_sample_rate ' => 1.0 ,
883+ 'profiles_sampler ' => static function (): float {
884+ return 0.0 ;
885+ },
886+ ]));
887+
888+ $ hub = new Hub ($ client );
889+ $ transaction = $ hub ->startTransaction (new TransactionContext ());
890+
891+ $ this ->assertTrue ($ transaction ->getSampled ());
892+ $ this ->assertNull ($ transaction ->getProfiler ());
893+ }
894+
895+ public function testStartTransactionWithProfilesSamplerReceivesCustomSamplingContext (): void
896+ {
897+ $ customSamplingContext = ['a ' => 'b ' ];
898+
899+ $ client = $ this ->createMock (ClientInterface::class);
900+ $ client ->expects ($ this ->once ())
901+ ->method ('getOptions ' )
902+ ->willReturn (new Options ([
903+ 'traces_sample_rate ' => 1.0 ,
904+ 'profiles_sampler ' => function (SamplingContext $ samplingContext ) use ($ customSamplingContext ): float {
905+ $ this ->assertSame ($ samplingContext ->getAdditionalContext (), $ customSamplingContext );
906+
907+ return 0.0 ;
908+ },
909+ ]));
910+
911+ $ hub = new Hub ($ client );
912+ $ hub ->startTransaction (new TransactionContext (), $ customSamplingContext );
913+ }
914+
915+ public function testStartTransactionDoesNotStartProfilerWhenProfilesSamplerReturnsInvalidValue (): void
916+ {
917+ $ client = $ this ->createMock (ClientInterface::class);
918+ $ client ->expects ($ this ->once ())
919+ ->method ('getOptions ' )
920+ ->willReturn (new Options ([
921+ 'traces_sample_rate ' => 1.0 ,
922+ 'profiles_sampler ' => static function (): string {
923+ return 'foo ' ;
924+ },
925+ ]));
926+
927+ $ hub = new Hub ($ client );
928+ $ transaction = $ hub ->startTransaction (new TransactionContext ());
929+
930+ $ this ->assertTrue ($ transaction ->getSampled ());
931+ $ this ->assertNull ($ transaction ->getProfiler ());
932+ }
933+
934+ public function testStartTransactionDoesNotCallProfilesSamplerWhenTransactionIsNotSampled (): void
935+ {
936+ $ profilesSamplerInvoked = false ;
937+
938+ $ client = $ this ->createMock (ClientInterface::class);
939+ $ client ->expects ($ this ->once ())
940+ ->method ('getOptions ' )
941+ ->willReturn (new Options ([
942+ 'traces_sample_rate ' => 0.0 ,
943+ 'profiles_sampler ' => static function () use (&$ profilesSamplerInvoked ): float {
944+ $ profilesSamplerInvoked = true ;
945+
946+ return 1.0 ;
947+ },
948+ ]));
949+
950+ $ hub = new Hub ($ client );
951+ $ transaction = $ hub ->startTransaction (new TransactionContext ());
952+
953+ $ this ->assertFalse ($ transaction ->getSampled ());
954+ $ this ->assertFalse ($ profilesSamplerInvoked );
955+ $ this ->assertNull ($ transaction ->getProfiler ());
956+ }
957+
837958 public function testStartTransactionUpdatesTheDscSampleRate (): void
838959 {
839960 $ client = $ this ->createMock (ClientInterface::class);
0 commit comments