From 32e940b597d3458d98183128f7783cd6f8e21015 Mon Sep 17 00:00:00 2001 From: sroda Date: Thu, 23 Feb 2023 13:26:33 +0200 Subject: [PATCH] Change Urllib metrics test to work with test_base.py --- .../tests/test_metrics_instrumentation.py | 170 +++++++----------- 1 file changed, 62 insertions(+), 108 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-urllib/tests/test_metrics_instrumentation.py b/instrumentation/opentelemetry-instrumentation-urllib/tests/test_metrics_instrumentation.py index a87e3f97b9..99978cc6d1 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib/tests/test_metrics_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-urllib/tests/test_metrics_instrumentation.py @@ -14,7 +14,6 @@ from timeit import default_timer -from typing import Optional, Union from urllib import request from urllib.parse import urlencode @@ -23,11 +22,6 @@ from opentelemetry.instrumentation.urllib import ( # pylint: disable=no-name-in-module,import-error URLLibInstrumentor, ) -from opentelemetry.sdk.metrics._internal.point import Metric -from opentelemetry.sdk.metrics.export import ( - HistogramDataPoint, - NumberDataPoint, -) from opentelemetry.semconv.metrics import MetricInstruments from opentelemetry.test.test_base import TestBase @@ -50,63 +44,13 @@ def tearDown(self): URLLibInstrumentor().uninstrument() httpretty.disable() - def get_sorted_metrics(self): - resource_metrics = ( - self.memory_metrics_reader.get_metrics_data().resource_metrics - ) - - all_metrics = [] - for metrics in resource_metrics: - for scope_metrics in metrics.scope_metrics: - all_metrics.extend(scope_metrics.metrics) - - return self.sorted_metrics(all_metrics) - - @staticmethod - def sorted_metrics(metrics): - """ - Sorts metrics by metric name. - """ - return sorted( - metrics, - key=lambda m: m.name, - ) - - def assert_metric_expected( - self, - metric: Metric, - expected_value: Union[int, float], - expected_attributes: dict, - est_delta: Optional[float] = None, - ): - data_point = next(iter(metric.data.data_points)) - - if isinstance(data_point, HistogramDataPoint): - self.assertEqual( - data_point.count, - 1, - ) - if est_delta is None: - self.assertEqual( - data_point.sum, - expected_value, - ) - else: - self.assertAlmostEqual( - data_point.sum, - expected_value, - delta=est_delta, - ) - elif isinstance(data_point, NumberDataPoint): - self.assertEqual( - data_point.value, - expected_value, + # Return Sequence with one histogram + def create_histogram_data_points(self, sum_data_point, attributes): + return [ + self.create_histogram_data_point( + sum_data_point, 1, sum_data_point, sum_data_point, attributes ) - - self.assertDictEqual( - expected_attributes, - dict(data_point.attributes), - ) + ] def test_basic_metric(self): start_time = default_timer() @@ -127,31 +71,33 @@ def test_basic_metric(self): ) self.assert_metric_expected( client_duration, - client_duration_estimated, - { - "http.status_code": str(result.code), - "http.method": "GET", - "http.url": str(result.url), - "http.flavor": "1.1", - }, - est_delta=200, + self.create_histogram_data_points( + client_duration_estimated, + attributes={ + "http.status_code": str(result.code), + "http.method": "GET", + "http.url": str(result.url), + "http.flavor": "1.1", + }, + ), + est_value_delta=200, ) - # net.peer.name - self.assertEqual( client_request_size.name, MetricInstruments.HTTP_CLIENT_REQUEST_SIZE, ) self.assert_metric_expected( client_request_size, - 0, - { - "http.status_code": str(result.code), - "http.method": "GET", - "http.url": str(result.url), - "http.flavor": "1.1", - }, + self.create_histogram_data_points( + 0, + attributes={ + "http.status_code": str(result.code), + "http.method": "GET", + "http.url": str(result.url), + "http.flavor": "1.1", + }, + ), ) self.assertEqual( @@ -160,13 +106,15 @@ def test_basic_metric(self): ) self.assert_metric_expected( client_response_size, - result.length, - { - "http.status_code": str(result.code), - "http.method": "GET", - "http.url": str(result.url), - "http.flavor": "1.1", - }, + self.create_histogram_data_points( + result.length, + attributes={ + "http.status_code": str(result.code), + "http.method": "GET", + "http.url": str(result.url), + "http.flavor": "1.1", + }, + ), ) def test_basic_metric_request_not_empty(self): @@ -191,14 +139,16 @@ def test_basic_metric_request_not_empty(self): ) self.assert_metric_expected( client_duration, - client_duration_estimated, - { - "http.status_code": str(result.code), - "http.method": "POST", - "http.url": str(result.url), - "http.flavor": "1.1", - }, - est_delta=200, + self.create_histogram_data_points( + client_duration_estimated, + attributes={ + "http.status_code": str(result.code), + "http.method": "POST", + "http.url": str(result.url), + "http.flavor": "1.1", + }, + ), + est_value_delta=200, ) self.assertEqual( @@ -207,13 +157,15 @@ def test_basic_metric_request_not_empty(self): ) self.assert_metric_expected( client_request_size, - len(data_encoded), - { - "http.status_code": str(result.code), - "http.method": "POST", - "http.url": str(result.url), - "http.flavor": "1.1", - }, + self.create_histogram_data_points( + len(data_encoded), + attributes={ + "http.status_code": str(result.code), + "http.method": "POST", + "http.url": str(result.url), + "http.flavor": "1.1", + }, + ), ) self.assertEqual( @@ -222,13 +174,15 @@ def test_basic_metric_request_not_empty(self): ) self.assert_metric_expected( client_response_size, - result.length, - { - "http.status_code": str(result.code), - "http.method": "POST", - "http.url": str(result.url), - "http.flavor": "1.1", - }, + self.create_histogram_data_points( + result.length, + attributes={ + "http.status_code": str(result.code), + "http.method": "POST", + "http.url": str(result.url), + "http.flavor": "1.1", + }, + ), ) def test_metric_uninstrument(self):