Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 82a5be2

Browse files
authored
add schema_url to TracerProvider.get_tracer (open-telemetry#2154)
* add schema_url to TracerProvider.get_tracer * update CHANGELOG * fix CHANGELOG * modify tests and optional parameter in InstrumentationInfo * fixed syntax * update instrumentation_library_version typed Optional * resolve syntax error * fix lint failures * resolve lint failure
1 parent b59e914 commit 82a5be2

File tree

7 files changed

+75
-25
lines changed

7 files changed

+75
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2828
([#2147](https://github.com/open-telemetry/opentelemetry-python/pull/2147))
2929
- Fix validity calculation for trace and span IDs
3030
([#2145](https://github.com/open-telemetry/opentelemetry-python/pull/2145))
31+
- Add `schema_url` to `TracerProvider.get_tracer`
32+
([#2154](https://github.com/open-telemetry/opentelemetry-python/pull/2154))
3133

3234
## [1.5.0-0.24b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.5.0-0.24b0) - 2021-08-26
3335

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575

7676

7777
import os
78+
import typing
7879
from abc import ABC, abstractmethod
7980
from contextlib import contextmanager
8081
from enum import Enum
@@ -186,7 +187,8 @@ class TracerProvider(ABC):
186187
def get_tracer(
187188
self,
188189
instrumenting_module_name: str,
189-
instrumenting_library_version: str = "",
190+
instrumenting_library_version: typing.Optional[str] = None,
191+
schema_url: typing.Optional[str] = None,
190192
) -> "Tracer":
191193
"""Returns a `Tracer` for use by the given instrumentation library.
192194
@@ -208,6 +210,8 @@ def get_tracer(
208210
instrumenting_library_version: Optional. The version string of the
209211
instrumenting library. Usually this should be the same as
210212
``pkg_resources.get_distribution(instrumenting_library_name).version``.
213+
214+
schema_url: Optional. Specifies the Schema URL of the emitted telemetry.
211215
"""
212216

213217

@@ -220,7 +224,8 @@ class _DefaultTracerProvider(TracerProvider):
220224
def get_tracer(
221225
self,
222226
instrumenting_module_name: str,
223-
instrumenting_library_version: str = "",
227+
instrumenting_library_version: typing.Optional[str] = None,
228+
schema_url: typing.Optional[str] = None,
224229
) -> "Tracer":
225230
# pylint:disable=no-self-use,unused-argument
226231
return _DefaultTracer()
@@ -230,14 +235,19 @@ class ProxyTracerProvider(TracerProvider):
230235
def get_tracer(
231236
self,
232237
instrumenting_module_name: str,
233-
instrumenting_library_version: str = "",
238+
instrumenting_library_version: typing.Optional[str] = None,
239+
schema_url: typing.Optional[str] = None,
234240
) -> "Tracer":
235241
if _TRACER_PROVIDER:
236242
return _TRACER_PROVIDER.get_tracer(
237-
instrumenting_module_name, instrumenting_library_version
243+
instrumenting_module_name,
244+
instrumenting_library_version,
245+
schema_url,
238246
)
239247
return ProxyTracer(
240-
instrumenting_module_name, instrumenting_library_version
248+
instrumenting_module_name,
249+
instrumenting_library_version,
250+
schema_url,
241251
)
242252

243253

@@ -375,10 +385,12 @@ class ProxyTracer(Tracer):
375385
def __init__(
376386
self,
377387
instrumenting_module_name: str,
378-
instrumenting_library_version: str,
388+
instrumenting_library_version: typing.Optional[str] = None,
389+
schema_url: typing.Optional[str] = None,
379390
):
380391
self._instrumenting_module_name = instrumenting_module_name
381392
self._instrumenting_library_version = instrumenting_library_version
393+
self._schema_url = schema_url
382394
self._real_tracer: Optional[Tracer] = None
383395
self._noop_tracer = _DefaultTracer()
384396

@@ -391,6 +403,7 @@ def _tracer(self) -> Tracer:
391403
self._real_tracer = _TRACER_PROVIDER.get_tracer(
392404
self._instrumenting_module_name,
393405
self._instrumenting_library_version,
406+
self._schema_url,
394407
)
395408
return self._real_tracer
396409
return self._noop_tracer
@@ -445,8 +458,9 @@ def start_as_current_span(
445458

446459
def get_tracer(
447460
instrumenting_module_name: str,
448-
instrumenting_library_version: str = "",
461+
instrumenting_library_version: typing.Optional[str] = None,
449462
tracer_provider: Optional[TracerProvider] = None,
463+
schema_url: typing.Optional[str] = None,
450464
) -> "Tracer":
451465
"""Returns a `Tracer` for use by the given instrumentation library.
452466
@@ -458,7 +472,7 @@ def get_tracer(
458472
if tracer_provider is None:
459473
tracer_provider = get_tracer_provider()
460474
return tracer_provider.get_tracer(
461-
instrumenting_module_name, instrumenting_library_version
475+
instrumenting_module_name, instrumenting_library_version, schema_url
462476
)
463477

464478

opentelemetry-api/tests/trace/test_globals.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ def tearDown(self) -> None:
3636
def test_get_tracer(self):
3737
"""trace.get_tracer should proxy to the global tracer provider."""
3838
trace.get_tracer("foo", "var")
39-
self._mock_tracer_provider.get_tracer.assert_called_with("foo", "var")
39+
self._mock_tracer_provider.get_tracer.assert_called_with(
40+
"foo", "var", None
41+
)
4042
mock_provider = unittest.mock.Mock()
4143
trace.get_tracer("foo", "var", mock_provider)
42-
mock_provider.get_tracer.assert_called_with("foo", "var")
44+
mock_provider.get_tracer.assert_called_with("foo", "var", None)
4345

4446

4547
class TestTracer(unittest.TestCase):

opentelemetry-api/tests/trace/test_proxy.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
# pylint: disable=W0212,W0222,W0221
16-
16+
import typing
1717
import unittest
1818

1919
from opentelemetry import trace
@@ -24,7 +24,8 @@ class TestProvider(trace._DefaultTracerProvider):
2424
def get_tracer(
2525
self,
2626
instrumenting_module_name: str,
27-
instrumenting_library_version: str = "",
27+
instrumenting_library_version: typing.Optional[str] = None,
28+
schema_url: typing.Optional[str] = None,
2829
) -> trace.Tracer:
2930
return TestTracer()
3031

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import logging
2121
import threading
2222
import traceback
23+
import typing
2324
from collections import OrderedDict
2425
from contextlib import contextmanager
2526
from os import environ
@@ -1099,18 +1100,23 @@ def resource(self) -> Resource:
10991100
def get_tracer(
11001101
self,
11011102
instrumenting_module_name: str,
1102-
instrumenting_library_version: str = "",
1103+
instrumenting_library_version: typing.Optional[str] = None,
1104+
schema_url: typing.Optional[str] = None,
11031105
) -> "trace_api.Tracer":
11041106
if not instrumenting_module_name: # Reject empty strings too.
11051107
instrumenting_module_name = ""
11061108
logger.error("get_tracer called with missing module name.")
1109+
if instrumenting_library_version is None:
1110+
instrumenting_library_version = ""
11071111
return Tracer(
11081112
self.sampler,
11091113
self.resource,
11101114
self._active_span_processor,
11111115
self.id_generator,
11121116
InstrumentationInfo(
1113-
instrumenting_module_name, instrumenting_library_version
1117+
instrumenting_module_name,
1118+
instrumenting_library_version,
1119+
schema_url,
11141120
),
11151121
self._span_limits,
11161122
)

opentelemetry-sdk/src/opentelemetry/sdk/util/instrumentation.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import typing
1415

1516

1617
class InstrumentationInfo:
@@ -20,31 +21,50 @@ class InstrumentationInfo:
2021
properties.
2122
"""
2223

23-
__slots__ = ("_name", "_version")
24+
__slots__ = ("_name", "_version", "_schema_url")
2425

25-
def __init__(self, name: str, version: str):
26+
def __init__(
27+
self,
28+
name: str,
29+
version: typing.Optional[str] = None,
30+
schema_url: typing.Optional[str] = None,
31+
):
2632
self._name = name
2733
self._version = version
34+
self._schema_url = schema_url
2835

2936
def __repr__(self):
30-
return f"{type(self).__name__}({self._name}, {self._version})"
37+
return f"{type(self).__name__}({self._name}, {self._version}, {self._schema_url})"
3138

3239
def __hash__(self):
33-
return hash((self._name, self._version))
40+
return hash((self._name, self._version, self._schema_url))
3441

3542
def __eq__(self, value):
36-
return type(value) is type(self) and (self._name, self._version) == (
37-
value._name,
38-
value._version,
43+
return (
44+
type(value) is type(self)
45+
and (
46+
self._name,
47+
self._version,
48+
self._schema_url,
49+
)
50+
== (value._name, value._version, value._schema_url)
3951
)
4052

4153
def __lt__(self, value):
4254
if type(value) is not type(self):
4355
return NotImplemented
44-
return (self._name, self._version) < (value._name, value._version)
56+
return (self._name, self._version, self._schema_url) < (
57+
value._name,
58+
value._version,
59+
value._schema_url,
60+
)
61+
62+
@property
63+
def schema_url(self) -> typing.Optional[str]:
64+
return self._schema_url
4565

4666
@property
47-
def version(self) -> str:
67+
def version(self) -> typing.Optional[str]:
4868
return self._version
4969

5070
@property

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,20 @@ def test_start_span_invalid_spancontext(self):
237237

238238
def test_instrumentation_info(self):
239239
tracer_provider = trace.TracerProvider()
240+
schema_url = "https://opentelemetry.io/schemas/1.3.0"
240241
tracer1 = tracer_provider.get_tracer("instr1")
241-
tracer2 = tracer_provider.get_tracer("instr2", "1.3b3")
242+
tracer2 = tracer_provider.get_tracer("instr2", "1.3b3", schema_url)
242243
span1 = tracer1.start_span("s1")
243244
span2 = tracer2.start_span("s2")
244245
self.assertEqual(
245246
span1.instrumentation_info, InstrumentationInfo("instr1", "")
246247
)
247248
self.assertEqual(
248-
span2.instrumentation_info, InstrumentationInfo("instr2", "1.3b3")
249+
span2.instrumentation_info,
250+
InstrumentationInfo("instr2", "1.3b3", schema_url),
249251
)
250252

253+
self.assertEqual(span2.instrumentation_info.schema_url, schema_url)
251254
self.assertEqual(span2.instrumentation_info.version, "1.3b3")
252255
self.assertEqual(span2.instrumentation_info.name, "instr2")
253256

@@ -267,6 +270,7 @@ def test_invalid_instrumentation_info(self):
267270
)
268271
span1 = tracer1.start_span("foo")
269272
self.assertTrue(span1.is_recording())
273+
self.assertEqual(tracer1.instrumentation_info.schema_url, None)
270274
self.assertEqual(tracer1.instrumentation_info.version, "")
271275
self.assertEqual(tracer1.instrumentation_info.name, "")
272276

@@ -275,6 +279,7 @@ def test_invalid_instrumentation_info(self):
275279
)
276280
span2 = tracer2.start_span("bar")
277281
self.assertTrue(span2.is_recording())
282+
self.assertEqual(tracer2.instrumentation_info.schema_url, None)
278283
self.assertEqual(tracer2.instrumentation_info.version, "")
279284
self.assertEqual(tracer2.instrumentation_info.name, "")
280285

0 commit comments

Comments
 (0)