-
Notifications
You must be signed in to change notification settings - Fork 760
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
Changes from all commits
ced12ac
459bb92
5fe94f2
beff373
be93e33
eb27734
b7343ca
11dbf84
cd429d5
bf84dac
44d56ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
|
@@ -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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
| ) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where are
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.