diff options
author | Sara Golemon <sara.golemon@mongodb.com> | 2020-08-28 17:02:46 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-09-17 21:48:31 +0000 |
commit | 001aed0981a442f8cccc091f330e31e3f22dfde3 (patch) | |
tree | 8fe04e40ede972d25332612e38fa613a0b7df2a7 /src | |
parent | 48ac387edfdba3415b26e0870c751809d9b9b45b (diff) | |
download | mongo-001aed0981a442f8cccc091f330e31e3f22dfde3.tar.gz |
SERVER-50605 Add logMessage test-only command
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/commands/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/db/commands/generic.cpp | 88 | ||||
-rw-r--r-- | src/mongo/db/commands/generic.idl | 68 | ||||
-rw-r--r-- | src/mongo/embedded/mongo_embedded/mongo_embedded_test.cpp | 1 | ||||
-rw-r--r-- | src/mongo/shell/check_log.js | 2 | ||||
-rw-r--r-- | src/mongo/util/str_escape.cpp | 2 |
6 files changed, 160 insertions, 2 deletions
diff --git a/src/mongo/db/commands/SConscript b/src/mongo/db/commands/SConscript index 46570b01440..6e383bfece3 100644 --- a/src/mongo/db/commands/SConscript +++ b/src/mongo/db/commands/SConscript @@ -110,6 +110,7 @@ env.Library( 'update_metrics.cpp', env.Idlc('parameters.idl')[0], env.Idlc('sessions_commands.idl')[0], + env.Idlc('generic.idl')[0], ], LIBDEPS_PRIVATE=[ '$BUILD_DIR/mongo/bson/mutable/mutable_bson', diff --git a/src/mongo/db/commands/generic.cpp b/src/mongo/db/commands/generic.cpp index 8a234d9761a..fa61660660c 100644 --- a/src/mongo/db/commands/generic.cpp +++ b/src/mongo/db/commands/generic.cpp @@ -27,14 +27,19 @@ * it in the license file. */ +#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kDefault + #include "mongo/platform/basic.h" #include "mongo/base/string_data.h" #include "mongo/bson/util/bson_extract.h" #include "mongo/bson/util/builder.h" +#include "mongo/db/auth/authorization_session.h" #include "mongo/db/commands.h" +#include "mongo/db/commands/generic_gen.h" #include "mongo/db/commands/test_commands_enabled.h" #include "mongo/db/log_process_details.h" +#include "mongo/logv2/log.h" #include "mongo/util/processinfo.h" #include "mongo/util/ramlog.h" @@ -196,5 +201,88 @@ public: } listCommandsCmd; +class CmdLogMessage : public TypedCommand<CmdLogMessage> { +public: + using Request = LogMessageCommand; + + class Invocation final : public InvocationBase { + public: + using InvocationBase::InvocationBase; + + void typedRun(OperationContext* opCtx) { + auto cmd = request(); + + logv2::DynamicAttributes attrs; + attrs.add("msg", cmd.getCommandParameter()); + if (auto extra = cmd.getExtra()) { + attrs.add("extra", *extra); + } + + auto options = logv2::LogOptions{logv2::LogComponent::kDefault}; + LOGV2_IMPL(5060500, getSeverity(cmd), options, "logMessage", attrs); + } + + private: + static logv2::LogSeverity getSeverity(const Request& cmd) { + auto severity = cmd.getSeverity(); + auto optDebugLevel = cmd.getDebugLevel(); + + if (optDebugLevel && (severity != MessageSeverityEnum::kDebug)) { + auto obj = cmd.toBSON({}); + LOGV2_DEBUG(5060599, + 3, + "Non-debug severity levels must not pass 'debugLevel'", + "severity"_attr = obj[Request::kSeverityFieldName].valueStringData(), + "debugLevel"_attr = optDebugLevel.get()); + } + + switch (severity) { + case MessageSeverityEnum::kSevere: + return logv2::LogSeverity::Severe(); + case MessageSeverityEnum::kError: + return logv2::LogSeverity::Error(); + case MessageSeverityEnum::kWarning: + return logv2::LogSeverity::Warning(); + case MessageSeverityEnum::kInfo: + return logv2::LogSeverity::Info(); + case MessageSeverityEnum::kLog: + return logv2::LogSeverity::Log(); + case MessageSeverityEnum::kDebug: + return logv2::LogSeverity::Debug( + boost::get_optional_value_or(optDebugLevel, 1)); + } + + MONGO_UNREACHABLE; + } + + bool supportsWriteConcern() const final { + return false; + } + + void doCheckAuthorization(OperationContext* opCtx) const final { + auto* client = opCtx->getClient(); + auto* as = AuthorizationSession::get(client); + uassert(ErrorCodes::Unauthorized, + "Not authorized to send custom message to log", + as->isAuthorizedForActionsOnResource(ResourcePattern::forClusterResource(), + ActionType::applicationMessage)); + } + + NamespaceString ns() const final { + return NamespaceString(request().getDbName(), ""); + } + }; + + bool adminOnly() const final { + return true; + } + + AllowedOnSecondary secondaryAllowed(ServiceContext*) const final { + return AllowedOnSecondary::kAlways; + } +}; + +MONGO_REGISTER_TEST_COMMAND(CmdLogMessage); + } // namespace } // namespace mongo diff --git a/src/mongo/db/commands/generic.idl b/src/mongo/db/commands/generic.idl new file mode 100644 index 00000000000..74e2f860546 --- /dev/null +++ b/src/mongo/db/commands/generic.idl @@ -0,0 +1,68 @@ +# Copyright (C) 2020-present MongoDB, Inc. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the Server Side Public License, version 1, +# as published by MongoDB, Inc. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# Server Side Public License for more details. +# +# You should have received a copy of the Server Side Public License +# along with this program. If not, see +# <http://www.mongodb.com/licensing/server-side-public-license>. +# +# As a special exception, the copyright holders give permission to link the +# code of portions of this program with the OpenSSL library under certain +# conditions as described in each individual source file and distribute +# linked combinations including the program with the OpenSSL library. You +# must comply with the Server Side Public License in all respects for +# all of the code used other than as permitted herein. If you modify file(s) +# with this exception, you may extend this exception to your version of the +# file(s), but you are not obligated to do so. If you do not wish to do so, +# delete this exception statement from your version. If you delete this +# exception statement from all source files in the program, then also delete +# it in the license file. +# +global: + cpp_namespace: "mongo" + +imports: + - "mongo/idl/basic_types.idl" + +enums: + MessageSeverity: + description: "Severity level to log message at" + type: string + values: + kSevere: "severe" + kError: "error" + kWarning: "warning" + kInfo: "info" + kLog: "log" + kDebug: "debug" + +commands: + logMessage: + description: "Log a message on the server" + namespace: type + type: string + cpp_name: LogMessageCommand + strict: true + fields: + extra: + description: "Structured data to include with log message" + type: object + optional: true + severity: + description: "Severity of log message" + type: MessageSeverity + default: kLog + debugLevel: + description: "When using 'debug' severity, what level of debug is to be used?" + type: int + optional: true + validator: + gte: 1 + lte: 5 diff --git a/src/mongo/embedded/mongo_embedded/mongo_embedded_test.cpp b/src/mongo/embedded/mongo_embedded/mongo_embedded_test.cpp index a3755223912..c913de20fdc 100644 --- a/src/mongo/embedded/mongo_embedded/mongo_embedded_test.cpp +++ b/src/mongo/embedded/mongo_embedded/mongo_embedded_test.cpp @@ -570,6 +570,7 @@ TEST_F(MongodbCAPITest, RunListCommands) { "listDatabases", "listIndexes", "lockInfo", + "logMessage", "ping", "planCacheClear", "planCacheClearFilters", diff --git a/src/mongo/shell/check_log.js b/src/mongo/shell/check_log.js index 82f0792a2b8..76f026af4ba 100644 --- a/src/mongo/shell/check_log.js +++ b/src/mongo/shell/check_log.js @@ -134,7 +134,7 @@ checkLog = (function() { return checkContainsOnceJson(conn, id, attrsDict); }, 'Could not find log entries containing the following id: ' + id + - ', and attrs: ' + attrsDict, + ', and attrs: ' + tojson(attrsDict), timeout, 300, {runHangAnalyzer: false}); diff --git a/src/mongo/util/str_escape.cpp b/src/mongo/util/str_escape.cpp index 9661d4da603..d191fb92252 100644 --- a/src/mongo/util/str_escape.cpp +++ b/src/mongo/util/str_escape.cpp @@ -384,7 +384,7 @@ void escapeForJSON(fmt::memory_buffer& buffer, StringData str) { writer(1, "\\u001b"_sd); break; case 0x1c: - writer(1, "\\u000c"_sd); + writer(1, "\\u001c"_sd); break; case 0x1d: writer(1, "\\u001d"_sd); |