Skip to content

Commit 6ac83c1

Browse files
PYTHON-2662 Deprecate database profiler helpers (#619)
(cherry picked from commit ac61cf8)
1 parent 5cb476e commit 6ac83c1

File tree

4 files changed

+98
-6
lines changed

4 files changed

+98
-6
lines changed

doc/changelog.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@ Deprecations
3232
............
3333

3434
- Deprecated support for Python 2.7, 3.4 and 3.5.
35+
- Deprecated support for database profiler helpers
36+
:meth:`~pymongo.database.Database.profiling_level`,
37+
:meth:`~pymongo.database.Database.set_profiling_level`,
38+
and :meth:`~pymongo.database.Database.profiling_info`. Instead, users
39+
should run the `profile command`_ with the
40+
:meth:`~pymongo.database.Database.command` helper directly.
3541

3642
.. _PYTHON-2466: https://jira.mongodb.org/browse/PYTHON-2466
3743
.. _PYTHON-1690: https://jira.mongodb.org/browse/PYTHON-1690
3844
.. _PYTHON-2472: https://jira.mongodb.org/browse/PYTHON-2472
45+
.. _profile command: https://docs.mongodb.com/manual/reference/command/profile/
3946

4047
Issues Resolved
4148
...............

pymongo/__init__.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,36 @@
6868
"""
6969

7070
OFF = 0
71-
"""No database profiling."""
71+
"""**DEPRECATED** - No database profiling.
72+
73+
**DEPRECATED** - :attr:`OFF` is deprecated and will be removed in PyMongo 4.0.
74+
Instead, specify this profiling level using the numeric value ``0``.
75+
See https://docs.mongodb.com/manual/tutorial/manage-the-database-profiler
76+
77+
.. versionchanged:: 3.12
78+
Deprecated
79+
"""
7280
SLOW_ONLY = 1
73-
"""Only profile slow operations."""
81+
"""**DEPRECATED** - Only profile slow operations.
82+
83+
**DEPRECATED** - :attr:`SLOW_ONLY` is deprecated and will be removed in
84+
PyMongo 4.0. Instead, specify this profiling level using the numeric
85+
value ``1``.
86+
See https://docs.mongodb.com/manual/tutorial/manage-the-database-profiler
87+
88+
.. versionchanged:: 3.12
89+
Deprecated
90+
"""
7491
ALL = 2
75-
"""Profile all operations."""
92+
"""**DEPRECATED** - Profile all operations.
93+
94+
**DEPRECATED** - :attr:`ALL` is deprecated and will be removed in PyMongo 4.0.
95+
Instead, specify this profiling level using the numeric value ``2``.
96+
See https://docs.mongodb.com/manual/tutorial/manage-the-database-profiler
97+
98+
.. versionchanged:: 3.12
99+
Deprecated
100+
"""
76101

77102
version_tuple = (3, 12, 0, 'b1.dev0')
78103

pymongo/database.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,18 @@ def current_op(self, include_all=False, session=None):
10671067
return self._current_op(include_all, session)
10681068

10691069
def profiling_level(self, session=None):
1070-
"""Get the database's current profiling level.
1070+
"""**DEPRECATED**: Get the database's current profiling level.
1071+
1072+
Starting with PyMongo 3.12, this helper is obsolete. Instead, users
1073+
can run the `profile command`_, using the :meth:`command`
1074+
helper to get the current profiler level. Running the
1075+
`profile command`_ with the level set to ``-1`` returns the current
1076+
profiler information without changing it::
1077+
1078+
res = db.command("profile", -1)
1079+
profiling_level = res["was"]
1080+
1081+
The format of ``res`` depends on the version of MongoDB in use.
10711082
10721083
Returns one of (:data:`~pymongo.OFF`,
10731084
:data:`~pymongo.SLOW_ONLY`, :data:`~pymongo.ALL`).
@@ -1076,19 +1087,32 @@ def profiling_level(self, session=None):
10761087
- `session` (optional): a
10771088
:class:`~pymongo.client_session.ClientSession`.
10781089
1090+
.. versionchanged:: 3.12
1091+
Deprecated.
1092+
10791093
.. versionchanged:: 3.6
10801094
Added ``session`` parameter.
10811095
10821096
.. mongodoc:: profiling
1097+
.. _profile command: https://docs.mongodb.com/manual/reference/command/profile/
10831098
"""
1099+
warnings.warn("profiling_level() is deprecated. See the documentation "
1100+
"for more information",
1101+
DeprecationWarning, stacklevel=2)
10841102
result = self.command("profile", -1, session=session)
10851103

10861104
assert result["was"] >= 0 and result["was"] <= 2
10871105
return result["was"]
10881106

10891107
def set_profiling_level(self, level, slow_ms=None, session=None,
10901108
sample_rate=None, filter=None):
1091-
"""Set the database's profiling level.
1109+
"""**DEPRECATED**: Set the database's profiling level.
1110+
1111+
Starting with PyMongo 3.12, this helper is obsolete. Instead, users
1112+
can directly run the `profile command`_, using the :meth:`command`
1113+
helper, e.g.::
1114+
1115+
res = db.command("profile", 2, filter={"op": "query"})
10921116
10931117
:Parameters:
10941118
- `level`: Specifies a profiling level, see list of possible values
@@ -1121,12 +1145,18 @@ def set_profiling_level(self, level, slow_ms=None, session=None,
11211145
11221146
.. versionchanged:: 3.12
11231147
Added the ``sample_rate`` and ``filter`` parameters.
1148+
Deprecated.
11241149
11251150
.. versionchanged:: 3.6
11261151
Added ``session`` parameter.
11271152
11281153
.. mongodoc:: profiling
1154+
.. _profile command: https://docs.mongodb.com/manual/reference/command/profile/
11291155
"""
1156+
warnings.warn("set_profiling_level() is deprecated. See the "
1157+
"documentation for more information",
1158+
DeprecationWarning, stacklevel=2)
1159+
11301160
if not isinstance(level, int) or level < 0 or level > 2:
11311161
raise ValueError("level must be one of (OFF, SLOW_ONLY, ALL)")
11321162

@@ -1147,17 +1177,34 @@ def set_profiling_level(self, level, slow_ms=None, session=None,
11471177
self.command(cmd, session=session)
11481178

11491179
def profiling_info(self, session=None):
1150-
"""Returns a list containing current profiling information.
1180+
"""**DEPRECATED**: Returns a list containing current profiling
1181+
information.
1182+
1183+
Starting with PyMongo 3.12, this helper is obsolete. Instead, users
1184+
can view the database profiler output by running
1185+
:meth:`~pymongo.collection.Collection.find` against the
1186+
``system.profile`` collection as detailed in the `profiler output`_
1187+
documentation::
1188+
1189+
profiling_info = list(db["system.profile"].find())
11511190
11521191
:Parameters:
11531192
- `session` (optional): a
11541193
:class:`~pymongo.client_session.ClientSession`.
11551194
1195+
.. versionchanged:: 3.12
1196+
Deprecated.
1197+
11561198
.. versionchanged:: 3.6
11571199
Added ``session`` parameter.
11581200
11591201
.. mongodoc:: profiling
1202+
.. _profiler output: https://docs.mongodb.com/manual/reference/database-profiler/
11601203
"""
1204+
warnings.warn("profiling_info() is deprecated. See the "
1205+
"documentation for more information",
1206+
DeprecationWarning, stacklevel=2)
1207+
11611208
return list(self["system.profile"].find(session=session))
11621209

11631210
def error(self):

test/test_database.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
rs_or_single_client,
5959
server_started_with_auth,
6060
wait_until,
61+
DeprecationFilter,
6162
IMPOSSIBLE_WRITE_CONCERN,
6263
OvertCommandListener)
6364
from test.test_custom_types import DECIMAL_CODECOPTS
@@ -390,6 +391,7 @@ def test_validate_collection_background(self):
390391
db.validate_collection(coll, full=True, background=True)
391392

392393
@client_context.require_no_mongos
394+
@ignore_deprecations
393395
def test_profiling_levels(self):
394396
db = self.client.pymongo_test
395397
self.assertEqual(db.profiling_level(), OFF) # default
@@ -420,6 +422,7 @@ def test_profiling_levels(self):
420422

421423
@client_context.require_no_mongos
422424
@client_context.require_version_min(3, 6)
425+
@ignore_deprecations
423426
def test_profiling_sample_rate(self):
424427
db = self.client.pymongo_test
425428
with self.assertRaises(TypeError):
@@ -438,6 +441,7 @@ def test_profiling_sample_rate(self):
438441

439442
@client_context.require_no_mongos
440443
@client_context.require_version_min(4, 4, 2)
444+
@ignore_deprecations
441445
def test_profiling_filter(self):
442446
db = self.client.pymongo_test
443447
db.set_profiling_level(ALL, filter={'ns': {'$eq': 'test.test'}})
@@ -448,6 +452,7 @@ def test_profiling_filter(self):
448452
self.assertEqual(100, db.command("profile", -1)['slowms'])
449453

450454
@client_context.require_no_mongos
455+
@ignore_deprecations
451456
def test_profiling_info(self):
452457
db = self.client.pymongo_test
453458

@@ -476,6 +481,14 @@ def test_profiling_info(self):
476481
self.assertTrue(isinstance(info[0]['op'], string_type))
477482
self.assertTrue(isinstance(info[0]["ts"], datetime.datetime))
478483

484+
def test_profiling_helpers_deprecated(self):
485+
filter = DeprecationFilter('error')
486+
self.addCleanup(filter.stop)
487+
db = self.client.pymongo_test
488+
self.assertRaises(DeprecationWarning, db.profiling_level)
489+
self.assertRaises(DeprecationWarning, db.profiling_info)
490+
self.assertRaises(DeprecationWarning, db.set_profiling_level, OFF)
491+
479492
# SERVER-47817 removes the resetError command.
480493
@client_context.require_version_max(4, 9)
481494
@client_context.require_no_mongos

0 commit comments

Comments
 (0)