-
Notifications
You must be signed in to change notification settings - Fork 542
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
Move opentelemetry-instrumentation from core #465
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4610f3e
add instr
lzchen 77c2413
tox
lzchen 5c6dea5
Update tox.ini
lzchen 2bd1195
Update test.yml
lzchen b7e2fea
Update test.yml
lzchen d50bebd
Update tox.ini
lzchen 3af088a
Update tox.ini
lzchen 2bfc78a
tox
lzchen 438678e
tox
lzchen bcd337f
benchmark
lzchen db1ddb6
Update test.yml
lzchen c348c75
fix docs
lzchen d099dad
lint
lzchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
OpenTelemetry Python Instrumentor | ||
================================= | ||
|
||
.. automodule:: opentelemetry.instrumentation | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
Submodules | ||
---------- | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
|
||
instrumentor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
opentelemetry.instrumentation.instrumentor package | ||
================================================== | ||
|
||
.. automodule:: opentelemetry.instrumentation.instrumentor | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
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: | ||
|
||
|
||
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/>`_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = cncf-opentelemetry-contributors@lists.cncf.io | ||
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.2.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 = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__"], | ||
) |
109 changes: 109 additions & 0 deletions
109
...emetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
py39 is not part of ci. would you mind adding it to env list https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/.github/workflows/test.yml#L13?
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.
Added this in a separate PR #466