/
mongodb.sub.ts
executable file
·43 lines (39 loc) · 1.61 KB
/
mongodb.sub.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import TelemetryClient = require("../../Library/TelemetryClient");
import { channel, IStandardEvent } from "diagnostic-channel";
import { mongodb } from "diagnostic-channel-publishers";
let clients: TelemetryClient[] = [];
export const subscriber = (event: IStandardEvent<mongodb.IMongoData>) => {
if (event.data.event.commandName === "ismaster") {
// suppress noisy ismaster commands
return;
}
clients.forEach((client) => {
const dbName = (event.data.startedData && event.data.startedData.databaseName) || "Unknown database";
client.trackDependency(
{
target: dbName,
data: event.data.event.commandName,
name: event.data.event.commandName,
duration: event.data.event.duration,
success: event.data.succeeded,
/* TODO: transmit result code from mongo */
resultCode: event.data.succeeded ? "0" : "1",
dependencyTypeName: 'mongodb'
});
});
};
export function enable(enabled: boolean, client: TelemetryClient) {
if (enabled) {
if (clients.length === 0) {
channel.subscribe<mongodb.IMongoData>("mongodb", subscriber);
};
clients.push(client);
} else {
clients = clients.filter((c) => c != client);
if (clients.length === 0) {
channel.unsubscribe("mongodb", subscriber);
}
}
}