-
Notifications
You must be signed in to change notification settings - Fork 657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add opentelemetry-instrumentation #1959
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,9 @@ sphinx-jekyll-builder | |
# doesn't work for pkg_resources. | ||
./opentelemetry-api | ||
./opentelemetry-semantic-conventions | ||
./opentelemetry-python-contrib/opentelemetry-instrumentation | ||
./opentelemetry-instrumentation | ||
./opentelemetry-sdk | ||
./opentelemetry-instrumentation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably need to delete this line? |
||
|
||
# Required by instrumentation and exporter packages | ||
ddtrace>=0.34.0 | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,6 +23,11 @@ | |||||
|
||||||
settings.configure() | ||||||
|
||||||
|
||||||
source_dirs = [ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this is what this was called before but we should change the name:
Suggested change
|
||||||
os.path.abspath("../opentelemetry-instrumentation/src/"), | ||||||
] | ||||||
|
||||||
exp = "../exporter" | ||||||
exp_dirs = [ | ||||||
os.path.abspath("/".join(["../exporter", f, "src"])) | ||||||
|
@@ -37,7 +42,7 @@ | |||||
if isdir(join(shim, f)) | ||||||
] | ||||||
|
||||||
sys.path[:0] = exp_dirs + shim_dirs | ||||||
sys.path[:0] = source_dirs + exp_dirs + shim_dirs | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
# -- Project information ----------------------------------------------------- | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ ignore= | |
sortfirst= | ||
opentelemetry-api | ||
opentelemetry-sdk | ||
opentelemetry-instrumentation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this go above |
||
opentelemetry-proto | ||
opentelemetry-distro | ||
tests/util | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
prune tests | ||
graft src | ||
global-exclude *.pyc | ||
global-exclude *.pyo | ||
global-exclude __pycache__/* | ||
include MANIFEST.in | ||
include README.rst |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
OpenTelemetry Instrumentation | ||
============================= | ||
|
||
|pypi| | ||
|
||
.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation.svg | ||
:target: https://pypi.org/project/opentelemetry-instrumentation/ | ||
|
||
Installation | ||
------------ | ||
|
||
:: | ||
|
||
pip install opentelemetry-instrumentation | ||
|
||
|
||
This package provides a couple of commands that help automatically instruments a program: | ||
|
||
.. note:: | ||
You need to install a distro package to get auto instrumentation working. The ``opentelemetry-distro`` | ||
package contains the default distro and automatically configures some of the common options for users. | ||
For more info about ``opentelemetry-distro`` check `here <https://opentelemetry-python.readthedocs.io/en/latest/examples/distro/README.html>`__ | ||
:: | ||
|
||
pip install opentelemetry-distro[otlp] | ||
|
||
|
||
opentelemetry-bootstrap | ||
----------------------- | ||
|
||
:: | ||
|
||
opentelemetry-bootstrap --action=install|requirements | ||
|
||
This commands inspects the active Python site-packages and figures out which | ||
instrumentation packages the user might want to install. By default it prints out | ||
a list of the suggested instrumentation packages which can be added to a requirements.txt | ||
file. It also supports installing the suggested packages when run with :code:`--action=install` | ||
flag. | ||
|
||
|
||
opentelemetry-instrument | ||
------------------------ | ||
|
||
:: | ||
|
||
opentelemetry-instrument python program.py | ||
|
||
The instrument command will try to automatically detect packages used by your python program | ||
and when possible, apply automatic tracing instrumentation on them. This means your program | ||
will get automatic distributed tracing for free without having to make any code changes | ||
at all. This will also configure a global tracer and tracing exporter without you having to | ||
make any code changes. By default, the instrument command will use the OTLP exporter but | ||
this can be overriden when needed. | ||
|
||
The command supports the following configuration options as CLI arguments and environment vars: | ||
|
||
|
||
* ``--trace-exporter`` or ``OTEL_TRACE_EXPORTER`` | ||
|
||
Used to specify which trace exporter to use. Can be set to one or more of the well-known exporter | ||
names (see below). | ||
|
||
- Defaults to `otlp`. | ||
- Can be set to `none` to disable automatic tracer initialization. | ||
|
||
You can pass multiple values to configure multiple exporters e.g, ``zipkin,prometheus`` | ||
|
||
Well known trace exporter names: | ||
|
||
- jaeger | ||
- opencensus | ||
- otlp | ||
- otlp_proto_grpc_span | ||
- zipkin | ||
|
||
``otlp`` is an alias for ``otlp_proto_grpc_span``. | ||
|
||
* ``--id-generator`` or ``OTEL_PYTHON_ID_GENERATOR`` | ||
|
||
Used to specify which IDs Generator to use for the global Tracer Provider. By default, it | ||
will use the random IDs generator. | ||
|
||
The code in ``program.py`` needs to use one of the packages for which there is | ||
an OpenTelemetry integration. For a list of the available integrations please | ||
check `here <https://opentelemetry-python.readthedocs.io/en/stable/index.html#integrations>`_ | ||
|
||
* ``OTEL_PYTHON_DISABLED_INSTRUMENTATIONS`` | ||
|
||
If set by the user, opentelemetry-instrument will read this environment variable to disable specific instrumentations. | ||
e.g OTEL_PYTHON_DISABLED_INSTRUMENTATIONS = "requests,django" | ||
|
||
|
||
Examples | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
:: | ||
|
||
opentelemetry-instrument --trace-exporter otlp flask run --port=3000 | ||
|
||
The above command will pass ``--trace-exporter otlp`` to the instrument command and ``--port=3000`` to ``flask run``. | ||
|
||
:: | ||
|
||
opentelemetry-instrument --trace-exporter zipkin,otlp celery -A tasks worker --loglevel=info | ||
|
||
The above command will configure global trace provider, attach zipkin and otlp exporters to it and then | ||
start celery with the rest of the arguments. | ||
|
||
:: | ||
|
||
opentelemetry-instrument --ids-generator random flask run --port=3000 | ||
|
||
The above command will configure the global trace provider to use the Random IDs Generator, and then | ||
pass ``--port=3000`` to ``flask run``. | ||
|
||
References | ||
---------- | ||
|
||
* `OpenTelemetry Project <https://opentelemetry.io/>`_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# 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. | ||
# | ||
[metadata] | ||
name = opentelemetry-instrumentation | ||
description = Instrumentation Tools & Auto Instrumentation for OpenTelemetry Python | ||
long_description = file: README.rst | ||
long_description_content_type = text/x-rst | ||
author = OpenTelemetry Authors | ||
author_email = [email protected] | ||
url = https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/opentelemetry-instrumentation | ||
platforms = any | ||
license = Apache-2.0 | ||
classifiers = | ||
Development Status :: 4 - Beta | ||
Intended Audience :: Developers | ||
License :: OSI Approved :: Apache Software License | ||
Programming Language :: Python | ||
Programming Language :: Python :: 3 | ||
Programming Language :: Python :: 3.6 | ||
Programming Language :: Python :: 3.7 | ||
Programming Language :: Python :: 3.8 | ||
Programming Language :: Python :: 3.9 | ||
|
||
[options] | ||
python_requires = >=3.6 | ||
package_dir= | ||
=src | ||
packages=find_namespace: | ||
zip_safe = False | ||
include_package_data = True | ||
install_requires = | ||
opentelemetry-api == 1.4.0.dev0 | ||
wrapt >= 1.0.0, < 2.0.0 | ||
|
||
[options.packages.find] | ||
where = src | ||
|
||
[options.entry_points] | ||
console_scripts = | ||
opentelemetry-instrument = opentelemetry.instrumentation.auto_instrumentation:run | ||
opentelemetry-bootstrap = opentelemetry.instrumentation.bootstrap:run | ||
|
||
[options.extras_require] | ||
test = |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# 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 os | ||
|
||
import setuptools | ||
|
||
BASE_DIR = os.path.dirname(__file__) | ||
VERSION_FILENAME = os.path.join( | ||
BASE_DIR, "src", "opentelemetry", "instrumentation", "version.py" | ||
) | ||
PACKAGE_INFO = {} | ||
with open(VERSION_FILENAME) as f: | ||
exec(f.read(), PACKAGE_INFO) | ||
|
||
setuptools.setup( | ||
version=PACKAGE_INFO["__version__"], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# 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 | ||
from logging import getLogger | ||
from os import environ, execl, getcwd | ||
from os.path import abspath, dirname, pathsep | ||
from shutil import which | ||
|
||
from opentelemetry.environment_variables import ( | ||
OTEL_PYTHON_ID_GENERATOR, | ||
OTEL_TRACES_EXPORTER, | ||
) | ||
|
||
logger = getLogger(__file__) | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser( | ||
description=""" | ||
opentelemetry-instrument automatically instruments a Python | ||
program and it's dependencies and then runs the program. | ||
""" | ||
) | ||
|
||
parser.add_argument( | ||
"--trace-exporter", | ||
required=False, | ||
help=""" | ||
Uses the specified exporter to export spans. | ||
Accepts multiple exporters as comma separated values. | ||
Examples: | ||
--trace-exporter=jaeger | ||
""", | ||
) | ||
|
||
parser.add_argument( | ||
"--id-generator", | ||
required=False, | ||
help=""" | ||
The IDs Generator to be used with the Tracer Provider. | ||
Examples: | ||
--id-generator=random | ||
""", | ||
) | ||
|
||
parser.add_argument("command", help="Your Python application.") | ||
parser.add_argument( | ||
"command_args", | ||
help="Arguments for your application.", | ||
nargs=argparse.REMAINDER, | ||
) | ||
return parser.parse_args() | ||
|
||
|
||
def load_config_from_cli_args(args): | ||
if args.trace_exporter: | ||
environ[OTEL_TRACES_EXPORTER] = args.trace_exporter | ||
if args.id_generator: | ||
environ[OTEL_PYTHON_ID_GENERATOR] = args.id_generator | ||
|
||
|
||
def run() -> None: | ||
args = parse_args() | ||
load_config_from_cli_args(args) | ||
|
||
python_path = environ.get("PYTHONPATH") | ||
|
||
if not python_path: | ||
python_path = [] | ||
|
||
else: | ||
python_path = python_path.split(pathsep) | ||
|
||
cwd_path = getcwd() | ||
|
||
# This is being added to support applications that are being run from their | ||
# own executable, like Django. | ||
# FIXME investigate if there is another way to achieve this | ||
if cwd_path not in python_path: | ||
python_path.insert(0, cwd_path) | ||
|
||
filedir_path = dirname(abspath(__file__)) | ||
|
||
python_path = [path for path in python_path if path != filedir_path] | ||
|
||
python_path.insert(0, filedir_path) | ||
|
||
environ["PYTHONPATH"] = pathsep.join(python_path) | ||
|
||
executable = which(args.command) | ||
execl(executable, executable, *args.command_args) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.