|
24 | 24 | from opentelemetry.instrumentation.urllib import ( # pylint: disable=no-name-in-module,import-error
|
25 | 25 | URLLibInstrumentor,
|
26 | 26 | )
|
27 |
| -from opentelemetry.semconv.metrics import MetricInstruments |
28 | 27 | from opentelemetry.sdk.metrics._internal.point import Metric
|
29 | 28 | from opentelemetry.sdk.metrics.export import (
|
30 | 29 | HistogramDataPoint,
|
31 | 30 | NumberDataPoint,
|
32 | 31 | )
|
| 32 | +from opentelemetry.semconv.metrics import MetricInstruments |
33 | 33 | from opentelemetry.test.test_base import TestBase
|
34 | 34 |
|
35 | 35 |
|
@@ -111,138 +111,137 @@ def assert_metric_expected(
|
111 | 111 |
|
112 | 112 | def test_basic_metric(self):
|
113 | 113 | start_time = default_timer()
|
114 |
| - result = urllib.request.urlopen(self.URL) |
115 |
| - client_duration_estimated = (default_timer() - start_time) * 1000 |
| 114 | + with request.urlopen(self.URL) as result: |
| 115 | + client_duration_estimated = (default_timer() - start_time) * 1000 |
116 | 116 |
|
117 |
| - metrics = self.get_sorted_metrics() |
118 |
| - self.assertEqual(len(metrics), 3) |
| 117 | + metrics = self.get_sorted_metrics() |
| 118 | + self.assertEqual(len(metrics), 3) |
119 | 119 |
|
120 |
| - ( |
121 |
| - client_duration, |
122 |
| - client_request_size, |
123 |
| - client_response_size, |
124 |
| - ) = metrics[:3] |
| 120 | + ( |
| 121 | + client_duration, |
| 122 | + client_request_size, |
| 123 | + client_response_size, |
| 124 | + ) = metrics[:3] |
125 | 125 |
|
126 |
| - self.assertEqual( |
127 |
| - client_duration.name, MetricInstruments.HTTP_CLIENT_DURATION |
128 |
| - ) |
129 |
| - self.assert_metric_expected( |
130 |
| - client_duration, |
131 |
| - client_duration_estimated, |
132 |
| - { |
133 |
| - "http.status_code": str(result.code), |
134 |
| - "http.method": "GET", |
135 |
| - "http.url": str(result.url), |
136 |
| - "http.flavor": "1.1", |
137 |
| - }, |
138 |
| - est_delta=200, |
139 |
| - ) |
| 126 | + self.assertEqual( |
| 127 | + client_duration.name, MetricInstruments.HTTP_CLIENT_DURATION |
| 128 | + ) |
| 129 | + self.assert_metric_expected( |
| 130 | + client_duration, |
| 131 | + client_duration_estimated, |
| 132 | + { |
| 133 | + "http.status_code": str(result.code), |
| 134 | + "http.method": "GET", |
| 135 | + "http.url": str(result.url), |
| 136 | + "http.flavor": "1.1", |
| 137 | + }, |
| 138 | + est_delta=200, |
| 139 | + ) |
140 | 140 |
|
141 |
| - # net.peer.name |
| 141 | + # net.peer.name |
142 | 142 |
|
143 |
| - self.assertEqual( |
144 |
| - client_request_size.name, |
145 |
| - MetricInstruments.HTTP_CLIENT_REQUEST_SIZE, |
146 |
| - ) |
147 |
| - self.assert_metric_expected( |
148 |
| - client_request_size, |
149 |
| - 0, |
150 |
| - { |
151 |
| - "http.status_code": str(result.code), |
152 |
| - "http.method": "GET", |
153 |
| - "http.url": str(result.url), |
154 |
| - "http.flavor": "1.1", |
155 |
| - }, |
156 |
| - ) |
| 143 | + self.assertEqual( |
| 144 | + client_request_size.name, |
| 145 | + MetricInstruments.HTTP_CLIENT_REQUEST_SIZE, |
| 146 | + ) |
| 147 | + self.assert_metric_expected( |
| 148 | + client_request_size, |
| 149 | + 0, |
| 150 | + { |
| 151 | + "http.status_code": str(result.code), |
| 152 | + "http.method": "GET", |
| 153 | + "http.url": str(result.url), |
| 154 | + "http.flavor": "1.1", |
| 155 | + }, |
| 156 | + ) |
157 | 157 |
|
158 |
| - self.assertEqual( |
159 |
| - client_response_size.name, |
160 |
| - MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE, |
161 |
| - ) |
162 |
| - self.assert_metric_expected( |
163 |
| - client_response_size, |
164 |
| - result.length, |
165 |
| - { |
166 |
| - "http.status_code": str(result.code), |
167 |
| - "http.method": "GET", |
168 |
| - "http.url": str(result.url), |
169 |
| - "http.flavor": "1.1", |
170 |
| - }, |
171 |
| - ) |
| 158 | + self.assertEqual( |
| 159 | + client_response_size.name, |
| 160 | + MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE, |
| 161 | + ) |
| 162 | + self.assert_metric_expected( |
| 163 | + client_response_size, |
| 164 | + result.length, |
| 165 | + { |
| 166 | + "http.status_code": str(result.code), |
| 167 | + "http.method": "GET", |
| 168 | + "http.url": str(result.url), |
| 169 | + "http.flavor": "1.1", |
| 170 | + }, |
| 171 | + ) |
172 | 172 |
|
173 | 173 | def test_basic_metric_request_not_empty(self):
|
174 | 174 | data = {"header1": "value1", "header2": "value2"}
|
175 |
| - data_encoded = urllib.parse.urlencode(data).encode() |
| 175 | + data_encoded = urlencode(data).encode() |
176 | 176 |
|
177 | 177 | start_time = default_timer()
|
178 |
| - result = urllib.request.urlopen(self.URL_POST, data=data_encoded) |
179 |
| - client_duration_estimated = (default_timer() - start_time) * 1000 |
| 178 | + with request.urlopen(self.URL_POST, data=data_encoded) as result: |
| 179 | + client_duration_estimated = (default_timer() - start_time) * 1000 |
180 | 180 |
|
181 |
| - metrics = self.get_sorted_metrics() |
182 |
| - self.assertEqual(len(metrics), 3) |
| 181 | + metrics = self.get_sorted_metrics() |
| 182 | + self.assertEqual(len(metrics), 3) |
183 | 183 |
|
184 |
| - ( |
185 |
| - client_duration, |
186 |
| - client_request_size, |
187 |
| - client_response_size, |
188 |
| - ) = metrics[:3] |
| 184 | + ( |
| 185 | + client_duration, |
| 186 | + client_request_size, |
| 187 | + client_response_size, |
| 188 | + ) = metrics[:3] |
189 | 189 |
|
190 |
| - self.assertEqual( |
191 |
| - client_duration.name, MetricInstruments.HTTP_CLIENT_DURATION |
192 |
| - ) |
193 |
| - self.assert_metric_expected( |
194 |
| - client_duration, |
195 |
| - client_duration_estimated, |
196 |
| - { |
197 |
| - "http.status_code": str(result.code), |
198 |
| - "http.method": "POST", |
199 |
| - "http.url": str(result.url), |
200 |
| - "http.flavor": "1.1", |
201 |
| - }, |
202 |
| - est_delta=200, |
203 |
| - ) |
| 190 | + self.assertEqual( |
| 191 | + client_duration.name, MetricInstruments.HTTP_CLIENT_DURATION |
| 192 | + ) |
| 193 | + self.assert_metric_expected( |
| 194 | + client_duration, |
| 195 | + client_duration_estimated, |
| 196 | + { |
| 197 | + "http.status_code": str(result.code), |
| 198 | + "http.method": "POST", |
| 199 | + "http.url": str(result.url), |
| 200 | + "http.flavor": "1.1", |
| 201 | + }, |
| 202 | + est_delta=200, |
| 203 | + ) |
204 | 204 |
|
205 |
| - self.assertEqual( |
206 |
| - client_request_size.name, |
207 |
| - MetricInstruments.HTTP_CLIENT_REQUEST_SIZE, |
208 |
| - ) |
209 |
| - self.assert_metric_expected( |
210 |
| - client_request_size, |
211 |
| - len(data_encoded), |
212 |
| - { |
213 |
| - "http.status_code": str(result.code), |
214 |
| - "http.method": "POST", |
215 |
| - "http.url": str(result.url), |
216 |
| - "http.flavor": "1.1", |
217 |
| - }, |
218 |
| - ) |
| 205 | + self.assertEqual( |
| 206 | + client_request_size.name, |
| 207 | + MetricInstruments.HTTP_CLIENT_REQUEST_SIZE, |
| 208 | + ) |
| 209 | + self.assert_metric_expected( |
| 210 | + client_request_size, |
| 211 | + len(data_encoded), |
| 212 | + { |
| 213 | + "http.status_code": str(result.code), |
| 214 | + "http.method": "POST", |
| 215 | + "http.url": str(result.url), |
| 216 | + "http.flavor": "1.1", |
| 217 | + }, |
| 218 | + ) |
219 | 219 |
|
220 |
| - self.assertEqual( |
221 |
| - client_response_size.name, |
222 |
| - MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE, |
223 |
| - ) |
224 |
| - self.assert_metric_expected( |
225 |
| - client_response_size, |
226 |
| - result.length, |
227 |
| - { |
228 |
| - "http.status_code": str(result.code), |
229 |
| - "http.method": "POST", |
230 |
| - "http.url": str(result.url), |
231 |
| - "http.flavor": "1.1", |
232 |
| - }, |
233 |
| - ) |
| 220 | + self.assertEqual( |
| 221 | + client_response_size.name, |
| 222 | + MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE, |
| 223 | + ) |
| 224 | + self.assert_metric_expected( |
| 225 | + client_response_size, |
| 226 | + result.length, |
| 227 | + { |
| 228 | + "http.status_code": str(result.code), |
| 229 | + "http.method": "POST", |
| 230 | + "http.url": str(result.url), |
| 231 | + "http.flavor": "1.1", |
| 232 | + }, |
| 233 | + ) |
234 | 234 |
|
235 | 235 | def test_metric_uninstrument(self):
|
236 |
| - urllib.request.urlopen(self.URL) |
237 |
| - metrics = self.get_sorted_metrics() |
238 |
| - self.assertEqual(len(metrics), 3) |
239 |
| - |
240 |
| - URLLibInstrumentor().uninstrument() |
241 |
| - urllib.request.urlopen(self.URL) |
242 |
| - |
243 |
| - metrics = self.get_sorted_metrics() |
244 |
| - self.assertEqual(len(metrics), 3) |
245 |
| - |
246 |
| - for metric in metrics: |
247 |
| - for point in list(metric.data.data_points): |
248 |
| - self.assertEqual(point.count, 1) |
| 236 | + with request.urlopen(self.URL): |
| 237 | + metrics = self.get_sorted_metrics() |
| 238 | + self.assertEqual(len(metrics), 3) |
| 239 | + |
| 240 | + URLLibInstrumentor().uninstrument() |
| 241 | + with request.urlopen(self.URL): |
| 242 | + metrics = self.get_sorted_metrics() |
| 243 | + self.assertEqual(len(metrics), 3) |
| 244 | + |
| 245 | + for metric in metrics: |
| 246 | + for point in list(metric.data.data_points): |
| 247 | + self.assertEqual(point.count, 1) |
0 commit comments