Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mysql2 probe #541

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions probes/mysql2-probe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*******************************************************************************
* Copyright 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
'use strict';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If mysql and mysql2 have identical APIs, can this file not be module.exports = require('./mysql-probe');? This might side-step test concerns, because it wouldn't introduce any new code.

var Probe = require('../lib/probe.js');
var aspect = require('../lib/aspect.js');
var request = require('../lib/request.js');
var util = require('util');
var am = require('../');

function MySql2Probe() {
Probe.call(this, 'mysql2');
}
util.inherits(MySql2Probe, Probe);

MySql2Probe.prototype.attach = function(name, target) {
var that = this;
if (name != 'mysql2') return target;
target.__ddProbeAttached__ = true;
var data = {};
aspect.after(target, 'createConnection', data, function(target, methodName, args, probeData, rc) {
aspect.before(rc, 'query', function(target, methodName, methodArgs, probeData) {
var method = 'query';
that.metricsProbeStart(probeData, method, methodArgs);
that.requestProbeStart(probeData, method, methodArgs);
if (aspect.findCallbackArg(methodArgs) != undefined) {
aspect.aroundCallback(methodArgs, probeData, function(target, args) {
// Call the transaction link with a name and the callback for strong trace
var callbackPosition = aspect.findCallbackArg(methodArgs);
if (typeof callbackPosition != 'undefined') {
aspect.strongTraceTransactionLink('mysql2: ', method, methodArgs[callbackPosition]);
}

that.metricsProbeEnd(probeData, method, methodArgs);
that.requestProbeEnd(probeData, method, methodArgs);
});
}
});
return rc;
});
return target;
};

/*
* Lightweight metrics probe for MySQL queries
*
* These provide:
* time: time event started
* query: The SQL executed
* duration: the time for the request to respond
*/
MySql2Probe.prototype.metricsEnd = function(probeData, method, methodArgs) {
if (probeData && probeData.timer) {
probeData.timer.stop();
var eventTimer = probeData.timer;
am.emit('mysql', {
time: eventTimer.startTimeMillis,
query: JSON.stringify(methodArgs[0]),
duration: eventTimer.timeDelta,
});
}
};

/*
* Heavyweight request probes for MySQL queries
*/
MySql2Probe.prototype.requestStart = function(probeData, method, methodArgs) {
probeData.req = request.startRequest('mysql', 'query', false, probeData.timer);
};

MySql2Probe.prototype.requestEnd = function(probeData, method, methodArgs) {
if (probeData && probeData.req) probeData.req.stop({ sql: JSON.stringify(methodArgs[0]) });
};

module.exports = MySql2Probe;