2323
2424final class GuzzleTracingMiddlewareTest extends TestCase
2525{
26- public function testTraceDoesNothingIfSpanIsNotSet (): void
26+ public function testTraceCreatesBreadcrumbIfSpanIsNotSet (): void
2727 {
2828 $ client = $ this ->createMock (ClientInterface::class);
29- $ client ->expects ($ this ->once ( ))
29+ $ client ->expects ($ this ->atLeast ( 2 ))
3030 ->method ('getOptions ' )
31- ->willReturn (new Options ());
31+ ->willReturn (new Options ([
32+ 'traces_sample_rate ' => 0 ,
33+ ]));
3234
3335 $ hub = new Hub ($ client );
3436
37+ $ transaction = $ hub ->startTransaction (TransactionContext::make ());
38+
39+ $ this ->assertFalse ($ transaction ->getSampled ());
40+
3541 $ expectedPromiseResult = new Response ();
3642
3743 $ middleware = GuzzleTracingMiddleware::trace ($ hub );
@@ -50,12 +56,59 @@ public function testTraceDoesNothingIfSpanIsNotSet(): void
5056
5157 $ this ->assertSame ($ expectedPromiseResult , $ promiseResult );
5258
59+ $ this ->assertNull ($ transaction ->getSpanRecorder ());
60+
5361 $ hub ->configureScope (function (Scope $ scope ): void {
5462 $ event = Event::createEvent ();
5563
5664 $ scope ->applyToEvent ($ event );
5765
58- $ this ->assertCount (0 , $ event ->getBreadcrumbs ());
66+ $ this ->assertCount (1 , $ event ->getBreadcrumbs ());
67+ });
68+ }
69+
70+ public function testTraceCreatesBreadcrumbIfSpanIsRecorded (): void
71+ {
72+ $ client = $ this ->createMock (ClientInterface::class);
73+ $ client ->expects ($ this ->atLeast (2 ))
74+ ->method ('getOptions ' )
75+ ->willReturn (new Options ([
76+ 'traces_sample_rate ' => 1 ,
77+ ]));
78+
79+ $ hub = new Hub ($ client );
80+
81+ $ transaction = $ hub ->startTransaction (TransactionContext::make ());
82+
83+ $ this ->assertTrue ($ transaction ->getSampled ());
84+
85+ $ expectedPromiseResult = new Response ();
86+
87+ $ middleware = GuzzleTracingMiddleware::trace ($ hub );
88+ $ function = $ middleware (static function () use ($ expectedPromiseResult ): PromiseInterface {
89+ return new FulfilledPromise ($ expectedPromiseResult );
90+ });
91+
92+ /** @var PromiseInterface $promise */
93+ $ promise = $ function (new Request ('GET ' , 'https://www.example.com ' ), []);
94+
95+ try {
96+ $ promiseResult = $ promise ->wait ();
97+ } catch (\Throwable $ exception ) {
98+ $ promiseResult = $ exception ;
99+ }
100+
101+ $ this ->assertSame ($ expectedPromiseResult , $ promiseResult );
102+
103+ $ this ->assertNotNull ($ transaction ->getSpanRecorder ());
104+ $ this ->assertCount (1 , $ transaction ->getSpanRecorder ()->getSpans ());
105+
106+ $ hub ->configureScope (function (Scope $ scope ): void {
107+ $ event = Event::createEvent ();
108+
109+ $ scope ->applyToEvent ($ event );
110+
111+ $ this ->assertCount (1 , $ event ->getBreadcrumbs ());
59112 });
60113 }
61114
@@ -95,7 +148,7 @@ public function testTraceHeaders(Request $request, Options $options, bool $heade
95148 /**
96149 * @dataProvider traceHeadersDataProvider
97150 */
98- public function testTraceHeadersWithTransacttion (Request $ request , Options $ options , bool $ headersShouldBePresent ): void
151+ public function testTraceHeadersWithTransaction (Request $ request , Options $ options , bool $ headersShouldBePresent ): void
99152 {
100153 $ client = $ this ->createMock (ClientInterface::class);
101154 $ client ->expects ($ this ->atLeast (2 ))
@@ -133,6 +186,15 @@ public function testTraceHeadersWithTransacttion(Request $request, Options $opti
133186
134187 public static function traceHeadersDataProvider (): iterable
135188 {
189+ // Test cases here are duplicated with sampling enabled and disabled because trace headers hould be added regardless of the sample decision
190+
191+ yield [
192+ new Request ('GET ' , 'https://www.example.com ' ),
193+ new Options ([
194+ 'traces_sample_rate ' => 0 ,
195+ ]),
196+ true ,
197+ ];
136198 yield [
137199 new Request ('GET ' , 'https://www.example.com ' ),
138200 new Options ([
@@ -141,6 +203,14 @@ public static function traceHeadersDataProvider(): iterable
141203 true ,
142204 ];
143205
206+ yield [
207+ new Request ('GET ' , 'https://www.example.com ' ),
208+ new Options ([
209+ 'traces_sample_rate ' => 0 ,
210+ 'trace_propagation_targets ' => null ,
211+ ]),
212+ true ,
213+ ];
144214 yield [
145215 new Request ('GET ' , 'https://www.example.com ' ),
146216 new Options ([
@@ -150,6 +220,16 @@ public static function traceHeadersDataProvider(): iterable
150220 true ,
151221 ];
152222
223+ yield [
224+ new Request ('GET ' , 'https://www.example.com ' ),
225+ new Options ([
226+ 'traces_sample_rate ' => 0 ,
227+ 'trace_propagation_targets ' => [
228+ 'www.example.com ' ,
229+ ],
230+ ]),
231+ true ,
232+ ];
153233 yield [
154234 new Request ('GET ' , 'https://www.example.com ' ),
155235 new Options ([
@@ -161,6 +241,14 @@ public static function traceHeadersDataProvider(): iterable
161241 true ,
162242 ];
163243
244+ yield [
245+ new Request ('GET ' , 'https://www.example.com ' ),
246+ new Options ([
247+ 'traces_sample_rate ' => 0 ,
248+ 'trace_propagation_targets ' => [],
249+ ]),
250+ false ,
251+ ];
164252 yield [
165253 new Request ('GET ' , 'https://www.example.com ' ),
166254 new Options ([
@@ -170,6 +258,16 @@ public static function traceHeadersDataProvider(): iterable
170258 false ,
171259 ];
172260
261+ yield [
262+ new Request ('GET ' , 'https://www.example.com ' ),
263+ new Options ([
264+ 'traces_sample_rate ' => 0 ,
265+ 'trace_propagation_targets ' => [
266+ 'example.com ' ,
267+ ],
268+ ]),
269+ false ,
270+ ];
173271 yield [
174272 new Request ('GET ' , 'https://www.example.com ' ),
175273 new Options ([
0 commit comments