Skip to content

Commit d0f1dac

Browse files
committed
clean _load, sitecustomize, dependencies
1 parent 65823a3 commit d0f1dac

File tree

3 files changed

+19
-91
lines changed

3 files changed

+19
-91
lines changed

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

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from os import environ
1818

1919
from opentelemetry.instrumentation.dependencies import (
20+
DependencyConflictError,
2021
get_dist_dependency_conflicts,
2122
)
2223
from opentelemetry.instrumentation.distro import BaseDistro, DefaultDistro
@@ -93,7 +94,6 @@ def _load_instrumentors(distro):
9394
entry_point.load()()
9495

9596
for entry_point in entry_points(group="opentelemetry_instrumentor"):
96-
print(entry_point.name)
9797
if entry_point.name in package_to_exclude:
9898
_logger.debug(
9999
"Instrumentation skipped for library %s", entry_point.name
@@ -104,7 +104,6 @@ def _load_instrumentors(distro):
104104
entry_point_dist = entry_point_finder.dist_for(entry_point)
105105
conflict = get_dist_dependency_conflicts(entry_point_dist)
106106
if conflict:
107-
# print("conflict found %s: %s" % (entry_point.name, conflict))
108107
_logger.debug(
109108
"Skipping instrumentation %s: %s",
110109
entry_point.name,
@@ -115,41 +114,29 @@ def _load_instrumentors(distro):
115114
# tell instrumentation to not run dep checks again as we already did it above
116115
distro.load_instrumentor(entry_point, skip_dep_check=True)
117116
_logger.debug("Instrumented %s", entry_point.name)
118-
# print("Instrumented %s" % entry_point.name)
119-
# except DependencyConflictError as exc:
120-
# print(
121-
# "Skipping instrumentation %s: %s" % (
122-
# entry_point.name,
123-
# exc.conflict,
124-
# )
125-
# )
126-
# _logger.debug(
127-
# "Skipping instrumentation %s: %s",
128-
# entry_point.name,
129-
# exc.conflict,
130-
# )
131-
# continue
132-
# except ModuleNotFoundError as exc:
133-
# # ModuleNotFoundError is raised when the library is not installed
134-
# # and the instrumentation is not required to be loaded.
135-
# # See https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3421
136-
# print(
137-
# "Skipping instrumentation %s: %s" % (entry_point.name, exc.msg)
138-
# )
139-
# _logger.debug(
140-
# "Skipping instrumentation %s: %s", entry_point.name, exc.msg
141-
# )
142-
# continue
117+
except DependencyConflictError as exc:
118+
# DependencyConflicts will generally arise without exception from the get_dist_dependency_conflicts call before instrumentation is attempted. Keeping this exception in case of custom distro and instrumentor behavior. See https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3610 for details.
119+
_logger.debug(
120+
"Skipping instrumentation %s: %s",
121+
entry_point.name,
122+
exc.conflict,
123+
)
124+
continue
125+
except ModuleNotFoundError as exc:
126+
# ModuleNotFoundError is raised when the library is not installed
127+
# and the instrumentation is not required to be loaded.
128+
# See https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3421
129+
_logger.debug(
130+
"Skipping instrumentation %s: %s", entry_point.name, exc.msg
131+
)
132+
continue
143133
except ImportError:
144134
# in scenarios using the kubernetes operator to do autoinstrumentation some
145135
# instrumentors (usually requiring binary extensions) may fail to load
146136
# because the injected autoinstrumentation code does not match the application
147137
# environment regarding python version, libc, etc... In this case it's better
148138
# to skip the single instrumentation rather than failing to load everything
149139
# so treat differently ImportError than the rest of exceptions
150-
# print(
151-
# "Skipping instrumentation %s: ImportError" % entry_point.name
152-
# )
153140
_logger.exception(
154141
"Importing of %s failed, skipping it", entry_point.name
155142
)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,3 @@
1515
from opentelemetry.instrumentation.auto_instrumentation import initialize
1616

1717
initialize()
18-
print("OpenTelemetry auto-instrumentation initialized.")

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

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
logger = getLogger(__name__)
2929

30-
# TODO: consider replacing _either with _any or _or
31-
3230

3331
class DependencyConflict:
3432
required: str | None = None
@@ -37,13 +35,10 @@ class DependencyConflict:
3735
required_either: Collection[str] = []
3836
found_either: Collection[str] = []
3937

40-
# TODO: No longer requires required field
4138
def __init__(
4239
self,
4340
required: str | None = None,
4441
found: str | None = None,
45-
# TODO: dangerous
46-
# TODO: dangerous
4742
required_either: Collection[str] = None,
4843
found_either: Collection[str] = None,
4944
):
@@ -55,27 +50,10 @@ def __init__(
5550

5651
def __str__(self):
5752
if not self.required and (self.required_either or self.found_either):
58-
print("EITHER STRING")
59-
# TODO: make sure this formats correctly
6053
return f'DependencyConflict: requested any of the following: "{self.required_either}" but found: "{self.found_either}"'
61-
print("AND STRING")
6254
return f'DependencyConflict: requested: "{self.required}" but found: "{self.found}"'
6355

6456

65-
# TODO: Figure out if this should be a subclass of DependencyConflict.
66-
# If now, change functions to return either and then ensure that all the dependents can handle the new value
67-
# class DependencyConflictEither(DependencyConflict):
68-
# required: Collection[str] = []
69-
# found: Collection[str] = []
70-
71-
# def __init__(self, required: Collection[str], found: Collection[str] = []):
72-
# self.required = required
73-
# self.found = found
74-
75-
# def __str__(self):
76-
# return f'DependencyConflictEither: requested: "{self.required}" but found: "{self.found}"'
77-
78-
7957
class DependencyConflictError(Exception):
8058
conflict: DependencyConflict
8159

@@ -96,59 +74,36 @@ def get_dist_dependency_conflicts(
9674
instruments_marker = {extra: instruments}
9775
instruments_either = "instruments_either"
9876
instruments_either_marker = {extra: instruments_either}
99-
# print(f"dist: {dist}")
100-
# print(f"dist.requires: {dist.requires}")
10177
if dist.requires:
10278
for dep in dist.requires:
103-
print(f"dep: {dep}")
10479
if extra not in dep:
105-
print(f"Skipping dep: {dep}")
10680
continue
10781
if instruments not in dep and instruments_either not in dep:
108-
print(f"Skipping dep: {dep}")
10982
continue
11083

11184
req = Requirement(dep)
112-
# print(f"req: {req}")
11385
if req.marker.evaluate(instruments_marker): # type: ignore
114-
# print("Evaluated. Append")
11586
instrumentation_deps.append(req) # type: ignore
11687
if req.marker.evaluate(instruments_either_marker): # type: ignore
117-
# print("Evaluated. either. Append")
118-
# Need someway to separate
11988
instrumentation_either_deps.append(req) # type: ignore
120-
dc = get_dependency_conflicts(
89+
return get_dependency_conflicts(
12190
instrumentation_deps, instrumentation_either_deps
12291
) # type: ignore
123-
print(f"dep conf: {dc}")
124-
return dc
125-
# return get_dependency_conflicts(instrumentation_deps, instrumentation_either_deps) # type: ignore
12692

12793

12894
def get_dependency_conflicts(
12995
deps: Collection[
13096
str | Requirement
131-
], # Dependenciesall of which are required
97+
], # Dependencies all of which are required
13298
deps_either: Collection[
13399
str | Requirement
134-
# TODO: dangerous
135100
] = None, # Dependencies any of which are required
136101
) -> DependencyConflict | None:
137-
# instruments_conflict = get_dependency_conflicts()
138-
# if instruments_conflict:
139-
# return instruments_conflict
140-
141-
# instruments_either_conflict = get_dependency_conflicts_either()
142-
# return instruments_either_conflict
143-
144102
for dep in deps:
145-
# TODO: what is this?
146103
if isinstance(dep, Requirement):
147-
print("REQUIREMENT")
148104
req = dep
149105
else:
150106
try:
151-
print("NOT REQUIREMENT")
152107
req = Requirement(dep)
153108
except InvalidRequirement as exc:
154109
logger.warning(
@@ -161,14 +116,11 @@ def get_dependency_conflicts(
161116
try:
162117
dist_version = version(req.name)
163118
except PackageNotFoundError:
164-
# TODO: Technically this field should allow Requirements type. Tackle this in a separate PR.
165119
return DependencyConflict(dep)
166120

167121
if not req.specifier.contains(dist_version):
168-
# TODO: Technically this field should allow Requirements type
169122
return DependencyConflict(dep, f"{req.name} {dist_version}")
170123

171-
# TODO: add eval of deps_either
172124
# If all the dependencies in "instruments" are present, check "instruments_either" for conflicts.
173125
if deps_either:
174126
return _get_dependency_conflicts_either(deps_either)
@@ -181,18 +133,14 @@ def _get_dependency_conflicts_either(
181133
) -> DependencyConflict | None:
182134
if not deps_either:
183135
return None
184-
# TODO: change to using DependencyConflict
185136
is_dependency_conflict = True
186137
required_either: Collection[str] = []
187138
found_either: Collection[str] = []
188139
for dep in deps_either:
189-
# TODO: what is this?
190140
if isinstance(dep, Requirement):
191-
print("REQUIREMENT")
192141
req = dep
193142
else:
194143
try:
195-
print("NOT REQUIREMENT")
196144
req = Requirement(dep)
197145
except InvalidRequirement as exc:
198146
logger.warning(
@@ -205,9 +153,6 @@ def _get_dependency_conflicts_either(
205153
try:
206154
dist_version = version(req.name)
207155
except PackageNotFoundError:
208-
# print(f"PackageNotFoundError EITHER: {req.name}")
209-
# TODO: anything here?
210-
# return DependencyConflict(dep)
211156
required_either.append(str(dep))
212157
continue
213158

@@ -220,9 +165,6 @@ def _get_dependency_conflicts_either(
220165
found_either.append(f"{req.name} {dist_version}")
221166

222167
if is_dependency_conflict:
223-
# return DependencyConflict(dep, f"{req.name} {dist_version}")
224-
# print (f"required_either: {required_either}")
225-
# print (f"found_either: {found_either}")
226168
return DependencyConflict(
227169
required_either=required_either,
228170
found_either=found_either,

0 commit comments

Comments
 (0)