8
8
use PHPUnit \Framework \TestCase ;
9
9
use Psr \Log \LoggerAwareInterface ;
10
10
use Psr \Log \NullLogger ;
11
+ use Sentry \ClientInterface ;
12
+ use Sentry \Options ;
11
13
use Sentry \SentryBundle \Tracing \HttpClient \AbstractTraceableResponse ;
12
14
use Sentry \SentryBundle \Tracing \HttpClient \TraceableHttpClient ;
13
15
use Sentry \State \HubInterface ;
@@ -68,6 +70,7 @@ public function testRequest(): void
68
70
$ this ->assertSame ('GET ' , $ response ->getInfo ('http_method ' ));
69
71
$ this ->assertSame ('https://www.example.com/test-page ' , $ response ->getInfo ('url ' ));
70
72
$ this ->assertSame (['sentry-trace: ' . $ transaction ->toTraceparent ()], $ mockResponse ->getRequestOptions ()['normalized_headers ' ]['sentry-trace ' ]);
73
+ $ this ->assertArrayNotHasKey ('baggage ' , $ mockResponse ->getRequestOptions ()['normalized_headers ' ]);
71
74
$ this ->assertNotNull ($ transaction ->getSpanRecorder ());
72
75
73
76
$ spans = $ transaction ->getSpanRecorder ()->getSpans ();
@@ -83,6 +86,102 @@ public function testRequest(): void
83
86
$ this ->assertSame ($ expectedTags , $ spans [1 ]->getTags ());
84
87
}
85
88
89
+ public function testRequestDoesNotContainBaggageHeader (): void
90
+ {
91
+ $ options = new Options ([
92
+ 'dsn ' =>
'http://public:[email protected] /sentry/1 ' ,
93
+ 'trace_propagation_targets ' => ['non-matching-host.invalid ' ],
94
+ ]);
95
+ $ client = $ this ->createMock (ClientInterface::class);
96
+ $ client
97
+ ->expects ($ this ->once ())
98
+ ->method ('getOptions ' )
99
+ ->willReturn ($ options );
100
+
101
+ $ transaction = new Transaction (new TransactionContext ());
102
+ $ transaction ->initSpanRecorder ();
103
+
104
+ $ this ->hub ->expects ($ this ->once ())
105
+ ->method ('getSpan ' )
106
+ ->willReturn ($ transaction );
107
+ $ this ->hub ->expects ($ this ->once ())
108
+ ->method ('getClient ' )
109
+ ->willReturn ($ client );
110
+
111
+ $ mockResponse = new MockResponse ();
112
+ $ decoratedHttpClient = new MockHttpClient ($ mockResponse );
113
+ $ httpClient = new TraceableHttpClient ($ decoratedHttpClient , $ this ->hub );
114
+ $ response = $ httpClient ->request ('PUT ' , 'https://www.example.com/test-page ' );
115
+
116
+ $ this ->assertInstanceOf (AbstractTraceableResponse::class, $ response );
117
+ $ this ->assertSame (200 , $ response ->getStatusCode ());
118
+ $ this ->assertSame ('PUT ' , $ response ->getInfo ('http_method ' ));
119
+ $ this ->assertSame ('https://www.example.com/test-page ' , $ response ->getInfo ('url ' ));
120
+ $ this ->assertSame (['sentry-trace: ' . $ transaction ->toTraceparent ()], $ mockResponse ->getRequestOptions ()['normalized_headers ' ]['sentry-trace ' ]);
121
+ $ this ->assertArrayNotHasKey ('baggage ' , $ mockResponse ->getRequestOptions ()['normalized_headers ' ]);
122
+ $ this ->assertNotNull ($ transaction ->getSpanRecorder ());
123
+
124
+ $ spans = $ transaction ->getSpanRecorder ()->getSpans ();
125
+ $ expectedTags = [
126
+ 'http.method ' => 'PUT ' ,
127
+ 'http.url ' => 'https://www.example.com/test-page ' ,
128
+ ];
129
+
130
+ $ this ->assertCount (2 , $ spans );
131
+ $ this ->assertNull ($ spans [1 ]->getEndTimestamp ());
132
+ $ this ->assertSame ('http.client ' , $ spans [1 ]->getOp ());
133
+ $ this ->assertSame ('HTTP PUT ' , $ spans [1 ]->getDescription ());
134
+ $ this ->assertSame ($ expectedTags , $ spans [1 ]->getTags ());
135
+ }
136
+
137
+ public function testRequestDoesContainBaggageHeader (): void
138
+ {
139
+ $ options = new Options ([
140
+ 'dsn ' =>
'http://public:[email protected] /sentry/1 ' ,
141
+ 'trace_propagation_targets ' => ['www.example.com ' ],
142
+ ]);
143
+ $ client = $ this ->createMock (ClientInterface::class);
144
+ $ client
145
+ ->expects ($ this ->once ())
146
+ ->method ('getOptions ' )
147
+ ->willReturn ($ options );
148
+
149
+ $ transaction = new Transaction (new TransactionContext ());
150
+ $ transaction ->initSpanRecorder ();
151
+
152
+ $ this ->hub ->expects ($ this ->once ())
153
+ ->method ('getSpan ' )
154
+ ->willReturn ($ transaction );
155
+ $ this ->hub ->expects ($ this ->once ())
156
+ ->method ('getClient ' )
157
+ ->willReturn ($ client );
158
+
159
+ $ mockResponse = new MockResponse ();
160
+ $ decoratedHttpClient = new MockHttpClient ($ mockResponse );
161
+ $ httpClient = new TraceableHttpClient ($ decoratedHttpClient , $ this ->hub );
162
+ $ response = $ httpClient ->request ('POST ' , 'https://www.example.com/test-page ' );
163
+
164
+ $ this ->assertInstanceOf (AbstractTraceableResponse::class, $ response );
165
+ $ this ->assertSame (200 , $ response ->getStatusCode ());
166
+ $ this ->assertSame ('POST ' , $ response ->getInfo ('http_method ' ));
167
+ $ this ->assertSame ('https://www.example.com/test-page ' , $ response ->getInfo ('url ' ));
168
+ $ this ->assertSame (['sentry-trace: ' . $ transaction ->toTraceparent ()], $ mockResponse ->getRequestOptions ()['normalized_headers ' ]['sentry-trace ' ]);
169
+ $ this ->assertSame (['baggage: ' . $ transaction ->toBaggage ()], $ mockResponse ->getRequestOptions ()['normalized_headers ' ]['baggage ' ]);
170
+ $ this ->assertNotNull ($ transaction ->getSpanRecorder ());
171
+
172
+ $ spans = $ transaction ->getSpanRecorder ()->getSpans ();
173
+ $ expectedTags = [
174
+ 'http.method ' => 'POST ' ,
175
+ 'http.url ' => 'https://www.example.com/test-page ' ,
176
+ ];
177
+
178
+ $ this ->assertCount (2 , $ spans );
179
+ $ this ->assertNull ($ spans [1 ]->getEndTimestamp ());
180
+ $ this ->assertSame ('http.client ' , $ spans [1 ]->getOp ());
181
+ $ this ->assertSame ('HTTP POST ' , $ spans [1 ]->getDescription ());
182
+ $ this ->assertSame ($ expectedTags , $ spans [1 ]->getTags ());
183
+ }
184
+
86
185
public function testStream (): void
87
186
{
88
187
$ transaction = new Transaction (new TransactionContext ());
0 commit comments