Skip to content

Commit a962283

Browse files
committed
Initial commit
0 parents  commit a962283

5 files changed

Lines changed: 230 additions & 0 deletions

File tree

.gitignore

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
.pytest_cache/
49+
50+
# Translations
51+
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
local_settings.py
57+
db.sqlite3
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
68+
69+
# PyBuilder
70+
target/
71+
72+
# Jupyter Notebook
73+
.ipynb_checkpoints
74+
75+
# pyenv
76+
.python-version
77+
78+
# celery beat schedule file
79+
celerybeat-schedule
80+
81+
# SageMath parsed files
82+
*.sage.py
83+
84+
# Environments
85+
.env
86+
.venv
87+
env/
88+
venv/
89+
ENV/
90+
env.bak/
91+
venv.bak/
92+
93+
# Spyder project settings
94+
.spyderproject
95+
.spyproject
96+
97+
# Rope project settings
98+
.ropeproject
99+
100+
# mkdocs documentation
101+
/site
102+
103+
# mypy
104+
.mypy_cache/

LICENSE

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

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# NewRelic extensions / asyncpg
2+
3+
4+
## Install
5+
6+
```
7+
pip install newrelic_asyncpg
8+
```
9+
10+
11+
1) Append in your newrelic.ini
12+
13+
```ini
14+
[import-hook:asyncpg]
15+
enabled = true
16+
execute = newrelic_asyncpg:instrument
17+
```
18+

newrelic_asyncpg/__init__.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from newrelic import agent
2+
from newrelic.hooks.database_dbapi2 import (
3+
ConnectionFactory as DBAPI2ConnectionFactory,
4+
)
5+
6+
7+
class ConnectionFactory(DBAPI2ConnectionFactory):
8+
""""""
9+
10+
@staticmethod
11+
async def __connection_wrapper__(wrapped, *args, **kwargs):
12+
return await wrapped
13+
14+
def __call__(self, *args, **kwargs):
15+
agent.wrap_function_wrapper(kwargs['connection_class'], '_execute', self._execute_wrapper)
16+
return super(ConnectionFactory, self).__call__(*args, **kwargs)
17+
18+
19+
async def _execute_wrapper(self, wrapped, instance, args, kwargs):
20+
21+
db_host, db_port = instance._addr
22+
db_name = instance._params.database
23+
24+
trace_kwargs = {
25+
"transaction": agent.current_transaction(),
26+
"dbapi2_module": self._nr_dbapi2_module,
27+
"connect_params": ((db_host, db_port, db_name), {}),
28+
"sql": args[0],
29+
"sql_parameters": args[1:]
30+
}
31+
32+
with agent.DatabaseTrace(**trace_kwargs):
33+
return await wrapped(*args, **kwargs)
34+
35+
36+
def instrument(module):
37+
38+
agent.register_database_client(
39+
module,
40+
database_product='Postgres',
41+
quoting_style='single+dollar',
42+
explain_query='explain',
43+
explain_stmts=('select', 'insert', 'update', 'delete'),
44+
)
45+
46+
agent.wrap_object(module, 'connection.connect', ConnectionFactory, (module,))
47+
agent.wrap_object(module, 'connect', ConnectionFactory, (module,))

setup.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python
2+
import pathlib
3+
from setuptools import setup, find_packages
4+
5+
__ROOT = pathlib.Path(__file__).parent
6+
7+
__version__ = '0.1.3'
8+
__author__ = 'Stanislav Kiryukhin'
9+
__license__ = "MIT"
10+
11+
with open(str(__ROOT / 'README.md')) as f:
12+
__description_long__ = f.read()
13+
14+
setup(
15+
name='newrelic-asyncpg',
16+
version=__version__,
17+
description='Unofficial asyncpg extensions for the NewRelic Python Agent',
18+
long_description=__description_long__,
19+
url='https://github.com/KorsaR-ZN/python-newrelic-asyncpg',
20+
author=__author__,
21+
author_email='korsar.zn@gmail.com',
22+
license=__license__,
23+
classifiers=[
24+
'Development Status :: 3 - Alpha',
25+
'Intended Audience :: Developers',
26+
'License :: OSI Approved :: MIT License',
27+
'Programming Language :: Python :: 3.6',
28+
],
29+
keywords=['asyncpg', 'newrelic', 'extensions'],
30+
packages=find_packages(exclude=['contrib', 'docs', 'test*']),
31+
python_requires='>=3.5',
32+
install_requires=[
33+
'newrelic>=4.10.0.112',
34+
],
35+
package_data={},
36+
data_files=[],
37+
entry_points={
38+
'console_scripts': [],
39+
},
40+
)

0 commit comments

Comments
 (0)