Skip to content

Commit

Permalink
Merge branch 'main' into dont-follow-imports
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Jan 9, 2025
2 parents a119b4b + 29aad2e commit 7916176
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ 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))
- sdk: don't log or print warnings when the SDK has been disabled
([#4371](https://github.com/open-telemetry/opentelemetry-python/pull/4371))

## Version 1.29.0/0.50b0 (2024-12-11)

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
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,6 @@ def get_logger(
attributes: Optional[Attributes] = None,
) -> Logger:
if self._disabled:
warnings.warn("SDK is disabled.")
return NoOpLogger(
name,
version=version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,6 @@ def get_meter(
attributes: Optional[Attributes] = None,
) -> Meter:
if self._disabled:
_logger.warning("SDK is disabled.")
return NoOpMeter(name, version=version, schema_url=schema_url)

if self._shutdown:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
these attributes can be included in the Resource.*
Resource objects are created with `Resource.create`, which accepts attributes
(key-values). Resources should NOT be created via constructor, and working with
(key-values). Resources should NOT be created via constructor except by `ResourceDetector`
instances which can't use `Resource.create` to avoid infinite loops. Working with
`Resource` objects should only be done via the Resource API methods. Resource
attributes can also be passed at process invocation in the
:envvar:`OTEL_RESOURCE_ATTRIBUTES` environment variable. You should register
Expand Down Expand Up @@ -175,6 +176,8 @@ def create(
) -> "Resource":
"""Creates a new `Resource` from attributes.
`ResourceDetector` instances should not call this method.
Args:
attributes: Optional zero or more key-value pairs.
schema_url: Optional URL pointing to the schema
Expand Down Expand Up @@ -316,6 +319,7 @@ def __init__(self, raise_on_error: bool = False) -> None:

@abc.abstractmethod
def detect(self) -> "Resource":
"""Don't call `Resource.create` here to avoid an infinite loop, instead instantiate `Resource` directly"""
raise NotImplementedError()


Expand Down
1 change: 0 additions & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,6 @@ def get_tracer(
attributes: typing.Optional[types.Attributes] = None,
) -> "trace_api.Tracer":
if self._disabled:
logger.warning("SDK is disabled.")
return NoOpTracer()
if not instrumenting_module_name: # Reject empty strings too.
instrumenting_module_name = ""
Expand Down
7 changes: 1 addition & 6 deletions opentelemetry-sdk/tests/logs/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import logging
import os
import unittest
import warnings
from unittest.mock import Mock, patch

from opentelemetry._logs import NoOpLoggerProvider, SeverityNumber
Expand Down Expand Up @@ -290,11 +289,7 @@ def test_handler_root_logger_with_disabled_sdk_does_not_go_into_recursion_error(
processor, logger = set_up_test_logging(
logging.NOTSET, root_logger=True
)
with warnings.catch_warnings(record=True) as cw:
logger.warning("hello")

self.assertEqual(len(cw), 1)
self.assertEqual("SDK is disabled.", str(cw[0].message))
logger.warning("hello")

self.assertEqual(processor.emit_count(), 0)

Expand Down
6 changes: 1 addition & 5 deletions opentelemetry-sdk/tests/logs/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# pylint: disable=protected-access

import unittest
import warnings
from unittest.mock import Mock, patch

from opentelemetry.sdk._logs import LoggerProvider
Expand Down Expand Up @@ -70,12 +69,9 @@ def test_get_logger(self):

@patch.dict("os.environ", {OTEL_SDK_DISABLED: "true"})
def test_get_logger_with_sdk_disabled(self):
with warnings.catch_warnings(record=True) as cw:
logger = LoggerProvider().get_logger(Mock())
logger = LoggerProvider().get_logger(Mock())

self.assertIsInstance(logger, NoOpLogger)
self.assertEqual(len(cw), 1)
self.assertEqual("SDK is disabled.", str(cw[0].message))

@patch.object(Resource, "create")
def test_logger_provider_init(self, resource_patch):
Expand Down

0 comments on commit 7916176

Please sign in to comment.