@kangc/skywalking-backend-js
Version:
The NodeJS agent for Apache SkyWalking
100 lines • 4.59 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 });
var tslib_1 = require("tslib");
var SwPlugin_1 = require("../core/SwPlugin");
var ContextManager_1 = tslib_1.__importDefault(require("../trace/context/ContextManager"));
var Component_1 = require("../trace/Component");
var Tag_1 = tslib_1.__importDefault(require("../Tag"));
var Tracing_pb_1 = require("../proto/language-agent/Tracing_pb");
var AgentConfig_1 = tslib_1.__importDefault(require("../config/AgentConfig"));
var MySQLPlugin = /** @class */ (function () {
function MySQLPlugin() {
this.module = 'pg';
this.versions = '*';
}
MySQLPlugin.prototype.install = function (installer) {
var Client = installer.require('pg/lib/client');
var Cursor;
try {
Cursor = installer.require('pg-cursor');
}
catch (_a) {
/* Linter food */
}
var _query = Client.prototype.query;
Client.prototype.query = function (config, values, callback) {
var query;
var host = this.host + ":" + this.port;
var span = ContextManager_1.default.current.newExitSpan('pg/query', Component_1.Component.POSTGRESQL);
span.start();
try {
span.component = Component_1.Component.POSTGRESQL;
span.layer = Tracing_pb_1.SpanLayer.DATABASE;
span.peer = host;
span.tag(Tag_1.default.dbType('PostgreSQL'));
span.tag(Tag_1.default.dbInstance("" + this.connectionParameters.database));
var _sql = void 0;
var _values = void 0;
if (typeof config === 'string')
_sql = config;
else if (config !== null && config !== undefined) {
_sql = config.text;
_values = config.values;
if (typeof config.callback === 'function')
config.callback = SwPlugin_1.wrapCallback(span, config.callback, 0);
}
if (typeof values === 'function')
values = SwPlugin_1.wrapCallback(span, values, 0);
else if (_values !== undefined)
_values = values;
if (typeof callback === 'function')
callback = SwPlugin_1.wrapCallback(span, callback, 0);
span.tag(Tag_1.default.dbStatement("" + _sql));
if (AgentConfig_1.default.sqlTraceParameters && _values) {
var vals = _values.map(function (v) { return (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, config, values, callback);
if (query) {
if (Cursor && query instanceof Cursor)
SwPlugin_1.wrapEmit(span, query, true, 'end');
else if (typeof query.then === 'function')
// generic Promise check
query = SwPlugin_1.wrapPromise(span, query);
// else we assume there was a callback
}
}
catch (e) {
span.error(e);
span.stop();
throw e;
}
span.async();
return query;
};
};
return MySQLPlugin;
}());
// noinspection JSUnusedGlobalSymbols
exports.default = new MySQLPlugin();
//# sourceMappingURL=PgPlugin.js.map