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

optional scope attribute for tracer creation #4028

Merged
merged 10 commits into from
Jul 18, 2024
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

- optional scope attribute for tracer creation
([#4028](https://github.com/open-telemetry/opentelemetry-python/pull/4028))
- OTLP exporter is encoding invalid span/trace IDs in the logs fix
([#4006](https://github.com/open-telemetry/opentelemetry-python/pull/4006))
- Update sdk process resource detector `process.command_args` attribute to also include the executable itself
Expand Down
15 changes: 14 additions & 1 deletion opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def get_tracer(
instrumenting_module_name: str,
instrumenting_library_version: typing.Optional[str] = None,
schema_url: typing.Optional[str] = None,
attributes: typing.Optional[types.Attributes] = None,
) -> "Tracer":
"""Returns a `Tracer` for use by the given instrumentation library.

Expand Down Expand Up @@ -216,6 +217,7 @@ def get_tracer(
``importlib.metadata.version(instrumenting_library_name)``.

schema_url: Optional. Specifies the Schema URL of the emitted telemetry.
attributes: Optional. Specifies the attributes of the emitted telemetry.
"""


Expand All @@ -230,6 +232,7 @@ def get_tracer(
instrumenting_module_name: str,
instrumenting_library_version: typing.Optional[str] = None,
schema_url: typing.Optional[str] = None,
attributes: typing.Optional[types.Attributes] = None,
) -> "Tracer":
# pylint:disable=no-self-use,unused-argument
return NoOpTracer()
Expand All @@ -249,17 +252,20 @@ def get_tracer(
instrumenting_module_name: str,
instrumenting_library_version: typing.Optional[str] = None,
schema_url: typing.Optional[str] = None,
attributes: typing.Optional[types.Attributes] = None,
) -> "Tracer":
if _TRACER_PROVIDER:
return _TRACER_PROVIDER.get_tracer(
instrumenting_module_name,
instrumenting_library_version,
schema_url,
attributes,
)
return ProxyTracer(
instrumenting_module_name,
instrumenting_library_version,
schema_url,
attributes,
)


Expand Down Expand Up @@ -407,10 +413,12 @@ def __init__(
instrumenting_module_name: str,
instrumenting_library_version: typing.Optional[str] = None,
schema_url: typing.Optional[str] = None,
attributes: typing.Optional[types.Attributes] = None,
):
self._instrumenting_module_name = instrumenting_module_name
self._instrumenting_library_version = instrumenting_library_version
self._schema_url = schema_url
self._attributes = attributes
self._real_tracer: Optional[Tracer] = None
self._noop_tracer = NoOpTracer()

Expand All @@ -424,6 +432,7 @@ def _tracer(self) -> Tracer:
self._instrumenting_module_name,
self._instrumenting_library_version,
self._schema_url,
self._attributes,
)
return self._real_tracer
return self._noop_tracer
Expand Down Expand Up @@ -492,6 +501,7 @@ def get_tracer(
instrumenting_library_version: typing.Optional[str] = None,
tracer_provider: Optional[TracerProvider] = None,
schema_url: typing.Optional[str] = None,
attributes: typing.Optional[types.Attributes] = None,
) -> "Tracer":
"""Returns a `Tracer` for use by the given instrumentation library.

Expand All @@ -503,7 +513,10 @@ def get_tracer(
if tracer_provider is None:
tracer_provider = get_tracer_provider()
return tracer_provider.get_tracer(
instrumenting_module_name, instrumenting_library_version, schema_url
instrumenting_module_name,
instrumenting_library_version,
schema_url,
attributes,
)


Expand Down
6 changes: 4 additions & 2 deletions opentelemetry-api/tests/trace/test_globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ class TestGlobals(TraceGlobalsTest, unittest.TestCase):
def test_get_tracer(mock_tracer_provider): # type: ignore
"""trace.get_tracer should proxy to the global tracer provider."""
trace.get_tracer("foo", "var")
mock_tracer_provider.get_tracer.assert_called_with("foo", "var", None)
mock_tracer_provider.get_tracer.assert_called_with(
"foo", "var", None, None
)
mock_provider = Mock()
trace.get_tracer("foo", "var", mock_provider)
mock_provider.get_tracer.assert_called_with("foo", "var", None)
mock_provider.get_tracer.assert_called_with("foo", "var", None, None)


class TestGlobalsConcurrency(TraceGlobalsTest, ConcurrencyTestBase):
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-api/tests/trace/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Span,
)
from opentelemetry.util._decorator import _agnosticcontextmanager
from opentelemetry.util.types import Attributes


class TestProvider(trace.NoOpTracerProvider):
Expand All @@ -32,6 +33,7 @@ def get_tracer(
instrumenting_module_name: str,
instrumenting_library_version: typing.Optional[str] = None,
schema_url: typing.Optional[str] = None,
attributes: typing.Optional[Attributes] = None,
) -> trace.Tracer:
return TestTracer()

Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,7 @@ def get_tracer(
instrumenting_module_name: str,
instrumenting_library_version: typing.Optional[str] = None,
schema_url: typing.Optional[str] = None,
attributes: typing.Optional[types.Attributes] = None,
) -> "trace_api.Tracer":
if self._disabled:
logger.warning("SDK is disabled.")
Expand Down Expand Up @@ -1267,6 +1268,7 @@ def get_tracer(
instrumenting_module_name,
instrumenting_library_version,
schema_url,
attributes,
),
)

Expand Down
24 changes: 24 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,30 @@ def test_tracer_provider_accepts_concurrent_multi_span_processor(self):
span_processor, tracer_provider._active_span_processor
)

def test_get_tracer_sdk(self):
tracer_provider = trace.TracerProvider()
tracer = tracer_provider.get_tracer(
"module_name",
"library_version",
"schema_url",
{"key1": "value1", "key2": 6},
)
# pylint: disable=protected-access
self.assertEqual(tracer._instrumentation_scope._name, "module_name")
# pylint: disable=protected-access
self.assertEqual(
tracer._instrumentation_scope._version, "library_version"
)
# pylint: disable=protected-access
self.assertEqual(
tracer._instrumentation_scope._schema_url, "schema_url"
)
# pylint: disable=protected-access
self.assertEqual(
tracer._instrumentation_scope._attributes,
{"key1": "value1", "key2": 6},
)

@mock.patch.dict("os.environ", {OTEL_SDK_DISABLED: "true"})
def test_get_tracer_with_sdk_disabled(self):
tracer_provider = trace.TracerProvider()
Expand Down