| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- const core_1 = require("@opencensus/core");
- const shimmer = require("shimmer");
- class Mysql2Plugin extends core_1.BasePlugin {
- constructor(moduleName) {
- super(moduleName);
- this.internalFileList = {
- '1 - 3': {
- 'Connection': 'lib/connection',
- 'Pool': 'lib/pool'
- }
- };
- }
- applyPatch() {
- this.logger.debug('Patched Mysql2');
- if (this.internalFilesExports.Connection) {
- this.logger.debug('patching mysql2.Connection.createQuery');
- shimmer.wrap(this.internalFilesExports.Connection, 'createQuery', this.getPatchCreateQuery());
- }
- if (this.internalFilesExports.Pool) {
- this.logger.debug('patching mysql2.Pool.prototype.getConnection');
- shimmer.wrap(this.internalFilesExports.Pool.prototype, 'getConnection', this.getPatchGetConnection());
- }
- return this.moduleExports;
- }
- applyUnpatch() {
- shimmer.unwrap(this.internalFilesExports.Connection, 'createQuery');
- shimmer.unwrap(this.internalFilesExports.Pool.prototype, 'getConnection');
- }
- getPatchCreateQuery() {
- const plugin = this;
- return (original) => {
- return function (...args) {
- const span = plugin.tracer.startChildSpan('mysql-query', core_1.SpanKind.CLIENT);
- if (span === null)
- return original.apply(this, arguments);
- const query = original.apply(this, arguments);
- span.addAttribute('sql', query.sql);
- if (plugin.options.detailedCommands === true && query.values) {
- span.addAttribute('values', query.values);
- }
- if (typeof query._callback === 'function') {
- query._callback = plugin.patchEnd(span, query._callback);
- }
- else {
- query.on('end', function () {
- span.end();
- });
- }
- return query;
- };
- };
- }
- getPatchGetConnection() {
- const plugin = this;
- return (original) => {
- return function (cb) {
- return original.call(this, plugin.tracer.wrap(cb));
- };
- };
- }
- patchEnd(span, resultHandler) {
- const plugin = this;
- const patchedEnd = function (err, res) {
- if (plugin.options.detailedCommands === true && err instanceof Error) {
- span.addAttribute('error', err.message);
- }
- if (span.ended === false) {
- span.end();
- }
- if (resultHandler) {
- return resultHandler.apply(this, arguments);
- }
- };
- return this.tracer.wrap(patchedEnd);
- }
- }
- exports.Mysql2Plugin = Mysql2Plugin;
- const plugin = new Mysql2Plugin('mysql2');
- exports.plugin = plugin;
|