How to use the diagnostic-channel.channel.bindToContext function in diagnostic-channel

To help you get started, we’ve selected a few diagnostic-channel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / node-diagnostic-channel / src / diagnostic-channel-publishers / src / mysql.pub.ts View on Github external
cbidx = i;
                            break;
                        } else if (typeof arguments[i] !== "undefined") {
                            break;
                        }
                    }
                    const cb = arguments[cbidx];

                    const resultContainer = {result: null, startTime: null};
                    if (typeof cb === "function") {
                        // Preserve context on the callback.
                        // If this is one of the functions that we want to track,
                        // then wrap the callback with the tracking wrapper
                        if (cbWrapper) {
                            resultContainer.startTime = process.hrtime();
                            arguments[cbidx] = channel.bindToContext(cbWrapper(resultContainer, cb));
                        } else {
                            arguments[cbidx] = channel.bindToContext(cb);
                        }
                    }
                    const result = originalFunc.apply(this, arguments);
                    resultContainer.result = result;
                    return result;
                };
            }
github microsoft / node-diagnostic-channel / src / diagnostic-channel-publishers / src / mongodb.pub.ts View on Github external
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;
    });
github microsoft / node-diagnostic-channel / src / diagnostic-channel-publishers / src / redis.pub.ts View on Github external
originalRedis.RedisClient.prototype.internal_send_command = function(commandObj) {
        if (commandObj) {
            const cb = commandObj.callback;
            if (!cb || !cb.pubsubBound) {
                const address = this.address;
                const startTime = process.hrtime();

                // Note: augmenting the callback on internal_send_command is correct for context
                // tracking, but may be too low-level for dependency tracking. There are some 'errors'
                // which higher levels expect in some cases
                // However, the only other option is to intercept every individual command.
                commandObj.callback = channel.bindToContext(function(err, result) {
                    const hrDuration = process.hrtime(startTime);
                    /* tslint:disable-next-line:no-bitwise */
                    const duration = (hrDuration[0] * 1e3 + hrDuration[1] / 1e6) | 0;
                    channel.publish("redis", {duration, address, commandObj, err, result});

                    if (typeof cb === "function") {
                        cb.apply(this, arguments);
                    }
                });
                commandObj.callback.pubsubBound = true;
            }
        }

        return originalSend.call(this, commandObj);
    };
github microsoft / node-diagnostic-channel / src / diagnostic-channel-publishers / src / pg.pub.ts View on Github external
function patchCallback(cb?: PostgresCallback): PostgresCallback {
            if (cb && cb[diagnosticOriginalFunc]) {
                cb = cb[diagnosticOriginalFunc];
            }

            const trackingCallback = channel.bindToContext(function(err: Error, res: IPostgresResult): any {
                const end = process.hrtime(start);
                data.result = res && { rowCount: res.rowCount, command: res.command };
                data.error = err;
                data.duration = Math.ceil((end[0] * 1e3) + (end[1] / 1e6));
                channel.publish("postgres", data);

                // emulate weird internal behavior in pg@6
                // on success, the callback is called *before* query events are emitted
                // on failure, the callback is called *instead of* the query emitting events
                // with no events, that means no promises (since the promise is resolved/rejected in an event handler)
                // since we are always inserting ourselves as a callback, we have to restore the original
                // behavior if the user didn't provide one themselves
                if (err) {
                    if (cb) {
                        return cb.apply(this, arguments);
                    } else if (queryResult && queryResult instanceof EventEmitter) {
github microsoft / node-diagnostic-channel / src / diagnostic-channel-publishers / src / tedious.pub.ts View on Github external
function getPatchedCallback(origCallback: CompletionCallback) {
            const start = process.hrtime();
            let data: ITediousData = {
                query: {},
                database: {
                    host: null,
                    port: null,
                },
                result: null,
                error: null,
                duration: 0,
            };
            return channel.bindToContext(function(err: Error | null, rowCount?: number | null, rows?: any) {
                const end = process.hrtime(start);
                data = { ...data,
                    database: {
                        host: this.connection.config.server,
                        port: this.connection.config.options.port,
                    },
                    result: !err && { rowCount, rows },
                    query: {
                        text: this.parametersByName.statement.value,
                    },
                    error: err,
                    duration: Math.ceil((end[0] * 1e3) + (end[1] / 1e6)),
                };
                channel.publish("tedious", data);
                origCallback.call(this, err, rowCount, rows);
            });
github microsoft / node-diagnostic-channel / src / diagnostic-channel-publishers / src / mongodb-core.pub.ts View on Github external
this.s.pool.logout = function contextPreservingLogout() {
            if (typeof arguments[1] === "function") {
                arguments[1] = channel.bindToContext(arguments[1]);
            }
            return originalLogout.apply(this, arguments);
        };
        return ret;
github microsoft / node-diagnostic-channel / src / diagnostic-channel-publishers / src / pg-pool.pub.ts View on Github external
originalPgPool.prototype.connect = function connect(callback?: Function): void | Promise {
        if (callback) {
            arguments[0] = channel.bindToContext(callback);
        }

        return originalConnect.apply(this, arguments);
    };
github microsoft / node-diagnostic-channel / src / diagnostic-channel-publishers / src / mongodb.pub.ts View on Github external
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;

diagnostic-channel

Provides a context-saving pub/sub channel to connect diagnostic event publishers and subscribers

MIT
Latest version published 9 months ago

Package Health Score

72 / 100
Full package analysis

Similar packages