|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright The OpenTelemetry Authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import argparse |
| 18 | +import pkgutil |
| 19 | +import subprocess |
| 20 | +import sys |
| 21 | +from logging import getLogger |
| 22 | + |
| 23 | +logger = getLogger(__file__) |
| 24 | + |
| 25 | + |
| 26 | +# target library to desired instrumentor path/versioned package name |
| 27 | +instrumentations = { |
| 28 | + "dbapi": "opentelemetry-ext-dbapi>=0.8.8", |
| 29 | + "django": "opentelemetry-ext-django>=0.8.8", |
| 30 | + "flask": "opentelemetry-ext-flask>=0.8.8", |
| 31 | + "grpc": "opentelemetry-ext-grpc>=0.8.8", |
| 32 | + "requests": "opentelemetry-ext-requests>=0.8.8", |
| 33 | + "jinja2": "opentelemetry-ext-jinja2>=0.8.8", |
| 34 | + "mysql": "opentelemetry-ext-mysql>=0.8.8", |
| 35 | + "psycopg2": "opentelemetry-ext-psycopg2>=0.8.8", |
| 36 | + "pymongo": "opentelemetry-ext-pymongo>=0.8.8", |
| 37 | + "pymysql": "opentelemetry-ext-pymysql>=0.8.8", |
| 38 | + "redis": "opentelemetry-ext-redis>=0.8.8", |
| 39 | + "sqlalchemy": "opentelemetry-ext-sqlalchemy>=0.8.8", |
| 40 | + "wsgi": "opentelemetry-ext-wsgi>=0.8.8", |
| 41 | +} |
| 42 | + |
| 43 | +# relevant instrumentors and tracers to uninstall and check for conflicts for target libraries |
| 44 | +libraries = { |
| 45 | + "dbapi": ("opentelemetry-ext-dbapi",), |
| 46 | + "django": ("opentelemetry-ext-django",), |
| 47 | + "flask": ("opentelemetry-ext-flask",), |
| 48 | + "grpc": ("opentelemetry-ext-grpc",), |
| 49 | + "requests": ("opentelemetry-ext-requests",), |
| 50 | + "jinja2": ("opentelemetry-ext-jinja2",), |
| 51 | + "mysql": ("opentelemetry-ext-mysql",), |
| 52 | + "psycopg2": ("opentelemetry-ext-psycopg2",), |
| 53 | + "pymongo": ("opentelemetry-ext-pymongo",), |
| 54 | + "pymysql": ("opentelemetry-ext-pymysql",), |
| 55 | + "redis": ("opentelemetry-ext-redis",), |
| 56 | + "sqlalchemy": ("opentelemetry-ext-sqlalchemy",), |
| 57 | + "wsgi": ("opentelemetry-ext-wsgi",), |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +def _install_package(library, instrumentation): |
| 62 | + """ |
| 63 | + Ensures that desired version is installed w/o upgrading its dependencies |
| 64 | + by uninstalling where necessary (if `target` is not provided). |
| 65 | +
|
| 66 | +
|
| 67 | + OpenTelemetry auto-instrumentation packages often have traced libraries |
| 68 | + as instrumentation dependency (e.g. flask for opentelemetry-ext-flask), |
| 69 | + so using -I on library could cause likely undesired Flask upgrade. |
| 70 | + Using --no-dependencies alone would leave potential for nonfunctional |
| 71 | + installations. |
| 72 | + """ |
| 73 | + pip_list = _sys_pip_freeze() |
| 74 | + for package in libraries[library]: |
| 75 | + if "{}==".format(package).lower() in pip_list: |
| 76 | + logger.info( |
| 77 | + "Existing %s installation detected. Uninstalling.", package |
| 78 | + ) |
| 79 | + _sys_pip_uninstall(package) |
| 80 | + _sys_pip_install(instrumentation) |
| 81 | + |
| 82 | + |
| 83 | +def _syscall(func): |
| 84 | + def wrapper(package=None): |
| 85 | + try: |
| 86 | + if package: |
| 87 | + return func(package) |
| 88 | + return func() |
| 89 | + except subprocess.SubprocessError as exp: |
| 90 | + cmd = getattr(exp, "cmd", None) |
| 91 | + if cmd: |
| 92 | + msg = 'Error calling system command "{0}"'.format( |
| 93 | + " ".join(cmd) |
| 94 | + ) |
| 95 | + if package: |
| 96 | + msg = '{0} for package "{1}"'.format(msg, package) |
| 97 | + raise RuntimeError(msg) |
| 98 | + |
| 99 | + return wrapper |
| 100 | + |
| 101 | + |
| 102 | +@_syscall |
| 103 | +def _sys_pip_freeze(): |
| 104 | + return ( |
| 105 | + subprocess.check_output([sys.executable, "-m", "pip", "freeze"]) |
| 106 | + .decode() |
| 107 | + .lower() |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +@_syscall |
| 112 | +def _sys_pip_install(package): |
| 113 | + # explicit upgrade strategy to override potential pip config |
| 114 | + subprocess.check_call( |
| 115 | + [ |
| 116 | + sys.executable, |
| 117 | + "-m", |
| 118 | + "pip", |
| 119 | + "install", |
| 120 | + "-U", |
| 121 | + "--upgrade-strategy", |
| 122 | + "only-if-needed", |
| 123 | + package, |
| 124 | + ] |
| 125 | + ) |
| 126 | + |
| 127 | + |
| 128 | +@_syscall |
| 129 | +def _sys_pip_uninstall(package): |
| 130 | + subprocess.check_call( |
| 131 | + [sys.executable, "-m", "pip", "uninstall", "-y", package] |
| 132 | + ) |
| 133 | + |
| 134 | + |
| 135 | +def _pip_check(): |
| 136 | + """Ensures none of the instrumentations have dependency conflicts. |
| 137 | + Clean check reported as: |
| 138 | + 'No broken requirements found.' |
| 139 | + Dependency conflicts are reported as: |
| 140 | + 'opentelemetry-ext-flask 1.0.1 has requirement opentelemetry-sdk<2.0,>=1.0, but you have opentelemetry-sdk 0.5.' |
| 141 | + To not be too restrictive, we'll only check for relevant packages. |
| 142 | + """ |
| 143 | + check_pipe = subprocess.Popen( |
| 144 | + [sys.executable, "-m", "pip", "check"], stdout=subprocess.PIPE |
| 145 | + ) |
| 146 | + pip_check = check_pipe.communicate()[0].decode() |
| 147 | + pip_check_lower = pip_check.lower() |
| 148 | + for package_tup in libraries.values(): |
| 149 | + for package in package_tup: |
| 150 | + if package.lower() in pip_check_lower: |
| 151 | + raise RuntimeError( |
| 152 | + "Dependency conflict found: {}".format(pip_check) |
| 153 | + ) |
| 154 | + |
| 155 | + |
| 156 | +def _is_installed(library): |
| 157 | + return library in sys.modules or pkgutil.find_loader(library) is not None |
| 158 | + |
| 159 | + |
| 160 | +def _find_installed_libraries(): |
| 161 | + return {k: v for k, v in instrumentations.items() if _is_installed(k)} |
| 162 | + |
| 163 | + |
| 164 | +def _run_requirements(packages): |
| 165 | + print("\n".join(packages.values()), end="") |
| 166 | + |
| 167 | + |
| 168 | +def _run_install(packages): |
| 169 | + for pkg, inst in packages.items(): |
| 170 | + _install_package(pkg, inst) |
| 171 | + |
| 172 | + _pip_check() |
| 173 | + |
| 174 | + |
| 175 | +def run() -> None: |
| 176 | + action_install = "install" |
| 177 | + action_requirements = "requirements" |
| 178 | + |
| 179 | + parser = argparse.ArgumentParser( |
| 180 | + description=""" |
| 181 | + opentelemetry-bootstrap detects installed libraries and automatically |
| 182 | + installs the relevant instrumentation packages for them. |
| 183 | + """ |
| 184 | + ) |
| 185 | + parser.add_argument( |
| 186 | + "-a", |
| 187 | + "--action", |
| 188 | + choices=[action_install, action_requirements], |
| 189 | + default=action_requirements, |
| 190 | + help=""" |
| 191 | + install - uses pip to install the new requirements using to the |
| 192 | + currently active site-package. |
| 193 | + requirements - prints out the new requirements to stdout. Action can |
| 194 | + be piped and appended to a requirements.txt file. |
| 195 | + """, |
| 196 | + ) |
| 197 | + args = parser.parse_args() |
| 198 | + |
| 199 | + cmd = { |
| 200 | + action_install: _run_install, |
| 201 | + action_requirements: _run_requirements, |
| 202 | + }[args.action] |
| 203 | + cmd(_find_installed_libraries()) |
0 commit comments