8
8
use PHPUnit \Framework \TestCase ;
9
9
use Psr \Log \LoggerAwareInterface ;
10
10
use Psr \Log \NullLogger ;
11
+ use Sentry \Client ;
11
12
use Sentry \ClientInterface ;
12
13
use Sentry \Options ;
13
14
use Sentry \SentryBundle \Tracing \HttpClient \AbstractTraceableResponse ;
14
15
use Sentry \SentryBundle \Tracing \HttpClient \TraceableHttpClient ;
16
+ use Sentry \SentrySdk ;
17
+ use Sentry \State \Hub ;
15
18
use Sentry \State \HubInterface ;
19
+ use Sentry \State \Scope ;
20
+ use Sentry \Tracing \PropagationContext ;
21
+ use Sentry \Tracing \SpanId ;
22
+ use Sentry \Tracing \TraceId ;
16
23
use Sentry \Tracing \Transaction ;
17
24
use Sentry \Tracing \TransactionContext ;
25
+ use Sentry \Transport \NullTransport ;
18
26
use Symfony \Component \HttpClient \MockHttpClient ;
19
27
use Symfony \Component \HttpClient \Response \MockResponse ;
20
28
use Symfony \Contracts \HttpClient \HttpClientInterface ;
21
29
use Symfony \Contracts \Service \ResetInterface ;
22
30
31
+ use function Sentry \configureScope ;
32
+
23
33
final class TraceableHttpClientTest extends TestCase
24
34
{
25
35
/**
@@ -53,12 +63,24 @@ protected function setUp(): void
53
63
54
64
public function testRequest (): void
55
65
{
66
+ $ options = new Options ([
67
+ 'dsn ' =>
'http://public:[email protected] /sentry/1 ' ,
68
+ ]);
69
+ $ client = $ this ->createMock (ClientInterface::class);
70
+ $ client
71
+ ->expects ($ this ->once ())
72
+ ->method ('getOptions ' )
73
+ ->willReturn ($ options );
74
+
56
75
$ transaction = new Transaction (new TransactionContext ());
57
76
$ transaction ->initSpanRecorder ();
58
77
59
78
$ this ->hub ->expects ($ this ->once ())
60
79
->method ('getSpan ' )
61
80
->willReturn ($ transaction );
81
+ $ this ->hub ->expects ($ this ->once ())
82
+ ->method ('getClient ' )
83
+ ->willReturn ($ client );
62
84
63
85
$ mockResponse = new MockResponse ();
64
86
$ decoratedHttpClient = new MockHttpClient ($ mockResponse );
@@ -69,8 +91,8 @@ public function testRequest(): void
69
91
$ this ->assertSame (200 , $ response ->getStatusCode ());
70
92
$ this ->assertSame ('GET ' , $ response ->getInfo ('http_method ' ));
71
93
$ this ->
assertSame (
'https://username:[email protected] /test-page?foo=bar#baz ' ,
$ response->
getInfo (
'url ' ));
72
- $ this ->assertSame (['sentry-trace: ' . $ transaction ->toTraceparent ()], $ mockResponse ->getRequestOptions ()['normalized_headers ' ]['sentry-trace ' ]);
73
- $ this ->assertArrayNotHasKey ( 'baggage ' , $ mockResponse ->getRequestOptions ()['normalized_headers ' ]);
94
+ $ this ->assertSame (['sentry-trace: ' . $ transaction ->getSpanRecorder ()-> getSpans ()[ 1 ]-> toTraceparent ()], $ mockResponse ->getRequestOptions ()['normalized_headers ' ]['sentry-trace ' ]);
95
+ $ this ->assertSame ([ 'baggage: ' . $ transaction -> toBaggage ()] , $ mockResponse ->getRequestOptions ()['normalized_headers ' ][ ' baggage ' ]);
74
96
$ this ->assertNotNull ($ transaction ->getSpanRecorder ());
75
97
76
98
$ spans = $ transaction ->getSpanRecorder ()->getSpans ();
@@ -91,15 +113,14 @@ public function testRequest(): void
91
113
$ this ->assertSame ($ expectedData , $ spans [1 ]->getData ());
92
114
}
93
115
94
- public function testRequestDoesNotContainBaggageHeader (): void
116
+ public function testRequestDoesNotContainTracingHeaders (): void
95
117
{
96
118
$ options = new Options ([
97
119
'dsn ' =>
'http://public:[email protected] /sentry/1 ' ,
98
- 'trace_propagation_targets ' => [ ' non-matching-host.invalid ' ] ,
120
+ 'trace_propagation_targets ' => null ,
99
121
]);
100
122
$ client = $ this ->createMock (ClientInterface::class);
101
- $ client
102
- ->expects ($ this ->once ())
123
+ $ client ->expects ($ this ->once ())
103
124
->method ('getOptions ' )
104
125
->willReturn ($ options );
105
126
@@ -122,7 +143,7 @@ public function testRequestDoesNotContainBaggageHeader(): void
122
143
$ this ->assertSame (200 , $ response ->getStatusCode ());
123
144
$ this ->assertSame ('PUT ' , $ response ->getInfo ('http_method ' ));
124
145
$ this ->assertSame ('https://www.example.com/test-page ' , $ response ->getInfo ('url ' ));
125
- $ this ->assertSame ([ 'sentry-trace: ' . $ transaction -> toTraceparent ()] , $ mockResponse ->getRequestOptions ()['normalized_headers ' ][ ' sentry-trace ' ]);
146
+ $ this ->assertArrayNotHasKey ( 'sentry-trace ' , $ mockResponse ->getRequestOptions ()['normalized_headers ' ]);
126
147
$ this ->assertArrayNotHasKey ('baggage ' , $ mockResponse ->getRequestOptions ()['normalized_headers ' ]);
127
148
$ this ->assertNotNull ($ transaction ->getSpanRecorder ());
128
149
@@ -139,52 +160,36 @@ public function testRequestDoesNotContainBaggageHeader(): void
139
160
$ this ->assertSame ($ expectedTags , $ spans [1 ]->getTags ());
140
161
}
141
162
142
- public function testRequestDoesContainBaggageHeader (): void
163
+ public function testRequestDoesContainsTracingHeadersWithoutTransaction (): void
143
164
{
144
- $ options = new Options ([
165
+ $ client = new Client ( new Options ([
145
166
'dsn ' =>
'http://public:[email protected] /sentry/1 ' ,
167
+ 'release ' => '1.0.0 ' ,
168
+ 'environment ' => 'test ' ,
146
169
'trace_propagation_targets ' => ['www.example.com ' ],
147
- ]);
148
- $ client = $ this ->createMock (ClientInterface::class);
149
- $ client
150
- ->expects ($ this ->once ())
151
- ->method ('getOptions ' )
152
- ->willReturn ($ options );
170
+ ]), new NullTransport ());
153
171
154
- $ transaction = new Transaction (new TransactionContext ());
155
- $ transaction ->initSpanRecorder ();
172
+ $ propagationContext = PropagationContext::fromDefaults ();
173
+ $ propagationContext ->setTraceId (new TraceId ('566e3688a61d4bc888951642d6f14a19 ' ));
174
+ $ propagationContext ->setSpanId (new SpanId ('566e3688a61d4bc8 ' ));
156
175
157
- $ this ->hub ->expects ($ this ->once ())
158
- ->method ('getSpan ' )
159
- ->willReturn ($ transaction );
160
- $ this ->hub ->expects ($ this ->once ())
161
- ->method ('getClient ' )
162
- ->willReturn ($ client );
176
+ $ scope = new Scope ($ propagationContext );
177
+
178
+ $ hub = new Hub ($ client , $ scope );
179
+
180
+ SentrySdk::setCurrentHub ($ hub );
163
181
164
182
$ mockResponse = new MockResponse ();
165
183
$ decoratedHttpClient = new MockHttpClient ($ mockResponse );
166
- $ httpClient = new TraceableHttpClient ($ decoratedHttpClient , $ this -> hub );
184
+ $ httpClient = new TraceableHttpClient ($ decoratedHttpClient , $ hub );
167
185
$ response = $ httpClient ->request ('POST ' , 'https://www.example.com/test-page ' );
168
186
169
187
$ this ->assertInstanceOf (AbstractTraceableResponse::class, $ response );
170
188
$ this ->assertSame (200 , $ response ->getStatusCode ());
171
189
$ this ->assertSame ('POST ' , $ response ->getInfo ('http_method ' ));
172
190
$ this ->assertSame ('https://www.example.com/test-page ' , $ response ->getInfo ('url ' ));
173
- $ this ->assertSame (['sentry-trace: ' . $ transaction ->toTraceparent ()], $ mockResponse ->getRequestOptions ()['normalized_headers ' ]['sentry-trace ' ]);
174
- $ this ->assertSame (['baggage: ' . $ transaction ->toBaggage ()], $ mockResponse ->getRequestOptions ()['normalized_headers ' ]['baggage ' ]);
175
- $ this ->assertNotNull ($ transaction ->getSpanRecorder ());
176
-
177
- $ spans = $ transaction ->getSpanRecorder ()->getSpans ();
178
- $ expectedTags = [
179
- 'http.method ' => 'POST ' ,
180
- 'http.url ' => 'https://www.example.com/test-page ' ,
181
- ];
182
-
183
- $ this ->assertCount (2 , $ spans );
184
- $ this ->assertNull ($ spans [1 ]->getEndTimestamp ());
185
- $ this ->assertSame ('http.client ' , $ spans [1 ]->getOp ());
186
- $ this ->assertSame ('POST https://www.example.com/test-page ' , $ spans [1 ]->getDescription ());
187
- $ this ->assertSame ($ expectedTags , $ spans [1 ]->getTags ());
191
+ $ this ->assertSame (['sentry-trace: 566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8 ' ], $ mockResponse ->getRequestOptions ()['normalized_headers ' ]['sentry-trace ' ]);
192
+ $ this ->assertSame (['baggage: sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-public_key=public,sentry-release=1.0.0,sentry-environment=test ' ], $ mockResponse ->getRequestOptions ()['normalized_headers ' ]['baggage ' ]);
188
193
}
189
194
190
195
public function testStream (): void
@@ -217,6 +222,7 @@ public function testStream(): void
217
222
218
223
$ this ->assertSame ('foobar ' , implode ('' , $ chunks ));
219
224
$ this ->assertCount (2 , $ spans );
225
+
220
226
$ this ->assertNotNull ($ spans [1 ]->getEndTimestamp ());
221
227
$ this ->assertSame ('http.client ' , $ spans [1 ]->getOp ());
222
228
$ this ->assertSame ('GET https://www.example.com/test-page ' , $ spans [1 ]->getDescription ());
0 commit comments