Skip to content

Commit 4ce17ee

Browse files
author
Yuri Shkuro
committed
Initial commit
0 parents  commit 4ce17ee

25 files changed

+1426
-0
lines changed

.gitignore

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
*.py[cod]
2+
*.orig
3+
4+
# Ignore gen/ directory where we temporarily store thrift classes to help with development
5+
gen/
6+
7+
# No local dbs
8+
*.db
9+
10+
# C extensions
11+
*.so
12+
13+
# OS X Junk
14+
.DS_Store
15+
16+
# Packages
17+
*.egg
18+
*.egg-info
19+
20+
bin
21+
build
22+
develop-eggs
23+
dist
24+
eggs
25+
parts
26+
sdist
27+
var
28+
29+
.installed.cfg
30+
lib64
31+
__pycache__
32+
.cache/
33+
34+
# Installer logs
35+
pip-log.txt
36+
37+
# Unit test / coverage reports
38+
.coverage
39+
.coverage*
40+
.tox
41+
.noseids
42+
43+
# Translations
44+
*.mo
45+
46+
# Ignore python virtual environments
47+
env*
48+
thrift_env
49+
50+
# Ignore local logs
51+
*.log
52+
logs/*
53+
!logs/.gitkeep
54+
55+
# Ignore local log
56+
npm-debug.log
57+
58+
# Ignore docs
59+
docs/_build/*
60+
61+
# ignore ipython profile stuff
62+
config/profile_default/
63+
!config/profile_default/ipython_config.py
64+
config/README
65+
protobuf/*.py
66+
67+
.phutil_module_cache
68+
69+
# Ignore coverage output
70+
*.xml
71+
coverage/
72+
73+
# Ignore benchmarks output
74+
perf.log
75+
perf.svg
76+
77+
# vim
78+
*.swp
79+
.idea/

CHANGELOG.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.. :changelog:
2+
3+
History
4+
-------
5+
6+
0.1.0 (unreleased)
7+
------------------
8+
9+
- Initial version

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2015 Uber Technologies, Inc.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Makefile

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
project := opentracing_instrumentation
2+
3+
pytest := PYTHONDONTWRITEBYTECODE=1 py.test --tb short -rxs \
4+
--cov-config .coveragerc --cov $(project) tests
5+
6+
html_report := --cov-report=html
7+
test_args := --cov-report xml --cov-report term-missing
8+
9+
.PHONY: clean-pyc clean-build docs clean
10+
.DEFAULT_GOAL : help
11+
12+
help:
13+
@echo "bootstrap - initialize local environement for development. Requires virtualenv."
14+
@echo "clean - remove all build, test, coverage and Python artifacts"
15+
@echo "clean-build - remove build artifacts"
16+
@echo "clean-pyc - remove Python file artifacts"
17+
@echo "clean-test - remove test and coverage artifacts"
18+
@echo "lint - check style with flake8"
19+
@echo "test - run tests quickly with the default Python"
20+
@echo "coverage - check code coverage quickly with the default Python"
21+
@echo "docs - generate Sphinx HTML documentation, including API docs"
22+
@echo "release - package and upload a release"
23+
@echo "dist - package"
24+
@echo "install - install the package to the active Python's site-packages"
25+
26+
bootstrap:
27+
@[ -d env ] || echo "Please run 'virtualenv env' first"
28+
@[ -d env ] || exit 1
29+
pip install -r requirements.txt
30+
pip install -r requirements-test.txt
31+
python setup.py develop
32+
33+
clean: clean-build clean-pyc clean-test
34+
35+
clean-build:
36+
rm -fr build/
37+
rm -fr dist/
38+
rm -fr .eggs/
39+
find . -name '*.egg-info' -exec rm -fr {} +
40+
find . -name '*.egg' -exec rm -rf {} +
41+
42+
clean-pyc:
43+
find . -name '*.pyc' -exec rm -f {} +
44+
find . -name '*.pyo' -exec rm -f {} +
45+
find . -name '*~' -exec rm -f {} +
46+
find . -name '__pycache__' -exec rm -fr {} +
47+
48+
clean-test:
49+
rm -f .coverage
50+
rm -fr htmlcov/
51+
52+
lint:
53+
flake8 $(project) tests
54+
55+
test:
56+
$(pytest) $(test_args)
57+
58+
jenkins:
59+
pip install -r requirements.txt
60+
pip install -r requirements-test.txt
61+
python setup.py develop
62+
CLAY_CONFIG=config/test.yaml $(pytest) $(test_args) --junit-xml=jenkins.xml
63+
64+
coverage:
65+
coverage run --source $(project) setup.py test
66+
coverage report -m
67+
coverage html
68+
open htmlcov/index.html
69+
70+
docs:
71+
$(MAKE) -C docs clean
72+
$(MAKE) -C docs html
73+
74+
release: clean
75+
python setup.py sdist upload
76+
python setup.py bdist_wheel upload
77+
78+
dist: clean
79+
python setup.py sdist
80+
python setup.py bdist_wheel
81+
ls -l dist
82+
83+
install:
84+
pip install -r requirements.txt
85+
pip install -r requirements-test.txt
86+
echo skipping pip install -r requirements-doc.txt
87+
python setup.py install

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# opentracing-python-instrumentation
2+
3+
A collection of instrumentation tools to enable tracing with OpenTracing API.
4+
5+
##Install
6+
7+
```
8+
virtualenv env
9+
source env/bin/activate
10+
make bootstrap
11+
make test
12+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) 2015 Uber Technologies, Inc.
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
21+
from trace_context import get_current_span # noqa
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright (c) 2015 Uber Technologies, Inc.
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright (c) 2015 Uber Technologies, Inc.
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
21+
from __future__ import absolute_import
22+
23+
import functools
24+
25+
26+
def singleton(func):
27+
"""
28+
This decorator allows you to make sure that a function is called once and
29+
only once. Note that recursive functions will still work.
30+
31+
Not thread-safe.
32+
"""
33+
NOT_CALLED, IN_CALL, CALLED = range(3)
34+
35+
@functools.wraps(func)
36+
def wrapper(*args, **kwargs):
37+
if wrapper.__call_state__ == CALLED:
38+
return
39+
old_state, wrapper.__call_state__ = wrapper.__call_state__, IN_CALL
40+
ret = func(*args, **kwargs)
41+
if old_state == NOT_CALLED:
42+
wrapper.__call_state__ = CALLED
43+
return ret
44+
45+
wrapper.__call_state__ = NOT_CALLED
46+
# save original func to be able to patch and restore multiple times from
47+
# unit tests
48+
wrapper.__original_func = func
49+
return wrapper
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright (c) 2015 Uber Technologies, Inc.
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
21+
from __future__ import absolute_import
22+
23+
import logging
24+
25+
import opentracing
26+
from ..trace_context import get_current_span
27+
from ._singleton import singleton
28+
29+
log = logging.getLogger(__name__)
30+
31+
32+
@singleton
33+
def install_patches():
34+
try:
35+
from sqlalchemy.engine import Engine
36+
from sqlalchemy import event
37+
except ImportError:
38+
# If SQLAlchemy cannot be imported, then the project we are
39+
# instrumenting does not depend on it and we do not need to install
40+
# the SQL hooks.
41+
return
42+
43+
log.info('Instrumenting SQL methods for tracing')
44+
45+
@event.listens_for(Engine, 'before_cursor_execute')
46+
def before_cursor_execute(conn, cursor, statement, parameters, context,
47+
executemany):
48+
operation = 'SQL'
49+
statement = statement.strip()
50+
if statement:
51+
operation = '%s %s' % (operation,
52+
statement.split(' ', 1)[0].upper())
53+
if get_current_span() is None:
54+
span = opentracing.tracer.start_trace(
55+
operation_name=operation) # TODO pass client=True
56+
else:
57+
span = get_current_span().start_child(operation_name=operation)
58+
if statement:
59+
span.add_tag('sql', statement)
60+
context.opentracing_span = span
61+
62+
@event.listens_for(Engine, 'after_cursor_execute')
63+
def after_cursor_execute(conn, cursor, statement, parameters, context,
64+
executemany):
65+
if hasattr(context, 'opentracing_span') and context.opentracing_span:
66+
context.opentracing_span.finish()
67+
context.opentracing_span = None

0 commit comments

Comments
 (0)