-
Notifications
You must be signed in to change notification settings - Fork 599
/
bootstrap.py
186 lines (158 loc) · 5.3 KB
/
bootstrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import logging
import sys
from subprocess import (
PIPE,
CalledProcessError,
Popen,
SubprocessError,
check_call,
)
from typing import Optional
from packaging.requirements import Requirement
from opentelemetry.instrumentation.bootstrap_gen import (
default_instrumentations as gen_default_instrumentations,
)
from opentelemetry.instrumentation.bootstrap_gen import (
libraries as gen_libraries,
)
from opentelemetry.instrumentation.version import __version__
from opentelemetry.util._importlib_metadata import (
PackageNotFoundError,
version,
)
logger = logging.getLogger(__name__)
def _syscall(func):
def wrapper(package=None):
try:
if package:
return func(package)
return func()
except SubprocessError as exp:
cmd = getattr(exp, "cmd", None)
if cmd:
msg = f'Error calling system command "{" ".join(cmd)}"'
if package:
msg = f'{msg} for package "{package}"'
raise RuntimeError(msg)
return wrapper
@_syscall
def _sys_pip_install(package):
# explicit upgrade strategy to override potential pip config
try:
check_call(
[
sys.executable,
"-m",
"pip",
"install",
"-U",
"--upgrade-strategy",
"only-if-needed",
package,
]
)
except CalledProcessError as error:
print(error)
def _pip_check(libraries):
"""Ensures none of the instrumentations have dependency conflicts.
Clean check reported as:
'No broken requirements found.'
Dependency conflicts are reported as:
'opentelemetry-instrumentation-flask 1.0.1 has requirement opentelemetry-sdk<2.0,>=1.0, but you have opentelemetry-sdk 0.5.'
To not be too restrictive, we'll only check for relevant packages.
"""
with Popen(
[sys.executable, "-m", "pip", "check"], stdout=PIPE
) as check_pipe:
pip_check = check_pipe.communicate()[0].decode()
pip_check_lower = pip_check.lower()
for package_tup in libraries:
for package in package_tup:
if package.lower() in pip_check_lower:
raise RuntimeError(f"Dependency conflict found: {pip_check}")
def _is_installed(req):
req = Requirement(req)
try:
dist_version = version(req.name)
except PackageNotFoundError:
return False
if not req.specifier.filter(dist_version):
logger.warning(
"instrumentation for package %s is available"
" but version %s is installed. Skipping.",
req,
dist_version,
)
return False
return True
def _find_installed_libraries(default_instrumentations, libraries):
for lib in default_instrumentations:
yield lib
for lib in libraries:
if _is_installed(lib["library"]):
yield lib["instrumentation"]
def _run_requirements(default_instrumentations, libraries):
logger.setLevel(logging.ERROR)
print(
"\n".join(
_find_installed_libraries(default_instrumentations, libraries)
)
)
def _run_install(default_instrumentations, libraries):
for lib in _find_installed_libraries(default_instrumentations, libraries):
_sys_pip_install(lib)
_pip_check(libraries)
def run(
default_instrumentations: Optional[list] = None,
libraries: Optional[list] = None,
) -> None:
action_install = "install"
action_requirements = "requirements"
parser = argparse.ArgumentParser(
description="""
opentelemetry-bootstrap detects installed libraries and automatically
installs the relevant instrumentation packages for them.
"""
)
parser.add_argument(
"--version",
help="print version information",
action="version",
version="%(prog)s " + __version__,
)
parser.add_argument(
"-a",
"--action",
choices=[action_install, action_requirements],
default=action_requirements,
help="""
install - uses pip to install the new requirements using to the
currently active site-package.
requirements - prints out the new requirements to stdout. Action can
be piped and appended to a requirements.txt file.
""",
)
args = parser.parse_args()
if libraries is None:
libraries = gen_libraries
if default_instrumentations is None:
default_instrumentations = gen_default_instrumentations
cmd = {
action_install: _run_install,
action_requirements: _run_requirements,
}[args.action]
cmd(default_instrumentations, libraries)