diff --git a/README.md b/README.md index 721b054..e25c0f5 100644 --- a/README.md +++ b/README.md @@ -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` | diff --git a/src/config/AgentConfig.ts b/src/config/AgentConfig.ts index 349e13a..1e72c0a 100644 --- a/src/config/AgentConfig.ts +++ b/src/config/AgentConfig.ts @@ -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( @@ -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(''), }; diff --git a/src/core/PluginInstaller.ts b/src/core/PluginInstaller.ts index 820b2bc..c42dfcc 100644 --- a/src/core/PluginInstaller.ts +++ b/src/core/PluginInstaller.ts @@ -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); @@ -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); diff --git a/src/plugins/MongoDBPlugin.ts b/src/plugins/MongoDBPlugin.ts index 136bbc5..c0de1ed 100644 --- a/src/plugins/MongoDBPlugin.ts +++ b/src/plugins/MongoDBPlugin.ts @@ -83,8 +83,8 @@ 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; } @@ -92,7 +92,7 @@ class MongoDBPlugin implements SwPlugin { 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); @@ -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); @@ -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]))); } } diff --git a/src/plugins/MySQLPlugin.ts b/src/plugins/MySQLPlugin.ts index 2b0dc8c..b6f4826 100644 --- a/src/plugins/MySQLPlugin.ts +++ b/src/plugins/MySQLPlugin.ts @@ -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}]`)); } diff --git a/src/plugins/PgPlugin.ts b/src/plugins/PgPlugin.ts index 8374688..ed6415a 100644 --- a/src/plugins/PgPlugin.ts +++ b/src/plugins/PgPlugin.ts @@ -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}]`)); }