Skip to content

Commit

Permalink
added sw_agent_disable_plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-pytel committed Mar 5, 2021
1 parent 5425181 commit 835adb1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 21 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ Environment Variable | Description | Default
| `SW_AGENT_INSTANCE` | The name of the service instance | Randomly generated |
| `SW_AGENT_COLLECTOR_BACKEND_SERVICES` | The backend OAP server address | `127.0.0.1:11800` |
| `SW_AGENT_AUTHENTICATION` | The authentication token to verify that the agent is trusted by the backend OAP, as for how to configure the backend, refer to [the yaml](https://github.com/apache/skywalking/blob/4f0f39ffccdc9b41049903cc540b8904f7c9728e/oap-server/server-bootstrap/src/main/resources/application.yml#L155-L158). | not set |
| `SW_AGENT_LOGGING_LEVEL` | The logging level, could be one of `CRITICAL`, `FATAL`, `ERROR`, `WARN`(`WARNING`), `INFO`, `DEBUG` | `INFO` |
| `SW_AGENT_LOGGING_LEVEL` | The logging level, could be one of `error`, `warn`, `info`, `debug` | `info` |
| `SW_AGENT_DISABLE_PLUGINS` | Comma-delimited list of plugins to disable in the plugins directory (e.g. "mysql", "express"). | `` |
| `SW_IGNORE_SUFFIX` | The suffices of endpoints that will be ignored (not traced), comma separated | `.jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,.mp4,.html,.svg` |
| `SW_TRACE_IGNORE_PATH` | The paths of endpoints that will be ignored (not traced), comma separated | `` |
| `SW_SQL_TRACE_PARAMETERS` | If set to 'true' then SQL query parameters will be included | `false` |
Expand Down
24 changes: 15 additions & 9 deletions src/config/AgentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ export type AgentConfig = {
collectorAddress?: string;
authorization?: string;
maxBufferSize?: number;
disablePlugins?: string;
ignoreSuffix?: string;
traceIgnorePath?: string;
sql_trace_parameters?: boolean;
sql_parameters_max_length?: number;
mongo_trace_parameters?: boolean;
mongo_parameters_max_length?: number;
sqlTraceParameters?: boolean;
sqlParametersMaxLength?: number;
mongoTraceParameters?: boolean;
mongoParametersMaxLength?: number;
// the following is internal state computed from config values
reDisablePlugins?: RegExp;
reIgnoreOperation?: RegExp;
};

export function finalizeConfig(config: AgentConfig): void {
const escapeRegExp = (s: string) => s.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");

config.reDisablePlugins = RegExp(`^(?:${config.disablePlugins!.split(',').map((s) => escapeRegExp(s.trim()) + 'Plugin\\.js').join('|')})$`, 'i');

const ignoreSuffix =`^.+(?:${config.ignoreSuffix!.split(',').map((s) => escapeRegExp(s.trim())).join('|')})$`;
const ignorePath = '^(?:' + config.traceIgnorePath!.split(',').map(
(s1) => s1.trim().split('**').map(
Expand All @@ -61,11 +65,13 @@ export default {
authorization: process.env.SW_AGENT_AUTHENTICATION,
maxBufferSize: Number.isSafeInteger(process.env.SW_AGENT_MAX_BUFFER_SIZE) ?
Number.parseInt(process.env.SW_AGENT_MAX_BUFFER_SIZE as string, 10) : 1000,
disable_plugins: process.env.SW_AGENT_DISABLE_PLUGINS || '',
ignoreSuffix: process.env.SW_IGNORE_SUFFIX ?? '.jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,.mp4,.html,.svg',
traceIgnorePath: process.env.SW_TRACE_IGNORE_PATH || '',
sql_trace_parameters: (process.env.SW_SQL_TRACE_PARAMETERS || '').toLowerCase() === 'true',
sql_parameters_max_length: Math.trunc(Math.max(0, Number(process.env.SW_SQL_PARAMETERS_MAX_LENGTH))) || 512,
mongo_trace_parameters: (process.env.SW_MONGO_TRACE_PARAMETERS || '').toLowerCase() === 'true',
mongo_parameters_max_length: Math.trunc(Math.max(0, Number(process.env.SW_MONGO_PARAMETERS_MAX_LENGTH))) || 512,
reIgnoreOperation: RegExp(''), // temporary placeholder so Typescript doesn't throw a fit
sqlTraceParameters: (process.env.SW_SQL_TRACE_PARAMETERS || '').toLowerCase() === 'true',
sqlParametersMaxLength: Math.trunc(Math.max(0, Number(process.env.SW_SQL_PARAMETERS_MAX_LENGTH))) || 512,
mongoTraceParameters: (process.env.SW_MONGO_TRACE_PARAMETERS || '').toLowerCase() === 'true',
mongoParametersMaxLength: Math.trunc(Math.max(0, Number(process.env.SW_MONGO_PARAMETERS_MAX_LENGTH))) || 512,
reDisablePlugins: RegExp(''), // temporary placeholder so Typescript doesn't throw a fit
reIgnoreOperation: RegExp(''),
};
6 changes: 6 additions & 0 deletions src/core/PluginInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as path from 'path';
import SwPlugin from '../core/SwPlugin';
import { createLogger } from '../logging';
import * as semver from 'semver';
import config from '../config/AgentConfig';

const logger = createLogger(__filename);

Expand Down Expand Up @@ -76,6 +77,11 @@ export default class PluginInstaller {
fs.readdirSync(this.pluginDir)
.filter((file) => !(file.endsWith('.d.ts') || file.endsWith('.js.map')))
.forEach((file) => {
if (file.match(config.reDisablePlugins)) {
logger.info(`Plugin ${file} not installed because it is disabled`);
return;
}

let plugin;
const pluginFile = path.join(this.pluginDir, file);

Expand Down
10 changes: 5 additions & 5 deletions src/plugins/MongoDBPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ class MongoDBPlugin implements SwPlugin {

let str = JSON.stringify(params);

if (str.length > agentConfig.mongo_parameters_max_length)
str = str.slice(0, agentConfig.mongo_parameters_max_length) + ' ...';
if (str.length > agentConfig.mongoParametersMaxLength)
str = str.slice(0, agentConfig.mongoParametersMaxLength) + ' ...';

return str;
}

const insertFunc = function(this: any, operation: string, span: any, args: any[]): boolean { // args = [doc(s), options, callback]
span.tag(Tag.dbStatement(`${this.s.namespace.collection}.${operation}()`));

if (agentConfig.mongo_trace_parameters)
if (agentConfig.mongoTraceParameters)
span.tag(Tag.dbMongoParameters(stringify(args[0])));

return wrapCallback(span, args, 1);
Expand All @@ -107,7 +107,7 @@ class MongoDBPlugin implements SwPlugin {
const updateFunc = function(this: any, operation: string, span: any, args: any[]): boolean { // args = [filter, update, options, callback]
span.tag(Tag.dbStatement(`${this.s.namespace.collection}.${operation}(${stringify(args[0])})`));

if (agentConfig.mongo_trace_parameters)
if (agentConfig.mongoTraceParameters)
span.tag(Tag.dbMongoParameters(stringify(args[1])));

return wrapCallback(span, args, 2);
Expand All @@ -132,7 +132,7 @@ class MongoDBPlugin implements SwPlugin {
params += ', ' + stringify(args[1]);

if (typeof args[2] !== 'function' && args[2] !== undefined) {
if (agentConfig.mongo_trace_parameters)
if (agentConfig.mongoTraceParameters)
span.tag(Tag.dbMongoParameters(stringify(args[2])));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/MySQLPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ class MySQLPlugin implements SwPlugin {

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

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

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

span.tag(Tag.dbSqlParameters(`[${vals}]`));
}
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/PgPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ class MySQLPlugin implements SwPlugin {

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

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

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

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

0 comments on commit 835adb1

Please sign in to comment.