diff --git a/doc/api/pymongo/database.rst b/doc/api/pymongo/database.rst index a88131b79..b40a77dff 100644 --- a/doc/api/pymongo/database.rst +++ b/doc/api/pymongo/database.rst @@ -5,9 +5,6 @@ :synopsis: Database level operations .. autodata:: pymongo.auth.MECHANISMS - .. autodata:: pymongo.OFF - .. autodata:: pymongo.SLOW_ONLY - .. autodata:: pymongo.ALL .. autoclass:: pymongo.database.Database :members: diff --git a/doc/changelog.rst b/doc/changelog.rst index c1b6bd9f3..f7009c225 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -37,6 +37,14 @@ Breaking Changes in 4.0 :meth:`pymongo.database.Database.reset_error_history`. - Removed :meth:`pymongo.database.Database.add_user` and :meth:`pymongo.database.Database.remove_user`. +- Removed support for database profiler helpers + :meth:`~pymongo.database.Database.profiling_level`, + :meth:`~pymongo.database.Database.set_profiling_level`, + and :meth:`~pymongo.database.Database.profiling_info`. Instead, users + should run the `profile command`_ with the + :meth:`~pymongo.database.Database.command` helper directly. +- Removed :attr:`pymongo.OFF`, :attr:`pymongo.SLOW_ONLY`, and + :attr:`pymongo.ALL`. - Removed :meth:`pymongo.collection.Collection.parallel_scan`. - Removed :meth:`pymongo.collection.Collection.ensure_index`. - Removed :meth:`pymongo.collection.Collection.reindex`. @@ -96,6 +104,7 @@ See the `PyMongo 4.0 release notes in JIRA`_ for the list of resolved issues in this release. .. _PyMongo 4.0 release notes in JIRA: https://jira.mongodb.org/secure/ReleaseNote.jspa?projectId=10004&version=18463 +.. _profile command: https://docs.mongodb.com/manual/reference/command/profile/ Changes in Version 3.11.1 ------------------------- diff --git a/doc/migrate-to-pymongo4.rst b/doc/migrate-to-pymongo4.rst index ba3116899..35aa0e668 100644 --- a/doc/migrate-to-pymongo4.rst +++ b/doc/migrate-to-pymongo4.rst @@ -207,6 +207,47 @@ PyMongo 3.6. Use the `dropUser command`_ instead:: .. _dropUser command: https://docs.mongodb.com/manual/reference/command/createUser/ +Database.profiling_level is removed +................................... + +Removed :meth:`pymongo.database.Database.profiling_level` which was deprecated in +PyMongo 3.12. Use the `profile command`_ instead. Code like this:: + + level = db.profiling_level() + +Can be changed to this:: + + profile = db.command('profile', -1) + level = profile['was'] + +.. _profile command: https://docs.mongodb.com/manual/reference/command/profile/ + +Database.set_profiling_level is removed +....................................... + +Removed :meth:`pymongo.database.Database.set_profiling_level` which was deprecated in +PyMongo 3.12. Use the `profile command`_ instead. Code like this:: + + db.set_profiling_level(pymongo.ALL, filter={'op': 'query'}) + +Can be changed to this:: + + res = db.command('profile', 2, filter={'op': 'query'}) + +Database.profiling_info is removed +.................................. + +Removed :meth:`pymongo.database.Database.profiling_info` which was deprecated in +PyMongo 3.12. Query the `'system.profile' collection`_ instead. Code like this:: + + profiling_info = db.profiling_info() + +Can be changed to this:: + + profiling_info = list(db['system.profile'].find()) + +.. _'system.profile' collection: https://docs.mongodb.com/manual/reference/database-profiler/ + Collection ---------- diff --git a/pymongo/__init__.py b/pymongo/__init__.py index fff8d4602..0b76e8756 100644 --- a/pymongo/__init__.py +++ b/pymongo/__init__.py @@ -67,38 +67,6 @@ TEXT = "text" .. _text index: http://docs.mongodb.org/manual/core/index-text/ """ -OFF = 0 -"""**DEPRECATED** - No database profiling. - -**DEPRECATED** - :attr:`OFF` is deprecated and will be removed in PyMongo 4.0. -Instead, specify this profiling level using the numeric value ``0``. -See https://docs.mongodb.com/manual/tutorial/manage-the-database-profiler - -.. versionchanged:: 3.12 - Deprecated -""" -SLOW_ONLY = 1 -"""**DEPRECATED** - Only profile slow operations. - -**DEPRECATED** - :attr:`SLOW_ONLY` is deprecated and will be removed in -PyMongo 4.0. Instead, specify this profiling level using the numeric -value ``1``. -See https://docs.mongodb.com/manual/tutorial/manage-the-database-profiler - -.. versionchanged:: 3.12 - Deprecated -""" -ALL = 2 -"""**DEPRECATED** - Profile all operations. - -**DEPRECATED** - :attr:`ALL` is deprecated and will be removed in PyMongo 4.0. -Instead, specify this profiling level using the numeric value ``2``. -See https://docs.mongodb.com/manual/tutorial/manage-the-database-profiler - -.. versionchanged:: 3.12 - Deprecated -""" - version_tuple = (4, 0, '.dev0') def get_version_string(): diff --git a/pymongo/database.py b/pymongo/database.py index 584824e06..61d87611d 100644 --- a/pymongo/database.py +++ b/pymongo/database.py @@ -850,147 +850,6 @@ class Database(common.BaseObject): return result - def profiling_level(self, session=None): - """**DEPRECATED**: Get the database's current profiling level. - - Starting with PyMongo 3.12, this helper is obsolete. Instead, users - can run the `profile command`_, using the :meth:`command` - helper to get the current profiler level. Running the - `profile command`_ with the level set to ``-1`` returns the current - profiler information without changing it:: - - res = db.command("profile", -1) - profiling_level = res["was"] - - The format of ``res`` depends on the version of MongoDB in use. - - Returns one of (:data:`~pymongo.OFF`, - :data:`~pymongo.SLOW_ONLY`, :data:`~pymongo.ALL`). - - :Parameters: - - `session` (optional): a - :class:`~pymongo.client_session.ClientSession`. - - .. versionchanged:: 3.12 - Deprecated. - - .. versionchanged:: 3.6 - Added ``session`` parameter. - - .. mongodoc:: profiling - .. _profile command: https://docs.mongodb.com/manual/reference/command/profile/ - """ - warnings.warn("profiling_level() is deprecated. See the documentation " - "for more information", - DeprecationWarning, stacklevel=2) - result = self.command("profile", -1, session=session) - - assert result["was"] >= 0 and result["was"] <= 2 - return result["was"] - - def set_profiling_level(self, level, slow_ms=None, session=None, - sample_rate=None, filter=None): - """**DEPRECATED**: Set the database's profiling level. - - Starting with PyMongo 3.12, this helper is obsolete. Instead, users - can directly run the `profile command`_, using the :meth:`command` - helper, e.g.:: - - res = db.command("profile", 2, filter={"op": "query"}) - - :Parameters: - - `level`: Specifies a profiling level, see list of possible values - below. - - `slow_ms`: Optionally modify the threshold for the profile to - consider a query or operation. Even if the profiler is off queries - slower than the `slow_ms` level will get written to the logs. - - `session` (optional): a - :class:`~pymongo.client_session.ClientSession`. - - `sample_rate` (optional): The fraction of slow operations that - should be profiled or logged expressed as a float between 0 and 1. - - `filter` (optional): A filter expression that controls which - operations are profiled and logged. - - Possible `level` values: - - +----------------------------+------------------------------------+ - | Level | Setting | - +============================+====================================+ - | :data:`~pymongo.OFF` | Off. No profiling. | - +----------------------------+------------------------------------+ - | :data:`~pymongo.SLOW_ONLY` | On. Only includes slow operations. | - +----------------------------+------------------------------------+ - | :data:`~pymongo.ALL` | On. Includes all operations. | - +----------------------------+------------------------------------+ - - Raises :class:`ValueError` if level is not one of - (:data:`~pymongo.OFF`, :data:`~pymongo.SLOW_ONLY`, - :data:`~pymongo.ALL`). - - .. versionchanged:: 3.12 - Added the ``sample_rate`` and ``filter`` parameters. - Deprecated. - - .. versionchanged:: 3.6 - Added ``session`` parameter. - - .. mongodoc:: profiling - .. _profile command: https://docs.mongodb.com/manual/reference/command/profile/ - """ - warnings.warn("set_profiling_level() is deprecated. See the " - "documentation for more information", - DeprecationWarning, stacklevel=2) - - if not isinstance(level, int) or level < 0 or level > 2: - raise ValueError("level must be one of (OFF, SLOW_ONLY, ALL)") - - if slow_ms is not None and not isinstance(slow_ms, int): - raise TypeError("slow_ms must be an integer") - - if sample_rate is not None and not isinstance(sample_rate, float): - raise TypeError( - "sample_rate must be a float, not %r" % (sample_rate,)) - - cmd = SON(profile=level) - if slow_ms is not None: - cmd['slowms'] = slow_ms - if sample_rate is not None: - cmd['sampleRate'] = sample_rate - if filter is not None: - cmd['filter'] = filter - self.command(cmd, session=session) - - def profiling_info(self, session=None): - """**DEPRECATED**: Returns a list containing current profiling - information. - - Starting with PyMongo 3.12, this helper is obsolete. Instead, users - can view the database profiler output by running - :meth:`~pymongo.collection.Collection.find` against the - ``system.profile`` collection as detailed in the `profiler output`_ - documentation:: - - profiling_info = list(db["system.profile"].find()) - - :Parameters: - - `session` (optional): a - :class:`~pymongo.client_session.ClientSession`. - - .. versionchanged:: 3.12 - Deprecated. - - .. versionchanged:: 3.6 - Added ``session`` parameter. - - .. mongodoc:: profiling - .. _profiler output: https://docs.mongodb.com/manual/reference/database-profiler/ - """ - warnings.warn("profiling_info() is deprecated. See the " - "documentation for more information", - DeprecationWarning, stacklevel=2) - - return list(self["system.profile"].find(session=session)) - def __iter__(self): return self diff --git a/test/test_cursor.py b/test/test_cursor.py index b2de09429..3ba455c18 100644 --- a/test/test_cursor.py +++ b/test/test_cursor.py @@ -27,12 +27,9 @@ sys.path[0:0] = [""] from bson import decode_all from bson.code import Code -from bson.raw_bson import RawBSONDocument from bson.son import SON from pymongo import (ASCENDING, - DESCENDING, - ALL, - OFF) + DESCENDING) from pymongo.collation import Collation from pymongo.cursor import Cursor, CursorType from pymongo.errors import (ConfigurationError, @@ -1290,7 +1287,7 @@ class TestCursor(IntegrationTest): query_key = "query.$comment" self.client.drop_database(self.db) - self.db.set_profiling_level(ALL) + self.db.command('profile', 2) # Profile ALL commands. try: list(self.db.test.find().comment('foo')) op = self.db.system.profile.find({'ns': 'pymongo_test.test', @@ -1312,7 +1309,7 @@ class TestCursor(IntegrationTest): 'command.comment': 'foo'}) self.assertEqual(op.count(), 1) finally: - self.db.set_profiling_level(OFF) + self.db.command('profile', 0) # Turn off profiling. self.db.system.profile.drop() self.db.test.insert_many([{}, {}]) diff --git a/test/test_database.py b/test/test_database.py index b19aba0ad..37bda18b7 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -26,10 +26,7 @@ from bson.regex import Regex from bson.dbref import DBRef from bson.objectid import ObjectId from bson.son import SON -from pymongo import (ALL, - auth, - OFF, - SLOW_ONLY, +from pymongo import (auth, helpers) from pymongo.collection import Collection from pymongo.database import Database @@ -370,105 +367,6 @@ class TestDatabase(IntegrationTest): with self.assertRaises(OperationFailure): db.validate_collection(coll, full=True, background=True) - @client_context.require_no_mongos - @ignore_deprecations - def test_profiling_levels(self): - db = self.client.pymongo_test - self.assertEqual(db.profiling_level(), OFF) # default - - self.assertRaises(ValueError, db.set_profiling_level, 5.5) - self.assertRaises(ValueError, db.set_profiling_level, None) - self.assertRaises(ValueError, db.set_profiling_level, -1) - self.assertRaises(TypeError, db.set_profiling_level, SLOW_ONLY, 5.5) - self.assertRaises(TypeError, db.set_profiling_level, SLOW_ONLY, '1') - - db.set_profiling_level(SLOW_ONLY) - self.assertEqual(db.profiling_level(), SLOW_ONLY) - - db.set_profiling_level(ALL) - self.assertEqual(db.profiling_level(), ALL) - - db.set_profiling_level(OFF) - self.assertEqual(db.profiling_level(), OFF) - - db.set_profiling_level(SLOW_ONLY, 50) - self.assertEqual(50, db.command("profile", -1)['slowms']) - - db.set_profiling_level(ALL, -1) - self.assertEqual(-1, db.command("profile", -1)['slowms']) - - db.set_profiling_level(OFF, 100) # back to default - self.assertEqual(100, db.command("profile", -1)['slowms']) - - @client_context.require_no_mongos - @client_context.require_version_min(3, 6) - @ignore_deprecations - def test_profiling_sample_rate(self): - db = self.client.pymongo_test - with self.assertRaises(TypeError): - db.set_profiling_level(SLOW_ONLY, 50, sample_rate='1') - with self.assertRaises(TypeError): - db.set_profiling_level(SLOW_ONLY, 50, sample_rate=1) - - db.set_profiling_level(SLOW_ONLY, 50, sample_rate=0.0) - db.set_profiling_level(SLOW_ONLY, 50, sample_rate=1.0) - db.set_profiling_level(SLOW_ONLY, 50, sample_rate=0.5) - profile = db.command("profile", -1) - self.assertEqual(50, profile['slowms']) - self.assertEqual(0.5, profile['sampleRate']) - db.set_profiling_level(OFF, 100) # back to default - self.assertEqual(100, db.command("profile", -1)['slowms']) - - @client_context.require_no_mongos - @client_context.require_version_min(4, 4, 2) - @ignore_deprecations - def test_profiling_filter(self): - db = self.client.pymongo_test - db.set_profiling_level(ALL, filter={'ns': {'$eq': 'test.test'}}) - profile = db.command("profile", -1) - self.assertEqual({'ns': {'$eq': 'test.test'}}, profile['filter']) - # filter='unset' resets the filter back to the default. - db.set_profiling_level(OFF, 100, filter='unset') - self.assertEqual(100, db.command("profile", -1)['slowms']) - - @client_context.require_no_mongos - @ignore_deprecations - def test_profiling_info(self): - db = self.client.pymongo_test - - db.system.profile.drop() - db.set_profiling_level(ALL) - db.test.find_one() - db.set_profiling_level(OFF) - - info = db.profiling_info() - self.assertTrue(isinstance(info, list)) - - # Check if we're going to fail because of SERVER-4754, in which - # profiling info isn't collected if mongod was started with --auth - if server_started_with_auth(self.client): - raise SkipTest( - "We need SERVER-4754 fixed for the rest of this test to pass" - ) - - self.assertTrue(len(info) >= 1) - # These basically clue us in to server changes. - self.assertTrue(isinstance(info[0]['responseLength'], int)) - self.assertTrue(isinstance(info[0]['millis'], int)) - self.assertTrue(isinstance(info[0]['client'], str)) - self.assertTrue(isinstance(info[0]['user'], str)) - self.assertTrue(isinstance(info[0]['ns'], str)) - self.assertTrue(isinstance(info[0]['op'], str)) - self.assertTrue(isinstance(info[0]["ts"], datetime.datetime)) - - def test_profiling_helpers_deprecated(self): - filter = DeprecationFilter('error') - self.addCleanup(filter.stop) - db = self.client.pymongo_test - self.assertRaises(DeprecationWarning, db.profiling_level) - self.assertRaises(DeprecationWarning, db.profiling_info) - self.assertRaises(DeprecationWarning, db.set_profiling_level, OFF) - def test_command(self): self.maxDiff = None db = self.client.admin diff --git a/test/test_session.py b/test/test_session.py index 5bcad4e75..45fcae636 100644 --- a/test/test_session.py +++ b/test/test_session.py @@ -25,7 +25,7 @@ sys.path[0:0] = [""] from bson import DBRef from gridfs import GridFS, GridFSBucket -from pymongo import ASCENDING, InsertOne, IndexModel, OFF, monitoring +from pymongo import ASCENDING, InsertOne, IndexModel, monitoring from pymongo.common import _MAX_END_SESSIONS from pymongo.errors import (ConfigurationError, InvalidOperation, @@ -249,14 +249,8 @@ class TestSession(IntegrationTest): (db.list_collection_names, [], {}), (db.validate_collection, ['collection'], {}), (db.drop_collection, ['collection'], {}), - (db.profiling_info, [], {}), (db.dereference, [DBRef('collection', 1)], {}), ] - - if not client_context.is_mongos: - ops.append((db.set_profiling_level, [OFF], {})) - ops.append((db.profiling_level, [], {})) - self._test_ops(client, *ops) @staticmethod