Skip to content

Commit ad81e4b

Browse files
committed
Add a DependencyConflictError Exception To Allow For Instrumentation To Stop When Dependcies Are Not Installed
1 parent ec3c51d commit ad81e4b

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/_load.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from opentelemetry.instrumentation.dependencies import (
2020
get_dist_dependency_conflicts,
21+
DependencyConflictError
2122
)
2223
from opentelemetry.instrumentation.distro import BaseDistro, DefaultDistro
2324
from opentelemetry.instrumentation.environment_variables import (
@@ -100,19 +101,15 @@ def _load_instrumentors(distro):
100101
continue
101102

102103
try:
103-
entry_point_dist = entry_point_finder.dist_for(entry_point)
104-
conflict = get_dist_dependency_conflicts(entry_point_dist)
105-
if conflict:
106-
_logger.debug(
107-
"Skipping instrumentation %s: %s",
108-
entry_point.name,
109-
conflict,
110-
)
111-
continue
112-
113-
# tell instrumentation to not run dep checks again as we already did it above
114-
distro.load_instrumentor(entry_point, skip_dep_check=True)
104+
distro.load_instrumentor(entry_point)
115105
_logger.debug("Instrumented %s", entry_point.name)
106+
except DependencyConflictError as exc:
107+
_logger.debug(
108+
"Skipping instrumentation %s: %s",
109+
entry_point.name,
110+
exc.conflict,
111+
)
112+
continue
116113
except ImportError:
117114
# in scenarios using the kubernetes operator to do autoinstrumentation some
118115
# instrumentors (usually requiring binary extensions) may fail to load

opentelemetry-instrumentation/src/opentelemetry/instrumentation/dependencies.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ def __init__(self, required: str | None, found: str | None = None):
3838

3939
def __str__(self):
4040
return f'DependencyConflict: requested: "{self.required}" but found: "{self.found}"'
41+
42+
class DependencyConflictError(Exception):
43+
def __init__(self, conflict: DependencyConflict):
44+
self.conflict = conflict
45+
46+
def __str__(self):
47+
return str(self.conflict)
4148

4249

4350
def get_dist_dependency_conflicts(

opentelemetry-instrumentation/src/opentelemetry/instrumentation/instrumentor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
)
2929
from opentelemetry.instrumentation.dependencies import (
3030
DependencyConflict,
31+
DependencyConflictError,
3132
get_dependency_conflicts,
3233
)
3334

@@ -108,7 +109,7 @@ def instrument(self, **kwargs: Any):
108109
conflict = self._check_dependency_conflicts()
109110
if conflict:
110111
_LOG.error(conflict)
111-
return None
112+
raise DependencyConflictError(conflict)
112113

113114
# initialize semantic conventions opt-in if needed
114115
_OpenTelemetrySemanticConventionStability._initialize()

0 commit comments

Comments
 (0)