diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 36c04fede81..3b2ded87471 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ env: # Otherwise, set variable to the commit of your branch on # opentelemetry-python-contrib which is compatible with these Core repo # changes. - CONTRIB_REPO_SHA: b541c59284100e617303a5c1f505ca2872045792 + CONTRIB_REPO_SHA: ad2594e166bd7f4cd40780df418f82389de970a6 # This is needed because we do not clone the core repo in contrib builds anymore. # When running contrib builds as part of core builds, we use actions/checkout@v2 which # does not set an environment variable (simply just runs tox), which is different when diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py index 0cdcbab6e9c..b3232b367e8 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py @@ -230,6 +230,10 @@ def get_meter( ) return NoOpMeter(name, version=version, schema_url=schema_url) + if not name: + _logger.warning("Meter name cannot be None or empty.") + return NoOpMeter(name, version=version, schema_url=schema_url) + info = InstrumentationInfo(name, version, schema_url) with self._meter_lock: if not self._meters.get(info): diff --git a/opentelemetry-sdk/tests/metrics/test_metrics.py b/opentelemetry-sdk/tests/metrics/test_metrics.py index 8a78eaecd8a..f72d059384e 100644 --- a/opentelemetry-sdk/tests/metrics/test_metrics.py +++ b/opentelemetry-sdk/tests/metrics/test_metrics.py @@ -17,6 +17,7 @@ from unittest import TestCase from unittest.mock import MagicMock, Mock, patch +from opentelemetry._metrics import NoOpMeter from opentelemetry.sdk._metrics import Meter, MeterProvider from opentelemetry.sdk._metrics.instrument import ( Counter, @@ -80,6 +81,28 @@ def test_get_meter(self): self.assertEqual(meter._instrumentation_info.version, "version") self.assertEqual(meter._instrumentation_info.schema_url, "schema_url") + def test_get_meter_empty(self): + """ + `MeterProvider.get_meter` called with None or empty string as name + should return a NoOpMeter. + """ + + meter = MeterProvider().get_meter( + None, + version="version", + schema_url="schema_url", + ) + self.assertIsInstance(meter, NoOpMeter) + self.assertEqual(meter._name, None) + + meter = MeterProvider().get_meter( + "", + version="version", + schema_url="schema_url", + ) + self.assertIsInstance(meter, NoOpMeter) + self.assertEqual(meter._name, "") + def test_get_meter_duplicate(self): """ Subsequent calls to `MeterProvider.get_meter` with the same arguments