|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +from timeit import default_timer |
| 17 | +from typing import Optional, Union |
| 18 | +from urllib import request |
| 19 | +from urllib.parse import urlencode |
| 20 | + |
| 21 | +import httpretty |
| 22 | + |
| 23 | +from opentelemetry.instrumentation.urllib import ( # pylint: disable=no-name-in-module,import-error |
| 24 | + URLLibInstrumentor, |
| 25 | +) |
| 26 | +from opentelemetry.sdk.metrics._internal.point import Metric |
| 27 | +from opentelemetry.sdk.metrics.export import ( |
| 28 | + HistogramDataPoint, |
| 29 | + NumberDataPoint, |
| 30 | +) |
| 31 | +from opentelemetry.semconv.metrics import MetricInstruments |
| 32 | +from opentelemetry.test.test_base import TestBase |
| 33 | + |
| 34 | + |
| 35 | +class TestRequestsIntegration(TestBase): |
| 36 | + URL = "http://httpbin.org/status/200" |
| 37 | + URL_POST = "http://httpbin.org/post" |
| 38 | + |
| 39 | + def setUp(self): |
| 40 | + super().setUp() |
| 41 | + URLLibInstrumentor().instrument() |
| 42 | + httpretty.enable() |
| 43 | + httpretty.register_uri(httpretty.GET, self.URL, body=b"Hello!") |
| 44 | + httpretty.register_uri( |
| 45 | + httpretty.POST, self.URL_POST, body=b"Hello World!" |
| 46 | + ) |
| 47 | + |
| 48 | + def tearDown(self): |
| 49 | + super().tearDown() |
| 50 | + URLLibInstrumentor().uninstrument() |
| 51 | + httpretty.disable() |
| 52 | + |
| 53 | + def get_sorted_metrics(self): |
| 54 | + resource_metrics = ( |
| 55 | + self.memory_metrics_reader.get_metrics_data().resource_metrics |
| 56 | + ) |
| 57 | + |
| 58 | + all_metrics = [] |
| 59 | + for metrics in resource_metrics: |
| 60 | + for scope_metrics in metrics.scope_metrics: |
| 61 | + all_metrics.extend(scope_metrics.metrics) |
| 62 | + |
| 63 | + return self.sorted_metrics(all_metrics) |
| 64 | + |
| 65 | + @staticmethod |
| 66 | + def sorted_metrics(metrics): |
| 67 | + """ |
| 68 | + Sorts metrics by metric name. |
| 69 | + """ |
| 70 | + return sorted( |
| 71 | + metrics, |
| 72 | + key=lambda m: m.name, |
| 73 | + ) |
| 74 | + |
| 75 | + def assert_metric_expected( |
| 76 | + self, |
| 77 | + metric: Metric, |
| 78 | + expected_value: Union[int, float], |
| 79 | + expected_attributes: dict, |
| 80 | + est_delta: Optional[float] = None, |
| 81 | + ): |
| 82 | + data_point = next(iter(metric.data.data_points)) |
| 83 | + |
| 84 | + if isinstance(data_point, HistogramDataPoint): |
| 85 | + self.assertEqual( |
| 86 | + data_point.count, |
| 87 | + 1, |
| 88 | + ) |
| 89 | + if est_delta is None: |
| 90 | + self.assertEqual( |
| 91 | + data_point.sum, |
| 92 | + expected_value, |
| 93 | + ) |
| 94 | + else: |
| 95 | + self.assertAlmostEqual( |
| 96 | + data_point.sum, |
| 97 | + expected_value, |
| 98 | + delta=est_delta, |
| 99 | + ) |
| 100 | + elif isinstance(data_point, NumberDataPoint): |
| 101 | + self.assertEqual( |
| 102 | + data_point.value, |
| 103 | + expected_value, |
| 104 | + ) |
| 105 | + |
| 106 | + self.assertDictEqual( |
| 107 | + expected_attributes, |
| 108 | + dict(data_point.attributes), |
| 109 | + ) |
| 110 | + |
| 111 | + def test_basic_metric(self): |
| 112 | + start_time = default_timer() |
| 113 | + with request.urlopen(self.URL) as result: |
| 114 | + client_duration_estimated = (default_timer() - start_time) * 1000 |
| 115 | + |
| 116 | + metrics = self.get_sorted_metrics() |
| 117 | + self.assertEqual(len(metrics), 3) |
| 118 | + |
| 119 | + ( |
| 120 | + client_duration, |
| 121 | + client_request_size, |
| 122 | + client_response_size, |
| 123 | + ) = metrics[:3] |
| 124 | + |
| 125 | + self.assertEqual( |
| 126 | + client_duration.name, MetricInstruments.HTTP_CLIENT_DURATION |
| 127 | + ) |
| 128 | + self.assert_metric_expected( |
| 129 | + client_duration, |
| 130 | + client_duration_estimated, |
| 131 | + { |
| 132 | + "http.status_code": str(result.code), |
| 133 | + "http.method": "GET", |
| 134 | + "http.url": str(result.url), |
| 135 | + "http.flavor": "1.1", |
| 136 | + }, |
| 137 | + est_delta=200, |
| 138 | + ) |
| 139 | + |
| 140 | + # net.peer.name |
| 141 | + |
| 142 | + self.assertEqual( |
| 143 | + client_request_size.name, |
| 144 | + MetricInstruments.HTTP_CLIENT_REQUEST_SIZE, |
| 145 | + ) |
| 146 | + self.assert_metric_expected( |
| 147 | + client_request_size, |
| 148 | + 0, |
| 149 | + { |
| 150 | + "http.status_code": str(result.code), |
| 151 | + "http.method": "GET", |
| 152 | + "http.url": str(result.url), |
| 153 | + "http.flavor": "1.1", |
| 154 | + }, |
| 155 | + ) |
| 156 | + |
| 157 | + self.assertEqual( |
| 158 | + client_response_size.name, |
| 159 | + MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE, |
| 160 | + ) |
| 161 | + self.assert_metric_expected( |
| 162 | + client_response_size, |
| 163 | + result.length, |
| 164 | + { |
| 165 | + "http.status_code": str(result.code), |
| 166 | + "http.method": "GET", |
| 167 | + "http.url": str(result.url), |
| 168 | + "http.flavor": "1.1", |
| 169 | + }, |
| 170 | + ) |
| 171 | + |
| 172 | + def test_basic_metric_request_not_empty(self): |
| 173 | + data = {"header1": "value1", "header2": "value2"} |
| 174 | + data_encoded = urlencode(data).encode() |
| 175 | + |
| 176 | + start_time = default_timer() |
| 177 | + with request.urlopen(self.URL_POST, data=data_encoded) as result: |
| 178 | + client_duration_estimated = (default_timer() - start_time) * 1000 |
| 179 | + |
| 180 | + metrics = self.get_sorted_metrics() |
| 181 | + self.assertEqual(len(metrics), 3) |
| 182 | + |
| 183 | + ( |
| 184 | + client_duration, |
| 185 | + client_request_size, |
| 186 | + client_response_size, |
| 187 | + ) = metrics[:3] |
| 188 | + |
| 189 | + self.assertEqual( |
| 190 | + client_duration.name, MetricInstruments.HTTP_CLIENT_DURATION |
| 191 | + ) |
| 192 | + self.assert_metric_expected( |
| 193 | + client_duration, |
| 194 | + client_duration_estimated, |
| 195 | + { |
| 196 | + "http.status_code": str(result.code), |
| 197 | + "http.method": "POST", |
| 198 | + "http.url": str(result.url), |
| 199 | + "http.flavor": "1.1", |
| 200 | + }, |
| 201 | + est_delta=200, |
| 202 | + ) |
| 203 | + |
| 204 | + self.assertEqual( |
| 205 | + client_request_size.name, |
| 206 | + MetricInstruments.HTTP_CLIENT_REQUEST_SIZE, |
| 207 | + ) |
| 208 | + self.assert_metric_expected( |
| 209 | + client_request_size, |
| 210 | + len(data_encoded), |
| 211 | + { |
| 212 | + "http.status_code": str(result.code), |
| 213 | + "http.method": "POST", |
| 214 | + "http.url": str(result.url), |
| 215 | + "http.flavor": "1.1", |
| 216 | + }, |
| 217 | + ) |
| 218 | + |
| 219 | + self.assertEqual( |
| 220 | + client_response_size.name, |
| 221 | + MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE, |
| 222 | + ) |
| 223 | + self.assert_metric_expected( |
| 224 | + client_response_size, |
| 225 | + result.length, |
| 226 | + { |
| 227 | + "http.status_code": str(result.code), |
| 228 | + "http.method": "POST", |
| 229 | + "http.url": str(result.url), |
| 230 | + "http.flavor": "1.1", |
| 231 | + }, |
| 232 | + ) |
| 233 | + |
| 234 | + def test_metric_uninstrument(self): |
| 235 | + with request.urlopen(self.URL): |
| 236 | + metrics = self.get_sorted_metrics() |
| 237 | + self.assertEqual(len(metrics), 3) |
| 238 | + |
| 239 | + URLLibInstrumentor().uninstrument() |
| 240 | + with request.urlopen(self.URL): |
| 241 | + metrics = self.get_sorted_metrics() |
| 242 | + self.assertEqual(len(metrics), 3) |
| 243 | + |
| 244 | + for metric in metrics: |
| 245 | + for point in list(metric.data.data_points): |
| 246 | + self.assertEqual(point.count, 1) |
0 commit comments