Skip to content

Commit 43160af

Browse files
Only get package version once (#928)
* Only get package version once * Add disconnect method * Add disconnect method --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 13e9891 commit 43160af

File tree

2 files changed

+65
-57
lines changed

2 files changed

+65
-57
lines changed

newrelic/hooks/datastore_aioredis.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
_redis_operation_re,
2323
)
2424

25+
AIOREDIS_VERSION = get_package_version_tuple("aioredis")
26+
2527

2628
def _conn_attrs_to_dict(connection):
2729
host = getattr(connection, "host", None)
@@ -58,14 +60,13 @@ def _nr_wrapper_AioRedis_method_(wrapped, instance, args, kwargs):
5860
# Check for transaction and return early if found.
5961
# Method will return synchronously without executing,
6062
# it will be added to the command stack and run later.
61-
aioredis_version = get_package_version_tuple("aioredis")
6263

6364
# This conditional is for versions of aioredis that are outside
6465
# New Relic's supportability window but will still work. New
6566
# Relic does not provide testing/support for this. In order to
6667
# keep functionality without affecting coverage metrics, this
6768
# segment is excluded from coverage analysis.
68-
if aioredis_version and aioredis_version < (2,): # pragma: no cover
69+
if AIOREDIS_VERSION and AIOREDIS_VERSION < (2,): # pragma: no cover
6970
# AioRedis v1 uses a RedisBuffer instead of a real connection for queueing up pipeline commands
7071
from aioredis.commands.transaction import _RedisBuffer
7172

@@ -75,7 +76,7 @@ def _nr_wrapper_AioRedis_method_(wrapped, instance, args, kwargs):
7576
return wrapped(*args, **kwargs)
7677
else:
7778
# AioRedis v2 uses a Pipeline object for a client and internally queues up pipeline commands
78-
if aioredis_version:
79+
if AIOREDIS_VERSION:
7980
from aioredis.client import Pipeline
8081
if isinstance(instance, Pipeline):
8182
return wrapped(*args, **kwargs)
@@ -139,6 +140,7 @@ async def wrap_Connection_send_command(wrapped, instance, args, kwargs):
139140
):
140141
return await wrapped(*args, **kwargs)
141142

143+
142144
# This wrapper is for versions of aioredis that are outside
143145
# New Relic's supportability window but will still work. New
144146
# Relic does not provide testing/support for this. In order to

tests/datastore_redis/test_custom_conn_pool.py

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
''' The purpose of these tests is to confirm that using a non-standard
15+
""" The purpose of these tests is to confirm that using a non-standard
1616
connection pool that does not have a `connection_kwargs` attribute
1717
will not result in an error.
18-
'''
18+
"""
1919

2020
import pytest
2121
import redis
22-
23-
from newrelic.api.background_task import background_task
24-
from newrelic.common.package_version_utils import get_package_version_tuple
25-
26-
from testing_support.fixtures import override_application_settings
27-
from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics
2822
from testing_support.db_settings import redis_settings
23+
from testing_support.fixtures import override_application_settings
2924
from testing_support.util import instance_hostname
25+
from testing_support.validators.validate_transaction_metrics import (
26+
validate_transaction_metrics,
27+
)
3028

29+
from newrelic.api.background_task import background_task
30+
from newrelic.common.package_version_utils import get_package_version_tuple
3131

3232
DB_SETTINGS = redis_settings()[0]
3333
REDIS_PY_VERSION = get_package_version_tuple("redis")
@@ -45,13 +45,17 @@ def get_connection(self, name, *keys, **options):
4545
def release(self, connection):
4646
self.connection.disconnect()
4747

48+
def disconnect(self):
49+
self.connection.disconnect()
50+
51+
4852
# Settings
4953

5054
_enable_instance_settings = {
51-
'datastore_tracer.instance_reporting.enabled': True,
55+
"datastore_tracer.instance_reporting.enabled": True,
5256
}
5357
_disable_instance_settings = {
54-
'datastore_tracer.instance_reporting.enabled': False,
58+
"datastore_tracer.instance_reporting.enabled": False,
5559
}
5660

5761
# Metrics
@@ -61,98 +65,100 @@ def release(self, connection):
6165
datastore_all_metric_count = 5 if REDIS_PY_VERSION >= (5, 0) else 3
6266

6367
_base_scoped_metrics = [
64-
('Datastore/operation/Redis/get', 1),
65-
('Datastore/operation/Redis/set', 1),
66-
('Datastore/operation/Redis/client_list', 1),
68+
("Datastore/operation/Redis/get", 1),
69+
("Datastore/operation/Redis/set", 1),
70+
("Datastore/operation/Redis/client_list", 1),
6771
]
6872
# client_setinfo was introduced in v5.0.0 and assigns info displayed in client_list output
6973
if REDIS_PY_VERSION >= (5, 0):
70-
_base_scoped_metrics.append(('Datastore/operation/Redis/client_setinfo', 2),)
74+
_base_scoped_metrics.append(
75+
("Datastore/operation/Redis/client_setinfo", 2),
76+
)
7177

7278
_base_rollup_metrics = [
73-
('Datastore/all', datastore_all_metric_count),
74-
('Datastore/allOther', datastore_all_metric_count),
75-
('Datastore/Redis/all', datastore_all_metric_count),
76-
('Datastore/Redis/allOther', datastore_all_metric_count),
77-
('Datastore/operation/Redis/get', 1),
78-
('Datastore/operation/Redis/set', 1),
79-
('Datastore/operation/Redis/client_list', 1),
79+
("Datastore/all", datastore_all_metric_count),
80+
("Datastore/allOther", datastore_all_metric_count),
81+
("Datastore/Redis/all", datastore_all_metric_count),
82+
("Datastore/Redis/allOther", datastore_all_metric_count),
83+
("Datastore/operation/Redis/get", 1),
84+
("Datastore/operation/Redis/set", 1),
85+
("Datastore/operation/Redis/client_list", 1),
8086
]
8187
if REDIS_PY_VERSION >= (5, 0):
82-
_base_rollup_metrics.append(('Datastore/operation/Redis/client_setinfo', 2),)
88+
_base_rollup_metrics.append(
89+
("Datastore/operation/Redis/client_setinfo", 2),
90+
)
8391

84-
_host = instance_hostname(DB_SETTINGS['host'])
85-
_port = DB_SETTINGS['port']
92+
_host = instance_hostname(DB_SETTINGS["host"])
93+
_port = DB_SETTINGS["port"]
8694

87-
_instance_metric_name = 'Datastore/instance/Redis/%s/%s' % (_host, _port)
95+
_instance_metric_name = "Datastore/instance/Redis/%s/%s" % (_host, _port)
8896

8997
instance_metric_count = 5 if REDIS_PY_VERSION >= (5, 0) else 3
9098

91-
_enable_rollup_metrics = _base_rollup_metrics.append(
92-
(_instance_metric_name, instance_metric_count)
93-
)
99+
_enable_rollup_metrics = _base_rollup_metrics.append((_instance_metric_name, instance_metric_count))
94100

95-
_disable_rollup_metrics = _base_rollup_metrics.append(
96-
(_instance_metric_name, None)
97-
)
101+
_disable_rollup_metrics = _base_rollup_metrics.append((_instance_metric_name, None))
98102

99103
# Operations
100104

105+
101106
def exercise_redis(client):
102-
client.set('key', 'value')
103-
client.get('key')
104-
client.execute_command('CLIENT', 'LIST', parse='LIST')
107+
client.set("key", "value")
108+
client.get("key")
109+
client.execute_command("CLIENT", "LIST", parse="LIST")
110+
105111

106112
# Tests
107113

108-
@pytest.mark.skipif(REDIS_PY_VERSION < (2, 7),
109-
reason='Client list command introduced in 2.7')
114+
115+
@pytest.mark.skipif(REDIS_PY_VERSION < (2, 7), reason="Client list command introduced in 2.7")
110116
@override_application_settings(_enable_instance_settings)
111117
@validate_transaction_metrics(
112-
'test_custom_conn_pool:test_fake_conn_pool_enable_instance',
113-
scoped_metrics=_base_scoped_metrics,
114-
rollup_metrics=_enable_rollup_metrics,
115-
background_task=True)
118+
"test_custom_conn_pool:test_fake_conn_pool_enable_instance",
119+
scoped_metrics=_base_scoped_metrics,
120+
rollup_metrics=_enable_rollup_metrics,
121+
background_task=True,
122+
)
116123
@background_task()
117124
def test_fake_conn_pool_enable_instance():
118-
client = redis.StrictRedis(host=DB_SETTINGS['host'],
119-
port=DB_SETTINGS['port'], db=0)
125+
client = redis.StrictRedis(host=DB_SETTINGS["host"], port=DB_SETTINGS["port"], db=0)
120126

121127
# Get a real connection
122128

123-
conn = client.connection_pool.get_connection('GET')
129+
conn = client.connection_pool.get_connection("GET")
124130

125131
# Replace the original connection pool with one that doesn't
126132
# have the `connection_kwargs` attribute.
127133

128134
fake_pool = FakeConnectionPool(conn)
129135
client.connection_pool = fake_pool
130-
assert not hasattr(client.connection_pool, 'connection_kwargs')
136+
assert not hasattr(client.connection_pool, "connection_kwargs")
131137

132138
exercise_redis(client)
133139

134-
@pytest.mark.skipif(REDIS_PY_VERSION < (2, 7),
135-
reason='Client list command introduced in 2.7')
140+
141+
@pytest.mark.skipif(REDIS_PY_VERSION < (2, 7), reason="Client list command introduced in 2.7")
136142
@override_application_settings(_disable_instance_settings)
137143
@validate_transaction_metrics(
138-
'test_custom_conn_pool:test_fake_conn_pool_disable_instance',
139-
scoped_metrics=_base_scoped_metrics,
140-
rollup_metrics=_disable_rollup_metrics,
141-
background_task=True)
144+
"test_custom_conn_pool:test_fake_conn_pool_disable_instance",
145+
scoped_metrics=_base_scoped_metrics,
146+
rollup_metrics=_disable_rollup_metrics,
147+
background_task=True,
148+
)
142149
@background_task()
143150
def test_fake_conn_pool_disable_instance():
144-
client = redis.StrictRedis(host=DB_SETTINGS['host'],
145-
port=DB_SETTINGS['port'], db=0)
151+
client = redis.StrictRedis(host=DB_SETTINGS["host"], port=DB_SETTINGS["port"], db=0)
146152

147153
# Get a real connection
148154

149-
conn = client.connection_pool.get_connection('GET')
155+
conn = client.connection_pool.get_connection("GET")
150156

151157
# Replace the original connection pool with one that doesn't
152158
# have the `connection_kwargs` attribute.
153159

154160
fake_pool = FakeConnectionPool(conn)
155161
client.connection_pool = fake_pool
156-
assert not hasattr(client.connection_pool, 'connection_kwargs')
162+
assert not hasattr(client.connection_pool, "connection_kwargs")
157163

158164
exercise_redis(client)

0 commit comments

Comments
 (0)