Skip to content
Merged
Show file tree
Hide file tree
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
93 changes: 93 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@
"amqplib": "^0.7.0",
"axios": "^0.21.0",
"express": "^4.17.1",
"grpc_tools_node_protoc_ts": "^4.0.0",
"grpc-tools": "^1.10.0",
"grpc_tools_node_protoc_ts": "^4.0.0",
"jest": "^26.6.3",
"mongodb": "^3.6.4",
"mongoose": "^5.12.2",
"mysql": "^2.18.1",
"mysql2": "^2.2.5",
"pg": "^8.5.1",
"prettier": "^2.0.5",
"testcontainers": "^6.2.0",
Expand Down
130 changes: 130 additions & 0 deletions src/plugins/MySQL2Plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*!
*
* 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.
*
*/

import SwPlugin, { wrapEmit, wrapCallback } from '../core/SwPlugin';
import ContextManager from '../trace/context/ContextManager';
import { Component } from '../trace/Component';
import Tag from '../Tag';
import { SpanLayer } from '../proto/language-agent/Tracing_pb';
import PluginInstaller from '../core/PluginInstaller';
import agentConfig from '../config/AgentConfig';

class MySQL2Plugin implements SwPlugin {
readonly module = 'mysql2';
readonly versions = '*';

install(installer: PluginInstaller): void {
const Connection = installer.require('mysql2/lib/connection');
const _query = Connection.prototype.query;

Connection.prototype.query = function (sql: any, values: any, cb: any) {
let query: any;

const host = `${this.config.host}:${this.config.port}`;
const span = ContextManager.current.newExitSpan('mysql/query', Component.MYSQL);

span.start();

try {
span.component = Component.MYSQL;
span.layer = SpanLayer.DATABASE;
span.peer = host;

span.tag(Tag.dbType('Mysql'));
span.tag(Tag.dbInstance(`${this.config.database}`));

let _sql: any;
let _values: any;
let streaming: any;

if (typeof sql === 'function') {
sql = wrapCallback(span, sql, 0);

} else if (typeof sql === 'object') {
_sql = sql.sql;

if (typeof values === 'function') {
values = wrapCallback(span, values, 0);
_values = sql.values;

} else if (values !== undefined) {
_values = values;

if (typeof cb === 'function') {
cb = wrapCallback(span, cb, 0);
} else {
streaming = true;
}

} else {
streaming = true;
}

} else {
_sql = sql;

if (typeof values === 'function') {
values = wrapCallback(span, values, 0);

} else if (values !== undefined) {
_values = values;

if (typeof cb === 'function') {
cb = wrapCallback(span, cb, 0);
} else {
streaming = true;
}

} else {
streaming = true;
}
}

span.tag(Tag.dbStatement(`${_sql}`));

if (agentConfig.sqlTraceParameters && _values) {
let vals = _values.map((v: any) => v === undefined ? 'undefined' : JSON.stringify(v)).join(', ');

if (vals.length > agentConfig.sqlParametersMaxLength)
vals = vals.slice(0, agentConfig.sqlParametersMaxLength) + ' ...';

span.tag(Tag.dbSqlParameters(`[${vals}]`));
}

query = _query.call(this, sql, values, cb);

if (streaming)
wrapEmit(span, query, true, 'end');

} catch (e) {
span.error(e);
span.stop();

throw e;
}

span.async();

return query;
};
}
}

// noinspection JSUnusedGlobalSymbols
export default new MySQL2Plugin();
38 changes: 38 additions & 0 deletions tests/plugins/mysql2/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*!
*
* 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.
*
*/

import * as http from 'http';
import agent from '../../../src';

agent.start({
serviceName: 'client',
maxBufferSize: 1000,
})

const server = http.createServer((req, res) => {
http
.request(`http://${process.env.SERVER || 'localhost:5000'}${req.url}`, (r) => {
let data = '';
r.on('data', (chunk) => (data += chunk));
r.on('end', () => res.end(data));
})
.end();
});

server.listen(5001, () => console.info('Listening on port 5001...'));
Loading