Skip to content

Commit 2a97615

Browse files
committed
Refactored sitecustomize and added tests
1 parent edafb0a commit 2a97615

File tree

4 files changed

+426
-110
lines changed

4 files changed

+426
-110
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
15+
from logging import getLogger
16+
from os import environ
17+
18+
from pkg_resources import iter_entry_points
19+
20+
from opentelemetry.instrumentation.dependencies import (
21+
get_dist_dependency_conflicts,
22+
)
23+
from opentelemetry.instrumentation.distro import BaseDistro, DefaultDistro
24+
from opentelemetry.instrumentation.environment_variables import (
25+
OTEL_PYTHON_CONFIGURATOR,
26+
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS,
27+
OTEL_PYTHON_DISTRO,
28+
)
29+
from opentelemetry.instrumentation.version import __version__
30+
31+
logger = getLogger(__name__)
32+
33+
34+
def _load_distros() -> BaseDistro:
35+
distro_name = environ.get(OTEL_PYTHON_DISTRO, None)
36+
for entry_point in iter_entry_points("opentelemetry_distro"):
37+
try:
38+
if distro_name is None or distro_name == entry_point.name:
39+
distro = entry_point.load()()
40+
if not isinstance(distro, BaseDistro):
41+
logger.debug(
42+
"%s is not an OpenTelemetry Distro. Skipping",
43+
entry_point.name,
44+
)
45+
continue
46+
logger.debug(
47+
"Distribution %s will be configured", entry_point.name
48+
)
49+
return distro
50+
except Exception as exc: # pylint: disable=broad-except
51+
logger.exception(
52+
"Distribution %s configuration failed", entry_point.name
53+
)
54+
raise exc
55+
return DefaultDistro()
56+
57+
58+
def _load_instrumentors(distro):
59+
package_to_exclude = environ.get(OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, [])
60+
if isinstance(package_to_exclude, str):
61+
package_to_exclude = package_to_exclude.split(",")
62+
# to handle users entering "requests , flask" or "requests, flask" with spaces
63+
package_to_exclude = [x.strip() for x in package_to_exclude]
64+
65+
for entry_point in iter_entry_points("opentelemetry_pre_instrument"):
66+
entry_point.load()()
67+
68+
for entry_point in iter_entry_points("opentelemetry_instrumentor"):
69+
if entry_point.name in package_to_exclude:
70+
logger.debug(
71+
"Instrumentation skipped for library %s", entry_point.name
72+
)
73+
continue
74+
75+
try:
76+
conflict = get_dist_dependency_conflicts(entry_point.dist)
77+
if conflict:
78+
logger.debug(
79+
"Skipping instrumentation %s: %s",
80+
entry_point.name,
81+
conflict,
82+
)
83+
continue
84+
85+
# tell instrumentation to not run dep checks again as we already did it above
86+
distro.load_instrumentor(entry_point, skip_dep_check=True)
87+
logger.debug("Instrumented %s", entry_point.name)
88+
except Exception as exc: # pylint: disable=broad-except
89+
logger.exception("Instrumenting of %s failed", entry_point.name)
90+
raise exc
91+
92+
for entry_point in iter_entry_points("opentelemetry_post_instrument"):
93+
entry_point.load()()
94+
95+
96+
def _load_configurators():
97+
configurator_name = environ.get(OTEL_PYTHON_CONFIGURATOR, None)
98+
configured = None
99+
for entry_point in iter_entry_points("opentelemetry_configurator"):
100+
if configured is not None:
101+
logger.warning(
102+
"Configuration of %s not loaded, %s already loaded",
103+
entry_point.name,
104+
configured,
105+
)
106+
continue
107+
try:
108+
if (
109+
configurator_name is None
110+
or configurator_name == entry_point.name
111+
):
112+
entry_point.load()().configure(auto_instrumentation_version=__version__) # type: ignore
113+
configured = entry_point.name
114+
else:
115+
logger.warning(
116+
"Configuration of %s not loaded because %s is set by %s",
117+
entry_point.name,
118+
configurator_name,
119+
OTEL_PYTHON_CONFIGURATOR,
120+
)
121+
except Exception as exc: # pylint: disable=broad-except
122+
logger.exception("Configuration of %s failed", entry_point.name)
123+
raise exc

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

+4-110
Original file line numberDiff line numberDiff line change
@@ -16,122 +16,16 @@
1616
from os import environ
1717
from os.path import abspath, dirname, pathsep
1818

19-
from pkg_resources import iter_entry_points
20-
21-
from opentelemetry.instrumentation.dependencies import (
22-
get_dist_dependency_conflicts,
23-
)
24-
from opentelemetry.instrumentation.distro import BaseDistro, DefaultDistro
25-
from opentelemetry.instrumentation.environment_variables import (
26-
OTEL_PYTHON_CONFIGURATOR,
27-
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS,
28-
OTEL_PYTHON_DISTRO,
19+
from opentelemetry.instrumentation.auto_instrumentation._load import (
20+
_load_configurators,
21+
_load_distros,
22+
_load_instrumentors,
2923
)
3024
from opentelemetry.instrumentation.utils import _python_path_without_directory
31-
from opentelemetry.instrumentation.version import __version__
3225

3326
logger = getLogger(__name__)
3427

3528

36-
def _load_distros() -> BaseDistro:
37-
distro_name = environ.get(OTEL_PYTHON_DISTRO, None)
38-
for entry_point in iter_entry_points("opentelemetry_distro"):
39-
try:
40-
if distro_name is None or distro_name == entry_point.name:
41-
distro = entry_point.load()()
42-
if not isinstance(distro, BaseDistro):
43-
logger.debug(
44-
"%s is not an OpenTelemetry Distro. Skipping",
45-
entry_point.name,
46-
)
47-
continue
48-
logger.debug(
49-
"Distribution %s will be configured", entry_point.name
50-
)
51-
return distro
52-
else:
53-
logger.warning(
54-
"%s distro not loaded because %s is set by %s",
55-
entry_point.name,
56-
distro_name,
57-
OTEL_PYTHON_DISTRO,
58-
)
59-
except Exception as exc: # pylint: disable=broad-except
60-
logger.exception(
61-
"Distribution %s configuration failed", entry_point.name
62-
)
63-
raise exc
64-
return DefaultDistro()
65-
66-
67-
def _load_instrumentors(distro):
68-
package_to_exclude = environ.get(OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, [])
69-
if isinstance(package_to_exclude, str):
70-
package_to_exclude = package_to_exclude.split(",")
71-
# to handle users entering "requests , flask" or "requests, flask" with spaces
72-
package_to_exclude = [x.strip() for x in package_to_exclude]
73-
74-
for entry_point in iter_entry_points("opentelemetry_pre_instrument"):
75-
entry_point.load()()
76-
77-
for entry_point in iter_entry_points("opentelemetry_instrumentor"):
78-
if entry_point.name in package_to_exclude:
79-
logger.debug(
80-
"Instrumentation skipped for library %s", entry_point.name
81-
)
82-
continue
83-
84-
try:
85-
conflict = get_dist_dependency_conflicts(entry_point.dist)
86-
if conflict:
87-
logger.debug(
88-
"Skipping instrumentation %s: %s",
89-
entry_point.name,
90-
conflict,
91-
)
92-
continue
93-
94-
# tell instrumentation to not run dep checks again as we already did it above
95-
distro.load_instrumentor(entry_point, skip_dep_check=True)
96-
logger.debug("Instrumented %s", entry_point.name)
97-
except Exception as exc: # pylint: disable=broad-except
98-
logger.exception("Instrumenting of %s failed", entry_point.name)
99-
raise exc
100-
101-
for entry_point in iter_entry_points("opentelemetry_post_instrument"):
102-
entry_point.load()()
103-
104-
105-
def _load_configurators():
106-
configurator_name = environ.get(OTEL_PYTHON_CONFIGURATOR, None)
107-
configured = None
108-
for entry_point in iter_entry_points("opentelemetry_configurator"):
109-
if configured is not None:
110-
logger.warning(
111-
"Configuration of %s not loaded, %s already loaded",
112-
entry_point.name,
113-
configured,
114-
)
115-
continue
116-
try:
117-
if (
118-
configurator_name is None
119-
or configurator_name == entry_point.name
120-
):
121-
entry_point.load()().configure(auto_instrumentation_version=__version__) # type: ignore
122-
configured = entry_point.name
123-
else:
124-
logger.warning(
125-
"Configuration of %s not loaded because %s is set by %s",
126-
entry_point.name,
127-
configurator_name,
128-
OTEL_PYTHON_CONFIGURATOR,
129-
)
130-
except Exception as exc: # pylint: disable=broad-except
131-
logger.exception("Configuration of %s failed", entry_point.name)
132-
raise exc
133-
134-
13529
def initialize():
13630
# prevents auto-instrumentation of subprocesses if code execs another python process
13731
environ["PYTHONPATH"] = _python_path_without_directory(

0 commit comments

Comments
 (0)