Skip to content
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

Remove resource attribute from Meter #9

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ def __init__(
):
self.instrumentation_info = instrumentation_info
self.processor = Processor(source.stateful, source.resource)
self.resource = source.resource
self.metrics = set()
self.observers = set()
self.metrics_lock = threading.Lock()
Expand Down
44 changes: 28 additions & 16 deletions opentelemetry-sdk/tests/metrics/export/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
class TestConsoleMetricsExporter(unittest.TestCase):
# pylint: disable=no-self-use
def test_export(self):
meter = metrics.MeterProvider().get_meter(__name__)
meter_provider = metrics.MeterProvider()
meter = meter_provider.get_meter(__name__)
exporter = ConsoleMetricsExporter()
metric = metrics.Counter(
"available memory",
Expand All @@ -50,13 +51,15 @@ def test_export(self):
)
labels = {"environment": "staging"}
aggregator = SumAggregator()
record = MetricRecord(metric, labels, aggregator, meter.resource)
record = MetricRecord(
metric, labels, aggregator, meter_provider.resource
)
result = '{}(data="{}", labels="{}", value={}, resource={})'.format(
ConsoleMetricsExporter.__name__,
metric,
labels,
aggregator.checkpoint,
meter.resource.attributes,
meter_provider.resource.attributes,
)
with mock.patch("sys.stdout") as mock_stdout:
exporter.export([record])
Expand All @@ -65,8 +68,9 @@ def test_export(self):

class TestProcessor(unittest.TestCase):
def test_checkpoint_set(self):
meter = metrics.MeterProvider().get_meter(__name__)
processor = Processor(True, meter.resource)
meter_provider = metrics.MeterProvider()
meter = meter_provider.get_meter(__name__)
processor = Processor(True, meter_provider.resource)
aggregator = SumAggregator()
metric = metrics.Counter(
"available memory", "available memory", "bytes", int, meter
Expand All @@ -88,8 +92,9 @@ def test_checkpoint_set_empty(self):
self.assertEqual(len(records), 0)

def test_finished_collection_stateless(self):
meter = metrics.MeterProvider().get_meter(__name__)
processor = Processor(False, meter.resource)
meter_provider = metrics.MeterProvider()
meter = meter_provider.get_meter(__name__)
processor = Processor(False, meter_provider.resource)
aggregator = SumAggregator()
metric = metrics.Counter(
"available memory", "available memory", "bytes", int, meter
Expand All @@ -103,8 +108,9 @@ def test_finished_collection_stateless(self):
self.assertEqual(len(processor._batch_map), 0)

def test_finished_collection_stateful(self):
meter = metrics.MeterProvider().get_meter(__name__)
processor = Processor(True, meter.resource)
meter_provider = metrics.MeterProvider()
meter = meter_provider.get_meter(__name__)
processor = Processor(True, meter_provider.resource)
aggregator = SumAggregator()
metric = metrics.Counter(
"available memory", "available memory", "bytes", int, meter
Expand All @@ -118,8 +124,9 @@ def test_finished_collection_stateful(self):
self.assertEqual(len(processor._batch_map), 1)

def test_processor_process_exists(self):
meter = metrics.MeterProvider().get_meter(__name__)
processor = Processor(True, meter.resource)
meter_provider = metrics.MeterProvider()
meter = meter_provider.get_meter(__name__)
processor = Processor(True, meter_provider.resource)
aggregator = SumAggregator()
aggregator2 = SumAggregator()
metric = metrics.Counter(
Expand All @@ -139,8 +146,9 @@ def test_processor_process_exists(self):
self.assertEqual(processor._batch_map.get(batch_key).checkpoint, 1.0)

def test_processor_process_not_exists(self):
meter = metrics.MeterProvider().get_meter(__name__)
processor = Processor(True, meter.resource)
meter_provider = metrics.MeterProvider()
meter = meter_provider.get_meter(__name__)
processor = Processor(True, meter_provider.resource)
aggregator = SumAggregator()
metric = metrics.Counter(
"available memory", "available memory", "bytes", int, meter
Expand All @@ -158,11 +166,15 @@ def test_processor_process_not_exists(self):
self.assertEqual(processor._batch_map.get(batch_key).checkpoint, 1.0)

def test_processor_process_not_stateful(self):
meter = metrics.MeterProvider().get_meter(__name__)
processor = Processor(True, meter.resource)
meter_provider = metrics.MeterProvider()
processor = Processor(True, meter_provider.resource)
aggregator = SumAggregator()
metric = metrics.Counter(
"available memory", "available memory", "bytes", int, meter
"available memory",
"available memory",
"bytes",
int,
meter_provider.get_meter(__name__),
)
labels = ()
_batch_map = {}
Expand Down
8 changes: 3 additions & 5 deletions opentelemetry-sdk/tests/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ def test_stateful(self):
def test_resource(self):
resource = resources.Resource.create({})
meter_provider = metrics.MeterProvider(resource=resource)
meter = meter_provider.get_meter(__name__)
self.assertIs(meter.resource, resource)
self.assertIs(meter_provider.resource, resource)

def test_resource_empty(self):
meter_provider = metrics.MeterProvider()
meter = meter_provider.get_meter(__name__)
# pylint: disable=protected-access
self.assertEqual(meter.resource, resources._DEFAULT_RESOURCE)
self.assertEqual(meter_provider.resource, resources._DEFAULT_RESOURCE)

def test_start_pipeline(self):
exporter = mock.Mock()
Expand Down Expand Up @@ -167,7 +165,7 @@ def test_create_metric(self):
self.assertIsInstance(counter, metrics.Counter)
self.assertEqual(counter.value_type, int)
self.assertEqual(counter.name, "name")
self.assertIs(counter.meter.resource, resource)
self.assertIs(meter_provider.resource, resource)
self.assertEqual(counter.meter, meter)

def test_create_updowncounter(self):
Expand Down