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 ;
@@ -53,24 +61,40 @@ protected function setUp(): void
53
61
54
62
public function testRequest (): void
55
63
{
64
+ $ options = new Options ([
65
+ 'dsn ' =>
'http://public:[email protected] /sentry/1 ' ,
66
+ ]);
67
+ $ client = $ this ->createMock (ClientInterface::class);
68
+ $ client
69
+ ->expects ($ this ->once ())
70
+ ->method ('getOptions ' )
71
+ ->willReturn ($ options );
72
+
56
73
$ transaction = new Transaction (new TransactionContext ());
57
74
$ transaction ->initSpanRecorder ();
58
75
59
76
$ this ->hub ->expects ($ this ->once ())
60
77
->method ('getSpan ' )
61
78
->willReturn ($ transaction );
79
+ $ this ->hub ->expects ($ this ->once ())
80
+ ->method ('getClient ' )
81
+ ->willReturn ($ client );
62
82
63
83
$ mockResponse = new MockResponse ();
64
84
$ decoratedHttpClient = new MockHttpClient ($ mockResponse );
65
85
$ httpClient = new TraceableHttpClient ($ decoratedHttpClient , $ this ->hub );
66
86
$ response =
$ httpClient->
request (
'GET ' ,
'https://username:[email protected] /test-page?foo=bar#baz ' );
67
87
88
+ $ this ->assertNotNull ($ transaction ->getSpanRecorder ());
89
+
90
+ $ spans = $ transaction ->getSpanRecorder ()->getSpans ();
91
+
68
92
$ this ->assertInstanceOf (AbstractTraceableResponse::class, $ response );
69
93
$ this ->assertSame (200 , $ response ->getStatusCode ());
70
94
$ this ->assertSame ('GET ' , $ response ->getInfo ('http_method ' ));
71
95
$ 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 ' ]);
96
+ $ this ->assertSame (['sentry-trace: ' . $ spans [ 1 ] ->toTraceparent ()], $ mockResponse ->getRequestOptions ()['normalized_headers ' ]['sentry-trace ' ]);
97
+ $ this ->assertSame ([ 'baggage: ' . $ transaction -> toBaggage ()] , $ mockResponse ->getRequestOptions ()['normalized_headers ' ][ ' baggage ' ]);
74
98
$ this ->assertNotNull ($ transaction ->getSpanRecorder ());
75
99
76
100
$ spans = $ transaction ->getSpanRecorder ()->getSpans ();
@@ -91,15 +115,14 @@ public function testRequest(): void
91
115
$ this ->assertSame ($ expectedData , $ spans [1 ]->getData ());
92
116
}
93
117
94
- public function testRequestDoesNotContainBaggageHeader (): void
118
+ public function testRequestDoesNotContainTracingHeaders (): void
95
119
{
96
120
$ options = new Options ([
97
121
'dsn ' =>
'http://public:[email protected] /sentry/1 ' ,
98
- 'trace_propagation_targets ' => [ ' non-matching-host.invalid ' ] ,
122
+ 'trace_propagation_targets ' => null ,
99
123
]);
100
124
$ client = $ this ->createMock (ClientInterface::class);
101
- $ client
102
- ->expects ($ this ->once ())
125
+ $ client ->expects ($ this ->once ())
103
126
->method ('getOptions ' )
104
127
->willReturn ($ options );
105
128
@@ -122,7 +145,7 @@ public function testRequestDoesNotContainBaggageHeader(): void
122
145
$ this ->assertSame (200 , $ response ->getStatusCode ());
123
146
$ this ->assertSame ('PUT ' , $ response ->getInfo ('http_method ' ));
124
147
$ this ->assertSame ('https://www.example.com/test-page ' , $ response ->getInfo ('url ' ));
125
- $ this ->assertSame ([ 'sentry-trace: ' . $ transaction -> toTraceparent ()] , $ mockResponse ->getRequestOptions ()['normalized_headers ' ][ ' sentry-trace ' ]);
148
+ $ this ->assertArrayNotHasKey ( 'sentry-trace ' , $ mockResponse ->getRequestOptions ()['normalized_headers ' ]);
126
149
$ this ->assertArrayNotHasKey ('baggage ' , $ mockResponse ->getRequestOptions ()['normalized_headers ' ]);
127
150
$ this ->assertNotNull ($ transaction ->getSpanRecorder ());
128
151
@@ -139,52 +162,36 @@ public function testRequestDoesNotContainBaggageHeader(): void
139
162
$ this ->assertSame ($ expectedTags , $ spans [1 ]->getTags ());
140
163
}
141
164
142
- public function testRequestDoesContainBaggageHeader (): void
165
+ public function testRequestDoesContainsTracingHeadersWithoutTransaction (): void
143
166
{
144
- $ options = new Options ([
167
+ $ client = new Client ( new Options ([
145
168
'dsn ' =>
'http://public:[email protected] /sentry/1 ' ,
169
+ 'release ' => '1.0.0 ' ,
170
+ 'environment ' => 'test ' ,
146
171
'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 );
172
+ ]), new NullTransport ());
153
173
154
- $ transaction = new Transaction (new TransactionContext ());
155
- $ transaction ->initSpanRecorder ();
174
+ $ propagationContext = PropagationContext::fromDefaults ();
175
+ $ propagationContext ->setTraceId (new TraceId ('566e3688a61d4bc888951642d6f14a19 ' ));
176
+ $ propagationContext ->setSpanId (new SpanId ('566e3688a61d4bc8 ' ));
156
177
157
- $ this ->hub ->expects ($ this ->once ())
158
- ->method ('getSpan ' )
159
- ->willReturn ($ transaction );
160
- $ this ->hub ->expects ($ this ->once ())
161
- ->method ('getClient ' )
162
- ->willReturn ($ client );
178
+ $ scope = new Scope ($ propagationContext );
179
+
180
+ $ hub = new Hub ($ client , $ scope );
181
+
182
+ SentrySdk::setCurrentHub ($ hub );
163
183
164
184
$ mockResponse = new MockResponse ();
165
185
$ decoratedHttpClient = new MockHttpClient ($ mockResponse );
166
- $ httpClient = new TraceableHttpClient ($ decoratedHttpClient , $ this -> hub );
186
+ $ httpClient = new TraceableHttpClient ($ decoratedHttpClient , $ hub );
167
187
$ response = $ httpClient ->request ('POST ' , 'https://www.example.com/test-page ' );
168
188
169
189
$ this ->assertInstanceOf (AbstractTraceableResponse::class, $ response );
170
190
$ this ->assertSame (200 , $ response ->getStatusCode ());
171
191
$ this ->assertSame ('POST ' , $ response ->getInfo ('http_method ' ));
172
192
$ 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 ());
193
+ $ this ->assertSame (['sentry-trace: 566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8 ' ], $ mockResponse ->getRequestOptions ()['normalized_headers ' ]['sentry-trace ' ]);
194
+ $ 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
195
}
189
196
190
197
public function testStream (): void
@@ -217,6 +224,7 @@ public function testStream(): void
217
224
218
225
$ this ->assertSame ('foobar ' , implode ('' , $ chunks ));
219
226
$ this ->assertCount (2 , $ spans );
227
+
220
228
$ this ->assertNotNull ($ spans [1 ]->getEndTimestamp ());
221
229
$ this ->assertSame ('http.client ' , $ spans [1 ]->getOp ());
222
230
$ this ->assertSame ('GET https://www.example.com/test-page ' , $ spans [1 ]->getDescription ());
0 commit comments