/
mongodb.pub.ts
executable file
·193 lines (170 loc) · 7.39 KB
/
mongodb.pub.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import {channel, IModulePatcher, PatchFunction} from "diagnostic-channel";
export interface IMongoData {
startedData: {
databaseName?: string;
command?: any;
};
event: {
commandName?: string;
duration?: number;
failure?: string;
reply?: any;
};
succeeded: boolean;
}
const mongodbPatchFunction: PatchFunction = function(originalMongo) {
const listener = originalMongo.instrument({
operationIdGenerator: {
next: function() {
return channel.bindToContext((cb) => cb());
},
},
});
const eventMap = {};
listener.on("started", function(event) {
if (eventMap[event.requestId]) {
// Note: Mongo can generate 2 completely separate requests
// which share the same requestId, if a certain race condition is triggered.
// For now, we accept that this can happen and potentially miss or mislabel some events.
return;
}
eventMap[event.requestId] = event;
});
listener.on("succeeded", function(event) {
const startedData = eventMap[event.requestId];
if (startedData) {
delete eventMap[event.requestId];
}
event.operationId(() => channel.publish<IMongoData>("mongodb", {startedData, event, succeeded: true}));
});
listener.on("failed", function(event) {
const startedData = eventMap[event.requestId];
if (startedData) {
delete eventMap[event.requestId];
}
event.operationId(() => channel.publish<IMongoData>("mongodb", {startedData, event, succeeded: false}));
});
return originalMongo;
};
const mongodb3PatchFunction: PatchFunction = function(originalMongo) {
const listener = originalMongo.instrument();
const eventMap = {};
const contextMap = {};
listener.on("started", function(event) {
if (eventMap[event.requestId]) {
// Note: Mongo can generate 2 completely separate requests
// which share the same requestId, if a certain race condition is triggered.
// For now, we accept that this can happen and potentially miss or mislabel some events.
return;
}
contextMap[event.requestId] = channel.bindToContext((cb) => cb());
eventMap[event.requestId] = event;
});
listener.on("succeeded", function(event) {
const startedData = eventMap[event.requestId];
if (startedData) {
delete eventMap[event.requestId];
}
if (typeof event === "object" && typeof contextMap[event.requestId] === "function") {
contextMap[event.requestId](() => channel.publish<IMongoData>("mongodb", {startedData, event, succeeded: true}));
delete contextMap[event.requestId];
}
});
listener.on("failed", function(event) {
const startedData = eventMap[event.requestId];
if (startedData) {
delete eventMap[event.requestId];
}
if (typeof event === "object" && typeof contextMap[event.requestId] === "function") {
contextMap[event.requestId](() => channel.publish<IMongoData>("mongodb", {startedData, event, succeeded: false}));
delete contextMap[event.requestId];
}
});
return originalMongo;
};
// In mongodb 3.3.0, mongodb-core was merged into mongodb, so the same patching
// can be used here. this.s.pool was changed to this.s.coreTopology.s.pool
const mongodbcorePatchFunction = function(originalMongo) {
const originalConnect = originalMongo.Server.prototype.connect;
originalMongo.Server.prototype.connect = function contextPreservingConnect() {
const ret = originalConnect.apply(this, arguments);
// Messages sent to mongo progress through a pool
// This can result in context getting mixed between different responses
// so we wrap the callbacks to restore appropriate state
const originalWrite = this.s.coreTopology.s.pool.write;
this.s.coreTopology.s.pool.write = function contextPreservingWrite() {
const cbidx = typeof arguments[1] === "function" ? 1 : 2;
if (typeof arguments[cbidx] === "function" ) {
arguments[cbidx] = channel.bindToContext(arguments[cbidx]);
}
return originalWrite.apply(this, arguments);
};
// Logout is a special case, it doesn't call the write function but instead
// directly calls into connection.write
const originalLogout = this.s.coreTopology.s.pool.logout;
this.s.coreTopology.s.pool.logout = function contextPreservingLogout() {
if (typeof arguments[1] === "function") {
arguments[1] = channel.bindToContext(arguments[1]);
}
return originalLogout.apply(this, arguments);
};
return ret;
};
return originalMongo;
};
const mongodb330PatchFunction: PatchFunction = function(originalMongo) {
mongodbcorePatchFunction(originalMongo); // apply mongodb-core patches
const listener = originalMongo.instrument();
const eventMap = {};
const contextMap = {};
listener.on("started", function(event) {
if (eventMap[event.requestId]) {
// Note: Mongo can generate 2 completely separate requests
// which share the same requestId, if a certain race condition is triggered.
// For now, we accept that this can happen and potentially miss or mislabel some events.
return;
}
contextMap[event.requestId] = channel.bindToContext((cb) => cb());
eventMap[event.requestId] = event;
});
listener.on("succeeded", function(event) {
const startedData = eventMap[event.requestId];
if (startedData) {
delete eventMap[event.requestId];
}
if (typeof event === "object" && typeof contextMap[event.requestId] === "function") {
contextMap[event.requestId](() => channel.publish<IMongoData>("mongodb", {startedData, event, succeeded: true}));
delete contextMap[event.requestId];
}
});
listener.on("failed", function(event) {
const startedData = eventMap[event.requestId];
if (startedData) {
delete eventMap[event.requestId];
}
if (typeof event === "object" && typeof contextMap[event.requestId] === "function") {
contextMap[event.requestId](() => channel.publish<IMongoData>("mongodb", {startedData, event, succeeded: false}));
delete contextMap[event.requestId];
}
});
return originalMongo;
};
export const mongo2: IModulePatcher = {
versionSpecifier: ">= 2.0.0 <= 3.0.5",
patch: mongodbPatchFunction,
};
export const mongo3: IModulePatcher = {
versionSpecifier: "> 3.0.5 < 3.3.0",
patch: mongodb3PatchFunction,
};
export const mongo330: IModulePatcher = {
versionSpecifier: ">= 3.3.0 < 4.0.0",
patch: mongodb330PatchFunction,
};
export function enable() {
channel.registerMonkeyPatch("mongodb", mongo2);
channel.registerMonkeyPatch("mongodb", mongo3);
channel.registerMonkeyPatch("mongodb", mongo330);
}