From f0b3942fa4eb86cc90be7dd757e37ff3457cad7a Mon Sep 17 00:00:00 2001 From: huangqicong Date: Wed, 6 Mar 2019 20:57:34 +0800 Subject: [PATCH 1/9] Add environment variable configuration --- modules/nodejs-agent/lib/config/index.js | 4 ++-- modules/skyapm-egg-require/index.js | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/modules/nodejs-agent/lib/config/index.js b/modules/nodejs-agent/lib/config/index.js index 3cf23e2..ecaf684 100644 --- a/modules/nodejs-agent/lib/config/index.js +++ b/modules/nodejs-agent/lib/config/index.js @@ -66,9 +66,9 @@ AgentConfig.prototype.initConfig = function(agentOptions) { if (!agentOptions.hasOwnProperty("serviceName")) { throw new Error("service name cannot be empty"); } - this._serviceName = agentOptions.serviceName; + this._serviceName = agentOptions.serviceName || process.env.SW_SERVICE_NAME || "You Application"; - this._directServices = "localhost:11800"; + this._directServices = process.env.SW_DIRECT_SERVERS || "localhost:11800"; if (agentOptions.hasOwnProperty("directServers")) { this._directServices = agentOptions.directServers; } diff --git a/modules/skyapm-egg-require/index.js b/modules/skyapm-egg-require/index.js index 6e706d2..e6eff00 100644 --- a/modules/skyapm-egg-require/index.js +++ b/modules/skyapm-egg-require/index.js @@ -18,8 +18,7 @@ "use strict"; const args = JSON.parse(process.argv.slice(2)); -let serviceName = "You Application"; -let directServers = "localhost:11800"; +let serviceName,directServers; if (args.hasOwnProperty("sw_service_name")) { serviceName = args["sw_service_name"]; } @@ -29,6 +28,6 @@ if (args.hasOwnProperty("sw_direct_Servers")) { } require("skyapm-nodejs").start({ - serviceName: serviceName, - directServers: directServers, + serviceName, + directServers }); From c9e2f875ce1f271777d2d76ab18bb9f60ec621c6 Mon Sep 17 00:00:00 2001 From: a526672351 <526672351@qq.com> Date: Wed, 6 Mar 2019 23:04:24 +0800 Subject: [PATCH 2/9] Fix the configuration load order --- modules/nodejs-agent/lib/config/index.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/modules/nodejs-agent/lib/config/index.js b/modules/nodejs-agent/lib/config/index.js index ecaf684..60bd0eb 100644 --- a/modules/nodejs-agent/lib/config/index.js +++ b/modules/nodejs-agent/lib/config/index.js @@ -67,11 +67,7 @@ AgentConfig.prototype.initConfig = function(agentOptions) { throw new Error("service name cannot be empty"); } this._serviceName = agentOptions.serviceName || process.env.SW_SERVICE_NAME || "You Application"; - - this._directServices = process.env.SW_DIRECT_SERVERS || "localhost:11800"; - if (agentOptions.hasOwnProperty("directServers")) { - this._directServices = agentOptions.directServers; - } + this._directServices = agentOptions.directServers || process.env.SW_DIRECT_SERVERS || "localhost:11800"; }; From 28967733a2e1ea451d68c78ae1f7f3752267178f Mon Sep 17 00:00:00 2001 From: a526672351 <526672351@qq.com> Date: Wed, 6 Mar 2019 23:34:34 +0800 Subject: [PATCH 3/9] Fix window environment eslint inconsistency --- modules/nodejs-agent/.eslintrc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/nodejs-agent/.eslintrc b/modules/nodejs-agent/.eslintrc index e528137..b07c083 100644 --- a/modules/nodejs-agent/.eslintrc +++ b/modules/nodejs-agent/.eslintrc @@ -24,7 +24,8 @@ ], "no-invalid-this": "off", "prefer-rest-params": "off", - "prefer-spread": "off" + "prefer-spread": "off", + "linebreak-style": [0 ,"error", "windows"] }, "parserOptions": { "ecmaVersion": 2017 From 252c96ddf62639690f3baf041fa175d8c1c5d4a7 Mon Sep 17 00:00:00 2001 From: a526672351 <526672351@qq.com> Date: Wed, 6 Mar 2019 23:44:53 +0800 Subject: [PATCH 4/9] Fix eslint format --- modules/skyapm-egg-require/.eslintrc | 3 ++- modules/skyapm-egg-require/index.js | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/skyapm-egg-require/.eslintrc b/modules/skyapm-egg-require/.eslintrc index 34c230f..90519a7 100644 --- a/modules/skyapm-egg-require/.eslintrc +++ b/modules/skyapm-egg-require/.eslintrc @@ -24,7 +24,8 @@ ], "no-invalid-this": "off", "prefer-rest-params": "off", - "prefer-spread": "off" + "prefer-spread": "off", + "linebreak-style": [0 ,"error", "windows"] }, "parserOptions": { "ecmaVersion": 2017 diff --git a/modules/skyapm-egg-require/index.js b/modules/skyapm-egg-require/index.js index e6eff00..d28c965 100644 --- a/modules/skyapm-egg-require/index.js +++ b/modules/skyapm-egg-require/index.js @@ -18,7 +18,8 @@ "use strict"; const args = JSON.parse(process.argv.slice(2)); -let serviceName,directServers; +let serviceName; +let directServers; if (args.hasOwnProperty("sw_service_name")) { serviceName = args["sw_service_name"]; } @@ -29,5 +30,5 @@ if (args.hasOwnProperty("sw_direct_Servers")) { require("skyapm-nodejs").start({ serviceName, - directServers + directServers, }); From 84f3ec15f399c8d54142a4c15d78693f48bb3c49 Mon Sep 17 00:00:00 2001 From: huangqicong Date: Tue, 12 Mar 2019 19:53:52 +0800 Subject: [PATCH 5/9] Merge trunk update --- modules/nodejs-agent/lib/cache/index.js | 1 + modules/nodejs-agent/lib/config/index.js | 4 ++- .../lib/dictionary/dictionary-manager.js | 2 ++ .../lib/dictionary/endpoint-dictionary.js | 1 + .../nodejs-agent/lib/dictionary/endpoint.js | 1 + .../dictionary/network-address-dictionary.js | 1 + modules/nodejs-agent/lib/logger/index.js | 1 + modules/nodejs-agent/lib/plugins/http/http.js | 1 + modules/nodejs-agent/lib/services/index.js | 1 + .../lib/services/register-service.js | 13 +++---- .../lib/services/remote-client-service.js | 1 + .../lib/services/trace-send-service.js | 1 + .../lib/trace/component-define.js | 1 + .../nodejs-agent/lib/trace/context-carrier.js | 1 + .../nodejs-agent/lib/trace/key-value-pair.js | 9 ++--- .../nodejs-agent/lib/trace/log-data-entity.js | 9 ++--- modules/nodejs-agent/lib/trace/noop-span.js | 12 +++---- .../lib/trace/noop-trace-segment.js | 4 +-- modules/nodejs-agent/lib/trace/span-layer.js | 1 + modules/nodejs-agent/lib/trace/tags.js | 23 +++++++------ .../lib/trace/trace-segment-id.js | 34 ++++++++++--------- .../lib/trace/trace-segment-ref.js | 1 + modules/nodejs-agent/lib/utils/hashset.js | 1 + modules/nodejs-agent/lib/utils/index.js | 1 + 24 files changed, 75 insertions(+), 50 deletions(-) diff --git a/modules/nodejs-agent/lib/cache/index.js b/modules/nodejs-agent/lib/cache/index.js index f455096..138ff23 100644 --- a/modules/nodejs-agent/lib/cache/index.js +++ b/modules/nodejs-agent/lib/cache/index.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; /** * diff --git a/modules/nodejs-agent/lib/config/index.js b/modules/nodejs-agent/lib/config/index.js index 60bd0eb..b0ef801 100644 --- a/modules/nodejs-agent/lib/config/index.js +++ b/modules/nodejs-agent/lib/config/index.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; const uuid = require("uuid/v4"); @@ -27,7 +28,7 @@ function AgentConfig() { this._serviceId = undefined; this._instanceId = undefined; this._directServices = undefined; - this._instanceUUID = uuid(); + this._instanceUUID = undefined; }; AgentConfig.prototype.getServiceId = function() { @@ -68,6 +69,7 @@ AgentConfig.prototype.initConfig = function(agentOptions) { } this._serviceName = agentOptions.serviceName || process.env.SW_SERVICE_NAME || "You Application"; this._directServices = agentOptions.directServers || process.env.SW_DIRECT_SERVERS || "localhost:11800"; + this._instanceUUID = agentOptions.instanceUUID || uuid(); }; diff --git a/modules/nodejs-agent/lib/dictionary/dictionary-manager.js b/modules/nodejs-agent/lib/dictionary/dictionary-manager.js index a42c4d0..12b567b 100644 --- a/modules/nodejs-agent/lib/dictionary/dictionary-manager.js +++ b/modules/nodejs-agent/lib/dictionary/dictionary-manager.js @@ -14,6 +14,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; + const NetworkAddressDictionary = require("./network-address-dictionary"); const EndpointDictionary = require("./endpoint-dictionary"); diff --git a/modules/nodejs-agent/lib/dictionary/endpoint-dictionary.js b/modules/nodejs-agent/lib/dictionary/endpoint-dictionary.js index 433f353..d2357d3 100644 --- a/modules/nodejs-agent/lib/dictionary/endpoint-dictionary.js +++ b/modules/nodejs-agent/lib/dictionary/endpoint-dictionary.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; module.exports = EndpointDictionary; const async = require("async"); diff --git a/modules/nodejs-agent/lib/dictionary/endpoint.js b/modules/nodejs-agent/lib/dictionary/endpoint.js index 4a71e78..bafb355 100644 --- a/modules/nodejs-agent/lib/dictionary/endpoint.js +++ b/modules/nodejs-agent/lib/dictionary/endpoint.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; module.exports = EndpointKey; diff --git a/modules/nodejs-agent/lib/dictionary/network-address-dictionary.js b/modules/nodejs-agent/lib/dictionary/network-address-dictionary.js index c92d8d6..546a87d 100644 --- a/modules/nodejs-agent/lib/dictionary/network-address-dictionary.js +++ b/modules/nodejs-agent/lib/dictionary/network-address-dictionary.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; module.exports = NetworkAddressDictionary; const async = require("async"); diff --git a/modules/nodejs-agent/lib/logger/index.js b/modules/nodejs-agent/lib/logger/index.js index 737e4be..bc4025a 100644 --- a/modules/nodejs-agent/lib/logger/index.js +++ b/modules/nodejs-agent/lib/logger/index.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; const Logger = function() { let _debug = require("debug"); diff --git a/modules/nodejs-agent/lib/plugins/http/http.js b/modules/nodejs-agent/lib/plugins/http/http.js index 9febfce..9565515 100644 --- a/modules/nodejs-agent/lib/plugins/http/http.js +++ b/modules/nodejs-agent/lib/plugins/http/http.js @@ -16,6 +16,7 @@ */ "use strict"; + const onFinished = require("on-finished"); const ContextCarrier = require("../../trace/context-carrier"); const layerDefine = require("../../trace/span-layer"); diff --git a/modules/nodejs-agent/lib/services/index.js b/modules/nodejs-agent/lib/services/index.js index 9feae91..1b58066 100644 --- a/modules/nodejs-agent/lib/services/index.js +++ b/modules/nodejs-agent/lib/services/index.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; const RegisterService = require("./register-service"); const TraceService = require("./trace-send-service"); diff --git a/modules/nodejs-agent/lib/services/register-service.js b/modules/nodejs-agent/lib/services/register-service.js index ccc0cdd..25c5185 100644 --- a/modules/nodejs-agent/lib/services/register-service.js +++ b/modules/nodejs-agent/lib/services/register-service.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; module.exports = RegisterService; const os = require("os"); @@ -35,7 +36,7 @@ RegisterService.prototype.launch = function() { let that = this; async.waterfall([ - registerService = function(successCallback) { + function registerService(successCallback) { let _serviceId = undefined; async.whilst( function() { @@ -47,10 +48,10 @@ RegisterService.prototype.launch = function() { _serviceId = serviceId; successCallback(null, serviceId); }, callback); - }, + } ); }, - registerInstance = function(serviceId, successCallback) { + function registerInstance(serviceId, successCallback) { let _instanceId = undefined; async.whilst( function() { @@ -73,7 +74,7 @@ RegisterService.prototype.launch = function() { _instanceId = instanceId; successCallback(null, serviceId, instanceId); }, callback); - }, + } ); /** @@ -93,12 +94,12 @@ RegisterService.prototype.launch = function() { return ipv4Address; } }, - changeConfiguration = function(serviceId, instanceId, callback) { + function changeConfiguration(serviceId, instanceId, callback) { agentConfig.setServiceId(serviceId); agentConfig.setInstanceId(instanceId); callback(null, serviceId, instanceId); }, - sendHeartBeat = function(serviceId, instanceID, callback) { + function sendHeartBeat(serviceId, instanceID, callback) { async.forever(function(next) { setTimeout(function() { logger.info("RegisterService", "The Service[%s, %d] send heart beat to Collector.", agentConfig.getServiceName(), agentConfig.getInstanceId()); diff --git a/modules/nodejs-agent/lib/services/remote-client-service.js b/modules/nodejs-agent/lib/services/remote-client-service.js index ffd08e7..26c66c0 100644 --- a/modules/nodejs-agent/lib/services/remote-client-service.js +++ b/modules/nodejs-agent/lib/services/remote-client-service.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; const grpc = require("grpc"); const async = require("async"); diff --git a/modules/nodejs-agent/lib/services/trace-send-service.js b/modules/nodejs-agent/lib/services/trace-send-service.js index d76992e..34ab864 100644 --- a/modules/nodejs-agent/lib/services/trace-send-service.js +++ b/modules/nodejs-agent/lib/services/trace-send-service.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; module.exports = TraceSendService; const traceCache = require("../cache"); diff --git a/modules/nodejs-agent/lib/trace/component-define.js b/modules/nodejs-agent/lib/trace/component-define.js index e4ec4ca..7da02da 100644 --- a/modules/nodejs-agent/lib/trace/component-define.js +++ b/modules/nodejs-agent/lib/trace/component-define.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; /** * @param {id} id diff --git a/modules/nodejs-agent/lib/trace/context-carrier.js b/modules/nodejs-agent/lib/trace/context-carrier.js index f036777..6c7b369 100644 --- a/modules/nodejs-agent/lib/trace/context-carrier.js +++ b/modules/nodejs-agent/lib/trace/context-carrier.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; module.exports = ContextCarrier; diff --git a/modules/nodejs-agent/lib/trace/key-value-pair.js b/modules/nodejs-agent/lib/trace/key-value-pair.js index 1d4f7af..e83cc1f 100644 --- a/modules/nodejs-agent/lib/trace/key-value-pair.js +++ b/modules/nodejs-agent/lib/trace/key-value-pair.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; module.exports = KeyValuePair; @@ -25,14 +26,14 @@ module.exports = KeyValuePair; * @author zhang xin */ function KeyValuePair(key, value) { - this.key = key; - this.value = value; + this.key = key; + this.value = value; } KeyValuePair.prototype.getKey = function() { - return this.key; + return this.key; }; KeyValuePair.prototype.getValue = function() { - return this.value; + return this.value; }; diff --git a/modules/nodejs-agent/lib/trace/log-data-entity.js b/modules/nodejs-agent/lib/trace/log-data-entity.js index 53b6cf9..4b30146 100644 --- a/modules/nodejs-agent/lib/trace/log-data-entity.js +++ b/modules/nodejs-agent/lib/trace/log-data-entity.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; module.exports = LogDataEntity; @@ -25,14 +26,14 @@ module.exports = LogDataEntity; * @author zhang xin */ function LogDataEntity(timestamp, logs) { - this._timestamp = timestamp; - this._data = logs; + this._timestamp = timestamp; + this._data = logs; } LogDataEntity.prototype.getTimestamp = function() { - return this._timestamp; + return this._timestamp; }; LogDataEntity.prototype.getData = function() { - return this._data; + return this._data; }; diff --git a/modules/nodejs-agent/lib/trace/noop-span.js b/modules/nodejs-agent/lib/trace/noop-span.js index 21d4b34..20c5a54 100644 --- a/modules/nodejs-agent/lib/trace/noop-span.js +++ b/modules/nodejs-agent/lib/trace/noop-span.js @@ -25,7 +25,7 @@ module.exports = NoopSpan; * @author zhang xin */ function NoopSpan(traceContext) { - this._traceContext = traceContext; + this._traceContext = traceContext; } NoopSpan.prototype.start = function() { @@ -36,20 +36,20 @@ NoopSpan.prototype.finish = function() { }; NoopSpan.prototype.traceContext = function() { - return this._traceContext; + return this._traceContext; }; NoopSpan.prototype.isEntrySpan = function() { - return false; + return false; }; NoopSpan.prototype.isExitSpan = function() { - return false; + return false; }; NoopSpan.prototype.isLocalSpan = function() { - return false; + return false; }; NoopSpan.prototype.tag = function(key, value) { @@ -62,7 +62,7 @@ NoopSpan.prototype.ref = function(traceSegmentRef) { }; NoopSpan.prototype.getSpanId = function() { - return -1; + return -1; }; NoopSpan.prototype.fetchPeerInfo = function( diff --git a/modules/nodejs-agent/lib/trace/noop-trace-segment.js b/modules/nodejs-agent/lib/trace/noop-trace-segment.js index 1ec8a36..dbbe9ff 100644 --- a/modules/nodejs-agent/lib/trace/noop-trace-segment.js +++ b/modules/nodejs-agent/lib/trace/noop-trace-segment.js @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +"use strict"; module.exports = NoopTraceSegment; @@ -26,7 +26,7 @@ function NoopTraceSegment() { } NoopTraceSegment.prototype.traceSegmentId = function() { - return undefined; + return undefined; }; NoopTraceSegment.prototype.ref = function(traceSegmentRef) { diff --git a/modules/nodejs-agent/lib/trace/span-layer.js b/modules/nodejs-agent/lib/trace/span-layer.js index c15e965..59cfd3a 100644 --- a/modules/nodejs-agent/lib/trace/span-layer.js +++ b/modules/nodejs-agent/lib/trace/span-layer.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; const TraceSegmentServiceParameters = require("../network/common/trace-common_pb"); diff --git a/modules/nodejs-agent/lib/trace/tags.js b/modules/nodejs-agent/lib/trace/tags.js index f42a488..9f7c0d1 100644 --- a/modules/nodejs-agent/lib/trace/tags.js +++ b/modules/nodejs-agent/lib/trace/tags.js @@ -14,29 +14,30 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; let Tags = function() { - this.DB_TYPE = new Tag("db.type"); - this.DB_INSTANCE = new Tag("db.instance"); - this.DB_STATEMENT = new Tag("db.statement"); + this.DB_TYPE = new Tag("db.type"); + this.DB_INSTANCE = new Tag("db.instance"); + this.DB_STATEMENT = new Tag("db.statement"); }; let Tag = function(key) { - this._key = key; + this._key = key; - this.tag = function(span, value) { - span.tag.apply(span, [this._key, value]); - }; + this.tag = function(span, value) { + span.tag.apply(span, [this._key, value]); + }; }; Tags.instance = null; Tags.getInstance = function() { - if (this.instance === null) { - this.instance = new Tags(); - } - return this.instance; + if (this.instance === null) { + this.instance = new Tags(); + } + return this.instance; }; module.exports = Tags.getInstance(); diff --git a/modules/nodejs-agent/lib/trace/trace-segment-id.js b/modules/nodejs-agent/lib/trace/trace-segment-id.js index 37f0832..46009ee 100644 --- a/modules/nodejs-agent/lib/trace/trace-segment-id.js +++ b/modules/nodejs-agent/lib/trace/trace-segment-id.js @@ -14,6 +14,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; + module.exports = ID; const AgentConfig = require("../config"); @@ -25,35 +27,35 @@ const process = require("process"); * @author zhang xin */ function ID(idParts) { - if (idParts) { - this._part1 = idParts.part1; - this._part2 = idParts.part2; - this._part3 = idParts.part3; - } else { - this._part1 = AgentConfig.getInstanceId(); - this._part2 = process.pid * - process.ppid ? process.ppid : (((1 + Math.random()) * 0x10000) | 0) - + (((1 + Math.random()) * 0x10000) | 0); - this._part3 = Number(process.hrtime().join("")); - } + if (idParts) { + this._part1 = idParts.part1; + this._part2 = idParts.part2; + this._part3 = idParts.part3; + } else { + this._part1 = AgentConfig.getInstanceId(); + this._part2 = process.pid * + process.ppid ? process.ppid : (((1 + Math.random()) * 0x10000) | 0) + + (((1 + Math.random()) * 0x10000) | 0); + this._part3 = Number(process.hrtime().join("")); + } } ID.prototype.toString = function() { - return this._part1 + "." + this._part2 + "." + this._part3; + return this._part1 + "." + this._part2 + "." + this._part3; }; ID.prototype.encode = function() { - return this.toString(); + return this.toString(); }; ID.prototype.part1 = function() { - return this._part1; + return this._part1; }; ID.prototype.part2 = function() { - return this._part2; + return this._part2; }; ID.prototype.part3 = function() { - return this._part3; + return this._part3; }; diff --git a/modules/nodejs-agent/lib/trace/trace-segment-ref.js b/modules/nodejs-agent/lib/trace/trace-segment-ref.js index 50e6cc8..adde1e5 100644 --- a/modules/nodejs-agent/lib/trace/trace-segment-ref.js +++ b/modules/nodejs-agent/lib/trace/trace-segment-ref.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; module.exports = TraceSegmentRef; diff --git a/modules/nodejs-agent/lib/utils/hashset.js b/modules/nodejs-agent/lib/utils/hashset.js index 3fddf1b..e2fd71a 100644 --- a/modules/nodejs-agent/lib/utils/hashset.js +++ b/modules/nodejs-agent/lib/utils/hashset.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; module.exports = HashSet; const Utils = require("./"); diff --git a/modules/nodejs-agent/lib/utils/index.js b/modules/nodejs-agent/lib/utils/index.js index 49cfeac..3674ba0 100644 --- a/modules/nodejs-agent/lib/utils/index.js +++ b/modules/nodejs-agent/lib/utils/index.js @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +"use strict"; /** * @constructor From 47ad40c4bba9d528918378ade649cf21f229ec37 Mon Sep 17 00:00:00 2001 From: a526672351 <526672351@qq.com> Date: Sun, 17 Mar 2019 18:37:58 +0800 Subject: [PATCH 6/9] Support trace node_redis framework --- .../lib/plugins/plugin-manager.js | 2 +- .../nodejs-agent/lib/plugins/redis/index.js | 36 +++++++++ .../lib/plugins/redis/node/redis.js | 80 +++++++++++++++++++ .../lib/trace/component-define.js | 1 + 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 modules/nodejs-agent/lib/plugins/redis/index.js create mode 100644 modules/nodejs-agent/lib/plugins/redis/node/redis.js diff --git a/modules/nodejs-agent/lib/plugins/plugin-manager.js b/modules/nodejs-agent/lib/plugins/plugin-manager.js index 1e1e611..f5d8763 100644 --- a/modules/nodejs-agent/lib/plugins/plugin-manager.js +++ b/modules/nodejs-agent/lib/plugins/plugin-manager.js @@ -19,7 +19,7 @@ module.exports = PluginManager; const logger = require("../logger"); -const OFFICER_SUPPORTED_MODULE = ["mysql", "http", "egg-core", "egg"]; +const OFFICER_SUPPORTED_MODULE = ["redis", "mysql", "http", "egg-core", "egg"]; /** * diff --git a/modules/nodejs-agent/lib/plugins/redis/index.js b/modules/nodejs-agent/lib/plugins/redis/index.js new file mode 100644 index 0000000..750e812 --- /dev/null +++ b/modules/nodejs-agent/lib/plugins/redis/index.js @@ -0,0 +1,36 @@ +/* + * Licensed to the SkyAPM 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. + */ + +"use strict"; + +const Plugin = require("../plugin"); + +module.exports = new Plugin("redis-plugin", "redis", [{ + _name: "node", + _description: "Only enhancements to node_redis", + _enhanceModules: ["redis"], + canEnhance: function(version, enhanceFile) { + if (this._enhanceModules.indexOf(enhanceFile) > -1) { + return true; + } + return false; + }, + getInterceptor: function(enhanceFile) { + return require("./" + this._name + "/" + enhanceFile); + }, +}]); + diff --git a/modules/nodejs-agent/lib/plugins/redis/node/redis.js b/modules/nodejs-agent/lib/plugins/redis/node/redis.js new file mode 100644 index 0000000..e341839 --- /dev/null +++ b/modules/nodejs-agent/lib/plugins/redis/node/redis.js @@ -0,0 +1,80 @@ +/* + * Licensed to the SkyAPM 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. + */ + +"use strict"; + +const spanLayer = require("../../../trace/span-layer"); +const componentDefine = require("../../../trace/component-define"); +const Tags = require("../../../trace/tags"); + +/** + * @param {originModule} originModule + * @param {instrumentation} instrumentation + * @param {contextManager} contextManager + * @return {*} + * @author huang qicong + */ +module.exports = function(originModule, instrumentation, contextManager) { + instrumentation.enhanceMethod(originModule, "createClient", wrapCreateClient); + + /** + * @this OriginObject + * @param {original} original + * @return {function(): *} + */ + function wrapCreateClient(original) { + return function() { + let client = original.apply(this, arguments); + enhanceCommandsMethod(client, instrumentation, contextManager); + return client; + }; + } + + return originModule; +}; + +/** + * @param {obj} obj + * @param {instrumentation} instrumentation + * @param {contextManager} contextManager + * @return {wrappedMethod} + */ +function enhanceCommandsMethod(obj, instrumentation, contextManager) { + let connection = obj; + return instrumentation.enhanceMethod(obj, "internal_send_command", commandInterceptor); + + /** + * + * @param {original} original + * @return {function(*=, *=, *=): *} + */ + function commandInterceptor(original) { + // eslint-disable-next-line camelcase + return function(command_obj) { + let span = contextManager.createExitSpan("Redis/command", connection.address); + span.component(componentDefine.Components.REDIS); + span.spanLayer(spanLayer.Layers.CACHE); + Tags.DB_TYPE.tag(span, "Redis"); + Tags.DB_INSTANCE.tag(span, connection.selected_db); + Tags.DB_STATEMENT.tag(span, command_obj.command); + contextManager.finishSpan(span); + // eslint-disable-next-line camelcase + const result = original.apply(this, [command_obj]); + return result; + }; + } +} diff --git a/modules/nodejs-agent/lib/trace/component-define.js b/modules/nodejs-agent/lib/trace/component-define.js index 7da02da..2ad5844 100644 --- a/modules/nodejs-agent/lib/trace/component-define.js +++ b/modules/nodejs-agent/lib/trace/component-define.js @@ -38,6 +38,7 @@ OfficeComponent.prototype.getName = function() { let Components = function() { this.HTTP = new OfficeComponent(2, "HTTP"); this.MYSQL = new OfficeComponent(5, "MYSQL"); + this.REDIS = new OfficeComponent(7, "REDIS"); this.EGG = new OfficeComponent(4003, "Egg"); }; From 8a73988d0ee2820c7c40f2560fae2f3bcd9b9e88 Mon Sep 17 00:00:00 2001 From: a526672351 <526672351@qq.com> Date: Sun, 17 Mar 2019 22:49:25 +0800 Subject: [PATCH 7/9] Update the reids version file --- modules/nodejs-agent/lib/plugins/redis/index.js | 4 ++-- .../nodejs-agent/lib/plugins/redis/{node => }/redis.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) rename modules/nodejs-agent/lib/plugins/redis/{node => }/redis.js (94%) diff --git a/modules/nodejs-agent/lib/plugins/redis/index.js b/modules/nodejs-agent/lib/plugins/redis/index.js index 750e812..10c96f5 100644 --- a/modules/nodejs-agent/lib/plugins/redis/index.js +++ b/modules/nodejs-agent/lib/plugins/redis/index.js @@ -20,7 +20,7 @@ const Plugin = require("../plugin"); module.exports = new Plugin("redis-plugin", "redis", [{ - _name: "node", + _name: "", _description: "Only enhancements to node_redis", _enhanceModules: ["redis"], canEnhance: function(version, enhanceFile) { @@ -30,7 +30,7 @@ module.exports = new Plugin("redis-plugin", "redis", [{ return false; }, getInterceptor: function(enhanceFile) { - return require("./" + this._name + "/" + enhanceFile); + return require("./" + enhanceFile); }, }]); diff --git a/modules/nodejs-agent/lib/plugins/redis/node/redis.js b/modules/nodejs-agent/lib/plugins/redis/redis.js similarity index 94% rename from modules/nodejs-agent/lib/plugins/redis/node/redis.js rename to modules/nodejs-agent/lib/plugins/redis/redis.js index e341839..22b3df5 100644 --- a/modules/nodejs-agent/lib/plugins/redis/node/redis.js +++ b/modules/nodejs-agent/lib/plugins/redis/redis.js @@ -17,9 +17,9 @@ "use strict"; -const spanLayer = require("../../../trace/span-layer"); -const componentDefine = require("../../../trace/component-define"); -const Tags = require("../../../trace/tags"); +const spanLayer = require("../../trace/span-layer"); +const componentDefine = require("../../trace/component-define"); +const Tags = require("../../trace/tags"); /** * @param {originModule} originModule @@ -71,9 +71,9 @@ function enhanceCommandsMethod(obj, instrumentation, contextManager) { Tags.DB_TYPE.tag(span, "Redis"); Tags.DB_INSTANCE.tag(span, connection.selected_db); Tags.DB_STATEMENT.tag(span, command_obj.command); - contextManager.finishSpan(span); // eslint-disable-next-line camelcase const result = original.apply(this, [command_obj]); + contextManager.finishSpan(span); return result; }; } From 6b4cf19bc463f9101239e5700e117db8857c02df Mon Sep 17 00:00:00 2001 From: a526672351 <526672351@qq.com> Date: Tue, 26 Mar 2019 23:55:36 +0800 Subject: [PATCH 8/9] Adjusting enhanced objects --- .../nodejs-agent/lib/plugins/redis/redis.js | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/modules/nodejs-agent/lib/plugins/redis/redis.js b/modules/nodejs-agent/lib/plugins/redis/redis.js index 22b3df5..c6be70e 100644 --- a/modules/nodejs-agent/lib/plugins/redis/redis.js +++ b/modules/nodejs-agent/lib/plugins/redis/redis.js @@ -51,18 +51,19 @@ module.exports = function(originModule, instrumentation, contextManager) { * @param {obj} obj * @param {instrumentation} instrumentation * @param {contextManager} contextManager - * @return {wrappedMethod} */ function enhanceCommandsMethod(obj, instrumentation, contextManager) { let connection = obj; - return instrumentation.enhanceMethod(obj, "internal_send_command", commandInterceptor); + let commandQueue = obj.command_queue; + instrumentation.enhanceMethod(commandQueue, "push", pushInterceptor); + instrumentation.enhanceMethod(commandQueue, "shift", shiftInterceptor); /** * * @param {original} original * @return {function(*=, *=, *=): *} */ - function commandInterceptor(original) { + function pushInterceptor(original) { // eslint-disable-next-line camelcase return function(command_obj) { let span = contextManager.createExitSpan("Redis/command", connection.address); @@ -70,10 +71,23 @@ function enhanceCommandsMethod(obj, instrumentation, contextManager) { span.spanLayer(spanLayer.Layers.CACHE); Tags.DB_TYPE.tag(span, "Redis"); Tags.DB_INSTANCE.tag(span, connection.selected_db); - Tags.DB_STATEMENT.tag(span, command_obj.command); + Tags.DB_STATEMENT.tag(span, command_obj.command + "args:" + command_obj.args.toString()); + command_obj["_span"] = span; // eslint-disable-next-line camelcase const result = original.apply(this, [command_obj]); - contextManager.finishSpan(span); + return result; + }; + } + + /** + * + * @param {original} original + * @return {function(*=, *=, *=): *} + */ + function shiftInterceptor(original) { + return function() { + const result = original.apply(this, arguments); + contextManager.finishSpan(result["_span"]); return result; }; } From ba9b4606ef1b8f39d485f733463811401331e755 Mon Sep 17 00:00:00 2001 From: a526672351 <526672351@qq.com> Date: Thu, 28 Mar 2019 23:44:58 +0800 Subject: [PATCH 9/9] Change enhancement method --- .../nodejs-agent/lib/plugins/redis/redis.js | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/modules/nodejs-agent/lib/plugins/redis/redis.js b/modules/nodejs-agent/lib/plugins/redis/redis.js index c6be70e..b62cb1d 100644 --- a/modules/nodejs-agent/lib/plugins/redis/redis.js +++ b/modules/nodejs-agent/lib/plugins/redis/redis.js @@ -54,40 +54,35 @@ module.exports = function(originModule, instrumentation, contextManager) { */ function enhanceCommandsMethod(obj, instrumentation, contextManager) { let connection = obj; - let commandQueue = obj.command_queue; - instrumentation.enhanceMethod(commandQueue, "push", pushInterceptor); - instrumentation.enhanceMethod(commandQueue, "shift", shiftInterceptor); + instrumentation.enhanceMethod(obj, "internal_send_command", commandInterceptor); /** * * @param {original} original * @return {function(*=, *=, *=): *} */ - function pushInterceptor(original) { + function commandInterceptor(original) { // eslint-disable-next-line camelcase return function(command_obj) { - let span = contextManager.createExitSpan("Redis/command", connection.address); + let span = contextManager.createExitSpan("Redis/" + command_obj.command, connection.address); span.component(componentDefine.Components.REDIS); span.spanLayer(spanLayer.Layers.CACHE); Tags.DB_TYPE.tag(span, "Redis"); Tags.DB_INSTANCE.tag(span, connection.selected_db); - Tags.DB_STATEMENT.tag(span, command_obj.command + "args:" + command_obj.args.toString()); - command_obj["_span"] = span; + Tags.DB_STATEMENT.tag(span, command_obj.command + " args: " + command_obj.args.toString()); // eslint-disable-next-line camelcase const result = original.apply(this, [command_obj]); - return result; - }; - } - - /** - * - * @param {original} original - * @return {function(*=, *=, *=): *} - */ - function shiftInterceptor(original) { - return function() { - const result = original.apply(this, arguments); - contextManager.finishSpan(result["_span"]); + let callback = command_obj.callback; + if (typeof command_obj.callback === "function") { + command_obj.callback = instrumentation.enhanceCallback(span.traceContext(), contextManager, function() { + contextManager.finishSpan(span); + return callback.apply(this, arguments); + }); + } else { + command_obj.callback = instrumentation.enhanceCallback(span.traceContext(), contextManager, function() { + contextManager.finishSpan(span); + }); + } return result; }; }