Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuri Shkuro committed Jan 1, 2016
0 parents commit 4ce17ee
Show file tree
Hide file tree
Showing 25 changed files with 1,426 additions and 0 deletions.
79 changes: 79 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
*.py[cod]
*.orig

# Ignore gen/ directory where we temporarily store thrift classes to help with development
gen/

# No local dbs
*.db

# C extensions
*.so

# OS X Junk
.DS_Store

# Packages
*.egg
*.egg-info

bin
build
develop-eggs
dist
eggs
parts
sdist
var

.installed.cfg
lib64
__pycache__
.cache/

# Installer logs
pip-log.txt

# Unit test / coverage reports
.coverage
.coverage*
.tox
.noseids

# Translations
*.mo

# Ignore python virtual environments
env*
thrift_env

# Ignore local logs
*.log
logs/*
!logs/.gitkeep

# Ignore local log
npm-debug.log

# Ignore docs
docs/_build/*

# ignore ipython profile stuff
config/profile_default/
!config/profile_default/ipython_config.py
config/README
protobuf/*.py

.phutil_module_cache

# Ignore coverage output
*.xml
coverage/

# Ignore benchmarks output
perf.log
perf.svg

# vim
*.swp
.idea/
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.. :changelog:
History
-------

0.1.0 (unreleased)
------------------

- Initial version
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2015 Uber Technologies, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

87 changes: 87 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
project := opentracing_instrumentation

pytest := PYTHONDONTWRITEBYTECODE=1 py.test --tb short -rxs \
--cov-config .coveragerc --cov $(project) tests

html_report := --cov-report=html
test_args := --cov-report xml --cov-report term-missing

.PHONY: clean-pyc clean-build docs clean
.DEFAULT_GOAL : help

help:
@echo "bootstrap - initialize local environement for development. Requires virtualenv."
@echo "clean - remove all build, test, coverage and Python artifacts"
@echo "clean-build - remove build artifacts"
@echo "clean-pyc - remove Python file artifacts"
@echo "clean-test - remove test and coverage artifacts"
@echo "lint - check style with flake8"
@echo "test - run tests quickly with the default Python"
@echo "coverage - check code coverage quickly with the default Python"
@echo "docs - generate Sphinx HTML documentation, including API docs"
@echo "release - package and upload a release"
@echo "dist - package"
@echo "install - install the package to the active Python's site-packages"

bootstrap:
@[ -d env ] || echo "Please run 'virtualenv env' first"
@[ -d env ] || exit 1
pip install -r requirements.txt
pip install -r requirements-test.txt
python setup.py develop

clean: clean-build clean-pyc clean-test

clean-build:
rm -fr build/
rm -fr dist/
rm -fr .eggs/
find . -name '*.egg-info' -exec rm -fr {} +
find . -name '*.egg' -exec rm -rf {} +

clean-pyc:
find . -name '*.pyc' -exec rm -f {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +
find . -name '__pycache__' -exec rm -fr {} +

clean-test:
rm -f .coverage
rm -fr htmlcov/

lint:
flake8 $(project) tests

test:
$(pytest) $(test_args)

jenkins:
pip install -r requirements.txt
pip install -r requirements-test.txt
python setup.py develop
CLAY_CONFIG=config/test.yaml $(pytest) $(test_args) --junit-xml=jenkins.xml

coverage:
coverage run --source $(project) setup.py test
coverage report -m
coverage html
open htmlcov/index.html

docs:
$(MAKE) -C docs clean
$(MAKE) -C docs html

release: clean
python setup.py sdist upload
python setup.py bdist_wheel upload

dist: clean
python setup.py sdist
python setup.py bdist_wheel
ls -l dist

install:
pip install -r requirements.txt
pip install -r requirements-test.txt
echo skipping pip install -r requirements-doc.txt
python setup.py install
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# opentracing-python-instrumentation

A collection of instrumentation tools to enable tracing with OpenTracing API.

##Install

```
virtualenv env
source env/bin/activate
make bootstrap
make test
```
21 changes: 21 additions & 0 deletions opentracing_instrumentation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) 2015 Uber Technologies, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from trace_context import get_current_span # noqa
19 changes: 19 additions & 0 deletions opentracing_instrumentation/client_hooks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2015 Uber Technologies, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
49 changes: 49 additions & 0 deletions opentracing_instrumentation/client_hooks/_singleton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright (c) 2015 Uber Technologies, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import absolute_import

import functools


def singleton(func):
"""
This decorator allows you to make sure that a function is called once and
only once. Note that recursive functions will still work.
Not thread-safe.
"""
NOT_CALLED, IN_CALL, CALLED = range(3)

@functools.wraps(func)
def wrapper(*args, **kwargs):
if wrapper.__call_state__ == CALLED:
return
old_state, wrapper.__call_state__ = wrapper.__call_state__, IN_CALL
ret = func(*args, **kwargs)
if old_state == NOT_CALLED:
wrapper.__call_state__ = CALLED
return ret

wrapper.__call_state__ = NOT_CALLED
# save original func to be able to patch and restore multiple times from
# unit tests
wrapper.__original_func = func
return wrapper
67 changes: 67 additions & 0 deletions opentracing_instrumentation/client_hooks/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (c) 2015 Uber Technologies, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import absolute_import

import logging

import opentracing
from ..trace_context import get_current_span
from ._singleton import singleton

log = logging.getLogger(__name__)


@singleton
def install_patches():
try:
from sqlalchemy.engine import Engine
from sqlalchemy import event
except ImportError:
# If SQLAlchemy cannot be imported, then the project we are
# instrumenting does not depend on it and we do not need to install
# the SQL hooks.
return

log.info('Instrumenting SQL methods for tracing')

@event.listens_for(Engine, 'before_cursor_execute')
def before_cursor_execute(conn, cursor, statement, parameters, context,
executemany):
operation = 'SQL'
statement = statement.strip()
if statement:
operation = '%s %s' % (operation,
statement.split(' ', 1)[0].upper())
if get_current_span() is None:
span = opentracing.tracer.start_trace(
operation_name=operation) # TODO pass client=True
else:
span = get_current_span().start_child(operation_name=operation)
if statement:
span.add_tag('sql', statement)
context.opentracing_span = span

@event.listens_for(Engine, 'after_cursor_execute')
def after_cursor_execute(conn, cursor, statement, parameters, context,
executemany):
if hasattr(context, 'opentracing_span') and context.opentracing_span:
context.opentracing_span.finish()
context.opentracing_span = None
Loading

0 comments on commit 4ce17ee

Please sign in to comment.