diff options
author | Andrii Dobroshynskyi <andrii.dobroshynskyi@mongodb.com> | 2020-07-01 15:30:38 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-12-10 23:49:39 +0000 |
commit | f94f958425b488b305c943af156d33e39de4b28c (patch) | |
tree | 587b697ac30118158ad5ac7f7cb526d273163288 | |
parent | 1c35a911de679fef5f64cb76e0bfd2acfa44c267 (diff) | |
download | mongo-f94f958425b488b305c943af156d33e39de4b28c.tar.gz |
SERVER-48742 Log changes to profiler settings
(cherry picked from commit 0d30729722165f15e52db2c02ebb56cfd1c6ccc1)
-rw-r--r-- | jstests/core/apitest_db_profile_level.js | 46 | ||||
-rw-r--r-- | src/mongo/db/commands/profile_common.cpp | 30 |
2 files changed, 65 insertions, 11 deletions
diff --git a/jstests/core/apitest_db_profile_level.js b/jstests/core/apitest_db_profile_level.js index adfc2b0ee43..d72e7e39f02 100644 --- a/jstests/core/apitest_db_profile_level.js +++ b/jstests/core/apitest_db_profile_level.js @@ -1,11 +1,13 @@ /** * Tests for setting of profile levels - * @tags: [does_not_support_stepdowns, requires_profiling] + * @tags: [does_not_support_stepdowns, requires_profiling, requires_fcv_42] */ (function() { 'use strict'; +load("jstests/libs/log.js"); // For findMatchingLogLine, findMatchingLogLines. + /* * be sure the public collection API is complete */ @@ -16,25 +18,51 @@ assert(db.setProfilingLevel, "setProfilingLevel"); // other tests that modify profiler level, when run in parallel. var profileLevelDB = db.getSiblingDB("apitest_db_profile_level"); +// Checks for the log that was expected to be created when profile level changed. +function profilerChangeWasLogged({from, to} = {}) { + const globalLog = assert.commandWorked(profileLevelDB.adminCommand({getLog: 'global'})); + + const fieldMatcher = {msg: "Profiler settings changed"}; + if (from && to) { + const lines = [...findMatchingLogLines(globalLog.log, fieldMatcher)]; + return lines.find(line => line.match(new RegExp(/"from":{/.source + from.source)) && + line.match(new RegExp(/"to":{/.source + to.source))); + } else { + return findMatchingLogLine(globalLog.log, fieldMatcher); + } +} + +profileLevelDB.getProfilingLevel(); +assert(!profilerChangeWasLogged({from: /"level":0/, to: /"level":-1/}), + "Didn't expect anything to be logged"); + +assert.throws(() => { + profileLevelDB.setProfilingLevel(-1); +}); + profileLevelDB.setProfilingLevel(0); assert(profileLevelDB.getProfilingLevel() == 0, "prof level 0"); +assert(profilerChangeWasLogged({from: /"level":0/, to: /"level":0/}), + "Didn't find expected log line"); profileLevelDB.setProfilingLevel(1); assert(profileLevelDB.getProfilingLevel() == 1, "p1"); +assert(profilerChangeWasLogged({from: /"level":0/, to: /"level":1/}), + "Didn't find expected log line"); profileLevelDB.setProfilingLevel(2); assert(profileLevelDB.getProfilingLevel() == 2, "p2"); +assert(profilerChangeWasLogged({from: /"level":1/, to: /"level":2/}), + "Didn't find expected log line"); profileLevelDB.setProfilingLevel(0); assert(profileLevelDB.getProfilingLevel() == 0, "prof level 0"); +assert(profilerChangeWasLogged({from: /"level":2/, to: /"level":0/}), + "Didn't find expected log line"); -var asserted = false; -try { +assert.throws(() => { profileLevelDB.setProfilingLevel(10); - assert(false); -} catch (e) { - asserted = true; - assert(e.dbSetProfilingException); -} -assert(asserted, "should have asserted"); +}); +// Check that didn't log an invalid profile level change. +assert(!profilerChangeWasLogged({from: /"level":0/, to: /"level":10/}), "Didn't expect log line"); })(); diff --git a/src/mongo/db/commands/profile_common.cpp b/src/mongo/db/commands/profile_common.cpp index bfb095630eb..1691848ee72 100644 --- a/src/mongo/db/commands/profile_common.cpp +++ b/src/mongo/db/commands/profile_common.cpp @@ -26,13 +26,16 @@ * exception statement from all source files in the program, then also delete * it in the license file. */ +#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kCommand #include "mongo/platform/basic.h" #include "mongo/db/auth/authorization_session.h" #include "mongo/db/commands/profile_common.h" #include "mongo/db/commands/profile_gen.h" +#include "mongo/db/jsobj.h" #include "mongo/idl/idl_parser.h" +#include "mongo/util/log.h" namespace mongo { @@ -69,10 +72,12 @@ bool ProfileCmdBase::run(OperationContext* opCtx, // Delegate to _applyProfilingLevel to set the profiling level appropriately whether we are on // mongoD or mongoS. int oldLevel = _applyProfilingLevel(opCtx, dbName, profilingLevel); + auto oldSlowMS = serverGlobalParams.slowMS; + auto oldSampleRate = serverGlobalParams.sampleRate; result.append("was", oldLevel); - result.append("slowms", serverGlobalParams.slowMS); - result.append("sampleRate", serverGlobalParams.sampleRate); + result.append("slowms", oldSlowMS); + result.append("sampleRate", oldSampleRate); if (auto slowms = request.getSlowms()) { serverGlobalParams.slowMS = *slowms; @@ -84,6 +89,27 @@ bool ProfileCmdBase::run(OperationContext* opCtx, serverGlobalParams.sampleRate = *sampleRate; } + // Log the change made to server's profiling settings, unless the request was to get the current + // value. + if (profilingLevel != -1) { + logv2::DynamicAttributes attrs; + + BSONObjBuilder oldState; + BSONObjBuilder newState; + + oldState.append("level"_sd, oldLevel); + oldState.append("slowms"_sd, oldSlowMS); + oldState.append("sampleRate"_sd, oldSampleRate); + attrs.add("from", oldState.obj()); + + newState.append("level"_sd, profilingLevel); + newState.append("slowms"_sd, serverGlobalParams.slowMS); + newState.append("sampleRate"_sd, serverGlobalParams.sampleRate); + attrs.add("to", newState.obj()); + + LOGV2(48742, "Profiler settings changed", attrs); + } + return true; } } // namespace mongo |