Skip to content

Commit

Permalink
Change Urllib metrics test to work with test_base.py
Browse files Browse the repository at this point in the history
  • Loading branch information
shalevr committed Feb 23, 2023
1 parent 44f1e89 commit 32e940b
Showing 1 changed file with 62 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@


from timeit import default_timer
from typing import Optional, Union
from urllib import request
from urllib.parse import urlencode

Expand All @@ -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

Expand All @@ -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()
Expand All @@ -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(
Expand All @@ -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):
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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):
Expand Down

0 comments on commit 32e940b

Please sign in to comment.