Skip to content

Commit

Permalink
Return noopmeter on invalid name getmeter (open-telemetry#2428)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Feb 1, 2022
1 parent 02863de commit d71cf68
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
23 changes: 23 additions & 0 deletions opentelemetry-sdk/tests/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d71cf68

Please sign in to comment.