Skip to content

Commit d4e13bd

Browse files
optional scope attribute for tracer creation (#4028)
1 parent e78675e commit d4e13bd

File tree

6 files changed

+48
-3
lines changed

6 files changed

+48
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- optional scope attribute for tracer creation
11+
([#4028](https://github.com/open-telemetry/opentelemetry-python/pull/4028))
1012
- OTLP exporter is encoding invalid span/trace IDs in the logs fix
1113
([#4006](https://github.com/open-telemetry/opentelemetry-python/pull/4006))
1214
- Update sdk process resource detector `process.command_args` attribute to also include the executable itself

opentelemetry-api/src/opentelemetry/trace/__init__.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ def get_tracer(
189189
instrumenting_module_name: str,
190190
instrumenting_library_version: typing.Optional[str] = None,
191191
schema_url: typing.Optional[str] = None,
192+
attributes: typing.Optional[types.Attributes] = None,
192193
) -> "Tracer":
193194
"""Returns a `Tracer` for use by the given instrumentation library.
194195
@@ -216,6 +217,7 @@ def get_tracer(
216217
``importlib.metadata.version(instrumenting_library_name)``.
217218
218219
schema_url: Optional. Specifies the Schema URL of the emitted telemetry.
220+
attributes: Optional. Specifies the attributes of the emitted telemetry.
219221
"""
220222

221223

@@ -230,6 +232,7 @@ def get_tracer(
230232
instrumenting_module_name: str,
231233
instrumenting_library_version: typing.Optional[str] = None,
232234
schema_url: typing.Optional[str] = None,
235+
attributes: typing.Optional[types.Attributes] = None,
233236
) -> "Tracer":
234237
# pylint:disable=no-self-use,unused-argument
235238
return NoOpTracer()
@@ -249,17 +252,20 @@ def get_tracer(
249252
instrumenting_module_name: str,
250253
instrumenting_library_version: typing.Optional[str] = None,
251254
schema_url: typing.Optional[str] = None,
255+
attributes: typing.Optional[types.Attributes] = None,
252256
) -> "Tracer":
253257
if _TRACER_PROVIDER:
254258
return _TRACER_PROVIDER.get_tracer(
255259
instrumenting_module_name,
256260
instrumenting_library_version,
257261
schema_url,
262+
attributes,
258263
)
259264
return ProxyTracer(
260265
instrumenting_module_name,
261266
instrumenting_library_version,
262267
schema_url,
268+
attributes,
263269
)
264270

265271

@@ -407,10 +413,12 @@ def __init__(
407413
instrumenting_module_name: str,
408414
instrumenting_library_version: typing.Optional[str] = None,
409415
schema_url: typing.Optional[str] = None,
416+
attributes: typing.Optional[types.Attributes] = None,
410417
):
411418
self._instrumenting_module_name = instrumenting_module_name
412419
self._instrumenting_library_version = instrumenting_library_version
413420
self._schema_url = schema_url
421+
self._attributes = attributes
414422
self._real_tracer: Optional[Tracer] = None
415423
self._noop_tracer = NoOpTracer()
416424

@@ -424,6 +432,7 @@ def _tracer(self) -> Tracer:
424432
self._instrumenting_module_name,
425433
self._instrumenting_library_version,
426434
self._schema_url,
435+
self._attributes,
427436
)
428437
return self._real_tracer
429438
return self._noop_tracer
@@ -492,6 +501,7 @@ def get_tracer(
492501
instrumenting_library_version: typing.Optional[str] = None,
493502
tracer_provider: Optional[TracerProvider] = None,
494503
schema_url: typing.Optional[str] = None,
504+
attributes: typing.Optional[types.Attributes] = None,
495505
) -> "Tracer":
496506
"""Returns a `Tracer` for use by the given instrumentation library.
497507
@@ -503,7 +513,10 @@ def get_tracer(
503513
if tracer_provider is None:
504514
tracer_provider = get_tracer_provider()
505515
return tracer_provider.get_tracer(
506-
instrumenting_module_name, instrumenting_library_version, schema_url
516+
instrumenting_module_name,
517+
instrumenting_library_version,
518+
schema_url,
519+
attributes,
507520
)
508521

509522

opentelemetry-api/tests/trace/test_globals.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ class TestGlobals(TraceGlobalsTest, unittest.TestCase):
3333
def test_get_tracer(mock_tracer_provider): # type: ignore
3434
"""trace.get_tracer should proxy to the global tracer provider."""
3535
trace.get_tracer("foo", "var")
36-
mock_tracer_provider.get_tracer.assert_called_with("foo", "var", None)
36+
mock_tracer_provider.get_tracer.assert_called_with(
37+
"foo", "var", None, None
38+
)
3739
mock_provider = Mock()
3840
trace.get_tracer("foo", "var", mock_provider)
39-
mock_provider.get_tracer.assert_called_with("foo", "var", None)
41+
mock_provider.get_tracer.assert_called_with("foo", "var", None, None)
4042

4143

4244
class TestGlobalsConcurrency(TraceGlobalsTest, ConcurrencyTestBase):

opentelemetry-api/tests/trace/test_proxy.py

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
Span,
2525
)
2626
from opentelemetry.util._decorator import _agnosticcontextmanager
27+
from opentelemetry.util.types import Attributes
2728

2829

2930
class TestProvider(trace.NoOpTracerProvider):
@@ -32,6 +33,7 @@ def get_tracer(
3233
instrumenting_module_name: str,
3334
instrumenting_library_version: typing.Optional[str] = None,
3435
schema_url: typing.Optional[str] = None,
36+
attributes: typing.Optional[Attributes] = None,
3537
) -> trace.Tracer:
3638
return TestTracer()
3739

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,7 @@ def get_tracer(
12301230
instrumenting_module_name: str,
12311231
instrumenting_library_version: typing.Optional[str] = None,
12321232
schema_url: typing.Optional[str] = None,
1233+
attributes: typing.Optional[types.Attributes] = None,
12331234
) -> "trace_api.Tracer":
12341235
if self._disabled:
12351236
logger.warning("SDK is disabled.")
@@ -1267,6 +1268,7 @@ def get_tracer(
12671268
instrumenting_module_name,
12681269
instrumenting_library_version,
12691270
schema_url,
1271+
attributes,
12701272
),
12711273
)
12721274

opentelemetry-sdk/tests/trace/test_trace.py

+24
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,30 @@ def test_tracer_provider_accepts_concurrent_multi_span_processor(self):
165165
span_processor, tracer_provider._active_span_processor
166166
)
167167

168+
def test_get_tracer_sdk(self):
169+
tracer_provider = trace.TracerProvider()
170+
tracer = tracer_provider.get_tracer(
171+
"module_name",
172+
"library_version",
173+
"schema_url",
174+
{"key1": "value1", "key2": 6},
175+
)
176+
# pylint: disable=protected-access
177+
self.assertEqual(tracer._instrumentation_scope._name, "module_name")
178+
# pylint: disable=protected-access
179+
self.assertEqual(
180+
tracer._instrumentation_scope._version, "library_version"
181+
)
182+
# pylint: disable=protected-access
183+
self.assertEqual(
184+
tracer._instrumentation_scope._schema_url, "schema_url"
185+
)
186+
# pylint: disable=protected-access
187+
self.assertEqual(
188+
tracer._instrumentation_scope._attributes,
189+
{"key1": "value1", "key2": 6},
190+
)
191+
168192
@mock.patch.dict("os.environ", {OTEL_SDK_DISABLED: "true"})
169193
def test_get_tracer_with_sdk_disabled(self):
170194
tracer_provider = trace.TracerProvider()

0 commit comments

Comments
 (0)