Skip to content

Commit

Permalink
hfjsakkdhfskdjhfsjakdh
Browse files Browse the repository at this point in the history
  • Loading branch information
rjduffner committed Jan 22, 2025
1 parent ad81e4b commit c174b80
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from functools import cached_property
from logging import getLogger
from os import environ

from opentelemetry.instrumentation.dependencies import (
get_dist_dependency_conflicts,
DependencyConflictError
)
from opentelemetry.instrumentation.dependencies import DependencyConflictError
from opentelemetry.instrumentation.distro import BaseDistro, DefaultDistro
from opentelemetry.instrumentation.environment_variables import (
OTEL_PYTHON_CONFIGURATOR,
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS,
OTEL_PYTHON_DISTRO,
)
from opentelemetry.instrumentation.version import __version__
from opentelemetry.util._importlib_metadata import (
EntryPoint,
distributions,
entry_points,
)
from opentelemetry.util._importlib_metadata import entry_points

_logger = getLogger(__name__)


class _EntryPointDistFinder:
@cached_property
def _mapping(self):
return {
self._key_for(ep): dist
for dist in distributions()
for ep in dist.entry_points
}

def dist_for(self, entry_point: EntryPoint):
dist = getattr(entry_point, "dist", None)
if dist:
return dist

return self._mapping.get(self._key_for(entry_point))

@staticmethod
def _key_for(entry_point: EntryPoint):
return f"{entry_point.group}:{entry_point.name}:{entry_point.value}"


def _load_distro() -> BaseDistro:
distro_name = environ.get(OTEL_PYTHON_DISTRO, None)
for entry_point in entry_points(group="opentelemetry_distro"):
Expand Down Expand Up @@ -84,7 +55,6 @@ def _load_distro() -> BaseDistro:

def _load_instrumentors(distro):
package_to_exclude = environ.get(OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, [])
entry_point_finder = _EntryPointDistFinder()
if isinstance(package_to_exclude, str):
package_to_exclude = package_to_exclude.split(",")
# to handle users entering "requests , flask" or "requests, flask" with spaces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def __init__(self, required: str | None, found: str | None = None):

def __str__(self):
return f'DependencyConflict: requested: "{self.required}" but found: "{self.found}"'



class DependencyConflictError(Exception):
def __init__(self, conflict: DependencyConflict):
self.conflict = conflict
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
OTEL_PYTHON_DISTRO,
)
from opentelemetry.instrumentation.version import __version__
from opentelemetry.util._importlib_metadata import EntryPoint, entry_points


class TestLoad(TestCase):
Expand Down Expand Up @@ -379,31 +378,3 @@ def test_load_instrumentors_no_entry_point_mocks(self):
_load._load_instrumentors(distro_mock)
# this has no specific assert because it is run for every instrumentation
self.assertTrue(distro_mock)

def test_entry_point_dist_finder(self):
entry_point_finder = _load._EntryPointDistFinder()
self.assertTrue(entry_point_finder._mapping)
entry_point = list(
entry_points(group="opentelemetry_environment_variables")
)[0]
self.assertTrue(entry_point)
self.assertTrue(entry_point.dist)

# this will not hit cache
entry_point_dist = entry_point_finder.dist_for(entry_point)
self.assertTrue(entry_point_dist)
# dist are not comparable so we are sure we are not hitting the cache
self.assertEqual(entry_point.dist, entry_point_dist)

# this will hit cache
entry_point_without_dist = EntryPoint(
name=entry_point.name,
group=entry_point.group,
value=entry_point.value,
)
self.assertIsNone(entry_point_without_dist.dist)
new_entry_point_dist = entry_point_finder.dist_for(
entry_point_without_dist
)
# dist are not comparable, being truthy is enough
self.assertTrue(new_entry_point_dist)

0 comments on commit c174b80

Please sign in to comment.