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

Return noopmeter on invalid name getmeter #2428

Merged
merged 3 commits into from
Feb 1, 2022
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
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
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