Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
477 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
# Changelog | ||
|
||
## Unreleased | ||
- Support tracing for the mysql module. | ||
|
||
## 1.24.0 | ||
- Collect healthcheck results. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
'use strict'; | ||
|
||
var shimmer = require('shimmer'); | ||
|
||
var requireHook = require('../../util/requireHook'); | ||
var transmission = require('../transmission'); | ||
var tracingUtil = require('../tracingUtil'); | ||
var hook = require('../hook'); | ||
|
||
var isActive = false; | ||
|
||
exports.init = function() { | ||
requireHook.on('mysql', instrument); | ||
}; | ||
|
||
|
||
function instrument(mysql) { | ||
var Connection = Object.getPrototypeOf(mysql.createConnection({})); | ||
var Pool = Object.getPrototypeOf(mysql.createPool({})); | ||
shimmer.wrap(Connection, 'query', shimQuery); | ||
shimmer.wrap(Pool, 'query', shimQuery); | ||
shimmer.wrap(Pool, 'getConnection', shimGetConnection); | ||
} | ||
|
||
|
||
function shimQuery(original) { | ||
return function() { | ||
if (isActive) { | ||
return instrumentedQuery(this, original, arguments[0], arguments[1], arguments[2]); | ||
} | ||
return original.apply(this, arguments); | ||
}; | ||
} | ||
|
||
|
||
function instrumentedQuery(ctx, originalQuery, statementOrOpts, valuesOrCallback, optCallback) { | ||
var argsForOriginalQuery = [statementOrOpts, valuesOrCallback]; | ||
if (typeof optCallback !== 'undefined') { | ||
argsForOriginalQuery.push(optCallback); | ||
} | ||
|
||
var uid = hook.initAndPreSimulated(); | ||
var tracingSuppressed = hook.isTracingSuppressed(uid); | ||
if (tracingSuppressed || hook.containsExitSpan(uid)) { | ||
return originalQuery.apply(ctx, argsForOriginalQuery); | ||
} | ||
|
||
var host; | ||
var port; | ||
var user; | ||
var db; | ||
if (ctx.config) { | ||
if (ctx.config.connectionConfig) { | ||
host = ctx.config.connectionConfig.host; | ||
port = ctx.config.connectionConfig.port; | ||
user = ctx.config.connectionConfig.user; | ||
db = ctx.config.connectionConfig.database; | ||
} else { | ||
host = ctx.config.host; | ||
port = ctx.config.port; | ||
user = ctx.config.user; | ||
db = ctx.config.database; | ||
} | ||
} | ||
|
||
hook.markAsExitSpan(uid); | ||
|
||
var spanId = tracingUtil.generateRandomSpanId(); | ||
var traceId = hook.getTraceId(uid); | ||
var parentId = undefined; | ||
if (!traceId) { | ||
traceId = spanId; | ||
} else { | ||
parentId = hook.getParentSpanId(uid); | ||
} | ||
|
||
var span = { | ||
s: spanId, | ||
t: traceId, | ||
p: parentId, | ||
f: tracingUtil.getFrom(), | ||
async: false, | ||
error: false, | ||
ec: 0, | ||
ts: Date.now(), | ||
d: 0, | ||
n: 'mysql', | ||
b: { | ||
s: 1 | ||
}, | ||
stack: tracingUtil.getStackTrace(instrumentedQuery), | ||
data: { | ||
mysql: { | ||
stmt: typeof statementOrOpts === 'string' ? statementOrOpts : statementOrOpts.sql, | ||
host: host, | ||
port: port, | ||
user: user, | ||
db: db | ||
} | ||
} | ||
}; | ||
hook.setSpanId(uid, span.s); | ||
|
||
var originalCallback = argsForOriginalQuery[argsForOriginalQuery.length - 1]; | ||
argsForOriginalQuery[argsForOriginalQuery.length - 1] = function onQueryResult(error) { | ||
if (error) { | ||
span.ec = 1; | ||
span.error = true; | ||
span.data.mysql.error = error.message; | ||
} | ||
|
||
span.d = Date.now() - span.ts; | ||
transmission.addSpan(span); | ||
hook.postAndDestroySimulated(uid); | ||
|
||
if (originalCallback) { | ||
return originalCallback.apply(this, arguments); | ||
} | ||
}; | ||
|
||
return originalQuery.apply(ctx, argsForOriginalQuery); | ||
} | ||
|
||
function shimGetConnection(original) { | ||
return function(cb) { | ||
var targetContextUid = hook.getCurrentUid(); | ||
return original.call(this, wrappedCallback); | ||
|
||
function wrappedCallback() { | ||
if (hook.isUidExisting(targetContextUid)) { | ||
var originalContextUid = hook.getCurrentUid(); | ||
hook.preAsync(targetContextUid); | ||
hook.initAndPreSimulated(); | ||
var result = cb.apply(this, arguments); | ||
hook.preAsync(originalContextUid); | ||
return result; | ||
} | ||
|
||
return cb.apply(this, arguments); | ||
} | ||
}; | ||
} | ||
|
||
exports.activate = function() { | ||
isActive = true; | ||
}; | ||
|
||
|
||
exports.deactivate = function() { | ||
isActive = false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* eslint-disable no-console */ | ||
|
||
'use strict'; | ||
|
||
require('../../')({ | ||
agentPort: process.env.AGENT_PORT, | ||
level: 'info', | ||
tracing: { | ||
enabled: process.env.TRACING_ENABLED === 'true', | ||
forceTransmissionStartingAt: 1 | ||
} | ||
}); | ||
|
||
var mysql = require('mysql'); | ||
var bodyParser = require('body-parser'); | ||
var express = require('express'); | ||
var morgan = require('morgan'); | ||
|
||
var app = express(); | ||
var logPrefix = 'Express / MySQL App (' + process.pid + '):\t'; | ||
var pool = mysql.createPool({ | ||
connectionLimit: 5, | ||
host: process.env.MYSQL_HOST, | ||
user: process.env.MYSQL_USER, | ||
password: process.env.MYSQL_PW, | ||
database: process.env.MYSQL_DB | ||
}); | ||
|
||
pool.getConnection(function(err, connection) { | ||
if (err) { | ||
log('Failed to get connection for table creation', err); | ||
return; | ||
} | ||
|
||
connection.query('CREATE TABLE random_values (value double);', function(queryError) { | ||
connection.release(); | ||
|
||
if (queryError && queryError.code !== 'ER_TABLE_EXISTS_ERROR') { | ||
log('Failed to execute query for table creation', queryError); | ||
return; | ||
} | ||
|
||
log('Successfully created table'); | ||
}); | ||
}); | ||
|
||
if (process.env.WITH_STDOUT) { | ||
app.use(morgan(logPrefix + ':method :url :status')); | ||
} | ||
|
||
app.use(bodyParser.json()); | ||
|
||
|
||
app.get('/', function(req, res) { | ||
res.sendStatus(200); | ||
}); | ||
|
||
app.get('/values', function(req, res) { | ||
pool.query('SELECT value FROM random_values', function(queryError, results) { | ||
if (queryError) { | ||
log('Failed to execute query', queryError); | ||
res.sendStatus(500); | ||
return; | ||
} | ||
|
||
res.json(results.map(function(result) { | ||
return result.value; | ||
})); | ||
}); | ||
}); | ||
|
||
app.post('/values', function(req, res) { | ||
pool.getConnection(function(err, connection) { | ||
if (err) { | ||
log('Failed to get connection', err); | ||
res.sendStatus(500); | ||
return; | ||
} | ||
|
||
connection.query('INSERT INTO random_values (value) VALUES (?)', [req.query.value], function(queryError) { | ||
connection.release(); | ||
|
||
if (queryError) { | ||
log('Failed to execute query', queryError); | ||
res.sendStatus(500); | ||
return; | ||
} | ||
|
||
res.sendStatus(200); | ||
}); | ||
}); | ||
}); | ||
|
||
app.listen(process.env.APP_PORT, function() { | ||
log('Listening on port: ' + process.env.APP_PORT); | ||
}); | ||
|
||
function log() { | ||
var args = Array.prototype.slice.call(arguments); | ||
args[0] = logPrefix + args[0]; | ||
console.log.apply(console, args); | ||
} |
Oops, something went wrong.