Skip to content

Move the metrics basic test to TestBase class #3092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
import logging
import unittest
from contextlib import contextmanager
from typing import Tuple
from typing import Optional, Tuple, Union

from opentelemetry import metrics as metrics_api
from opentelemetry import trace as trace_api
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import InMemoryMetricReader, MetricReader
from opentelemetry.sdk.metrics._internal.point import Metric
from opentelemetry.sdk.metrics.export import (
HistogramDataPoint,
InMemoryMetricReader,
MetricReader,
NumberDataPoint,
)
from opentelemetry.sdk.trace import TracerProvider, export
from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
InMemorySpanExporter,
Expand Down Expand Up @@ -85,6 +91,63 @@ def sorted_spans(self, spans): # pylint: disable=R0201
reverse=True,
)

def sorted_metrics(self, metrics): # pylint: disable=R0201
"""
Sorts metrics by metric name.
"""
return sorted(
metrics,
key=lambda m: m.name,
)

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)

def assert_metric_expected(
self,
metric: Metric,
expected_value: Union[int, float],
Comment on lines +117 to +118
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way I would find this useful is if it takes the current metric and the expected metric, not a single float/int value. The following code has things that don't make it useable for many use cases; why does the count have to be 1? How can one use this when the count is not 1? There are no assertions of min, max etc.?

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,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are min, max, count etc... assertions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

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,
)

self.assertDictEqual(
expected_attributes,
dict(data_point.attributes),
)

@staticmethod
def create_tracer_provider(**kwargs):
"""Helper to create a configured tracer provider.
Expand Down