Skip to content

Commit 3ad63e5

Browse files
committed
stash. Strings look good. No tests. Need to remove prints
1 parent 4dca66f commit 3ad63e5

File tree

4 files changed

+98
-26
lines changed

4 files changed

+98
-26
lines changed

instrumentation/opentelemetry-instrumentation-fastapi/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ dependencies = [
3535
[project.optional-dependencies]
3636
instruments = [
3737
"fastapi ~= 0.92",
38+
"fastapi-slim ~= 0.92",
3839
]
3940
instruments_either = [
4041
"fastapi ~= 0.92",

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

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,50 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from functools import cached_property
1516
from logging import getLogger
1617
from os import environ
1718

18-
from opentelemetry.instrumentation.dependencies import DependencyConflictError
19+
from opentelemetry.instrumentation.dependencies import (
20+
get_dist_dependency_conflicts,
21+
)
1922
from opentelemetry.instrumentation.distro import BaseDistro, DefaultDistro
2023
from opentelemetry.instrumentation.environment_variables import (
2124
OTEL_PYTHON_CONFIGURATOR,
2225
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS,
2326
OTEL_PYTHON_DISTRO,
2427
)
2528
from opentelemetry.instrumentation.version import __version__
26-
from opentelemetry.util._importlib_metadata import entry_points
29+
from opentelemetry.util._importlib_metadata import (
30+
EntryPoint,
31+
distributions,
32+
entry_points,
33+
)
2734

2835
_logger = getLogger(__name__)
2936

3037

38+
class _EntryPointDistFinder:
39+
@cached_property
40+
def _mapping(self):
41+
return {
42+
self._key_for(ep): dist
43+
for dist in distributions()
44+
for ep in dist.entry_points
45+
}
46+
47+
def dist_for(self, entry_point: EntryPoint):
48+
dist = getattr(entry_point, "dist", None)
49+
if dist:
50+
return dist
51+
52+
return self._mapping.get(self._key_for(entry_point))
53+
54+
@staticmethod
55+
def _key_for(entry_point: EntryPoint):
56+
return f"{entry_point.group}:{entry_point.name}:{entry_point.value}"
57+
58+
3159
def _load_distro() -> BaseDistro:
3260
distro_name = environ.get(OTEL_PYTHON_DISTRO, None)
3361
for entry_point in entry_points(group="opentelemetry_distro"):
@@ -55,6 +83,7 @@ def _load_distro() -> BaseDistro:
5583

5684
def _load_instrumentors(distro):
5785
package_to_exclude = environ.get(OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, [])
86+
entry_point_finder = _EntryPointDistFinder()
5887
if isinstance(package_to_exclude, str):
5988
package_to_exclude = package_to_exclude.split(",")
6089
# to handle users entering "requests , flask" or "requests, flask" with spaces
@@ -64,44 +93,77 @@ def _load_instrumentors(distro):
6493
entry_point.load()()
6594

6695
for entry_point in entry_points(group="opentelemetry_instrumentor"):
96+
print(entry_point.name)
6797
if entry_point.name in package_to_exclude:
6898
_logger.debug(
6999
"Instrumentation skipped for library %s", entry_point.name
70100
)
71101
continue
72102

73103
try:
104+
entry_point_dist = entry_point_finder.dist_for(entry_point)
105+
conflict = get_dist_dependency_conflicts(entry_point_dist)
106+
if conflict:
107+
print(
108+
"conflict found %s: %s" % (
109+
entry_point.name, conflict
110+
)
111+
)
112+
_logger.debug(
113+
"Skipping instrumentation %s: %s",
114+
entry_point.name,
115+
conflict,
116+
)
117+
continue
118+
119+
# tell instrumentation to not run dep checks again as we already did it above
74120
distro.load_instrumentor(
75121
entry_point, raise_exception_on_conflict=True
76122
)
77123
_logger.debug("Instrumented %s", entry_point.name)
78-
except DependencyConflictError as exc:
79-
_logger.debug(
80-
"Skipping instrumentation %s: %s",
81-
entry_point.name,
82-
exc.conflict,
83-
)
84-
continue
85-
except ModuleNotFoundError as exc:
86-
# ModuleNotFoundError is raised when the library is not installed
87-
# and the instrumentation is not required to be loaded.
88-
# See https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3421
89-
_logger.debug(
90-
"Skipping instrumentation %s: %s", entry_point.name, exc.msg
91-
)
92-
continue
124+
print("Instrumented %s" % entry_point.name)
125+
# except DependencyConflictError as exc:
126+
# print(
127+
# "Skipping instrumentation %s: %s" % (
128+
# entry_point.name,
129+
# exc.conflict,
130+
# )
131+
# )
132+
# _logger.debug(
133+
# "Skipping instrumentation %s: %s",
134+
# entry_point.name,
135+
# exc.conflict,
136+
# )
137+
# continue
138+
# except ModuleNotFoundError as exc:
139+
# # ModuleNotFoundError is raised when the library is not installed
140+
# # and the instrumentation is not required to be loaded.
141+
# # See https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3421
142+
# print(
143+
# "Skipping instrumentation %s: %s" % (entry_point.name, exc.msg)
144+
# )
145+
# _logger.debug(
146+
# "Skipping instrumentation %s: %s", entry_point.name, exc.msg
147+
# )
148+
# continue
93149
except ImportError:
94150
# in scenarios using the kubernetes operator to do autoinstrumentation some
95151
# instrumentors (usually requiring binary extensions) may fail to load
96152
# because the injected autoinstrumentation code does not match the application
97153
# environment regarding python version, libc, etc... In this case it's better
98154
# to skip the single instrumentation rather than failing to load everything
99155
# so treat differently ImportError than the rest of exceptions
156+
print(
157+
"Skipping instrumentation %s: ImportError" % entry_point.name
158+
)
100159
_logger.exception(
101160
"Importing of %s failed, skipping it", entry_point.name
102161
)
103162
continue
104163
except Exception as exc: # pylint: disable=broad-except
164+
print(
165+
"Skipping instrumentation %s: %s" % (entry_point.name, exc)
166+
)
105167
_logger.exception("Instrumenting of %s failed", entry_point.name)
106168
raise exc
107169

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
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: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ def __init__(self, required: str | None = None, found: str | None = None, requir
4646

4747
def __str__(self):
4848
if not self.required and (self.required_either or self.found_either):
49+
print("EITHER STRING")
4950
# TODO: make sure this formats correctly
5051
return f'DependencyConflict: requested any of the following: "{self.required_either}" but found: "{self.found_either}"'
52+
print("AND STRING")
5153
return f'DependencyConflict: requested: "{self.required}" but found: "{self.found}"'
5254

5355
# TODO: Figure out if this should be a subclass of DependencyConflict.
@@ -84,8 +86,8 @@ def get_dist_dependency_conflicts(
8486
instruments_marker = {extra: instruments}
8587
instruments_either = "instruments_either"
8688
instruments_either_marker = {extra: instruments_either}
87-
print(f"dist: {dist}")
88-
print(f"dist.requires: {dist.requires}")
89+
# print(f"dist: {dist}")
90+
# print(f"dist.requires: {dist.requires}")
8991
if dist.requires:
9092
for dep in dist.requires:
9193
print(f"dep: {dep}")
@@ -97,12 +99,12 @@ def get_dist_dependency_conflicts(
9799
continue
98100

99101
req = Requirement(dep)
100-
print(f"req: {req}")
102+
# print(f"req: {req}")
101103
if req.marker.evaluate(instruments_marker): # type: ignore
102-
print("Evaluated. Append")
104+
# print("Evaluated. Append")
103105
instrumentation_deps.append(req) # type: ignore
104106
if req.marker.evaluate(instruments_either_marker): # type: ignore
105-
print("Evaluated. either. Append")
107+
# print("Evaluated. either. Append")
106108
# Need someway to separate
107109
instrumentation_either_deps.append(req) # type: ignore
108110
dc = get_dependency_conflicts(instrumentation_deps, instrumentation_either_deps) # type: ignore
@@ -118,9 +120,11 @@ def get_dependency_conflicts(
118120
for dep in deps:
119121
# TODO: what is this?
120122
if isinstance(dep, Requirement):
123+
print("REQUIREMENT")
121124
req = dep
122125
else:
123126
try:
127+
print("NOT REQUIREMENT")
124128
req = Requirement(dep)
125129
except InvalidRequirement as exc:
126130
logger.warning(
@@ -149,9 +153,11 @@ def get_dependency_conflicts(
149153
for dep in deps_either:
150154
# TODO: what is this?
151155
if isinstance(dep, Requirement):
156+
print("REQUIREMENT")
152157
req = dep
153158
else:
154159
try:
160+
print("NOT REQUIREMENT")
155161
req = Requirement(dep)
156162
except InvalidRequirement as exc:
157163
logger.warning(
@@ -164,21 +170,23 @@ def get_dependency_conflicts(
164170
try:
165171
dist_version = version(req.name)
166172
except PackageNotFoundError:
167-
print(f"PackageNotFoundError: {req.name}")
168-
continue
173+
# print(f"PackageNotFoundError EITHER: {req.name}")
169174
# TODO: anything here?
170175
# return DependencyConflict(dep)
171-
required_either.append(dep)
176+
required_either.append(str(dep))
177+
continue
172178

173179
if req.specifier.contains(dist_version):
174180
# is_dependency_conflict = False
175181
# Since only one of the instrumentation_either dependencies is required, there is no depdendency conflict.
176182
break
177183
else:
178-
required_either.append(dep)
184+
required_either.append(str(dep))
179185
found_either.append(f"{req.name} {dist_version}")
180186

181187
# return DependencyConflict(dep, f"{req.name} {dist_version}")
188+
# print (f"required_either: {required_either}")
189+
# print (f"found_either: {found_either}")
182190
return DependencyConflict(
183191
required_either=required_either,
184192
found_either=found_either,

0 commit comments

Comments
 (0)