diff options
author | Gabriel Russell <gabriel.russell@mongodb.com> | 2020-07-10 17:54:23 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-07-23 15:06:00 +0000 |
commit | b03eec1e1f0443ffa1b7a5da1805dd2ed3145f44 (patch) | |
tree | 27901d65a9a3eb5a27f0d25bdba10bd18e17c75c /src/mongo/rpc | |
parent | bae276d5c8f255f34bdf1eeb045133b170bf735c (diff) | |
download | mongo-b03eec1e1f0443ffa1b7a5da1805dd2ed3145f44.tar.gz |
SERVER-47922 pids and names in client metadata in debug builds
Diffstat (limited to 'src/mongo/rpc')
-rw-r--r-- | src/mongo/rpc/metadata/client_metadata.cpp | 45 | ||||
-rw-r--r-- | src/mongo/rpc/metadata/client_metadata.h | 13 | ||||
-rw-r--r-- | src/mongo/rpc/metadata/client_metadata_test.cpp | 64 |
3 files changed, 69 insertions, 53 deletions
diff --git a/src/mongo/rpc/metadata/client_metadata.cpp b/src/mongo/rpc/metadata/client_metadata.cpp index adbb2eb39e8..e9203e66bb2 100644 --- a/src/mongo/rpc/metadata/client_metadata.cpp +++ b/src/mongo/rpc/metadata/client_metadata.cpp @@ -43,6 +43,7 @@ #include "mongo/db/operation_context.h" #include "mongo/logv2/log.h" #include "mongo/s/is_mongos.h" +#include "mongo/util/debug_util.h" #include "mongo/util/processinfo.h" #include "mongo/util/str.h" @@ -57,6 +58,7 @@ constexpr auto kOperatingSystem = "os"_sd; constexpr auto kArchitecture = "architecture"_sd; constexpr auto kName = "name"_sd; +constexpr auto kPid = "pid"_sd; constexpr auto kType = "type"_sd; constexpr auto kVersion = "version"_sd; @@ -333,37 +335,27 @@ void ClientMetadata::serialize(StringData driverName, ProcessInfo processInfo; + std::string appName; + if (kDebugBuild) { + appName = processInfo.getProcessName(); + if (appName.length() > kMaxApplicationNameByteLength) { + static constexpr auto kEllipsis = "..."_sd; + appName.replace(appName.begin() + kMaxApplicationNameByteLength - kEllipsis.size(), + appName.end(), + kEllipsis.begin(), + kEllipsis.end()); + } + } + serializePrivate(driverName, driverVersion, processInfo.getOsType(), processInfo.getOsName(), processInfo.getArch(), processInfo.getOsVersion(), - builder); -} - -void ClientMetadata::serializePrivate(StringData driverName, - StringData driverVersion, - StringData osType, - StringData osName, - StringData osArchitecture, - StringData osVersion, - BSONObjBuilder* builder) { - BSONObjBuilder metaObjBuilder(builder->subobjStart(kMetadataDocumentName)); - - { - BSONObjBuilder subObjBuilder(metaObjBuilder.subobjStart(kDriver)); - subObjBuilder.append(kName, driverName); - subObjBuilder.append(kVersion, driverVersion); - } - - { - BSONObjBuilder subObjBuilder(metaObjBuilder.subobjStart(kOperatingSystem)); - subObjBuilder.append(kType, osType); - subObjBuilder.append(kName, osName); - subObjBuilder.append(kArchitecture, osArchitecture); - subObjBuilder.append(kVersion, osVersion); - } + appName, + builder) + .ignore(); } Status ClientMetadata::serialize(StringData driverName, @@ -405,6 +397,9 @@ Status ClientMetadata::serializePrivate(StringData driverName, if (!appName.empty()) { BSONObjBuilder subObjBuilder(metaObjBuilder.subobjStart(kApplication)); subObjBuilder.append(kName, appName); + if (kDebugBuild) { + subObjBuilder.append(kPid, ProcessId::getCurrent().toString()); + } } { diff --git a/src/mongo/rpc/metadata/client_metadata.h b/src/mongo/rpc/metadata/client_metadata.h index eb467caab6a..524fdda3710 100644 --- a/src/mongo/rpc/metadata/client_metadata.h +++ b/src/mongo/rpc/metadata/client_metadata.h @@ -199,19 +199,6 @@ public: /** * Create a new client metadata document. * - * Exposed for Unit Test purposes - */ - static void serializePrivate(StringData driverName, - StringData driverVersion, - StringData osType, - StringData osName, - StringData osArchitecture, - StringData osVersion, - BSONObjBuilder* builder); - - /** - * Create a new client metadata document. - * * driverName - name of the driver * driverVersion - a string for the driver version * osType - name of host operating system of client, i.e. uname -s diff --git a/src/mongo/rpc/metadata/client_metadata_test.cpp b/src/mongo/rpc/metadata/client_metadata_test.cpp index 815df3ad87c..bae05d00e97 100644 --- a/src/mongo/rpc/metadata/client_metadata_test.cpp +++ b/src/mongo/rpc/metadata/client_metadata_test.cpp @@ -38,8 +38,10 @@ #include "mongo/bson/bsonobj.h" #include "mongo/bson/bsonobjbuilder.h" +#include "mongo/bson/json.h" #include "mongo/s/is_mongos.h" #include "mongo/unittest/unittest.h" +#include "mongo/util/processinfo.h" #include "mongo/util/scopeguard.h" namespace mongo { @@ -48,6 +50,7 @@ constexpr auto kMetadataDoc = "client"_sd; constexpr auto kApplication = "application"_sd; constexpr auto kDriver = "driver"_sd; constexpr auto kName = "name"_sd; +constexpr auto kPid = "pid"_sd; constexpr auto kType = "type"_sd; constexpr auto kVersion = "version"_sd; constexpr auto kOperatingSystem = "os"_sd; @@ -74,7 +77,7 @@ constexpr auto kUnknown = "unkown"_sd; TEST(ClientMetadatTest, TestLoopbackTest) { - // Serialize without application name + // serializePrivate with appName { BSONObjBuilder builder; ASSERT_OK(ClientMetadata::serializePrivate("a", "b", "c", "d", "e", "f", "g", &builder)); @@ -84,23 +87,39 @@ TEST(ClientMetadatTest, TestLoopbackTest) { ASSERT_OK(swParseStatus.getStatus()); ASSERT_EQUALS("g", swParseStatus.getValue().get().getApplicationName()); + auto pid = ProcessId::getCurrent().toString(); + + using BOB = BSONObjBuilder; BSONObj outDoc = - BSON(kMetadataDoc << BSON(kApplication - << BSON(kName << "g") << kDriver - << BSON(kName << "a" << kVersion << "b") << kOperatingSystem - << BSON(kType << "c" << kName << "d" << kArchitecture << "e" - << kVersion << "f"))); + BOB{} + .append(kMetadataDoc, + BOB{} + .append(kApplication, + BOB{} + .append(kName, "g") + .appendElements(kDebugBuild ? BOB{}.append(kPid, pid).obj() + : BOB{}.obj()) + .obj()) + .append(kDriver, BOB{}.append(kName, "a").append(kVersion, "b").obj()) + .append(kOperatingSystem, + BOB{} + .append(kType, "c") + .append(kName, "d") + .append(kArchitecture, "e") + .append(kVersion, "f") + .obj()) + .obj()) + .obj(); ASSERT_BSONOBJ_EQ(obj, outDoc); } - // Serialize without application name + // serializePrivate without appName { BSONObjBuilder builder; - ClientMetadata::serializePrivate("a", "b", "c", "d", "e", "f", &builder); + ASSERT_OK(ClientMetadata::serializePrivate( + "a", "b", "c", "d", "e", "f", std::string{}, &builder)); auto obj = builder.obj(); - auto swParseStatus = ClientMetadata::parse(obj[kMetadataDoc]); - ASSERT_OK(swParseStatus.getStatus()); BSONObj outDoc = BSON(kMetadataDoc << BSON(kDriver @@ -295,12 +314,27 @@ TEST(ClientMetadatTest, TestMongoSAppend) { constexpr auto kClient = "client"_sd; constexpr auto kHost = "host"_sd; + auto pid = ProcessId::getCurrent().toString(); + + using BOB = BSONObjBuilder; BSONObj outDoc = - BSON(kApplication << BSON(kName << "g") << kDriver << BSON(kName << "a" << kVersion << "b") - << kOperatingSystem - << BSON(kType << "c" << kName << "d" << kArchitecture << "e" << kVersion - << "f") - << kMongos << BSON(kHost << "h" << kClient << "i" << kVersion << "j")); + BOB{} + .append(kApplication, + BOB{} + .append(kName, "g") + .appendElements(kDebugBuild ? BOB{}.append(kPid, pid).obj() : BOB{}.obj()) + .obj()) + .append(kDriver, BOB{}.append(kName, "a").append(kVersion, "b").obj()) + .append(kOperatingSystem, + BOB{} + .append(kType, "c") + .append(kName, "d") + .append(kArchitecture, "e") + .append(kVersion, "f") + .obj()) + .append(kMongos, + BOB{}.append(kHost, "h").append(kClient, "i").append(kVersion, "j").obj()) + .obj(); ASSERT_BSONOBJ_EQ(doc, outDoc); } |