Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit 9124b67

Browse files
committed
feat(op-msg): add support for OP_MSG to command monitoring
1 parent c81f609 commit 9124b67

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

lib/connection/apm.js

+32-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
2-
const Query = require('../connection/commands').Query;
2+
// const Query = require('../connection/commands').Query;
3+
const Msg = require('../connection/msg').Msg;
34
const KillCursor = require('../connection/commands').KillCursor;
45
const GetMore = require('../connection/commands').GetMore;
56
const calculateDurationInMs = require('../utils').calculateDurationInMs;
@@ -18,7 +19,7 @@ const SENSITIVE_COMMANDS = new Set([
1819
]);
1920

2021
// helper methods
21-
const extractCommandName = command => Object.keys(command)[0];
22+
const extractCommandName = commandDoc => Object.keys(commandDoc)[0];
2223
const namespace = command => command.ns;
2324
const databaseName = command => command.ns.split('.')[0];
2425
const collectionName = command => command.ns.split('.')[1];
@@ -76,41 +77,44 @@ const extractCommand = command => {
7677
};
7778
}
7879

79-
command = command instanceof Query ? command.query : command.command;
80-
if (command.$query == null) {
81-
return command;
80+
if (command instanceof Msg) {
81+
return command.command;
8282
}
8383

84-
let result;
85-
if (command.ns === 'admin.$cmd') {
86-
// upconvert legacy command
87-
result = Object.assign({}, command.query.$query);
88-
} else {
89-
// upconvert legacy find command
90-
result = { find: collectionName(command) };
91-
Object.keys(LEGACY_FIND_QUERY_MAP).forEach(key => {
92-
if (typeof command.query[key] !== 'undefined')
93-
result[LEGACY_FIND_QUERY_MAP[key]] = command.query[key];
84+
if (command.query && command.query.$query) {
85+
let result;
86+
if (command.ns === 'admin.$cmd') {
87+
// upconvert legacy command
88+
result = Object.assign({}, command.query.$query);
89+
} else {
90+
// upconvert legacy find command
91+
result = { find: collectionName(command) };
92+
Object.keys(LEGACY_FIND_QUERY_MAP).forEach(key => {
93+
if (typeof command.query[key] !== 'undefined')
94+
result[LEGACY_FIND_QUERY_MAP[key]] = command.query[key];
95+
});
96+
}
97+
98+
Object.keys(LEGACY_FIND_OPTIONS_MAP).forEach(key => {
99+
if (typeof command[key] !== 'undefined') result[LEGACY_FIND_OPTIONS_MAP[key]] = command[key];
94100
});
95-
}
96101

97-
Object.keys(LEGACY_FIND_OPTIONS_MAP).forEach(key => {
98-
if (typeof command[key] !== 'undefined') result[LEGACY_FIND_OPTIONS_MAP[key]] = command[key];
99-
});
102+
OP_QUERY_KEYS.forEach(key => {
103+
if (command[key]) result[key] = command[key];
104+
});
100105

101-
OP_QUERY_KEYS.forEach(key => {
102-
if (command[key]) result[key] = command[key];
103-
});
106+
if (typeof command.pre32Limit !== 'undefined') {
107+
result.limit = command.pre32Limit;
108+
}
104109

105-
if (typeof command.pre32Limit !== 'undefined') {
106-
result.limit = command.pre32Limit;
107-
}
110+
if (command.query.$explain) {
111+
return { explain: result };
112+
}
108113

109-
if (command.query.$explain) {
110-
return { explain: result };
114+
return result;
111115
}
112116

113-
return result;
117+
return command.query ? command.query : command;
114118
};
115119

116120
const extractReply = (command, reply) => {

0 commit comments

Comments
 (0)