instrumentation.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Copyright The OpenTelemetry Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from json import dumps
  15. from typing import Optional
  16. from deprecated import deprecated
  17. from opentelemetry.attributes import BoundedAttributes
  18. from opentelemetry.util.types import Attributes
  19. class InstrumentationInfo:
  20. """Immutable information about an instrumentation library module.
  21. See `opentelemetry.trace.TracerProvider.get_tracer` for the meaning of these
  22. properties.
  23. """
  24. __slots__ = ("_name", "_version", "_schema_url")
  25. @deprecated(version="1.11.1", reason="You should use InstrumentationScope")
  26. def __init__(
  27. self,
  28. name: str,
  29. version: Optional[str] = None,
  30. schema_url: Optional[str] = None,
  31. ):
  32. self._name = name
  33. self._version = version
  34. if schema_url is None:
  35. schema_url = ""
  36. self._schema_url = schema_url
  37. def __repr__(self):
  38. return f"{type(self).__name__}({self._name}, {self._version}, {self._schema_url})"
  39. def __hash__(self):
  40. return hash((self._name, self._version, self._schema_url))
  41. def __eq__(self, value):
  42. return type(value) is type(self) and (
  43. self._name,
  44. self._version,
  45. self._schema_url,
  46. ) == (value._name, value._version, value._schema_url)
  47. def __lt__(self, value):
  48. if type(value) is not type(self):
  49. return NotImplemented
  50. return (self._name, self._version, self._schema_url) < (
  51. value._name,
  52. value._version,
  53. value._schema_url,
  54. )
  55. @property
  56. def schema_url(self) -> Optional[str]:
  57. return self._schema_url
  58. @property
  59. def version(self) -> Optional[str]:
  60. return self._version
  61. @property
  62. def name(self) -> str:
  63. return self._name
  64. class InstrumentationScope:
  65. """A logical unit of the application code with which the emitted telemetry can be
  66. associated.
  67. See `opentelemetry.trace.TracerProvider.get_tracer` for the meaning of these
  68. properties.
  69. """
  70. __slots__ = ("_name", "_version", "_schema_url", "_attributes")
  71. def __init__(
  72. self,
  73. name: str,
  74. version: Optional[str] = None,
  75. schema_url: Optional[str] = None,
  76. attributes: Optional[Attributes] = None,
  77. ) -> None:
  78. self._name = name
  79. self._version = version
  80. if schema_url is None:
  81. schema_url = ""
  82. self._schema_url = schema_url
  83. self._attributes = BoundedAttributes(attributes=attributes)
  84. def __repr__(self) -> str:
  85. return f"{type(self).__name__}({self._name}, {self._version}, {self._schema_url}, {self._attributes})"
  86. def __hash__(self) -> int:
  87. return hash((self._name, self._version, self._schema_url))
  88. def __eq__(self, value: object) -> bool:
  89. if not isinstance(value, InstrumentationScope):
  90. return NotImplemented
  91. return (
  92. self._name,
  93. self._version,
  94. self._schema_url,
  95. self._attributes,
  96. ) == (
  97. value._name,
  98. value._version,
  99. value._schema_url,
  100. value._attributes,
  101. )
  102. def __lt__(self, value: object) -> bool:
  103. if not isinstance(value, InstrumentationScope):
  104. return NotImplemented
  105. return (
  106. self._name,
  107. self._version,
  108. self._schema_url,
  109. self._attributes,
  110. ) < (
  111. value._name,
  112. value._version,
  113. value._schema_url,
  114. value._attributes,
  115. )
  116. @property
  117. def schema_url(self) -> Optional[str]:
  118. return self._schema_url
  119. @property
  120. def version(self) -> Optional[str]:
  121. return self._version
  122. @property
  123. def name(self) -> str:
  124. return self._name
  125. @property
  126. def attributes(self) -> Attributes:
  127. return self._attributes
  128. def to_json(self, indent: Optional[int] = 4) -> str:
  129. return dumps(
  130. {
  131. "name": self._name,
  132. "version": self._version,
  133. "schema_url": self._schema_url,
  134. "attributes": (
  135. dict(self._attributes) if bool(self._attributes) else None
  136. ),
  137. },
  138. indent=indent,
  139. )