Skip to content

Commit

Permalink
fix: add attributes to get_meter wrapper (#4364)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxi91f authored Dec 20, 2024
1 parent 9e94fd2 commit 33acdf3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Add `attributes` field in `metrics.get_meter` wrapper function
([#4364](https://github.com/open-telemetry/opentelemetry-python/pull/4364))
- Add Python 3.13 support
([#4353](https://github.com/open-telemetry/opentelemetry-python/pull/4353))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ def get_meter(
version: str = "",
meter_provider: Optional[MeterProvider] = None,
schema_url: Optional[str] = None,
attributes: Optional[Attributes] = None,
) -> "Meter":
"""Returns a `Meter` for use by the given instrumentation library.
Expand All @@ -774,7 +775,7 @@ def get_meter(
"""
if meter_provider is None:
meter_provider = get_meter_provider()
return meter_provider.get_meter(name, version, schema_url)
return meter_provider.get_meter(name, version, schema_url, attributes)


def _set_meter_provider(meter_provider: MeterProvider, log: bool) -> None:
Expand Down
25 changes: 24 additions & 1 deletion opentelemetry-api/tests/metrics/test_meter_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
get_meter_provider,
set_meter_provider,
)
from opentelemetry.metrics._internal import _ProxyMeter, _ProxyMeterProvider
from opentelemetry.metrics._internal import (
_ProxyMeter,
_ProxyMeterProvider,
get_meter,
)
from opentelemetry.metrics._internal.instrument import (
_ProxyCounter,
_ProxyGauge,
Expand Down Expand Up @@ -141,6 +145,25 @@ def test_invalid_name(self):

self.assertEqual(meter.name, None)

def test_get_meter_wrapper(self):
"""
`metrics._internal.get_meter` called with valid parameters and a NoOpMeterProvider
should return a NoOpMeter with the same parameters.
"""

meter = get_meter(
"name",
version="version",
meter_provider=NoOpMeterProvider(),
schema_url="schema_url",
attributes={"key": "value", "key2": 5, "key3": "value3"},
)

self.assertIsInstance(meter, NoOpMeter)
self.assertEqual(meter.name, "name")
self.assertEqual(meter.version, "version")
self.assertEqual(meter.schema_url, "schema_url")


class TestProxy(MetricsGlobalsTest, TestCase):
def test_global_proxy_meter_provider(self):
Expand Down

0 comments on commit 33acdf3

Please sign in to comment.