skywalking-backend-js-netease
Version:
The NodeJS agent for Apache SkyWalking
121 lines • 5.26 kB
JavaScript
;
/*!
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const SwPlugin_1 = require("../core/SwPlugin");
const ContextManager_1 = tslib_1.__importDefault(require("../trace/context/ContextManager"));
const Component_1 = require("../trace/Component");
const Tag_1 = tslib_1.__importDefault(require("../Tag"));
const Tracing_pb_1 = require("../proto/language-agent/Tracing_pb");
const AgentConfig_1 = tslib_1.__importDefault(require("../config/AgentConfig"));
const fs = tslib_1.__importStar(require("fs"));
const path = tslib_1.__importStar(require("path"));
class MySQL2Plugin {
constructor() {
this.module = 'mysql2';
this.versions = '*';
}
getVersion(installer) {
let indexPath = installer.resolve(this.module);
let packageSJonStr = fs.readFileSync(`${path.dirname(indexPath)}${path.sep}package.json`, { encoding: 'utf-8' });
const pkg = JSON.parse(packageSJonStr);
return pkg.version;
}
install(installer) {
const Connection = installer.require('mysql2').Connection;
const _query = Connection.prototype.query;
Connection.prototype.query = function (sql, values, cb) {
let query;
const host = `${this.config.host}:${this.config.port}`;
const span = ContextManager_1.default.current.newExitSpan('mysql/query', Component_1.Component.MYSQL);
span.start();
try {
span.component = Component_1.Component.MYSQL;
span.layer = Tracing_pb_1.SpanLayer.DATABASE;
span.peer = host;
span.tag(Tag_1.default.dbType('Mysql'));
span.tag(Tag_1.default.dbInstance(`${this.config.database}`));
let _sql;
let _values;
let streaming;
if (typeof sql === 'function') {
sql = SwPlugin_1.wrapCallback(span, sql, 0);
}
else if (typeof sql === 'object') {
_sql = sql.sql;
if (typeof values === 'function') {
values = SwPlugin_1.wrapCallback(span, values, 0);
_values = sql.values;
}
else if (values !== undefined) {
_values = values;
if (typeof cb === 'function') {
cb = SwPlugin_1.wrapCallback(span, cb, 0);
}
else {
streaming = true;
}
}
else {
streaming = true;
}
}
else {
_sql = sql;
if (typeof values === 'function') {
values = SwPlugin_1.wrapCallback(span, values, 0);
}
else if (values !== undefined) {
_values = values;
if (typeof cb === 'function') {
cb = SwPlugin_1.wrapCallback(span, cb, 0);
}
else {
streaming = true;
}
}
else {
streaming = true;
}
}
span.tag(Tag_1.default.dbStatement(`${_sql}`));
if (AgentConfig_1.default.sqlTraceParameters && _values) {
let vals = _values.map((v) => (v === undefined ? 'undefined' : JSON.stringify(v))).join(', ');
if (vals.length > AgentConfig_1.default.sqlParametersMaxLength)
vals = vals.slice(0, AgentConfig_1.default.sqlParametersMaxLength) + ' ...';
span.tag(Tag_1.default.dbSqlParameters(`[${vals}]`));
}
query = _query.call(this, sql, values, cb);
if (streaming)
SwPlugin_1.wrapEmit(span, query, true, 'end');
}
catch (e) {
span.error(e);
span.stop();
throw e;
}
span.async();
return query;
};
}
}
// noinspection JSUnusedGlobalSymbols
exports.default = new MySQL2Plugin();
//# sourceMappingURL=MySQL2Plugin.js.map