|
19 | 19 | use Sentry\State\Scope;
|
20 | 20 | use Sentry\Tracing\PropagationContext;
|
21 | 21 | use Sentry\Tracing\SpanId;
|
| 22 | +use Sentry\Tracing\SpanStatus; |
22 | 23 | use Sentry\Tracing\TraceId;
|
23 | 24 | use Sentry\Tracing\Transaction;
|
24 | 25 | use Sentry\Tracing\TransactionContext;
|
@@ -107,10 +108,17 @@ public function testRequest(): void
|
107 | 108 | 'http.fragment' => 'baz',
|
108 | 109 | ];
|
109 | 110 |
|
| 111 | + // Call gc to invoke destructors at the right time. |
| 112 | + unset($response); |
| 113 | + |
| 114 | + gc_mem_caches(); |
| 115 | + gc_collect_cycles(); |
| 116 | + |
110 | 117 | $this->assertCount(2, $spans);
|
111 |
| - $this->assertNull($spans[1]->getEndTimestamp()); |
| 118 | + $this->assertNotNull($spans[1]->getEndTimestamp()); |
112 | 119 | $this->assertSame('http.client', $spans[1]->getOp());
|
113 | 120 | $this->assertSame('GET https://www.example.com/test-page', $spans[1]->getDescription());
|
| 121 | + $this->assertSame(SpanStatus::ok(), $spans[1]->getStatus()); |
114 | 122 | $this->assertSame($expectedTags, $spans[1]->getTags());
|
115 | 123 | $this->assertSame($expectedData, $spans[1]->getData());
|
116 | 124 | }
|
@@ -194,6 +202,50 @@ public function testRequestDoesContainsTracingHeadersWithoutTransaction(): void
|
194 | 202 | $this->assertSame(['baggage: sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-public_key=public,sentry-release=1.0.0,sentry-environment=test'], $mockResponse->getRequestOptions()['normalized_headers']['baggage']);
|
195 | 203 | }
|
196 | 204 |
|
| 205 | + public function testRequestSetsUnknownErrorAsSpanStatusIfResponseStatusCodeIsUnavailable(): void |
| 206 | + { |
| 207 | + $client = $this->createMock(ClientInterface::class); |
| 208 | + $client->expects($this->once()) |
| 209 | + ->method('getOptions') |
| 210 | + -> willReturn( new Options([ 'dsn' => 'http://public:[email protected]/sentry/1'])); |
| 211 | + |
| 212 | + $transaction = new Transaction(new TransactionContext()); |
| 213 | + $transaction->initSpanRecorder(); |
| 214 | + |
| 215 | + $this->hub->expects($this->once()) |
| 216 | + ->method('getSpan') |
| 217 | + ->willReturn($transaction); |
| 218 | + |
| 219 | + $this->hub->expects($this->once()) |
| 220 | + ->method('getClient') |
| 221 | + ->willReturn($client); |
| 222 | + |
| 223 | + $decoratedHttpClient = new MockHttpClient(new MockResponse()); |
| 224 | + $httpClient = new TraceableHttpClient($decoratedHttpClient, $this->hub); |
| 225 | + |
| 226 | + // Cancelling the response is the only way that does not override in any |
| 227 | + // way the status code and leave it set to 0. This is a required precondition |
| 228 | + // for the span status to be set to the expected value. |
| 229 | + $response = $httpClient->request('GET', 'https://www.example.com/test-page'); |
| 230 | + $response->cancel(); |
| 231 | + |
| 232 | + $this->assertNotNull($transaction->getSpanRecorder()); |
| 233 | + $this->assertInstanceOf(AbstractTraceableResponse::class, $response); |
| 234 | + |
| 235 | + // Call gc to invoke destructors at the right time. |
| 236 | + unset($response); |
| 237 | + |
| 238 | + gc_mem_caches(); |
| 239 | + gc_collect_cycles(); |
| 240 | + |
| 241 | + $spans = $transaction->getSpanRecorder()->getSpans(); |
| 242 | + |
| 243 | + $this->assertCount(2, $spans); |
| 244 | + $this->assertNotNull($spans[1]->getEndTimestamp()); |
| 245 | + $this->assertSame('GET https://www.example.com/test-page', $spans[1]->getDescription()); |
| 246 | + $this->assertSame(SpanStatus::unknownError(), $spans[1]->getStatus()); |
| 247 | + } |
| 248 | + |
197 | 249 | public function testStream(): void
|
198 | 250 | {
|
199 | 251 | $transaction = new Transaction(new TransactionContext());
|
|
0 commit comments