|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 | #
|
| 15 | +from timeit import default_timer |
15 | 16 | from unittest.mock import Mock, patch
|
16 | 17 |
|
17 | 18 | import pytest
|
|
26 | 27 | get_global_response_propagator,
|
27 | 28 | set_global_response_propagator,
|
28 | 29 | )
|
| 30 | +from opentelemetry.instrumentation.wsgi import ( |
| 31 | + _active_requests_count_attrs, |
| 32 | + _duration_attrs, |
| 33 | +) |
| 34 | +from opentelemetry.sdk.metrics.export import ( |
| 35 | + HistogramDataPoint, |
| 36 | + NumberDataPoint, |
| 37 | +) |
29 | 38 | from opentelemetry.sdk.resources import Resource
|
30 | 39 | from opentelemetry.semconv.trace import SpanAttributes
|
31 | 40 | from opentelemetry.test.test_base import TestBase
|
|
38 | 47 |
|
39 | 48 | from .app import make_app
|
40 | 49 |
|
| 50 | +_expected_metric_names = [ |
| 51 | + "http.server.active_requests", |
| 52 | + "http.server.duration", |
| 53 | +] |
| 54 | +_recommended_attrs = { |
| 55 | + "http.server.active_requests": _active_requests_count_attrs, |
| 56 | + "http.server.duration": _duration_attrs, |
| 57 | +} |
| 58 | + |
41 | 59 |
|
42 | 60 | class TestFalconBase(TestBase):
|
43 | 61 | def setUp(self):
|
@@ -254,6 +272,87 @@ def test_uninstrument_after_instrument(self):
|
254 | 272 | spans = self.memory_exporter.get_finished_spans()
|
255 | 273 | self.assertEqual(len(spans), 0)
|
256 | 274 |
|
| 275 | + def test_falcon_metrics(self): |
| 276 | + self.client().simulate_get("/hello/756") |
| 277 | + self.client().simulate_get("/hello/756") |
| 278 | + self.client().simulate_get("/hello/756") |
| 279 | + metrics_list = self.memory_metrics_reader.get_metrics_data() |
| 280 | + number_data_point_seen = False |
| 281 | + histogram_data_point_seen = False |
| 282 | + self.assertTrue(len(metrics_list.resource_metrics) != 0) |
| 283 | + for resource_metric in metrics_list.resource_metrics: |
| 284 | + self.assertTrue(len(resource_metric.scope_metrics) != 0) |
| 285 | + for scope_metric in resource_metric.scope_metrics: |
| 286 | + self.assertTrue(len(scope_metric.metrics) != 0) |
| 287 | + for metric in scope_metric.metrics: |
| 288 | + self.assertIn(metric.name, _expected_metric_names) |
| 289 | + data_points = list(metric.data.data_points) |
| 290 | + self.assertEqual(len(data_points), 1) |
| 291 | + for point in data_points: |
| 292 | + if isinstance(point, HistogramDataPoint): |
| 293 | + self.assertEqual(point.count, 3) |
| 294 | + histogram_data_point_seen = True |
| 295 | + if isinstance(point, NumberDataPoint): |
| 296 | + number_data_point_seen = True |
| 297 | + for attr in point.attributes: |
| 298 | + self.assertIn( |
| 299 | + attr, _recommended_attrs[metric.name] |
| 300 | + ) |
| 301 | + self.assertTrue(number_data_point_seen and histogram_data_point_seen) |
| 302 | + |
| 303 | + def test_falcon_metric_values(self): |
| 304 | + expected_duration_attributes = { |
| 305 | + "http.method": "GET", |
| 306 | + "http.host": "falconframework.org", |
| 307 | + "http.scheme": "http", |
| 308 | + "http.flavor": "1.1", |
| 309 | + "http.server_name": "falconframework.org", |
| 310 | + "net.host.port": 80, |
| 311 | + "http.status_code": 404, |
| 312 | + } |
| 313 | + expected_requests_count_attributes = { |
| 314 | + "http.method": "GET", |
| 315 | + "http.host": "falconframework.org", |
| 316 | + "http.scheme": "http", |
| 317 | + "http.flavor": "1.1", |
| 318 | + "http.server_name": "falconframework.org", |
| 319 | + } |
| 320 | + start = default_timer() |
| 321 | + self.client().simulate_get("/hello/756") |
| 322 | + duration = max(round((default_timer() - start) * 1000), 0) |
| 323 | + metrics_list = self.memory_metrics_reader.get_metrics_data() |
| 324 | + for resource_metric in metrics_list.resource_metrics: |
| 325 | + for scope_metric in resource_metric.scope_metrics: |
| 326 | + for metric in scope_metric.metrics: |
| 327 | + for point in list(metric.data.data_points): |
| 328 | + if isinstance(point, HistogramDataPoint): |
| 329 | + self.assertDictEqual( |
| 330 | + expected_duration_attributes, |
| 331 | + dict(point.attributes), |
| 332 | + ) |
| 333 | + self.assertEqual(point.count, 1) |
| 334 | + self.assertAlmostEqual( |
| 335 | + duration, point.sum, delta=10 |
| 336 | + ) |
| 337 | + if isinstance(point, NumberDataPoint): |
| 338 | + self.assertDictEqual( |
| 339 | + expected_requests_count_attributes, |
| 340 | + dict(point.attributes), |
| 341 | + ) |
| 342 | + self.assertEqual(point.value, 0) |
| 343 | + |
| 344 | + def test_metric_uninstrument(self): |
| 345 | + self.client().simulate_request(method="POST", path="/hello/756") |
| 346 | + FalconInstrumentor().uninstrument() |
| 347 | + self.client().simulate_request(method="POST", path="/hello/756") |
| 348 | + metrics_list = self.memory_metrics_reader.get_metrics_data() |
| 349 | + for resource_metric in metrics_list.resource_metrics: |
| 350 | + for scope_metric in resource_metric.scope_metrics: |
| 351 | + for metric in scope_metric.metrics: |
| 352 | + for point in list(metric.data.data_points): |
| 353 | + if isinstance(point, HistogramDataPoint): |
| 354 | + self.assertEqual(point.count, 1) |
| 355 | + |
257 | 356 |
|
258 | 357 | class TestFalconInstrumentationWithTracerProvider(TestBase):
|
259 | 358 | def setUp(self):
|
|
0 commit comments